Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix generate now can't throw exception #160

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/index.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,21 @@ 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) {
// Note that randomBytes(32) can not pass isValidPrivate with very low probability
// see more https://github.com/paulmillr/noble-secp256k1/master/test/vectors/privates.json
while (true) {
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)
}
} else {
const privateKey = randomBytes(32) as Buffer
if (!isValidPrivate(privateKey)) continue
return new Wallet(privateKey)
}
} else {
return new Wallet(randomBytes(32))
}
}

Expand All @@ -288,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)
}
}
Expand Down