4 - 4 送出比特幣 - Raw Transaction - 2
一開始我們用 sendtoaddress 直接送比特幣到地址,接下來用 raw transaction 送出交易,不過都是直接送出一大筆比特幣
現在再會用 JQ 之後我們可以送出小額的比特幣,也就是要指定找零錢的收款方
可以用 getrawchangwaddress
得到一組新地址,用來收零錢(目前我不知道跟 getnewaddres
有什麼差別)
$ changeaddress=$(bitcoin-cli getrawchangeaddress)
$ echo $changeaddress
mwViMPJuypbwXP3BYoSnCX8sXVHxjVk4CP
另一點是通常我們要從 UTXO 裡面組合出足夠的 BTC 數量,因為 UTXO 可以一直切分,所以有很多的 UTXO 很正常
例如要用第一個和第二個 UTXO 當作交易 vin(總和為 2.14),我要發給收款方1.5,手續費 0.005,
找回自己 2.14 - 1.5 - 0.005 = 0.64
先指定好變數
$ utxo_txid_1=$(bitcoin-cli listunspent | jq -r '.[0] | .txid')
$ utxo_vout_1=$(bitcoin-cli listunspent | jq -r '.[0] | .vout')
$ utxo_txid_2=$(bitcoin-cli listunspent | jq -r '.[1] | .txid')
$ utxo_vout_2=$(bitcoin-cli listunspent | jq -r '.[1] | .vout')
寫好 raw transaction
$ rawtxhex2=$(bitcoin-cli -named createrawtransaction
> inputs='''[ {"txid": "'$utxo_txid_1'", "vout": '$utxo_vout_1'},
{"txid": "'$utxo_txid_2'", "vout": '$utxo_vout_2'} ]'''
> outputs='''{"'$recipient'": 1.5,"'$changeaddress'": 0.64}''')
簽名
$ signedtx2=$(bitcoin-cli -named signrawtransaction hexstring=$rawtxhex2 | jq -r '.hex')
送出
$ bitcoin-cli -named sendrawtransaction hexstring=$signedtx2
9a1833e9a40fbc96ffaa28257294da5cedba2dbb4f2013a8cc9ccd244ba46a3e
so called Signed, Seal, Deliver!!!
可以來這裡查詢測試網路的交易:https://live.blockcypher.com/