bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.65k stars 2.09k forks source link

TypeError: bitcoin.ECPair is not a constructor #1359

Closed cancerts closed 5 years ago

cancerts commented 5 years ago

var keys = new bitcoin.ECPair(bigi.fromHex(my_hex_private_key));

junderw commented 5 years ago

please read the examples.

That usage is old and outdated.

cancerts commented 5 years ago

please read the examples.

That usage is old and outdated.

how used ?

junderw commented 5 years ago

https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/ecpair.js

galki commented 4 years ago

The above link is outdated and I assume the new one is [https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/ecpair.spec.ts]()

Example:

import { ECPair } from 'bitcoinjs-lib';

const keys = ECPair.fromPrivateKey(Buffer.from(my_hex_private_key, 'hex'));

If you want to sign a transaction, for instance, you can then:

tmpTx.pubkeys = [];
tmpTx.signatures = tmpTx.tosign.map(function (tosign, _n) {
  tmpTx.pubkeys.push(keys.publicKey.toString('hex'));
  return keys.sign(Buffer.from(tosign, 'hex')).toString('hex');
});

However, this results in a signature with the wrong length... any solutions?

Akkii4 commented 4 years ago

The above link is outdated and I assume the new one is https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/ecpair.spec.ts

Example:

import { ECPair } from 'bitcoinjs-lib';

const keys = ECPair.fromPrivateKey(Buffer.from(my_hex_private_key, 'hex'));

If you want to sign a transaction, for instance, you can then:

tmpTx.pubkeys = [];
tmpTx.signatures = tmpTx.tosign.map(function (tosign, _n) {
  tmpTx.pubkeys.push(keys.publicKey.toString('hex'));
  return keys.sign(Buffer.from(tosign, 'hex')).toString('hex');
});

However, this results in a signature with the wrong length... any solutions?

Try this it worked for me, actually your methods create signature of length 128 chars while this is 140 chars-:

  tmptx.pubkeys = [];
  tmptx.signatures = tmptx.tosign.map(function (tosign, _n) 
 {
    tmptx.pubkeys.push(keys.publicKey.toString('hex'));
    let signature = keys.sign(Buffer.from(tosign, "hex"));
    let encodedSignature = bitcoin.script.signature.encode(signature,bitcoin.Transaction.SIGHASH_ALL);
    let hexStr = encodedSignature.toString("hex").slice(0, -2); 
    return hexStr;
 });