天天看點

bitcoin-cli指令行組裝交易并成功廣播

=====交易組裝過程實驗與簽名廣播過程=====

參考《精通比特币》一書

1、在百度雲上開了台ubuntu伺服器,跑比特币核心節點和預設錢包,就操作這個預設錢包

2、檢視錢包資訊

bitcoin-cli getwalletinfo
{
  "walletname": "",
  "walletversion": 169900,
  "balance": 0.00028705,
  "unconfirmed_balance": 0.00000000,
  "immature_balance": 0.00000000,
  "txcount": 1,
  "keypoololdest": 1573899758,
  "keypoolsize": 996,
  "keypoolsize_hd_internal": 1000,
  "unlocked_until": 0,
  "paytxfee": 0.00000000,
  "hdseedid": "0050867795360ed580ae46ff96e9f73e9b0d6a82",
  "hdmasterkeyid": "0050867795360ed580ae46ff96e9f73e9b0d6a82",
  "private_keys_enabled": true
}
           

關鍵資訊,此錢包有 0.00028705 BTC的餘額

"balance": 0.00028705,

3、檢視錢包中的未花費交易

bitcoin-cli listunspent
[
  {
    "txid": "b68683b017be3845147056175a5a26ca30a049fb5325f55432429592591403ec",
    "vout": 0,
    "address": "38RjyvZ4CNdv11rbwLJvt6iTHMptAzJLM9",
    "label": "",
    "account": "",
    "redeemScript": "0014eb387703e36b54890827fde17152b70bd02089c7",
    "scriptPubKey": "a91449e55565c2b31b5a834e1c0078a7ed99173b1f5787",
    "amount": 0.00028705,
    "confirmations": 261,
    "spendable": true,
    "solvable": true,
    "safe": true
  }
]
           

關鍵資訊:

在位址:38RjyvZ4CNdv11rbwLJvt6iTHMptAzJLM9 上存在一筆未花費交易0.00028705

4、或者通過我搭建的electrumx接口getutxo查這個位址的未花費交易

curl --data-binary '{"jsonrpc": "2.0", "id":"curltest", "method":"/bitcoin/getutxo/get_utxo","address":"38RjyvZ4CNdv11rbwLJvt6iTHMptAzJLM9"}' -H 'content-type: application/json;' http://106.13.51.96:8001

{"jsonrpc": "2.0", "result": [{"tx_hash": "b68683b017be3845147056175a5a26ca30a049fb5325f55432429592591403ec", "tx_pos": 0, "height": 605461, "value": 28705}], "id": 1}

           

5、通過txid擷取這筆交易輸出的詳情,後面的0是表示這筆交易的第一個vout

bitcoin-cli gettxout "b68683b017be3845147056175a5a26ca30a049fb5325f55432429592591403ec" 0
{
  "bestblock": "000000000000000000024e060c7d74f0d2c9779b2adfb0c4c2f3e50b737013d1",
  "confirmations": 262,
  "value": 0.00028705,
  "scriptPubKey": {
    "asm": "OP_HASH160 49e55565c2b31b5a834e1c0078a7ed99173b1f57 OP_EQUAL",
    "hex": "a91449e55565c2b31b5a834e1c0078a7ed99173b1f5787",
    "reqSigs": 1,
    "type": "scripthash",
    "addresses": [
      "38RjyvZ4CNdv11rbwLJvt6iTHMptAzJLM9"
    ]
  },
  "coinbase": false
}
           

6、使用getnewaddress擷取一個新的位址

bitcoin-cli getnewaddress
37xD5z25Lzgdjw9TxQ6YkSHi7YoBucLDGi
           

7、使用未花費的交易向新位址轉 0.0001 BTC,組這個交易體,這個是比較關鍵的一步

bitcoin-cli createrawtransaction '[{"txid":"b68683b017be3845147056175a5a26ca30a049fb5325f55432429592591403ec","vout":0}]' '{"37xD5z25Lzgdjw9TxQ6YkSHi7YoBucLDGi":0.0001,"38RjyvZ4CNdv11rbwLJvt6iTHMptAzJLM9":0.00013705}'

0200000001ec0314599295423254f52553fb49a030ca265a5a175670144538be17b08386b60000000000ffffffff02102700000000000017a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87893500000000000017a91449e55565c2b31b5a834e1c0078a7ed99173b1f578700000000
           

8、decoderawtransaction,解析原始交易

bitcoin-cli decoderawtransaction 0200000001ec0314599295423254f52553fb49a030ca265a5a175670144538be17b08386b60000000000ffffffff02102700000000000017a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87893500000000000017a91449e55565c2b31b5a834e1c0078a7ed99173b1f578700000000 true

