4

Im trying to sign a BlockCypher transaction on the bitcoin testnet using bitcoinjs as described here but I keep getting the error:

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

I have searched around and can find no documentation on what the problem is. Below is the code im using to try and sign the transaction.

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);
});

It seems this line return keys.sign(new buffer.Buffer(tosign, "hex")).toString("hex"); is the problem but im not sure on what is wrong.

brandbei37
  • 501
  • 3
  • 13

1 Answers1

-1

This question was discussed and answered here. This post and this one are to be looked into in particular.

As far as I understand, according to the issue respective one was opened at BlockCypher repo. Although its status is still opened till this date, current BlockCypher JS docs respective API description contains altered version of the line

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

with toDER() conversion prior to toString(), consequently it looks like this now

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