Bitcoin-Core

比特幣 RPC createrawtransaction 解析錯誤

  • September 17, 2022

我嘗試通過 curl 使用比特幣核心 RPC 創建原始交易。

Python腳本:

import requests

data = '{"jsonrpc":"2.0","id":"curltext","method":"createrawtransaction", "params": [\'[{"txid":"bffa31485fe5bfbb7e86463ff6c7ee4f084936d54d0471fba7763538e7fd0aa6","vout":0, "sequence":1}]\' \'{"2Mt1FE9NVXP7fRYuPEZBHrFgoCEc4TGb2ZY":0.01}\']}'
print(data)
rawtx = requests.post("http://user:pass@127.0.0.1:18443", data=data).text
print(rawtx)

輸出錯誤:

{"jsonrpc":"2.0","id":"curltext","method":"createrawtransaction", "params": ['[{"txid":"bffa31485fe5bfbb7e86463ff6c7ee4f084936d54d0471fba7763538e7fd0aa6","vout":0, "sequence":1}]' '{"2Mt1FE9NVXP7fRYuPEZBHrFgoCEc4TGb2ZY":0.01}']}
{'result': None, 'error': {'code': -32700, 'message': 'Parse error'}, 'id': None}

我嘗試過多種方式進行轉義,但無法正常工作。

比特幣 CLI 命令:

bitcoin-cli -testnet createrawtransaction '[{"txid":"bffa31485fe5bfbb7e86463ff6c7ee4f084936d54d0471fba7763538e7fd0aa6","vout":0, "sequence":1}]' '{"2Mt1FE9NVXP7fRYuPEZBHrFgoCEc4TGb2ZY":0.01}'

預期輸出(tx 十六進制):

0200000001a60afde7383576a7fb71044dd53649084feec7f63f46867ebbbfe55f4831fabf0000000000010000000140420f000000000017a9140853562996d502a0abe2538657e5bf7bb261b6868700000000

您的data字元串的 JSON 格式不正確。這應該是正確的版本:

data = '{"jsonrpc":"2.0","id":"curltext","method":"createrawtransaction", "params": [[{"txid":"bffa31485fe5bfbb7e86463ff6c7ee4f084936d54d0471fba7763538e7fd0aa6","vout":0,"sequence":1}],[{"2Mt1FE9NVXP7fRYuPEZBHrFgoCEc4TGb2ZY":0.01}]]}'

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