Add low R grinding option

This commit is contained in:
junderw 2019-04-12 17:44:55 +09:00
parent a52ef82e4f
commit 3b402d00c6
No known key found for this signature in database
GPG key ID: B256185D3A971908
3 changed files with 34 additions and 5 deletions

View file

@ -35,10 +35,25 @@ class ECPair {
throw new Error('Missing private key'); throw new Error('Missing private key');
return wif.encode(this.network.wif, this.__D, this.compressed); return wif.encode(this.network.wif, this.__D, this.compressed);
} }
sign(hash) { sign(hash, lowR = false) {
if (!this.__D) if (!this.__D)
throw new Error('Missing private key'); 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, signature) { verify(hash, signature) {
return ecc.verify(hash, this.publicKey, signature); return ecc.verify(hash, this.publicKey, signature);

View file

@ -61,9 +61,23 @@ class ECPair implements ECPairInterface {
return wif.encode(this.network.wif, this.__D, this.compressed); 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'); 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): Buffer { verify(hash: Buffer, signature: Buffer): Buffer {

2
types/ecpair.d.ts vendored
View file

@ -24,7 +24,7 @@ declare class ECPair implements ECPairInterface {
readonly privateKey: Buffer | undefined; readonly privateKey: Buffer | undefined;
readonly publicKey: Buffer | undefined; readonly publicKey: Buffer | undefined;
toWIF(): string; toWIF(): string;
sign(hash: Buffer): Buffer; sign(hash: Buffer, lowR?: boolean): Buffer;
verify(hash: Buffer, signature: Buffer): Buffer; verify(hash: Buffer, signature: Buffer): Buffer;
} }
declare function fromPrivateKey(buffer: Buffer, options?: ECPairOptions): ECPair; declare function fromPrivateKey(buffer: Buffer, options?: ECPairOptions): ECPair;