From: Chris Duncan Date: Sat, 26 Jul 2025 05:58:20 +0000 (-0700) Subject: Move restore wallet method to parent class and implement type in wallet ID for easy... X-Git-Tag: v0.10.5~50^2~19 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=eec670d4a56cd55ffb4bf63d734e34d5dab6688b;p=libnemo.git Move restore wallet method to parent class and implement type in wallet ID for easy visibility in db. --- diff --git a/src/lib/wallets/bip44-wallet.ts b/src/lib/wallets/bip44-wallet.ts index a3dff4f..2d9e125 100644 --- a/src/lib/wallets/bip44-wallet.ts +++ b/src/lib/wallets/bip44-wallet.ts @@ -190,20 +190,6 @@ export class Bip44Wallet extends Wallet { return wallet } - /** - * Retrieves an existing HD wallet from storage using its ID. - * - * @param {string} id - Generated when the wallet was initially created - * @returns {Bip44Wallet} Restored locked Bip44Wallet - */ - static async restore (id: string): Promise { - if (typeof id !== 'string' || id === '') { - throw new TypeError('Wallet ID is required to restore') - } - Bip44Wallet.#isInternal = true - return new this(await Entropy.import(id)) - } - /** * Derives BIP-44 Nano account private keys. * diff --git a/src/lib/wallets/blake2b-wallet.ts b/src/lib/wallets/blake2b-wallet.ts index 9bd6f74..8dd93a4 100644 --- a/src/lib/wallets/blake2b-wallet.ts +++ b/src/lib/wallets/blake2b-wallet.ts @@ -139,20 +139,6 @@ export class Blake2bWallet extends Wallet { return wallet } - /** - * Retrieves an existing BLAKE2b wallet from storage using its ID. - * - * @param {string} id - Generated when the wallet was initially created - * @returns {Blake2bWallet} Restored locked Blake2bWallet - */ - static async restore (id: string): Promise { - if (typeof id !== 'string' || id === '') { - throw new TypeError('Wallet ID is required to restore') - } - Blake2bWallet.#isInternal = true - return new this(await Entropy.import(id)) - } - /** * Derives BLAKE2b account private keys. * diff --git a/src/lib/wallets/ledger-wallet.ts b/src/lib/wallets/ledger-wallet.ts index 87e25e6..839294e 100644 --- a/src/lib/wallets/ledger-wallet.ts +++ b/src/lib/wallets/ledger-wallet.ts @@ -169,28 +169,6 @@ export class LedgerWallet extends Wallet { } } - /** - * Retrieves an existing Ledger wallet from storage using its ID. - * - * @param {string} id - Generated when the wallet was initially created - * @returns {LedgerWallet} Restored LedgerWallet - */ - static async restore (id: string): Promise { - if (typeof id !== 'string' || id === '') { - throw new TypeError('Wallet ID is required to restore') - } - try { - const transport = LedgerWallet.checkBrowserSupport() - LedgerWallet.#isInternal = true - const wallet = new this(await Entropy.import(id)) - wallet.DynamicTransport = transport - return wallet - } catch (err) { - console.error(err) - throw new Error('failed to restore wallet', { cause: err }) - } - } - /** * Sign a block with the Ledger device. * diff --git a/src/lib/wallets/wallet.ts b/src/lib/wallets/wallet.ts index 010d51d..a483134 100644 --- a/src/lib/wallets/wallet.ts +++ b/src/lib/wallets/wallet.ts @@ -39,6 +39,33 @@ export abstract class Wallet { } } + /** + * Reinitializes a wallet from storage using its ID. + * + * @param {string} id - Generated when the wallet was initially created + * @returns {Wallet} Wallet locked with password used when initially created + */ + static async restore (id: string): Promise { + if (typeof id !== 'string' || id === '') { + throw new TypeError('Wallet ID is required to restore') + } + const idSplit = id.split('_') + if (idSplit.length !== 2) { + throw new Error('Invalid wallet ID') + } + const idType = idSplit[0] + if (idType !== 'BIP-44' && idType !== 'BLAKE2b' && idType !== 'Ledger') { + throw new Error('Invalid wallet ID') + } + try { + const idEntropy = await Entropy.import(idSplit[1]) + return new this(idEntropy, idType) + } catch (err) { + console.error(err) + throw new Error('Failed to restore wallet', { cause: err }) + } + } + #accounts: AccountList #id: Entropy #locked: boolean @@ -46,7 +73,7 @@ export abstract class Wallet { #s?: Uint8Array #type: WalletType - get id () { return this.#id.hex } + get id () { return `${this.type}_${this.#id.hex}` } get isLocked () { return this.#locked } get isUnlocked () { return !this.#locked } get mnemonic () {