Bitcoind
如何使用nodejs中的bitcoin-core包將sendRawTransaction發送到bitcoind JSON-RPC
我正在嘗試做
sendrawtransaction <transaction_hex>
從
nodejs
使用比特幣核心包我做到了npm install bitcoin-core --save
我已經有最新的
bitcoind
設置和執行,rpcusername
並rpcpassword
設置在bitcoin.conf
我目前的程式碼如下所示。
const Client = require('bitcoin-core'); const client = new Client({ username: 'adminuser', password : 'adminpassword'}); client.getInfo((error, help) => console.log(help));
我設法只得到回應
getInfo
其他一切都給出
empty output
或undefined
或null
到目前為止,我已經嘗試了很多這樣的變化
tx_hex = "0200000001949e0e3ac02fef76dffa8e2191ee0041f71a90c5ec9bbcba77db5f57e49fe6dd010000006b483045022100d6ac8520a1cfe80f6f3c2fbe6c3f903894203715928382e77c3c7ad0de74112502203c14601e5195dde88c9cc16c45dc273af168bd4fd03b6c045f832b7ba0134f8c012102183e719a510ff322fcb0dbfa279ef2500dd87c0b8c6184480846262e1949fbebfeffffff028bec0900000000001976a9141b0c5cb82d59cb2e07e8d5e343167fda48a9d60e88ace2920f00000000001976a9148108670170211830be2c3c557afe6bc2205d27de88ac864a0700" client.sendRawTransaction((tx_hex,response) => console.log(response)); client.sendRawTransaction((tx_hex) => console.log()); client.sendRawTransaction((tx_hex), function(){console.log()});
但以上都沒有給出確認,也沒有給出正確的輸出。
我在這裡想念什麼?
我的意思是,如果我
bitcoin-cli
在終端上使用它會返回txhash
。例如
[root@localhost deploy]# bitcoin-cli sendrawtransaction 0200000001949e0e3ac02fef76dffa8e2191ee0041f71a90c5ec9bbcba77db5f57e49fe6dd010000006b483045022100d6ac8520a1cfe80f6f3c2fbe6c3f903894203715928382e77c3c7ad0de74112502203c14601e5195dde88c9cc16c45dc273af168bd4fd03b6c045f832b7ba0134f8c012102183e719a510ff322fcb0dbfa279ef2500dd87c0b8c6184480846262e1949fbebfeffffff028bec0900000000001976a9141b0c5cb82d59cb2e07e8d5e343167fda48a9d60e88ace2920f00000000001976a9148108670170211830be2c3c557afe6bc2205d27de88ac864a0700 b9ceb300bc4a94dad591d8ecca7e840e7cfafa47c029488424e07666c1e1778b
更新1:
tx_hex = "0200000001949e0e3ac02fef76dffa8e2191ee0041f71a90c5ec9bbcba77db5f57e49fe6dd010000006b483045022100d6ac8520a1cfe80f6f3c2fbe6c3f903894203715928382e77c3c7ad0de74112502203c14601e5195dde88c9cc16c45dc273af168bd4fd03b6c045f832b7ba0134f8c012102183e719a510ff322fcb0dbfa279ef2500dd87c0b8c6184480846262e1949fbebfeffffff028bec0900000000001976a9141b0c5cb82d59cb2e07e8d5e343167fda48a9d60e88ace2920f00000000001976a9148108670170211830be2c3c557afe6bc2205d27de88ac864a0700" client.sendRawTransaction((tx_hex,response) => console.log(tx_hex + ':' + response));
返回
RpcError: -1 sendrawtransaction "hexstring" ( allowhighfees ) Submits raw transaction (serialized, hex-encoded) to local node and network. Also see createrawtransaction and signrawtransaction calls. Arguments: 1. "hexstring" (string, required) The hex string of the raw transaction) 2. allowhighfees (boolean, optional, default=false) Allow high fees Result: "hex" (string) The transaction hash in hex Examples: Create a transaction > bitcoin-cli createrawtransaction "[{\"txid\" : \"mytxid\",\"vout\":0}]" "{\"myaddress\":0.01}" Sign the transaction, and get back the hex > bitcoin-cli signrawtransaction "myhex" Send the transaction (signed hex) > bitcoin-cli sendrawtransaction "signedhex" As a json rpc call > curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "sendrawtransaction", "params": ["signedhex"] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/ :undefined
你傳遞參數的方式是錯誤的。我使用下面的程式碼,得到了正確的答案:
const Client = require('bitcoin-core'); const client = new Client({username: 'admin', password: 'password', network: 'testnet'}) const txHex = '0200000001999539009d0d02f2332d257716faa75cc4c9a8318c153ba6a084c719fa33103b000000006a4730440220032470b6b45825fc37d08e1985c7ad2044272edacfa2c76cf49c6b70c09a0dac02202466ede3e660c6d57f65b7ed21f2c1f778116ef92bef9ed2f9b18fcb1881a7b9012103b91ff254ae3bb3861e4a6c16ab356d6c52ccfd2b58bedf0dda84657dfd9d9afcffffffff0130750000000000001976a914ba05ece7632d78336848214b525cfdbd1c4b06a188ac00000000' client.sendRawTransaction(txHex, (error, response) => { if (error) console.log(error); console.log(response); });