繁体   English   中英

如何使用 bitcoinjs 签署 BlockCypher 交易

[英]How to sign a BlockCypher transaction using bitcoinjs

我试图按照 此处所述使用 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"); 是问题所在,但我不确定出了什么问题。

这个问题在这里讨论和回答。 这篇文章这篇文章将被特别研究。

据我了解,根据问题,在 BlockCypher 回购中打开了各自的一个 虽然它的状态直到今天仍然是opened ,当前的BlockCypher JS 文档各自的 API 描述包含该行的更改版本

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM