Exemple #1
0
 def test_addr_pubkey_hash(self):
     pairs = [
         (
             "e201ee2f37bcc0ba0e93f82322e48333a92b9355",
             "DsmZvWuokf5NzFwFfJk5cALZZBZivjkhMSQ",
         ),
         (
             "5643d59202de158b509544d40b32e85bfaf6243e",
             "DsYq2s8mwpM6vXLbjb8unhNmBXFofPzcrrv",
         ),
         (
             "c5fa0d15266e055eaf8ec7c4d7a679885266ef0d",
             "Dsj1iA5PBCU6Nmpe6jqucwfHK17WmSKd3uG",
         ),
         (
             "73612f7b7b1ed32ff44dded7a2cf87c206fabf8a",
             "DsbUyd4DueVNyvfh542kZDXNEGKByUAi1RV",
         ),
         (
             "a616bc09179e31e6d9e3abfcb16ac2d2baf45141",
             "Dsg76ttvZmTFchZ5mWRnAUg6UGfCyrq86ch",
         ),
     ]
     for pubkeyHash, addrStr in pairs:
         pubkeyHashBA = ByteArray(pubkeyHash)
         addr = addrlib.AddressPubKeyHash(pubkeyHashBA, mainnet)
         assert addr.string() == addrStr
         assert addr.scriptAddress() == pubkeyHashBA
         assert addr.hash160() == pubkeyHashBA
Exemple #2
0
            def utxosource(amt, filter):
                nextVal = 10
                total = 0
                utxos = []

                while total < amt:
                    atoms = int(nextVal * 1e8)
                    privKey = Curve.generateKey()
                    pkHash = crypto.hash160(
                        privKey.pub.serializeCompressed().b)
                    addr = addrlib.AddressPubKeyHash(pkHash, testnet)
                    addrs.append(addr)
                    addrString = addr.string()
                    keys[addrString] = privKey
                    pkScript = txscript.makePayToAddrScript(
                        addrString, testnet)
                    txHash = rando.newHash()
                    txid = reversed(txHash).hex()
                    utxos.append(
                        account.UTXO(
                            address=addrString,
                            txHash=txHash,
                            vout=0,
                            ts=int(time.time()),
                            scriptPubKey=pkScript,
                            satoshis=atoms,
                        ))
                    tx = msgtx.MsgTx.new()
                    tx.addTxOut(msgtx.TxOut(value=atoms, pkScript=pkScript))
                    txs[txid] = tx
                    total += atoms
                    nextVal *= 2
                return utxos, True
Exemple #3
0
 def internal():
     privKey = Curve.generateKey()
     pkHash = crypto.hash160(privKey.pub.serializeCompressed().b)
     addr = addrlib.AddressPubKeyHash(pkHash, testnet)
     addrs.append(addr)
     keys[addr.string()] = privKey
     return addr.string()
Exemple #4
0
def main():
    ba = ByteArray(1, length=32)
    priv = crypto.privKeyFromBytes(ba)
    pub = priv.pub
    pkh = crypto.hash160(pub.serializeCompressed().b)
    addrPKH = addrlib.AddressPubKeyHash(pkh, mainnet)
    print([x for x in ba])
    print(pub.serializeCompressed().b.hex())
    print(addrPKH.string())
    h = ByteArray("751e76e8199196d454941c45d1b3a323f1433bd6")
    addrPKH = addrlib.AddressPubKeyHash(h, mainnet)
    print(addrPKH.string())
    addrPKH = addrlib.AddressPubKeyHash(h, testnet)
    print(addrPKH.string())
    addrPKH = addrlib.AddressPubKeyHash(h, simnet)
    print(addrPKH.string())
    h = "rltc1qzhe0hmteg5u6ap7488vq9f3wtqsx4ma9dps9t4"
    r = "bcrt1q322tg0y2hzyp9zztr7d2twdclhqg88anvzxwwr"
    #b = b58decode(h)
    print([x for x in h.encode("utf_8")])
    print([x for x in r.encode("utf_8")])
