From: Chris Duncan Date: Sat, 2 Nov 2024 08:36:02 +0000 (-0700) Subject: Add option to restore LedgerWallet from a wallet ID. X-Git-Tag: v0.0.18~1 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=c1bdb120112a90f16e42f105f78d704fe70ff2ff;p=libnemo.git Add option to restore LedgerWallet from a wallet ID. --- diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index bfa6df2..96b65ad 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -562,11 +562,11 @@ export class LedgerWallet extends Wallet { get ledger () { return this.#ledger } - constructor (ledger: Ledger) { + constructor (ledger: Ledger, id?: string) { if (!LedgerWallet.#isInternal) { throw new Error(`LedgerWallet cannot be instantiated directly. Use 'await LedgerWallet.create()' instead.`) } - super() + super(undefined, undefined, id) this.#ledger = ledger LedgerWallet.#isInternal = false } @@ -584,6 +584,22 @@ export class LedgerWallet extends Wallet { return new this(l) } + /** + * Retrieves an existing Ledger wallet from session storage using its ID. + * + * @param {string} id - Generated when the wallet was initially created + * @returns {LedgerWallet} Restored LedgerWallet + */ + static async restore (id: string): Promise { + LedgerWallet.#isInternal = true + if (typeof id !== 'string' || id === '') { + throw new TypeError('Wallet ID is required to restore') + } + const { Ledger } = await import('./ledger.js') + const l = await Ledger.init() + return new this(l, id) + } + /** * Gets the public key for an account from the Ledger device. *