3

我尝试使用这里描述的 bitcoinjs 在比特币测试网上签署 BlockCypher 交易,但我不断收到错误消息:

{"error": "Couldn't deserialize request: invalid character 'x' in literal true (expecting 'r')"}

我四处搜索,找不到有关问题所在的文档。下面是我用来尝试签署交易的代码。

var bitcoin = require("bitcoinjs-lib");
var buffer  = require('buffer');
var keys    = new bitcoin.ECPair.fromWIF('cMvPQZiG5mLARSjxbBwMxKwzhTHaxgpTsXB6ymx7SGAeYUqF8HAT', bitcoin.networks.testnet);
const publicKey = keys.publicKey;

console.log(keys.publicKey.toString("hex"));

var newtx = {
  inputs: [{addresses: ['ms9ySK54aEC2ykDviet9jo4GZE6GxEZMzf']}],
  outputs: [{addresses: ['msWccFYm5PPCn6TNPbNEnprA4hydPGadBN'], value: 1000}]
};
// calling the new endpoint, same as above
$.post('https://api.blockcypher.com/v1/btc/test3/txs/new', JSON.stringify(newtx))
  .then(function(tmptx) {
    // signing each of the hex-encoded string required to finalize the transaction
    tmptx.pubkeys = [];
    tmptx.signatures = tmptx.tosign.map(function(tosign, n) {
      tmptx.pubkeys.push(keys.publicKey.toString("hex"));
      return keys.sign(new buffer.Buffer(tosign, "hex")).toString("hex");
    });
    // sending back the transaction with all the signatures to broadcast
    $.post('https://api.blockcypher.com/v1/btc/test3/txs/send', tmptx).then(function(finaltx) {
      console.log(finaltx);
    }).catch(function (response) {
   console.log(response.responseText);
});
  }).catch(function (response) {
   console.log(response.responseText);
});

似乎这条线return keys.sign(new buffer.Buffer(tosign, "hex")).toString("hex");是问题所在,但我不确定出了什么问题。

4

1 回答 1

-1

这个问题在这里得到了讨论和回答。这篇文章这篇文章要特别研究。

据我了解,根据问题,相应的问题在 BlockCypher 回购中打开的。opened虽然它的状态直到这个日期仍然存在,但当前BlockCypher JS 文档各自的 API 描述包含该行的更改版本

return keys.sign(new buffer.Buffer(tosign, "hex")).toString("hex"); 

之前的 toDER()转换toString(),因此现在看起来像这样

return keys.sign(new buffer.Buffer(tosign, "hex")).toDER().toString("hex"); 
于 2020-09-11T09:50:31.077 回答