From c1bdb120112a90f16e42f105f78d704fe70ff2ff Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 2 Nov 2024 01:36:02 -0700 Subject: [PATCH] Add option to restore LedgerWallet from a wallet ID. --- src/lib/wallet.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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. * -- 2.34.1