pycoin学习笔记(4) 生成交易,使用jsonrpc签名并广播

注:本篇所有实验均在比特币测试网络(testnet)下进行

前置步骤:

1)下载比特币钱包客户端: bitcoin core. 选择testnet并开启jsonrpc的相关功能。

2)去网上乞讨一些比特币


这是我乞讨得到的1.03个比特币

该交易的hex表示为:

0200000001ae2e375fe76f30e3451e2f5fd7441c66ce884a189886abe1e6eccbeff6b6cda8000000006a473044022010ce551c1f7e1f8fa4a4a9d4d67c1acf9581ede52e0b1030c6974b798721c0ff02203191b63e3a19de477f30034c749056afce104d8d0e5365e95edc1aaa34392b360121037ca1f015fbaf30ca61d9a9e555c5fb35cb8ee63c6a5c4b28f6f1194ce289e408feffffff02f8ff43b3650000001976a91418022dedf153d20b8bf10adae95c12eb6deb8de488ac4eb62c060000000017a91418c1047730048de078cf35649aa6ef2b6827a07187c9a41400

接下里,实验一下将我乞讨得到的这1.03btc转出0.2btc到其他比特币地址上。

正式撸码:

from pycoin.tx.Tx import Tx
from pycoin.tx.tx_utils import create_tx

tx_hex = "0200000001ae2e375fe76f30e3451e2f5fd7441c66ce884a189886abe1e6eccbeff6b6cda8000000006a473044022010ce551c1f7e1f8fa4a4a9d4d67c1acf9581ede52e0b1030c6974b798721c0ff02203191b63e3a19de477f30034c749056afce104d8d0e5365e95edc1aaa34392b360121037ca1f015fbaf30ca61d9a9e555c5fb35cb8ee63c6a5c4b28f6f1194ce289e408feffffff02f8ff43b3650000001976a91418022dedf153d20b8bf10adae95c12eb6deb8de488ac4eb62c060000000017a91418c1047730048de078cf35649aa6ef2b6827a07187c9a41400"
tx = Tx.from_hex(tx_hex)
spendable = tx.tx_outs_as_spendable()[1]
to_address = "mh4xx1pgjqLmjVUJxfdj3qV8BJaYARLdh9"
change_address = "2MxrJiqSAhss8Hm8PJM76cFzX8RS6Ve3afn"

tx_hex即我乞讨的那次交易,将作为新交易的资金来源;to_address表示转账地址, change_address表示找零地址。

new_tx = create_tx([spendable],[(to_address, 20000000), change_address])
new_tx_hex = new_tx.as_hex()
print(new_tx_hex)
#0100000001d56e1e4579ac2b407c105eb02d9749a73defd082837229dc290260d199f828350100000000ffffffff02002d3101000000001976a9141105fb9ff85657afb12a5c814776346b23db3a6688ac3e62fb040000000017a9143d7b2397ebb714da591028219b1162d6f21b40378700000000

利用pycoin,创建此次转账交易,即new_tx。20000000的单位是聪,一聪是亿分之一btc,所以其等于0.2btc