Exemple #5
0
def test_Client(dcrdConfig):
    if dcrdConfig is None:
        pytest.skip("did not locate a dcrd config file")
    rpcClient = rpc.Client(
        urlunsplit(("https", dcrdConfig["rpclisten"], "/", "", "")),
        dcrdConfig["rpcuser"],
        dcrdConfig["rpcpass"],
        dcrdConfig["rpccert"],
    )

    stringify = rpc.stringify
    assert stringify(ByteArray("face")) == "cefa"
    assert stringify([ByteArray("face"),
                      ByteArray("baadf00d")]) == ["cefa", "0df0adba"]
    assert stringify("face") == "face"
    assert stringify(["face", "baadf00d"]) == ["face", "baadf00d"]
    alist = [ByteArray("face"), "baadf00d", ByteArray("deadbeef")]
    alistReversed = ["cefa", "baadf00d", "efbeadde"]
    assert set(stringify((b for b in alist))) == set(
        (s for s in alistReversed))
    assert stringify([
        ByteArray("face"),
        (ByteArray("badd"), "d00d", ByteArray("babe")),
        [alist, [alist], [alist, alist, [alist]]],
        [ByteArray("feed"), ByteArray("1100")],
        set({
            "hey": ByteArray("1234"),
            "there": ByteArray("4321")
        }.keys()),
        ByteArray("baadf00d"),
    ]) == [
        "cefa",
        ("ddba", "d00d", "beba"),
        [
            ["cefa", "baadf00d", "efbeadde"],
            [["cefa", "baadf00d", "efbeadde"]],
            [
                ["cefa", "baadf00d", "efbeadde"],
                ["cefa", "baadf00d", "efbeadde"],
                [["cefa", "baadf00d", "efbeadde"]],
            ],
        ],
        ["edfe", "0011"],
        {"there", "hey"},
        "0df0adba",
    ]

    with pytest.raises(DecredError):
        rpcClient.call("no_such_method")

    assert rpcClient.addNode("127.0.0.1", "onetry") is None

    debugLevel = rpcClient.debugLevel("show")
    assert isinstance(debugLevel, str)

    debugLevel = rpcClient.debugLevel("info")
    assert debugLevel == "Done."

    estimateFee = rpcClient.estimateFee()
    assert isinstance(estimateFee, int)

    estimateSmartFee = rpcClient.estimateSmartFee(32)
    assert isinstance(estimateSmartFee, int)

    estimateStakeDiff = rpcClient.estimateStakeDiff(0)
    assert isinstance(estimateStakeDiff, rpc.EstimateStakeDiffResult)

    existsAddress = rpcClient.existsAddress(mainnetAddress)
    assert existsAddress

    existsAddress = rpcClient.existsAddress(cookedAddress1)
    assert not existsAddress

    existsAddresses = rpcClient.existsAddresses([
        cookedAddress1,
        cookedAddress2,
        mainnetAddress,
        cookedAddress2,
        cookedAddress1,
        cookedAddress2,
        mainnetAddress,
    ])
    assert existsAddresses == [False, False, True, False, False, False, True]

    liveTickets = rpcClient.liveTickets()
    assert isinstance(liveTickets, list)

    aTicket = liveTickets[0]

    for ticket in liveTickets:
        getRawTransaction = rpcClient.getRawTransaction(ticket)

        script = getRawTransaction.txOut[0].pkScript

        if txscript.extractStakeScriptHash(script, opcode.OP_SSTX):
            decodeScript = rpcClient.decodeScript(script)
            assert isinstance(decodeScript, rpc.DecodeScriptResult)
            break

    else:
        raise RuntimeError("did not find a suitable script to decode")

    existsExpiredTickets = rpcClient.existsExpiredTickets([aTicket, aTicket])
    assert existsExpiredTickets == [False, False]

    bestBlock = rpcClient.getBestBlock()
    assert isinstance(bestBlock, rpc.GetBestBlockResult)

    blockchainInfo = rpcClient.getBlockchainInfo()
    assert isinstance(blockchainInfo, rpc.GetBlockChainInfoResult)

    getAddedNodeInfo = rpcClient.getAddedNodeInfo(True)
    assert isinstance(getAddedNodeInfo, list)

    getAddedNodeInfo = rpcClient.getAddedNodeInfo(False)
    assert isinstance(getAddedNodeInfo, list)

    getBestBlockHash = rpcClient.getBestBlockHash()
    assert isinstance(getBestBlockHash, ByteArray)

    getBlock = rpcClient.getBlock(blkHash414000, False)
    assert getBlock == blkHex414000

    getBlock = rpcClient.getBlock(blkHash414000)
    assert isinstance(getBlock, rpc.GetBlockVerboseResult)

    getBlock = rpcClient.getBlock(blkHash414000, True, True)
    assert isinstance(getBlock, rpc.GetBlockVerboseResult)

    getBlockCount = rpcClient.getBlockCount()
    assert isinstance(getBlockCount, int)

    getBlockHash = rpcClient.getBlockHash(0)
    assert getBlockHash == reversed(ByteArray(genesisHash))

    getBlockHeader = rpcClient.getBlockHeader(blkHash414000)
    assert isinstance(getBlockHeader, rpc.GetBlockHeaderVerboseResult)

    getBlockHeader = rpcClient.getBlockHeader(blkHash414000, False)
    assert isinstance(getBlockHeader, BlockHeader)

    getBlockSubsidy = rpcClient.getBlockSubsidy(414500, 5)
    assert isinstance(getBlockSubsidy, rpc.GetBlockSubsidyResult)

    getCFilter = rpcClient.getCFilter(blkHash414000, "extended")
    assert getCFilter == cFilter414000

    getCFilterHeader = rpcClient.getCFilterHeader(blkHash414000, "extended")
    assert getCFilterHeader == cFilterHeader414000

    getCFilterV2 = rpcClient.getCFilterV2(blkHash414000)
    assert isinstance(getCFilterV2, rpc.GetCFilterV2Result)

    getChainTips = rpcClient.getChainTips()
    assert isinstance(getChainTips[0], rpc.GetChainTipsResult)

    getCoinSupply = rpcClient.getCoinSupply()
    assert isinstance(getCoinSupply, int)

    getConnectionCount = rpcClient.getConnectionCount()
    assert isinstance(getConnectionCount, int)

    getCurrentNet = rpcClient.getCurrentNet()
    assert isinstance(getCurrentNet, int)

    getDifficulty = rpcClient.getDifficulty()
    assert isinstance(getDifficulty, float)

    getGenerate = rpcClient.getGenerate()
    assert isinstance(getGenerate, bool)

    getHashesPerSec = rpcClient.getHashesPerSec()
    assert isinstance(getHashesPerSec, int)

    getInfo = rpcClient.getInfo()
    assert isinstance(getInfo, rpc.InfoChainResult)

    getMempoolInfo = rpcClient.getMempoolInfo()
    assert isinstance(getMempoolInfo, rpc.GetMempoolInfoResult)

    getMiningInfo = rpcClient.getMiningInfo()
    assert isinstance(getMiningInfo, rpc.GetMiningInfoResult)

    getNetTotals = rpcClient.getNetTotals()
    assert isinstance(getNetTotals, rpc.GetNetTotalsResult)

    getNetworkHashPS = rpcClient.getNetworkHashPS()
    assert isinstance(getNetworkHashPS, int)

    getNetworkInfo = rpcClient.getNetworkInfo()
    assert isinstance(getNetworkInfo, rpc.GetNetworkInfoResult)

    getPeerInfo = rpcClient.getPeerInfo()
    assert isinstance(getPeerInfo[0], rpc.GetPeerInfoResult)

    getRawMempool = rpcClient.getRawMempool()
    assert isinstance(getRawMempool, list)
    mempoolTx = getRawMempool[0]

    existsMempoolTxs = rpcClient.existsMempoolTxs(getRawMempool[:3] +
                                                  [aTicket])
    assert existsMempoolTxs == [True, True, True, False]

    getRawMempool = rpcClient.getRawMempool(True)
    assert isinstance(getRawMempool[reversed(mempoolTx).hex()],
                      rpc.GetRawMempoolVerboseResult)

    getHeaders = rpcClient.getHeaders([blkHash414000], blkHash414005)
    assert blkHeader414002 in [header.serialize() for header in getHeaders]

    # This test will fail if --addrindex is not enabled in dcrd.
    getRawTransaction = rpcClient.getRawTransaction(aTicket)
    assert isinstance(getRawTransaction, MsgTx)

    decodeRawTransaction = rpcClient.decodeRawTransaction(getRawTransaction)
    assert isinstance(decodeRawTransaction, rpc.RawTransactionResult)

    rawaddr = txscript.extractStakeScriptHash(
        getRawTransaction.txOut[0].pkScript, opcode.OP_SSTX)
    if rawaddr:
        addressWithTickets = addrlib.AddressScriptHash(rawaddr,
                                                       mainnet).string()
    else:
        rawaddr = txscript.extractStakePubKeyHash(
            getRawTransaction.txOut[0].pkScript, opcode.OP_SSTX)
        addressWithTickets = addrlib.AddressPubKeyHash(rawaddr,
                                                       mainnet).string()

    getRawTransaction = rpcClient.getRawTransaction(aTicket, 1)
    assert isinstance(getRawTransaction, rpc.RawTransactionResult)

    getStakeDifficulty = rpcClient.getStakeDifficulty()
    assert isinstance(getStakeDifficulty, rpc.GetStakeDifficultyResult)

    getStakeVersionInfo = rpcClient.getStakeVersionInfo()
    assert isinstance(getStakeVersionInfo, rpc.GetStakeVersionInfoResult)

    getStakeVersions = rpcClient.getStakeVersions(blkHash414000, 3)
    assert isinstance(getStakeVersions[0], rpc.GetStakeVersionsResult)

    getTicketPoolValue = rpcClient.getTicketPoolValue()
    assert isinstance(getTicketPoolValue, float)

    getVoteInfo = rpcClient.getVoteInfo(7)
    assert isinstance(getVoteInfo, rpc.GetVoteInfoResult)

    # getWork will fail if --mininaddr is not set when starting dcrd
    with pytest.raises(DecredError):
        rpcClient.getWork()
    # getWork = rpcClient.getWork()
    # assert isinstance(getWork, rpc.GetWorkResult)

    dcrdHelp = rpcClient.help()
    assert isinstance(dcrdHelp, str)

    dcrdHelp = rpcClient.help("getinfo")
    assert isinstance(dcrdHelp, str)

    missedTickets = rpcClient.missedTickets()
    assert isinstance(missedTickets, list)

    assert rpcClient.node("connect", "127.0.0.1", "temp") is None

    revocableTicket = rpcClient.getRawTransaction(missedTickets[0])

    # create a ticket revoking transaction using txscript
    revocation = txscript.makeRevocation(revocableTicket, 3000)
    createRawSSRTx = rpcClient.createRawSSRTx(revocableTicket, 3000)

    # ours is just missing the block index
    revocation.txIn[0].blockIndex = createRawSSRTx.txIn[0].blockIndex
    assert createRawSSRTx.txHex() == revocation.txHex()

    # Using the revocation as an unspent output
    amt = revocableTicket.txOut[0].value
    script = revocableTicket.txOut[0].pkScript

    utxo = account.UTXO(
        address="",
        txHash=revocableTicket.hash(),
        vout=0,
        ts=None,
        scriptPubKey=script,
        satoshis=1,
        maturity=0,
        tinfo=None,
    )
    utxo2 = account.UTXO(
        address="",
        txHash=revocableTicket.hash(),
        vout=0,
        ts=None,
        scriptPubKey=script,
        satoshis=amt,
        maturity=0,
        tinfo=None,
    )
    amount = {cookedAddress2: amt + 1}

    zeroed = ByteArray(b"", length=20)
    changeAddr = addrlib.AddressPubKeyHash(zeroed, mainnet,
                                           crypto.STEcdsaSecp256k1).string()
    # only the first argument for couts is a non-zero value
    cout = rpc.COut(
        addr=mainnetAddress,
        commitAmt=0,
        changeAddr=changeAddr,
        changeAmt=0,
    )

    op = OutPoint(txHash=revocableTicket.hash(), idx=0, tree=wire.TxTreeStake)
    inputPool = txscript.ExtendedOutPoint(
        op=op,
        amt=1,
        pkScript=script,
    )
    inputMain = txscript.ExtendedOutPoint(
        op=op,
        amt=amt,
        pkScript=script,
    )
    ticketAddr = addrlib.AddressScriptHash(
        ByteArray(b58decode(cookedAddress2)[2:-4]), mainnet)
    mainAddr = addrlib.AddressScriptHash(
        ByteArray(b58decode(mainnetAddress)[2:-4]), mainnet)

    # create a ticket purchasing transaction using txscript
    ticketPurchase = txscript.makeTicket(mainnet, inputPool, inputMain,
                                         ticketAddr, mainAddr, amt + 1,
                                         mainAddr, 0)
    createRawSSTx = rpcClient.createRawSSTx([utxo, utxo2], amount,
                                            [cout, cout])

    # ours is just missing the block index
    ticketPurchase.txIn[0].blockIndex = createRawSSTx.txIn[0].blockIndex
    ticketPurchase.txIn[1].blockIndex = createRawSSTx.txIn[1].blockIndex
    assert createRawSSTx.txHex() == ticketPurchase.txHex()

    amount = {mainnetAddress: amt}
    txIn = TxIn(previousOutPoint=op, valueIn=amt)
    txOut = TxOut(
        value=amt,
        version=0,
        pkScript=txscript.payToAddrScript(mainAddr),
    )
    rawTx = MsgTx(
        serType=wire.TxSerializeFull,
        version=1,
        txIn=[txIn],
        txOut=[txOut],
        lockTime=0,
        expiry=0,
        cachedHash=None,
    )

    createRawTransaction = rpcClient.createRawTransaction([utxo2], amount)

    rawTx.txIn[0].blockIndex = createRawTransaction.txIn[0].blockIndex
    assert createRawTransaction.txHex() == rawTx.txHex()

    getTxOut = rpcClient.getTxOut(missedTickets[0], 0)
    assert isinstance(getTxOut, rpc.GetTxOutResult)

    existsLiveTicket = rpcClient.existsLiveTicket(liveTickets[0])
    assert existsLiveTicket

    existsLiveTicket = rpcClient.existsLiveTicket(missedTickets[0])
    assert not existsLiveTicket

    existsLiveTickets = rpcClient.existsLiveTickets(liveTickets[:5] +
                                                    missedTickets[:1] +
                                                    liveTickets[:2])
    assert existsLiveTickets == [
        True, True, True, True, True, False, True, True
    ]

    existsMissedTickets = rpcClient.existsMissedTickets(missedTickets[:8])
    assert existsMissedTickets == [True for _ in range(8)]

    with pytest.raises(DecredError):
        rpcClient.generate(2)

    rpcClient.ping()

    searchRawTransactions = rpcClient.searchRawTransactions(mainnetAddress)
    assert isinstance(searchRawTransactions[0], rpc.RawTransactionResult)

    searchRawTransactions = rpcClient.searchRawTransactions(
        mainnetAddress, False)
    msgtx = searchRawTransactions[0]
    assert isinstance(msgtx, MsgTx)

    with pytest.raises(DecredError):
        rpcClient.sendRawTransaction(msgtx)

    assert rpcClient.setGenerate(False) is None

    with pytest.raises(DecredError):
        rpcClient.submitBlock(ByteArray(b""))

    ticketFeeInfo = rpcClient.ticketFeeInfo()
    assert isinstance(ticketFeeInfo, rpc.TicketFeeInfoResult)

    ticketFeeInfo = rpcClient.ticketFeeInfo(5, 5)
    assert isinstance(ticketFeeInfo, rpc.TicketFeeInfoResult)

    ticketsForAddress = rpcClient.ticketsForAddress(addressWithTickets)
    assert aTicket in ticketsForAddress

    ticketVWAP = rpcClient.ticketVWAP()
    assert isinstance(ticketVWAP, float)

    ticketVWAP = rpcClient.ticketVWAP(414500)
    assert isinstance(ticketVWAP, float)

    ticketVWAP = rpcClient.ticketVWAP(414500, 414510)
    assert isinstance(ticketVWAP, float)

    txFeeInfo = rpcClient.txFeeInfo()
    assert isinstance(txFeeInfo, rpc.TxFeeInfoResult)

    txFeeInfo = rpcClient.txFeeInfo(5)
    assert isinstance(txFeeInfo, rpc.TxFeeInfoResult)
    tip = txFeeInfo.feeInfoBlocks[0].height

    txFeeInfo = rpcClient.txFeeInfo(5, tip - 5, tip)
    assert isinstance(txFeeInfo, rpc.TxFeeInfoResult)

    validateAddress = rpcClient.validateAddress(mainnetAddress)
    assert isinstance(validateAddress, rpc.ValidateAddressChainResult)
    assert validateAddress.isValid

    # Address for wrong network.
    validateAddress = rpcClient.validateAddress(testnetAddress)
    assert isinstance(validateAddress, rpc.ValidateAddressChainResult)
    assert not validateAddress.isValid

    # Address is bogus.
    validateAddress = rpcClient.validateAddress(nonsense)
    assert isinstance(validateAddress, rpc.ValidateAddressChainResult)
    assert not validateAddress.isValid

    verifyChain = rpcClient.verifyChain()
    assert verifyChain

    verifyMessage = rpcClient.verifyMessage(ownedAddress, signedMessage,
                                            message)
    assert verifyMessage

    # Signature is bogus.
    verifyMessage = rpcClient.verifyMessage(
        ownedAddress,
        nonsense,
        message,
    )
    assert not verifyMessage

    version = rpcClient.version()
    assert isinstance(version["dcrd"], rpc.VersionResult)
    assert isinstance(version["dcrdjsonrpcapi"], rpc.VersionResult)
