From 6ee26f68184ab3d9fb7a82653904a0c5b09bb5a0 Mon Sep 17 00:00:00 2001 From: vird Date: Tue, 2 Nov 2021 09:55:16 +0000 Subject: [PATCH 1/2] fix generate now can't throw exception --- src/index.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) mode change 100644 => 100755 src/index.ts diff --git a/src/index.ts b/src/index.ts old mode 100644 new mode 100755 index 421cd71..c366580 --- a/src/index.ts +++ b/src/index.ts @@ -263,16 +263,22 @@ export default class Wallet { * @param icapDirect setting this to `true` will generate an address suitable for the `ICAP Direct mode` */ public static generate(icapDirect: boolean = false): Wallet { - if (icapDirect) { - const max = new BN('088f924eeceeda7fe92e1f5b0fffffffffffffff', 16) - while (true) { - const privateKey = randomBytes(32) as Buffer - if (new BN(privateToAddress(privateKey)).lte(max)) { - return new Wallet(privateKey) + // Note that randomBytes(32) can not pass isValidPrivate with very low probability + // see more https://raw.githubusercontent.com/paulmillr/noble-secp256k1/master/test/vectors/privates.json + while (true) { + try { + if (icapDirect) { + const max = new BN('088f924eeceeda7fe92e1f5b0fffffffffffffff', 16) + const privateKey = randomBytes(32) as Buffer + if (new BN(privateToAddress(privateKey)).lte(max)) { + return new Wallet(privateKey) + } + } else { + return new Wallet(randomBytes(32)) } + } catch (ignoreError) { + // Do not crash } - } else { - return new Wallet(randomBytes(32)) } } From 59e4eb9b0f30908e25f5bafb647263f53ed0e853 Mon Sep 17 00:00:00 2001 From: vird Date: Thu, 4 Nov 2021 16:54:05 +0000 Subject: [PATCH 2/2] do not throw only on specific exception --- src/index.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index c366580..8d9a902 100755 --- a/src/index.ts +++ b/src/index.ts @@ -266,18 +266,17 @@ export default class Wallet { // Note that randomBytes(32) can not pass isValidPrivate with very low probability // see more https://raw.githubusercontent.com/paulmillr/noble-secp256k1/master/test/vectors/privates.json while (true) { - try { - if (icapDirect) { - const max = new BN('088f924eeceeda7fe92e1f5b0fffffffffffffff', 16) - const privateKey = randomBytes(32) as Buffer - if (new BN(privateToAddress(privateKey)).lte(max)) { - return new Wallet(privateKey) - } - } else { - return new Wallet(randomBytes(32)) + if (icapDirect) { + const max = new BN('088f924eeceeda7fe92e1f5b0fffffffffffffff', 16) + const privateKey = randomBytes(32) as Buffer + if (new BN(privateToAddress(privateKey)).lte(max)) { + if (!isValidPrivate(privateKey)) continue + return new Wallet(privateKey) } - } catch (ignoreError) { - // Do not crash + } else { + const privateKey = randomBytes(32) as Buffer + if (!isValidPrivate(privateKey)) continue + return new Wallet(privateKey) } } } @@ -294,7 +293,7 @@ export default class Wallet { const privateKey = randomBytes(32) as Buffer const address = privateToAddress(privateKey) - if (pattern.test(address.toString('hex'))) { + if (pattern.test(address.toString('hex')) && isValidPrivate(privateKey)) { return new Wallet(privateKey) } }