2

I have a unsigned transaction hex. Now I want to sign this transaction input with my private key in Python. Are there any Python libraries?

Transaction Hex

010000000414ae2264cdbe754b9ae4be18d84bfeb4f578af553b9b9e4f9cb2303d04ee9e45000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffffec15d27b74516fefd921cecbe043ea63124d28a3903aef8fb1682ccc926b1c62000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffff9878f76e1424c1d1eeb6b15d06902dd8f0c78f9bdb61263e4ca3ae68c571a292000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acfffffffffdac7f1c83b01a8924a8965d356b804c2608bc261fc18041116ddb4a143bc499000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffff0336150000000000001976a9141485d9d03b41aaa9dca7d70d7f63ff4a0826100e88ac00000000000000001e6a1c246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e92180000000000001976a91458b6e991b45487df810f4d96d5315da739637f1788ac00000000

Decoded Transaction

{
   "txid":"6c521937d8fa889d71609ebed2e23b694cbca89778c8b8d967167e5a4c9d5b78",
   "size":381,
   "version":1,
   "locktime":0,
   "vin":[
      {
         "txid":"459eee043d30b29c4f9e9b3b55af78f5b4fe4bd818bee49a4b75becd6422ae14",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      },
      {
         "txid":"621c6b92cc2c68b18fef3a90a3284d1263ea43e0cbce21d9ef6f51747bd215ec",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      },
      {
         "txid":"92a271c568aea34c3e2661db9b8fc7f0d82d90065db1b6eed1c124146ef77898",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      },
      {
         "txid":"99c43b144adb6d114180c11f26bc08264c806b355d96a824891ab0831c7facfd",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      }
   ],
   "vout":[
      {
         "value":5.43e-5,
         "n":0,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 1485d9d03b41aaa9dca7d70d7f63ff4a0826100e OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a9141485d9d03b41aaa9dca7d70d7f63ff4a0826100e88ac",
            "reqSigs":1,
            "type":"pubkeyhash",
            "addresses":[
               "12sWrxRY7E7Nhmuyjbz4TtGE9jRewGqEZD"
            ]
         }
      },
      {
         "value":0,
         "n":1,
         "scriptPubKey":{
            "asm":"OP_RETURN 246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e",
            "hex":"6a1c246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e",
            "type":"nulldata"
         }
      },
      {
         "value":6.29e-5,
         "n":2,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac",
            "reqSigs":1,
            "type":"pubkeyhash",
            "addresses":[
               "1965areciqapsuL2hsia2yKkRLfAsH1smG"
            ]
         }
      }
   ]
}

Update 1

Solved thanks to hartmut.

1 Install pybitcointools

git clone https://github.com/vbuterin/pybitcointools.git
cd pybitcointools
sudo python setup.py # I got an error in Python3. So I used Python2.

2 Sign by using pybitcointools

import bitcoin

def signAllIns(aTx, priv, nbIns=None):
    signedTx = aTx
    utxo = nbIns
    if utxo == None:
        utxo = len(bitcoin.unspent(bitcoin.privtoaddr(priv)))
    for i in range(utxo):
        signedTx = bitcoin.sign(signedTx, i, priv)
    return signedTx

deserializeHex = bitcoin.deserialize(HEX)
ins = deserializeHex['ins']
signedTx = signAllIns(HEX, PRIVATE_KEY, len(ins))

# Broadcast signedTx here
3

There is pybitcointools, which allows you to do just that using the sign(tx, index, priv) function. There are others but I at least found this one the easiest to work with.

EDIT: You have many inputs I can see..the following could help

def signAllIns(aTx, priv, nbIns=None):
    signedTx = aTx
    utxo = nbIns

    if utxo == None:
        utxo = len(unspent(privtoaddr(priv)))

    for i in range(utxo):
        signedTx = sign(signedTx, i, priv)
    return signedTx

It loops over all the inputs of the transaction to sign them all.

  • Thank. I will try it and let you know the result later. At first, I'll find how to convert my "tx hex" to "aTx" object. – zono Jul 26 '16 at 16:07
  • sure, let me know. The aTx parameter is a tx hex. No need to convert x) – hartmut Jul 26 '16 at 16:13
  • It works when I set nbIns=None. However I got 'IndexError: list index out of range' when I use another hex. I guess it does not match utxos between the hex and pybitcointool generated one. Do you know how to convert from my hex to utxo? I want to set it as 'nbIns' parameter. – zono Jul 27 '16 at 3:29
  • I'm sorry to bother you. I think I made it. I will post the result after my transaction is confirmed. – zono Jul 27 '16 at 3:58
  • Cool. The nbIns parameter is an int. – hartmut Jul 27 '16 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.