Exemple #6
0
    def test_purchase_ticket(self):
        blockchain = dcrdata.DcrdataBlockchain(":memory:", testnet,
                                               "https://testnet.dcrdata.org")
        try:
            blockchain.connect()

            def broadcast(txHex):
                print("test skipping broadcast of transaction: %s" % txHex)
                return True

            blockchain.broadcast = broadcast
            txs = {}

            def getTx(txid):
                return txs[txid]

            blockchain.tx = getTx
            addrs = []
            keys = {}

            def internal():
                privKey = Curve.generateKey()
                pkHash = crypto.hash160(privKey.pub.serializeCompressed().b)
                addr = addrlib.AddressPubKeyHash(pkHash, testnet)
                addrs.append(addr)
                keys[addr.string()] = privKey
                return addr.string()

            def priv(addr):
                return keys[addr]

            class KeySource:
                def priv(self, *a):
                    return priv(*a)

                def internal(self):
                    return internal()

            def utxosource(amt, filter):
                nextVal = 10
                total = 0
                utxos = []

                while total < amt:
                    atoms = int(nextVal * 1e8)
                    privKey = Curve.generateKey()
                    pkHash = crypto.hash160(
                        privKey.pub.serializeCompressed().b)
                    addr = addrlib.AddressPubKeyHash(pkHash, testnet)
                    addrs.append(addr)
                    addrString = addr.string()
                    keys[addrString] = privKey
                    pkScript = txscript.makePayToAddrScript(
                        addrString, testnet)
                    txHash = rando.newHash()
                    txid = reversed(txHash).hex()
                    utxos.append(
                        account.UTXO(
                            address=addrString,
                            txHash=txHash,
                            vout=0,
                            ts=int(time.time()),
                            scriptPubKey=pkScript,
                            satoshis=atoms,
                        ))
                    tx = msgtx.MsgTx.new()
                    tx.addTxOut(msgtx.TxOut(value=atoms, pkScript=pkScript))
                    txs[txid] = tx
                    total += atoms
                    nextVal *= 2
                return utxos, True

            poolPriv = Curve.generateKey()
            pkHash = crypto.hash160(poolPriv.pub.serializeCompressed().b)
            poolAddr = addrlib.AddressPubKeyHash(pkHash, testnet)
            scriptHash = crypto.hash160("some script. doesn't matter".encode())
            scriptAddr = addrlib.AddressScriptHash(scriptHash, testnet)
            ticketPrice = blockchain.stakeDiff()

            request = account.TicketRequest(
                minConf=0,
                expiry=0,
                spendLimit=ticketPrice * 2 * 1.1,
                poolAddress=poolAddr.string(),
                votingAddress=scriptAddr.string(),
                ticketFee=0,
                poolFees=7.5,
                count=2,
                txFee=0,
            )

            ticket, spent, newUTXOs = blockchain.purchaseTickets(
                KeySource(), utxosource, request)
        finally:
            blockchain.close()