Как подписать транзакцию BlockCypher с помощью bitcoinjs - PullRequest
0 голосов
/ 21 мая 2019

Я пытаюсь подписать транзакцию BlockCypher в биткойн-тест-сети, используя bitcoinjs, как описано здесь , но я получаю сообщение об ошибке:

{"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"); - проблема, но я не уверен в том, что не так.

...