Bitcoin-Core

如何使用 JSON RPC 從交易中獲取發件人地址?

  • March 10, 2022

我正在使用 JSON-RPC 呼叫連接到我的雲伺服器上的本地比特幣節點。

我可以使用錢包名稱獲取所有交易gettransactions。現在我需要為每筆交易獲取發件人的地址,但結果只包含收件人的地址。

以下是gettransactions請求的響應。

{
   "result": [
       {
           "address": "bc1qd9mgd4ynryaw2kp0pzfjdxguhz9dzvs5vh0nfn",
           "category": "receive",
           "amount": 0.001,
           "label": "",
           "vout": 27,
           "confirmations": 1322,
           "blockhash": "000000000000000000074551080f954e790edfe6ad810ecebf4691e881175eaf",
           "blockheight": 660029,
           "blockindex": 106,
           "blocktime": 1607147554,
           "txid": "07ab59c8d3a204172887085258460d0058c20a9acdb451daa5ad4290cbd461b0",
           "walletconflicts": [],
           "time": 1607147511,
           "timereceived": 1607147511,
           "bip125-replaceable": "no"
       },
       {
           "address": "bc1q6dh3h5e3qgku8u9s8y664844fj4fkm08n7cnwt",
           "category": "receive",
           "amount": 0.0011,
           "label": "",
           "vout": 9,
           "confirmations": 1313,
           "blockhash": "00000000000000000000960f4476a8552cac203eec6e0f826886b41fae402bec",
           "blockheight": 660038,
           "blockindex": 393,
           "blocktime": 1607153799,
           "txid": "af0885e4c1d10e935de525680c6c33d221ab506742b08e34a0322bba90b56fb0",
           "walletconflicts": [],
           "time": 1607152236,
           "timereceived": 1607152236,
           "bip125-replaceable": "no"
       },
       {
           "address": "1L8ZNCibVVpPjJ8VJ1ALLuTjtctA4mzGPw",
           "category": "send",
           "amount": -0.001,
           "vout": 0,
           "fee": -0.00008536,
           "confirmations": 706,
           "blockhash": "0000000000000000000c3230522e267980bb990883cc0b36460ad3efc1f8fed4",
           "blockheight": 660645,
           "blockindex": 1049,
           "blocktime": 1607529332,
           "txid": "a2056e7a49bc608435713519bc038ed7da2e65f588495f36c6628f5ca6fe2d7d",
           "walletconflicts": [],
           "time": 1607528963,
           "timereceived": 1607528963,
           "bip125-replaceable": "no",
           "comment": " ",
           "to": "seans outpost",
           "abandoned": false
       },
       {
           "address": "1L8ZNCibVVpPjJ8VJ1ALLuTjtctA4mzGPw",
           "category": "send",
           "amount": -0.0005,
           "vout": 1,
           "fee": -0.00008536,
           "confirmations": 705,
           "blockhash": "0000000000000000000994e21412109847caffa2bd990884f49461d53c047055",
           "blockheight": 660646,
           "blockindex": 844,
           "blocktime": 1607529675,
           "txid": "ddbe1dce4537926b6cf5bc1e222e5cd32de224cf346ca89251c127ce32226f30",
           "walletconflicts": [],
           "time": 1607529401,
           "timereceived": 1607529401,
           "bip125-replaceable": "no",
           "comment": " ",
           "to": "seans outpost",
           "abandoned": false
       }
   ],
   "error": null,
   "id": "curltest"
}

我怎樣才能得到發件人的地址?

儘管它在大多數區塊瀏覽器上看起來如何,但比特幣實際上並沒有“發件人地址”的概念,至少在原始交易級別上沒有。交易的內容是輸入,區塊瀏覽器所做的是查找這些輸入並猜測用於創建這些 UTXO 的地址。

您可能會發現這些相關問題很有用(只需在此站點中搜尋“發件人地址”):

如何找出交易的發件人

來自 DecodeRawTransaction (C#) 的發件人地址

要從 Bitcoin Core 獲取發件人地址,您必須通過txindex=1在配置文件中進行設置來跟踪有關區塊鏈的其他資訊。這是因為相關交易的發送地址僅與之前的交易相關聯。設置txindex=1將比特幣核心配置為維護整個交易索引,超出與您的錢包或只讀地址相關的索引,允許您呼叫getrawtransaction任何 txid。

decoderawtransaction(getrawtransaction(txid))正確配置和同步比特幣核心後,您可以通過使用(從此答案中提取的程式碼)查看每個輸入 tx 來檢索交易的發送地址:

txid = <relevant transaction id>
addresses = []
raw_tx = decoderawtransaction(getrawtransaction(txid))
for(input in raw_tx['vin']) {
 input_raw_tx = decoderawtransaction(getrawtransaction(input['txid']))
 addresses.push(input_raw_tx['vout'][input['vout']]['scriptPubKey']['addresses'][0])
}

許多區塊鏈瀏覽器都有免費的低容量 API 訪問,您可以只查詢他們的數據,而不是維護一個完整的 txindex 節點。想到的兩個是BlockchairBlockCypher(BlockCypher 甚至在其 tx 端點中包含所有發件人地址,因此您可以通過單個 API 呼叫檢索這些地址,而不是像使用 Bitcoin Core 那樣從 Blockchair 查詢每個輸入 tx)。

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