Signature

如何使用 Python 庫簽署交易十六進制

  • July 26, 2021

我有一個未簽名的交易十六進制。現在我想用我在 Python 中的私鑰簽署這個交易輸入。有沒有 Python 庫?

事務十六進制

010000000414ae2264cdbe754b9ae4be18d84bfeb4f578af553b9b9e4f9cb2303d04ee9e45000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffffec15d27b74516fefd921cecbe043ea63124d28a3903aef8fb1682ccc926b1c62000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffff9878f76e1424c1d1eeb6b15d06902dd8f0c78f9bdb61263e4ca3ae68c571a292000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acfffffffffdac7f1c83b01a8924a8965d356b804c2608bc261fc18041116ddb4a143bc499000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffff0336150000000000001976a9141485d9d03b41aaa9dca7d70d7f63ff4a0826100e88ac00000000000000001e6a1c246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e92180000000000001976a91458b6e991b45487df810f4d96d5315da739637f1788ac00000000

解碼交易

{
  "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"
           ]
        }
     }
  ]
}

更新 1

感謝 hartmut 解決了。

1 安裝 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 使用 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

pybitcointools,它允許你使用 sign(tx, index, priv) 函式來做到這一點。還有其他的,但我至少發現這個最容易使用。

編輯:你有很多我可以看到的輸入..以下可能會有所幫助

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

它遍歷交易的所有輸入以對它們進行簽名。

引用自:https://bitcoin.stackexchange.com/questions/46735