{
  "txid": "782c5fceb431838e654b4610bdd8b61e5cca738767bb1233004bbebf8cafe235",
  "hash": "782c5fceb431838e654b4610bdd8b61e5cca738767bb1233004bbebf8cafe235",
  "version": 2,
  "size": 115,
  "vsize": 115,
  "weight": 460,
  "locktime": 0,
  "vin": [
    {
      "txid": "b68683b017be3845147056175a5a26ca30a049fb5325f55432429592591403ec",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.00010000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 44b04ff5110cc8f2ba7e78c0e33e241b298285fd OP_EQUAL",
        "hex": "a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "37xD5z25Lzgdjw9TxQ6YkSHi7YoBucLDGi"
        ]
      }
    },
    {
      "value": 0.00013705,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_HASH160 49e55565c2b31b5a834e1c0078a7ed99173b1f57 OP_EQUAL",
        "hex": "a91449e55565c2b31b5a834e1c0078a7ed99173b1f5787",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "38RjyvZ4CNdv11rbwLJvt6iTHMptAzJLM9"
        ]
      }
    }
  ]
}
           

檢查交易體沒有問題,因為沒有簽名,是以這個交易體的vin部分的scriptSig是空的

"scriptSig": {

        "asm": "",

        "hex": ""

      },

9、使用錢包簽署交易 signrawtransactionwithwallet 

bitcoin-cli signrawtransaction 0200000001ec0314599295423254f52553fb49a030ca265a5a175670144538be17b08386b60000000000ffffffff02102700000000000017a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87893500000000000017a91449e55565c2b31b5a834e1c0078a7ed99173b1f578700000000 '[{"txid": "b68683b017be3845147056175a5a26ca30a049fb5325f55432429592591403ec","vout": 0,"scriptPubKey": "a91449e55565c2b31b5a834e1c0078a7ed99173b1f5787","redeemScript": "0014eb387703e36b54890827fde17152b70bd02089c7","amount": 0.00028705}]'

{
  "hex": "02000000000101ec0314599295423254f52553fb49a030ca265a5a175670144538be17b08386b60000000017160014eb387703e36b54890827fde17152b70bd02089c7ffffffff02102700000000000017a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87893500000000000017a91449e55565c2b31b5a834e1c0078a7ed99173b1f57870247304402204bd2fc34313d8bb9b8577c9edcd55fd82c60cc93a5139d5e874e55dc3eb75098022042b6ae3666204ec845cc5c713b26c58dbaca313e1b21e3e07c9c7573ace88968012102df45c8df0c95ec67bde03835fedec8296946e9628bf7be69f9a64e08bf1fdc5300000000",
  "complete": true
}
           

10、可以使用decoderawtransaction檢視下簽名過的交易體

bitcoin-cli decoderawtransaction 02000000000101ec0314599295423254f52553fb49a030ca265a5a175670144538be17b08386b60000000017160014eb387703e36b54890827fde17152b70bd02089c7ffffffff02102700000000000017a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87893500000000000017a91449e55565c2b31b5a834e1c0078a7ed99173b1f57870247304402204bd2fc34313d8bb9b8577c9edcd55fd82c60cc93a5139d5e874e55dc3eb75098022042b6ae3666204ec845cc5c713b26c58dbaca313e1b21e3e07c9c7573ace88968012102df45c8df0c95ec67bde03835fedec8296946e9628bf7be69f9a64e08bf1fdc5300000000 true

  "vin": [
    {
      "txid": "b68683b017be3845147056175a5a26ca30a049fb5325f55432429592591403ec",
      "vout": 0,
      "scriptSig": {
        "asm": "0014eb387703e36b54890827fde17152b70bd02089c7",
        "hex": "160014eb387703e36b54890827fde17152b70bd02089c7"
      },
      "txinwitness": [
        "304402204bd2fc34313d8bb9b8577c9edcd55fd82c60cc93a5139d5e874e55dc3eb75098022042b6ae3666204ec845cc5c713b26c58dbaca313e1b21e3e07c9c7573ace8896801",
        "02df45c8df0c95ec67bde03835fedec8296946e9628bf7be69f9a64e08bf1fdc53"
      ],
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.00010000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 44b04ff5110cc8f2ba7e78c0e33e241b298285fd OP_EQUAL",
        "hex": "a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "37xD5z25Lzgdjw9TxQ6YkSHi7YoBucLDGi"
        ]
      }
    },
    {
      "value": 0.00013705,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_HASH160 49e55565c2b31b5a834e1c0078a7ed99173b1f57 OP_EQUAL",
        "hex": "a91449e55565c2b31b5a834e1c0078a7ed99173b1f5787",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "38RjyvZ4CNdv11rbwLJvt6iTHMptAzJLM9"
        ]
      }
    }
  ]
}

           

