How do I sign a transaction in Python?

by eric   Last Updated October 09, 2019 12:27 PM

How do I sign a P2SH transaction in Python? I am using the Python API that ships with the bitcoin source code, in directory bitcoin/test/functional. I'm using regtest.

Here is a fragment of my unit test, it works as I expect. It creates a transaction, signs it, and broadcasts it:

    # w0_rpc is a wallet created with get_wallet_rpc():
    addr1 = w0_rpc.getnewaddress()
    tx2 = CTransaction()
    # txid0 is the ID of a previous TX, v is the index of the relevant output:
    tx2.vin.append(CTxIn(COutPoint(int(txid0, 16), v), b""))
    # For now, send to a dummy address:
    tx2.vout.append(CTxOut(int(9 * COIN), CScript([OP_TRUE])))
    tx2 = w0_rpc.signrawtransactionwithwallet(ToHex(tx2))["hex"]
    #tx2.vin[0].scriptSig = CScript(signature, public_key)
    txid = n1.sendrawtransaction(tx2, True)

Instead of making the call to signrawtransactionwithwallet(), I would like to explicity assign a value to tx2.vin[0].scriptSig. I gather that this value should look something like (signature, public_key), but I can't figure out how to do it.

How do I go about this? Is there any documentation of this Python library? So far I am looking at the existing Python test cases, and following the docs for the Bitcoin RPC interface.

Tags : python


Related Questions


Updated September 27, 2017 22:27 PM

Updated September 25, 2017 20:27 PM

Updated March 30, 2018 14:27 PM

Updated April 20, 2018 14:27 PM

Updated November 30, 2017 22:27 PM