pycoin学习笔记(5) 本地对交易签名(p2pkh)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lucyTheSlayer/article/details/81032630

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

这次实验对交易进行本地签名,即不利用第三方钱包的签名功能。

本次的场景是,将mh4xx1pgjqLmjVUJxfdj3qV8BJaYARLdh9上的0.2btc转到2N113Ujyc6Pj7kvnZMyZNpAtAfj9EzoHStQ上去。

开始撸码:

from pycoin.ecdsa import generator_secp256k1, public_pair_for_secret_exponent
from pycoin.encoding import wif_to_secret_exponent, public_pair_to_sec
from pycoin.tx.Tx import Tx
from pycoin.tx.pay_to import build_hash160_lookup
from pycoin.tx.tx_utils import create_tx


exponent = wif_to_secret_exponent("KyBuY4RgVcvGfyqhv3DmET5UpYT3rA74nR3uY7cCA1JLXbDiSgsS")

初始化mh4xx1pgjqLmjVUJxfdj3qV8BJaYARLdh9的私钥。(这是十分机密的东西,绝对不能暴露在网络上,但由于是testnet,便无所谓了。)


src_tx_hex = "01000000000101d56e1e4579ac2b407c105eb02d9749a73defd082837229dc290260d199f8283501000000171600148c943eca5ee4d2fef125ba53efc267b1f012a7abffffffff02002d3101000000001976a9141105fb9ff85657afb12a5c814776346b23db3a6688ac3e62fb040000000017a9143d7b2397ebb714da591028219b1162d6f21b4037870247304402204069d19f33b5b3b2bbfd8808059b73fccb288d9bd7273eae9178c24232d0ab360220723dd5d2f16a63ff3794e4a8c5381a616c6f5266b4427dd9808674a51a914dc4012103edaad325dd4297b31a5953aec10e6a0240f404ba4a4eb72f51be2a4b7ae8c02100000000"
src_tx = Tx.from_hex(src_tx_hex)
spendable = src_tx.tx_outs_as_spendable()[0]

初始化即将花费掉的交易。

to_address = "2N113Ujyc6Pj7kvnZMyZNpAtAfj9EzoHStQ"
unsigned_new_tx = create_tx([spendable],[(to_address, 19990000)])
unsigned_new_tx_hex = unsigned_new_tx.as_hex()
print(unsigned_new_tx_hex)
#01000000013fe9b07e927cd99ec70c92cdb083a3cb133253c1106064b14727ff68a284be150000000000ffffffff01f00531010000000017a914551268f89b61451ad1bd9912d8277b31e17764d18700000000

创建待签名的交易,即将0.2btc转给2N113Ujyc6Pj7kvnZMyZNpAtAfj9EzoHStQ。此处实际是0.1999btc,少掉的0.0001btc其实是给矿工的辛苦费。

rom bitcoinrpc.authproxy import AuthServiceProxy
conn = AuthServiceProxy("http://%s:%s@192.168.1.103:18332"%("fftest","fftest123"))
print(conn.decoderawtransaction(unsigned_new_tx_hex))
#{'txid': '80436f3af55c5940e6ff7babd98699b3a7b0f4a698b3eb61c3a9a561510e5ba3', 'hash': '80436f3af55c5940e6ff7babd98699b3a7b0f4a698b3eb61c3a9a561510e5ba3', 'version': 1, 'size': 83, 'vsize': 83, 'locktime': 0, 'vin': [{'txid': '15be84a268ff2747b1646010c1533213cba383b0cd920cc79ed97c927eb0e93f', 'vout': 0, 'scriptSig': {'asm': '', 'hex': ''}, 'sequence': 4294967295}], 'vout': [{'value': Decimal('0.19990000'), 'n': 0, 'scriptPubKey': {'asm': 'OP_HASH160 551268f89b61451ad1bd9912d8277b31e17764d1 OP_EQUAL', 'hex': 'a914551268f89b61451ad1bd9912d8277b31e17764d187', 'reqSigs': 1, 'type': 'scripthash', 'addresses': ['2N113Ujyc6Pj7kvnZMyZNpAtAfj9EzoHStQ']}}]}

用jsonrpc查看下新交易,此步骤无关紧要。

solver = build_hash160_lookup([exponent])
signed_new_tx = unsigned_new_tx.sign(solver)
print(signed_new_tx.bad_signature_count())
#0
signed_new_tx_hex = signed_new_tx.as_hex()
print(signed_new_tx_hex)
#01000000013fe9b07e927cd99ec70c92cdb083a3cb133253c1106064b14727ff68a284be15000000006a47304402201216e53509fccdd6d71a6a4f5ac029921cc3ef49196f64aeddc97660e8149755022039eb50609def923b09e43c9f5d08649a1349249afafc4cae918e350f8ac1f3370121027d3bac55298238844b087f727c63965f728bda4955c778c7609053646f03977dffffffff01f00531010000000017a914551268f89b61451ad1bd9912d8277b31e17764d18700000000

这里到了最关键的签名步骤了,首先构造出一个私钥的字典,然后直接调用sign进行签名,得到签好名后的交易。复杂的东西都被pycoin封装好了,更进一步需要对pycoin的实现源码好好研究。

txid = conn.sendrawtransaction(signed_new_tx_hex)
print(txid)
#dae85dc12e57d42bb6a7d8d3627fbff628751cfa44eb96c9edd467a1d82014b2

广播到比特币网络中:https://test-insight.bitpay.com/tx/dae85dc12e57d42bb6a7d8d3627fbff628751cfa44eb96c9edd467a1d82014b2

阅读更多
相关热词
换一批

没有更多推荐了,返回首页