11、廣播交易: sendrawtransaction

bitcoin-cli sendrawtransaction 02000000000101ec0314599295423254f52553fb49a030ca265a5a175670144538be17b08386b60000000017160014eb387703e36b54890827fde17152b70bd02089c7ffffffff02102700000000000017a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87893500000000000017a91449e55565c2b31b5a834e1c0078a7ed99173b1f57870247304402204bd2fc34313d8bb9b8577c9edcd55fd82c60cc93a5139d5e874e55dc3eb75098022042b6ae3666204ec845cc5c713b26c58dbaca313e1b21e3e07c9c7573ace88968012102df45c8df0c95ec67bde03835fedec8296946e9628bf7be69f9a64e08bf1fdc5300000000
51ff35fdb4d9e68a4e6932ab42881e20ba520c70c8e3c7bcbfe3f92fb51b3f7d

51ff35fdb4d9e68a4e6932ab42881e20ba520c70c8e3c7bcbfe3f92fb51b3f7d
           

12、通過txid到節點讀取原始交易: getrawtransaction

bitcoin-cli getrawtransaction 51ff35fdb4d9e68a4e6932ab42881e20ba520c70c8e3c7bcbfe3f92fb51b3f7d 1
{
  "txid": "51ff35fdb4d9e68a4e6932ab42881e20ba520c70c8e3c7bcbfe3f92fb51b3f7d",
  "hash": "fd92080f8ae12ab03d1b54497828ecdeedcd73b014e42d13b25dd8786a995a98",
  "version": 2,
  "size": 247,
  "vsize": 166,
  "weight": 661,
  "locktime": 0,
  "vin": [
    {
      "txid": "b68683b017be3845147056175a5a26ca30a049fb5325f55432429592591403ec",
      "vout": 0,
      "scriptSig": {
        "asm": "0014eb387703e36b54890827fde17152b70bd02089c7",
        "hex": "160014eb387703e36b54890827fde17152b70bd02089c7"
      },
      "txinwitness": [
        "304402204bd2fc34313d8bb9b8577c9edcd55fd82c60cc93a5139d5e874e55dc3eb75098022042b6ae3666204ec845cc5c713b26c58dbaca313e1b21e3e07c9c7573ace8896801",
        "02df45c8df0c95ec67bde03835fedec8296946e9628bf7be69f9a64e08bf1fdc53"
      ],
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.00010000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 44b04ff5110cc8f2ba7e78c0e33e241b298285fd OP_EQUAL",
        "hex": "a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "37xD5z25Lzgdjw9TxQ6YkSHi7YoBucLDGi"
        ]
      }
    },
    {
      "value": 0.00013705,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_HASH160 49e55565c2b31b5a834e1c0078a7ed99173b1f57 OP_EQUAL",
        "hex": "a91449e55565c2b31b5a834e1c0078a7ed99173b1f5787",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "38RjyvZ4CNdv11rbwLJvt6iTHMptAzJLM9"
        ]
      }
    }
  ],
  "hex": "02000000000101ec0314599295423254f52553fb49a030ca265a5a175670144538be17b08386b60000000017160014eb387703e36b54890827fde17152b70bd02089c7ffffffff02102700000000000017a91444b04ff5110cc8f2ba7e78c0e33e241b298285fd87893500000000000017a91449e55565c2b31b5a834e1c0078a7ed99173b1f57870247304402204bd2fc34313d8bb9b8577c9edcd55fd82c60cc93a5139d5e874e55dc3eb75098022042b6ae3666204ec845cc5c713b26c58dbaca313e1b21e3e07c9c7573ace88968012102df45c8df0c95ec67bde03835fedec8296946e9628bf7be69f9a64e08bf1fdc5300000000",
  "blockhash": "0000000000000000000b537e9c567bacef8553edbd0b29f6c295fd9fac87a431",
  "confirmations": 1,
  "time": 1574938331,
  "blocktime": 1574938331
}

           

13、bitcoin浏覽器中可以檢視到這筆真實的交易

https://www.blockchain.com/btc/tx/51ff35fdb4d9e68a4e6932ab42881e20ba520c70c8e3c7bcbfe3f92fb51b3f7d

繼續閱讀