bitcoinjs-lib: Getting keys.getPublicKeyBuffer is not a function

Im trying to sign a transaction for blockcypher described here, but I have run into this error and when searching around I cannot find a solution. Im trying to sign a transaction for the bitcoin testnet, here is the code im using, does anyone know what the problem could be?

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

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.getPublicKeyBuffer().toString("hex"));
      return keys.sign(new buffer.Buffer(tosign, "hex")).toDER().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);
});

Edit: In fact it provided ‘undefined’ which I didnt notice before. So the full error i get is:

jQuery.Deferred exception: keys.getPublicKeyBuffer is not a function undefined

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 19 (7 by maintainers)

Most upvoted comments

@brandb12 How did you solve your last mentioned issue in this thread? Thanks in advance

Finally I seemed to have solved the problem, but as usual that brings another problem. I now get the error when trying to send:

Error building input: Error generating scriptsig when building transaction: Invalid signature: Non-canonical signature: wrong length marker

Does this have something to do with this code?

const SIGHASH_ALL = 0x01;
return bitcoin.script.signature.encode(
  keys.sign(new buffer.Buffer(tosign, "hex")),
  SIGHASH_ALL,
).toString("hex");

Hi @fedecaccia, @vineettyagi28, @brandb12 apologies if no one answered you at BlockCypher. The reason why it fails is because BlockCypher adds the SIGHASH_ALL automatically. Here is a quick way to fix it.

                            return bitcoin.script.signature.encode(
                                keys.sign(new buffer.Buffer(tosign, "hex")),
                                0x01,
                                ).toString("hex").slice(0, -2);

I’ll update the doc accordingly.

Im quite new to this

I hope no one puts a lot of money in your wallet then 😛

jk

This should work.

const SIGHASH_ALL = 0x01;
return bitcoin.script.signature.encode(
  keys.sign(new buffer.Buffer(tosign, "hex")),
  SIGHASH_ALL,
).toString("hex");