Skip to content

Commit

Permalink
update ecpair dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
doersf committed Aug 28, 2022
1 parent 1345810 commit ceadc9a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 203 deletions.
40 changes: 20 additions & 20 deletions lib/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,70 @@ const crypto = require('crypto');
const Bitcoin = require('bitcoinjs-lib');
const ecc = require('tiny-secp256k1');
const ecpair = require('ecpair');
const ecpairFactory = ecpair.ECPairFactory(ecc);

function fixed_size_low_r_sign(d, hash, lowR) {
// forces low R signatures so 3rd byte of DER sig (size of R) is 0x20 exactly
// our tests use this because our other libraries use fixed size R values
if (!d) throw new Error('Missing private key');
if (lowR === undefined) lowR = true;
if (lowR === false) {
return ecc.sign(hash, d);
return Buffer.from(ecc.sign(hash, d).buffer);
} else {
let sig = ecc.sign(hash, d);
let sig = Buffer.from(ecc.sign(hash, d).buffer);
const extraData = Buffer.alloc(32, 0);
let counter = 0;
// if first try is lowR, skip the loop
// for second try and on, add extra entropy counting up
while (Bitcoin.script.signature.encode(sig, Bitcoin.Transaction.SIGHASH_ALL)[3] !== 0x20 || sig[0] > 0x7f) {
counter++;
extraData.writeUIntLE(counter, 0, 6);
sig = ecc.signWithEntropy(hash, d, extraData);
sig = Buffer.from(ecc.sign(hash, d, extraData).buffer);
}
return sig;
}

}

function Key (d, compressed) {
const pair = Buffer.isBuffer(d) ?
new ecpair.ECPair(d, undefined, { compressed: compressed }) : d;
const pair = Buffer.isBuffer(d) ? ecpairFactory.fromPrivateKey(d, {compressed: compressed}) : d;

Object.defineProperty(this, 'ecpair', { value: pair, writable: false });
Object.defineProperty(this, 'ecpair', { value: pair, writable: false });

Object.defineProperty(this, 'pub', { get: () => this.ecpair.publicKey });
Object.defineProperty(this, 'priv', { get: () => this.ecpair.privateKey });
Object.defineProperty(this, 'lowR', {
get: () => this.ecpair.lowR,
set: val => { this.ecpair.lowR = !!val },
});
Object.defineProperty(this, 'pub', { get: () => this.ecpair.publicKey });
Object.defineProperty(this, 'priv', { get: () => this.ecpair.privateKey });
Object.defineProperty(this, 'lowR', {
get: () => this.ecpair.lowR,
set: val => { this.ecpair.lowR = !!val },
});

// lowR is default
this.lowR = true;
// lowR is default
this.lowR = true;

}

// inherit factory ECKey.makeRandom();
Key.makeRandom = function () {
const pair = ecpair.ECPair.makeRandom.apply(ecpair.ECPair, arguments);
return new Key(pair);
const pair = ecpairFactory.makeRandom.apply(ecpair.ECPairAPI, arguments);
return new Key(pair);
};

// inherit factory ECKey.makeRandom();
Key.fromWIF = function () {
const pair = ecpair.ECPair.fromWIF.apply(ecpair.ECPair, arguments);
const pair = ecpairFactory.fromWIF.apply(ecpair.ECPairAPI, arguments);
return new Key(pair);
};

Key.prototype.signHex = function (hexData) {
const buf = Buffer.from(hexData, 'hex');
const sig = fixed_size_low_r_sign(this.ecpair.__D, buf, this.lowR); //this.ecpair.sign(buf)
const scriptSig = Bitcoin.script.signature.encode(sig, Bitcoin.Transaction.SIGHASH_ALL);
const sig = fixed_size_low_r_sign(this.ecpair.__D, buf, this.lowR); //Buffer.from(this.ecpair.sign(buf.buffer)
const scriptSig = Bitcoin.script.signature.encode(sig, Bitcoin.Transaction.SIGHASH_ALL);
return scriptSig.slice(0, scriptSig.length-1).toString('hex');
};

Key.fromBuffer = function (buf) {
//assert(Buffer.isBuffer(buf));
const pair = ecpair.ECPair.fromPrivateKey(buf, {compressed: true});
const pair = ecpairFactory.fromPrivateKey(buf, {compressed: true});
return new Key(pair);
};

Expand Down
Loading

0 comments on commit ceadc9a

Please sign in to comment.