Merge pull request from bitcoinjs/lowR

Add low R value signing as option to ECPair.sign
This commit is contained in:
Jonathan Underwood 2019-05-21 15:40:35 +09:00 committed by GitHub
commit 39bd08002b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 1174 additions and 1147 deletions
ts_src

View file

@ -25,7 +25,7 @@ export interface ECPairInterface {
publicKey: Buffer;
privateKey?: Buffer;
toWIF(): string;
sign(hash: Buffer): Buffer;
sign(hash: Buffer, lowR?: boolean): Buffer;
verify(hash: Buffer, signature: Buffer): boolean;
getPublicKey?(): Buffer;
}
@ -62,9 +62,23 @@ class ECPair implements ECPairInterface {
return wif.encode(this.network.wif, this.__D, this.compressed);
}
sign(hash: Buffer): Buffer {
sign(hash: Buffer, lowR: boolean = false): Buffer {
if (!this.__D) throw new Error('Missing private key');
return ecc.sign(hash, this.__D);
if (lowR === false) {
return ecc.sign(hash, this.__D);
} else {
let sig = ecc.sign(hash, this.__D);
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 (sig[0] > 0x7f) {
counter++;
extraData.writeUIntLE(counter, 0, 6);
sig = ecc.signWithEntropy(hash, this.__D, extraData);
}
return sig;
}
}
verify(hash: Buffer, signature: Buffer): boolean {