Bitcoin-Core

如何使用比特幣核心 rpc 從 vin 獲取輸入地址和金額

  • July 22, 2019

我正在嘗試使用比特幣核心 rpc 查找每筆交易的詳細資訊。首先,我使用 bitcoin-cli getblock 獲取區塊數據,然後使用 bitcoin-cli getrawtransaction 獲取交易詳情。

例如:

bitcoin-cli getrawtransaction "1024cb12a576b69defa67dbc2f1899700ab58e5ad3d5e058edefb907f59865bc" true "00000000000000000024fb37364cbf81fd49cc2d51c09c75c35433c3a1945d04" 

get:

{
 "in_active_chain": true,
 "txid": "1024cb12a576b69defa67dbc2f1899700ab58e5ad3d5e058edefb907f59865bc",
 "hash": "1024cb12a576b69defa67dbc2f1899700ab58e5ad3d5e058edefb907f59865bc",
 "version": 2,
 "size": 339,
 "vsize": 339,
 "weight": 1356,
 "locktime": 499989,
 "vin": [
   {
     "txid": "0d6cbc21a1b1a3e5df1eae4fc37aa849174b6b727e53f9f505a605f066b7f000",
     "vout": 0,
     "scriptSig": {
       "asm": "304402203f198d9695a293c41d2124e790d473e68f4b35a09ece5517c7adfffc797f91760220304feb1cc2829d3c937665bcb4b7f9735acbcac50e9169bc53c9e689ce20b3bc[ALL] 025743bce4e775bb754e784dd2cfdc8dabc2023154eee539bb155612101a583e7e",
       "hex": "47304402203f198d9695a293c41d2124e790d473e68f4b35a09ece5517c7adfffc797f91760220304feb1cc2829d3c937665bcb4b7f9735acbcac50e9169bc53c9e689ce20b3bc0121025743bce4e775bb754e784dd2cfdc8dabc2023154eee539bb155612101a583e7e"
     },
     "sequence": 4294967294
   },
   {
     "txid": "aac498279d50ee81237d704cb495b243b50def5ecdcb36d9d78f596f73b1d3e9",
     "vout": 1,
     "scriptSig": {
       "asm": "3045022100b763b986b17beddac1ac96c3656fa7af188a28cfacfc87390e1e796b7fdbc0d102201e76f77db6093306028e681e2fe6354ae12649afb5c27c549243b67d505a1bb6[ALL] 0349ebb9d180938ab3a571d8c2e9771fb4fecb9f4484c337bb35d8fd8b48283ca9",
       "hex": "483045022100b763b986b17beddac1ac96c3656fa7af188a28cfacfc87390e1e796b7fdbc0d102201e76f77db6093306028e681e2fe6354ae12649afb5c27c549243b67d505a1bb601210349ebb9d180938ab3a571d8c2e9771fb4fecb9f4484c337bb35d8fd8b48283ca9"
     },
     "sequence": 4294967294
   }
 ],
 "vout": [
   {
     "value": 0.17450000,
     "n": 0,
     "scriptPubKey": {
       "asm": "OP_DUP OP_HASH160 d63cc1e3b6009e31d03bd5f8046cbe0f7e37e8c0 OP_EQUALVERIFY OP_CHECKSIG",
       "hex": "76a914d63cc1e3b6009e31d03bd5f8046cbe0f7e37e8c088ac",
       "reqSigs": 1,
       "type": "pubkeyhash",
       "addresses": [
         "1LXnPYpHTwQeWfBVnQZ4yDP23b57NwoyrP"
       ]
     }
   }
 ],
 "hex": "020000000200f0b766f005a605f5f9537e726b4b1749a87ac34fae1edfe5a3b1a121bc6c0d000000006a47304402203f198d9695a293c41d2124e790d473e68f4b35a09ece5517c7adfffc797f91760220304feb1cc2829d3c937665bcb4b7f9735acbcac50e9169bc53c9e689ce20b3bc0121025743bce4e775bb754e784dd2cfdc8dabc2023154eee539bb155612101a583e7efeffffffe9d3b1736f598fd7d936cbcd5eef0db543b295b44c707d2381ee509d2798c4aa010000006b483045022100b763b986b17beddac1ac96c3656fa7af188a28cfacfc87390e1e796b7fdbc0d102201e76f77db6093306028e681e2fe6354ae12649afb5c27c549243b67d505a1bb601210349ebb9d180938ab3a571d8c2e9771fb4fecb9f4484c337bb35d8fd8b48283ca9feffffff0110440a01000000001976a914d63cc1e3b6009e31d03bd5f8046cbe0f7e37e8c088ac15a10700",
 "blockhash": "00000000000000000024fb37364cbf81fd49cc2d51c09c75c35433c3a1945d04",
 "confirmations": 85927,
 "time": 1513622125,
 "blocktime": 1513622125
}

我知道一些 web api,如 blockchain.info 為每筆交易提供這些資訊。如何使用其他比特幣核心 rpc 獲取地址和對應的 vin 數量?

