如何在 python bitcoinlib 中创建和广播具有多个输入的 tx?
How to create and broadcast tx with multiple inputs in python bitcoinlib?
我正在尝试在 bitcoinlib 中创建一个包含多个输入的交易。我从 examples in bitcoinlib repo:
粘贴了一个例子
from bitcoinlib.transactions import *
from bitcoinlib.services.services import *
import requests
amount = 19605 # in satoshis
t = Transaction(fee=5000, outputs=[Output(amount, address="to address")])
transaction_inputs = [
("b0514d3d47bffdf588e14e0324c88e4934a36423f011d634b4e379b3a65c5207", 0, "wif private key"),
("4e831f9221470f93cc0cd2e0ef0e2f731c5bbfde0e8fc2ae430b31149ffd25bb", 1, "wif private key")
]
for ti in transaction_inputs:
ki = Key(ti[2])
t.add_input(prev_txid=ti[0], output_n=ti[1], keys=ki.public())
icount = 0
for ti in transaction_inputs:
ki = Key(ti[2])
t.sign(ki.private_byte, icount)
icount += 1
print(t.verify())
rawhextx = t.raw_hex()
tx = Service().sendrawtransaction(rawhextx)
print(tx)
sendrawtransaction returns 错误。 t.verify() 为真。
我还尝试通过 POST 请求 blockchain.info:
自己广播 tx
>>> resp = requests.post("https://blockchain.info/pushtx", data={"tx": rawhextx})
>>> resp.text
'Code: -25, Error: bad-txns-inputs-missingorspent'
据我了解,这个错误意味着我的 UTXO 已经用完了,但它们没有用完(我在上面用我的 UTXO 指定了 txs)。
我想使用这个库,但如果这个库不适合我的任务,我愿意接受替代建议。
我解决了。 “output_n”是输出的索引,与“prev_txid”tx 中的发件人地址相关,而不是 tx 输入的索引。将元组中的所有第二个值更改为 0 有帮助,因为其地址的输出具有第一个索引。
from bitcoinlib.transactions import *
from bitcoinlib.services.services import *
import requests
amount = 19605 # in satoshis
t = Transaction(fee=5000, outputs=[Output(amount, address="to address")])
transaction_inputs = [
("b0514d3d47bffdf588e14e0324c88e4934a36423f011d634b4e379b3a65c5207", 0, "wif private key"),
("4e831f9221470f93cc0cd2e0ef0e2f731c5bbfde0e8fc2ae430b31149ffd25bb", 0, "wif private key")
]
for ti in transaction_inputs:
ki = Key(ti[2])
t.add_input(prev_txid=ti[0], output_n=ti[1], keys=ki.public())
icount = 0
for ti in transaction_inputs:
ki = Key(ti[2])
t.sign(ki.private_byte, icount)
icount += 1
print(t.verify())
rawhextx = t.raw_hex()
tx = Service().sendrawtransaction(rawhextx)
print(tx)
我正在尝试在 bitcoinlib 中创建一个包含多个输入的交易。我从 examples in bitcoinlib repo:
粘贴了一个例子from bitcoinlib.transactions import *
from bitcoinlib.services.services import *
import requests
amount = 19605 # in satoshis
t = Transaction(fee=5000, outputs=[Output(amount, address="to address")])
transaction_inputs = [
("b0514d3d47bffdf588e14e0324c88e4934a36423f011d634b4e379b3a65c5207", 0, "wif private key"),
("4e831f9221470f93cc0cd2e0ef0e2f731c5bbfde0e8fc2ae430b31149ffd25bb", 1, "wif private key")
]
for ti in transaction_inputs:
ki = Key(ti[2])
t.add_input(prev_txid=ti[0], output_n=ti[1], keys=ki.public())
icount = 0
for ti in transaction_inputs:
ki = Key(ti[2])
t.sign(ki.private_byte, icount)
icount += 1
print(t.verify())
rawhextx = t.raw_hex()
tx = Service().sendrawtransaction(rawhextx)
print(tx)
sendrawtransaction returns 错误。 t.verify() 为真。 我还尝试通过 POST 请求 blockchain.info:
自己广播 tx>>> resp = requests.post("https://blockchain.info/pushtx", data={"tx": rawhextx})
>>> resp.text
'Code: -25, Error: bad-txns-inputs-missingorspent'
据我了解,这个错误意味着我的 UTXO 已经用完了,但它们没有用完(我在上面用我的 UTXO 指定了 txs)。 我想使用这个库,但如果这个库不适合我的任务,我愿意接受替代建议。
我解决了。 “output_n”是输出的索引,与“prev_txid”tx 中的发件人地址相关,而不是 tx 输入的索引。将元组中的所有第二个值更改为 0 有帮助,因为其地址的输出具有第一个索引。
from bitcoinlib.transactions import *
from bitcoinlib.services.services import *
import requests
amount = 19605 # in satoshis
t = Transaction(fee=5000, outputs=[Output(amount, address="to address")])
transaction_inputs = [
("b0514d3d47bffdf588e14e0324c88e4934a36423f011d634b4e379b3a65c5207", 0, "wif private key"),
("4e831f9221470f93cc0cd2e0ef0e2f731c5bbfde0e8fc2ae430b31149ffd25bb", 0, "wif private key")
]
for ti in transaction_inputs:
ki = Key(ti[2])
t.add_input(prev_txid=ti[0], output_n=ti[1], keys=ki.public())
icount = 0
for ti in transaction_inputs:
ki = Key(ti[2])
t.sign(ki.private_byte, icount)
icount += 1
print(t.verify())
rawhextx = t.raw_hex()
tx = Service().sendrawtransaction(rawhextx)
print(tx)