【问题标题】:How to sign a BlockCypher transaction using bitcoinjs如何使用 bitcoinjs 签署 BlockCypher 交易
【发布时间】:2020-09-11 17:50:31
【问题描述】:

我正在尝试使用here 中描述的 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"); 似乎是问题所在,但我不确定是什么问题。

【问题讨论】:

    标签: javascript blockchain bitcoin blockcypher


    【解决方案1】:

    here 讨论并回答了这个问题。 This postthis one 将被特别调查。

    据我了解,根据问题 respective one 已在 BlockCypher 存储库中打开。虽然到现在它的状态仍然是opened,但当前的BlockCypher JS docs respective API description 包含该行的更改版本

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-05-31
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      相关资源
      最近更新 更多