謝謝。

有一些相關連結: How to get address from scriptSig?

如何從僅給出 txid 的 vin 獲取輸入地址(Insight-Api)

可以使用 getrawtransaction 命令從 ’txid’ 和 ‘vout’ 中獲取發件人的地址和金額。我們看例子,塊的vin部分如下:

"vin": [
   {
     "txid": "0d6cbc21a1b1a3e5df1eae4fc37aa849174b6b727e53f9f505a605f066b7f000",
     "vout": 0,
     "scriptSig": {
       "asm": "304402203f198d9695a293c41d2124e790d473e68f4b35a09ece5517c7adfffc797f91760220304feb1cc2829d3c937665bcb4b7f9735acbcac50e9169bc53c9e689ce20b3bc[ALL] 025743bce4e775bb754e784dd2cfdc8dabc2023154eee539bb155612101a583e7e",
       "hex": "47304402203f198d9695a293c41d2124e790d473e68f4b35a09ece5517c7adfffc797f91760220304feb1cc2829d3c937665bcb4b7f9735acbcac50e9169bc53c9e689ce20b3bc0121025743bce4e775bb754e784dd2cfdc8dabc2023154eee539bb155612101a583e7e"
     },
     "sequence": 4294967294
   },
   {
     "txid": "aac498279d50ee81237d704cb495b243b50def5ecdcb36d9d78f596f73b1d3e9",
     "vout": 1,
     "scriptSig": {
       "asm": "3045022100b763b986b17beddac1ac96c3656fa7af188a28cfacfc87390e1e796b7fdbc0d102201e76f77db6093306028e681e2fe6354ae12649afb5c27c549243b67d505a1bb6[ALL] 0349ebb9d180938ab3a571d8c2e9771fb4fecb9f4484c337bb35d8fd8b48283ca9",
       "hex": "483045022100b763b986b17beddac1ac96c3656fa7af188a28cfacfc87390e1e796b7fdbc0d102201e76f77db6093306028e681e2fe6354ae12649afb5c27c549243b67d505a1bb601210349ebb9d180938ab3a571d8c2e9771fb4fecb9f4484c337bb35d8fd8b48283ca9"
     },
     "sequence": 4294967294
   }
 ]

這意味著,有兩個輸入,第一輸入是從“VOUT”交易“TxID添加”“0d6cbc21a1b1a3e5df1eae4fc37aa849174b6b727e53f9f505a605f066b7f000 0”,第二輸入是從“VOUT”1 FO交易“TxID添加”:“aac498279d50ee81237d704cb495b243b50def5ecdcb36d9d78f596f73b1d3e9”。由於一筆交易的 vout 部分有地址和金額,我們可以得到輸入地址和對應的金額。

例如,讓我們看一下第一個輸入:

     "txid": "0d6cbc21a1b1a3e5df1eae4fc37aa849174b6b727e53f9f505a605f066b7f000",
     "vout": 0,

執行以下核心 rpc 命令(注意我們需要讓伺服器索引每個事務以支持 getrawtransaction,可以通過在 bitcoin.conf 中添加“txindex=1”來完成)

bitcoin-cli -rpcuser=jun -rpcpassword=123 getrawtransaction "0d6cbc21a1b1a3e5df1eae4fc37aa849174b6b727e53f9f505a605f066b7f000" true

我們得到以下 vouts 部分:

"vout": [
   {
     "value": 0.01040000,
     "n": 0,
     "scriptPubKey": {
       "asm": "OP_DUP OP_HASH160 af9e84db382823b9c36c40d70090ff81890c5ef5 OP_EQUALVERIFY OP_CHECKSIG",
       "hex": "76a914af9e84db382823b9c36c40d70090ff81890c5ef588ac",
       "reqSigs": 1,
       "type": "pubkeyhash",
       "addresses": [
         "1H1b9fN7wYYwTSweyPjE7wCGSHuduCcE2o"
       ]
     }
   },
   {
     "value": 147.75014395,
     "n": 1,
     "scriptPubKey": {
       "asm": "OP_DUP OP_HASH160 35f72ce8b9123a055e27be9a0bb723b6cb502419 OP_EQUALVERIFY OP_CHECKSIG",
       "hex": "76a91435f72ce8b9123a055e27be9a0bb723b6cb50241988ac",
       "reqSigs": 1,
       "type": "pubkeyhash",
       "addresses": [
         "15vLvDZyAjWCrfWyazMtvHRe8hDJBxXJEC"
       ]
     }
   }
 ],

然後我們有地址和金額,它們是 vout 的第一部分。這是我們交易的第一個輸入。

"addresses": ["1H1b9fN7wYYwTSweyPjE7wCGSHuduCcE2o"]
"value": 147.75014395

有一個關於 vin 和 vout 解釋的非常好的部落格 <http://www.michaelnielsen.org/ddi/how-the-bitcoin-protocol-actually-works/>

引用自:https://bitcoin.stackexchange.com/questions/89207