from bitcoinrpc.authproxy import AuthServiceProxy
conn = AuthServiceProxy("http://%s:%[email protected]:18332"%("fftest","fftest123"))
print(conn.decoderawtransaction(new_tx_hex))
#{'txid': 'd8088b4821057a3e9e168d58c7d4cbc85b1728b1145da3e4e773d0d65141effa', 'hash': 'd8088b4821057a3e9e168d58c7d4cbc85b1728b1145da3e4e773d0d65141effa', 'version': 1, 'size': 117, 'vsize': 117, 'locktime': 0, 'vin': [{'txid': '3528f899d1600229dc29728382d0ef3da749972db05e107c402bac79451e6ed5', 'vout': 1, 'scriptSig': {'asm': '', 'hex': ''}, 'sequence': 4294967295}], 'vout': [{'value': Decimal('0.20000000'), 'n': 0, 'scriptPubKey': {'asm': 'OP_DUP OP_HASH160 1105fb9ff85657afb12a5c814776346b23db3a66 OP_EQUALVERIFY OP_CHECKSIG', 'hex': '76a9141105fb9ff85657afb12a5c814776346b23db3a6688ac', 'reqSigs': 1, 'type': 'pubkeyhash', 'addresses': ['mh4xx1pgjqLmjVUJxfdj3qV8BJaYARLdh9']}}, {'value': Decimal('0.83583550'), 'n': 1, 'scriptPubKey': {'asm': 'OP_HASH160 3d7b2397ebb714da591028219b1162d6f21b4037 OP_EQUAL', 'hex': 'a9143d7b2397ebb714da591028219b1162d6f21b403787', 'reqSigs': 1, 'type': 'scripthash', 'addresses': ['2MxrJiqSAhss8Hm8PJM76cFzX8RS6Ve3afn']}}]}
print(conn.signrawtransaction(new_tx_hex))
#{'hex': '01000000000101d56e1e4579ac2b407c105eb02d9749a73defd082837229dc290260d199f8283501000000171600148c943eca5ee4d2fef125ba53efc267b1f012a7abffffffff02002d3101000000001976a9141105fb9ff85657afb12a5c814776346b23db3a6688ac3e62fb040000000017a9143d7b2397ebb714da591028219b1162d6f21b4037870247304402204069d19f33b5b3b2bbfd8808059b73fccb288d9bd7273eae9178c24232d0ab360220723dd5d2f16a63ff3794e4a8c5381a616c6f5266b4427dd9808674a51a914dc4012103edaad325dd4297b31a5953aec10e6a0240f404ba4a4eb72f51be2a4b7ae8c02100000000', 'complete': True}

此处连入bticoin core钱包提供的server,使用的库是python-bitcoinrpc。首先解码看一下new_tx,接下来对其进行签名。注意由于私钥在钱包里,所以此处不在本地离线签名,而是让钱包进行签名,并且返回签名结果。

signed_new_tx_hex = "01000000000101d56e1e4579ac2b407c105eb02d9749a73defd082837229dc290260d199f8283501000000171600148c943eca5ee4d2fef125ba53efc267b1f012a7abffffffff02002d3101000000001976a9141105fb9ff85657afb12a5c814776346b23db3a6688ac3e62fb040000000017a9143d7b2397ebb714da591028219b1162d6f21b4037870247304402204069d19f33b5b3b2bbfd8808059b73fccb288d9bd7273eae9178c24232d0ab360220723dd5d2f16a63ff3794e4a8c5381a616c6f5266b4427dd9808674a51a914dc4012103edaad325dd4297b31a5953aec10e6a0240f404ba4a4eb72f51be2a4b7ae8c02100000000"
print(conn.sendrawtransaction(signed_new_tx_hex))
#15be84a268ff2747b1646010c1533213cba383b0cd920cc79ed97c927eb0e93f

这里也不用json解析签名结果了,直接手工黏贴,并且使用jsonrpc将签名好的交易广播到比特币网络中去。其返回结果即为交易id,再次查看之:

pycoin学习笔记(4) 生成交易,使用jsonrpc签名并广播_第1张图片

该交易已被比特币网络接受。地址mh4xx1pgjqLmjVUJxfdj3qV8BJaYARLdh9现在有了0.2btc。


本次实验中,最重要的一环“对交易进行签名“,是委托bitcoin core钱包做的,并且由于其地址是p2sh地址,如过要本地解锁,除了私钥外,还需要redeem script,这个还没研究如何获取。下一次实验,将使用地址mh4xx1pgjqLmjVUJxfdj3qV8BJaYARLdh9上的0.2btc作为资金来源,在本地离线对交易进行签名,然后再用jsonrpc将其广播出去。这样,第三方钱包的作用仅仅是广播交易,其重要性就不言而喻了。




你可能感兴趣的:(杂项,pycoin)