4 - 5 自訂交易自動化
bitcoind 提供一個自動挑出 UTXO, 計算手續費的方法 fundrawtransaction
用法很簡單,先創造一個沒有 input 的交易,並指定要傳多少 BTC
$ unfinishedtx=$(bitcoin-cli -named createrawtransaction inputs='''[]''' outputs='''{"'$recipient'": 0.63 }''')
接者就可以用 fundrawtransaction 讓 bitcoind 自己挑
$ bitcoin-cli -named fundrawtransaction hexstring=$unfinishedtx
{
"hex": "02000000013e6aa44b24cd9ccca813204fbb2dbaed5cda94722528aaff96bc0fa4e933189a0100000000feffffff02c04dc1030000000017a914a9974100aeee974a20cda9a2f545704a0ab54fdc87c0ea0e00000000001976a914ad7f3adbc38115d0ee68c8c2df06fd00604fabe088ac00000000",
"changepos": 1,
"fee": 0.00022400
}
我們一樣可以將交易的 hex 弄成變數,方便之後的使用
$ rawtxhex3=$(bitcoin-cli fundrawtransaction hexstring=$unfinishedtx | jq -r '.hex')
如果怕挑出來的會有問題,可以看看是不是正確
$ bitcoin-cli -named decoderawtransaction hexstring=$rawtxhex3
{
"txid": "c217d4314b6d8965beac6235972188db4e4c9e13d78d126ade36b359f0e5858e",
"hash": "c217d4314b6d8965beac6235972188db4e4c9e13d78d126ade36b359f0e5858e",
"version": 2,
"size": 117,
"vsize": 117,
"locktime": 0,
"vin": [
{
"txid": "9a1833e9a40fbc96ffaa28257294da5cedba2dbb4f2013a8cc9ccd244ba46a3e",
"vout": 1,
"scriptSig": {
"asm": "",
"hex": ""
},
"sequence": 4294967294
}
],
"vout": [
{
"value": 0.00977600,
"n": 0,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 70d2a885d58edb1a3d0e7e06587687f69dfdb9ce OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a91470d2a885d58edb1a3d0e7e06587687f69dfdb9ce88ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"mqoWLLK3Z1kGEEGF94iPomFApAg4WcExVM"
]
}
},
{
"value": 0.63000000,
"n": 1,
"scriptPubKey": {
"asm": "OP_HASH160 a9974100aeee974a20cda9a2f545704a0ab54fdc OP_EQUAL",
"hex": "a914a9974100aeee974a20cda9a2f545704a0ab54fdc87",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF"
]
}
}
]
}
確認無誤就可以 signe & seal & deliver!