From: Chris Duncan Date: Mon, 9 Dec 2024 15:52:13 +0000 (-0800) Subject: Update entropy references. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=0afb207178567fa71897be9f99fcb0a7f19be260;p=libnemo.git Update entropy references. --- diff --git a/src/lib/bip39-mnemonic.ts b/src/lib/bip39-mnemonic.ts index cae009d..87ac879 100644 --- a/src/lib/bip39-mnemonic.ts +++ b/src/lib/bip39-mnemonic.ts @@ -56,7 +56,7 @@ export class Bip39Mnemonic { * @returns {string} Mnemonic phrase created using the BIP-39 wordlist */ static async fromEntropy (entropy: string): Promise { - const e = new Entropy(entropy) + const e = await Entropy.import(entropy) const checksum = await this.checksum(e) let concatenation = `${e.bits}${checksum}` const words: string[] = [] diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 38a93d0..213b543 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -49,14 +49,12 @@ abstract class Wallet { abstract ckd (index: number[]): Promise - constructor (seed?: string, mnemonic?: Bip39Mnemonic, id?: string) { + constructor (id: Entropy, seed?: string, mnemonic?: Bip39Mnemonic) { if (this.constructor === Wallet) { throw new Error('Wallet is an abstract class and cannot be instantiated directly.') } this.#accounts = [] this.#id = id - ? new Entropy(id) - : new Entropy(16) this.#mnemonic = mnemonic ?? null this.#safe = new Safe() this.#seed = seed ?? null @@ -267,12 +265,12 @@ abstract class Wallet { export class Bip44Wallet extends Wallet { static #isInternal: boolean = false - constructor (seed: string, mnemonic?: Bip39Mnemonic, id?: string) { + constructor (id: Entropy, seed: string, mnemonic?: Bip39Mnemonic) { if (!Bip44Wallet.#isInternal) { throw new Error(`Bip44Wallet cannot be instantiated directly. Use 'await Bip44Wallet.create()' instead.`) } Bip44Wallet.#isInternal = false - super(seed, mnemonic, id) + super(id, seed, mnemonic) } /** @@ -295,7 +293,7 @@ export class Bip44Wallet extends Wallet { static async create (key: CryptoKey, salt?: string): Promise static async create (passkey: string | CryptoKey, salt: string = ''): Promise { try { - const e = new Entropy() + const e = await Entropy.create() return await Bip44Wallet.fromEntropy(passkey as string, e.hex, salt) } catch (err) { throw new Error(`Error creating new Bip44Wallet: ${err}`) @@ -324,11 +322,12 @@ export class Bip44Wallet extends Wallet { static async fromEntropy (key: CryptoKey, entropy: string, salt?: string): Promise static async fromEntropy (passkey: string | CryptoKey, entropy: string, salt: string = ''): Promise { try { - const e = new Entropy(entropy) + const id = await Entropy.create(16) + const e = await Entropy.import(entropy) const m = await Bip39Mnemonic.fromEntropy(e.hex) const s = await m.toBip39Seed(salt) Bip44Wallet.#isInternal = true - const wallet = new this(s, m) + const wallet = new this(id, s, m) await wallet.lock(passkey as string) return wallet } catch (err) { @@ -356,10 +355,11 @@ export class Bip44Wallet extends Wallet { static async fromMnemonic (key: CryptoKey, mnemonic: string, salt?: string): Promise static async fromMnemonic (passkey: string | CryptoKey, mnemonic: string, salt: string = ''): Promise { try { + const id = await Entropy.create(16) const m = await Bip39Mnemonic.fromPhrase(mnemonic) const s = await m.toBip39Seed(salt) Bip44Wallet.#isInternal = true - const wallet = new this(s, m) + const wallet = new this(id, s, m) await wallet.lock(passkey as string) return wallet } catch (err) { @@ -394,8 +394,9 @@ export class Bip44Wallet extends Wallet { if (!/^[0-9a-fA-F]+$/i.test(seed)) { throw new Error('Seed contains invalid hexadecimal characters.') } + const id = await Entropy.create(16) Bip44Wallet.#isInternal = true - const wallet = new this(seed) + const wallet = new this(id, seed) await wallet.lock(passkey as string) return wallet } @@ -411,7 +412,7 @@ export class Bip44Wallet extends Wallet { throw new TypeError('Wallet ID is required to restore') } Bip44Wallet.#isInternal = true - return new this('', undefined, id) + return new this(await Entropy.import(id), '') } /** @@ -449,12 +450,12 @@ export class Bip44Wallet extends Wallet { export class Blake2bWallet extends Wallet { static #isInternal: boolean = false - constructor (seed: string, mnemonic?: Bip39Mnemonic, id?: string) { + constructor (id: Entropy, seed: string, mnemonic?: Bip39Mnemonic) { if (!Blake2bWallet.#isInternal) { throw new Error(`Blake2bWallet cannot be instantiated directly. Use 'await Blake2bWallet.create()' instead.`) } Blake2bWallet.#isInternal = false - super(seed, mnemonic, id) + super(id, seed, mnemonic) } /** @@ -475,7 +476,7 @@ export class Blake2bWallet extends Wallet { static async create (key: CryptoKey): Promise static async create (passkey: string | CryptoKey): Promise { try { - const seed = new Entropy() + const seed = await Entropy.create() return await Blake2bWallet.fromSeed(passkey as string, seed.hex) } catch (err) { throw new Error(`Error creating new Blake2bWallet: ${err}`) @@ -507,10 +508,11 @@ export class Blake2bWallet extends Wallet { if (!/^[0-9a-fA-F]+$/i.test(seed)) { throw new Error('Seed contains invalid hexadecimal characters.') } + const id = await Entropy.create(16) const s = seed const m = await Bip39Mnemonic.fromEntropy(seed) Blake2bWallet.#isInternal = true - const wallet = new this(s, m) + const wallet = new this(id, s, m) await wallet.lock(passkey as string) return wallet } @@ -533,10 +535,11 @@ export class Blake2bWallet extends Wallet { static async fromMnemonic (key: CryptoKey, mnemonic: string): Promise static async fromMnemonic (passkey: string | CryptoKey, mnemonic: string): Promise { try { + const id = await Entropy.create(16) const m = await Bip39Mnemonic.fromPhrase(mnemonic) const s = await m.toBlake2bSeed() Blake2bWallet.#isInternal = true - const wallet = new this(s, m) + const wallet = new this(id, s, m) await wallet.lock(passkey as string) return wallet } catch (err) { @@ -555,7 +558,7 @@ export class Blake2bWallet extends Wallet { throw new TypeError('Wallet ID is required to restore') } Blake2bWallet.#isInternal = true - return new this('', undefined, id) + return new this(await Entropy.import(id), '') } /** @@ -594,12 +597,12 @@ export class LedgerWallet extends Wallet { get ledger () { return this.#ledger } - constructor (ledger: Ledger, id?: string) { + constructor (id: Entropy, ledger: Ledger) { if (!LedgerWallet.#isInternal) { throw new Error(`LedgerWallet cannot be instantiated directly. Use 'await LedgerWallet.create()' instead.`) } LedgerWallet.#isInternal = false - super(undefined, undefined, id) + super(id) this.#ledger = ledger } @@ -612,8 +615,9 @@ export class LedgerWallet extends Wallet { static async create (): Promise { const { Ledger } = await import('./ledger.js') const l = await Ledger.init() + const id = await Entropy.create(16) LedgerWallet.#isInternal = true - return new this(l) + return new this(id, l) } /** @@ -629,7 +633,7 @@ export class LedgerWallet extends Wallet { const { Ledger } = await import('./ledger.js') const l = await Ledger.init() LedgerWallet.#isInternal = true - return new this(l, id) + return new this(await Entropy.import(id), l) } /**