]> zoso.dev Git - libnemo.git/commitdiff
Add option to restore LedgerWallet from a wallet ID.
authorChris Duncan <chris@zoso.dev>
Sat, 2 Nov 2024 08:36:02 +0000 (01:36 -0700)
committerChris Duncan <chris@zoso.dev>
Sat, 2 Nov 2024 08:36:02 +0000 (01:36 -0700)
src/lib/wallet.ts

index bfa6df2ca594d384a4dc91078ff933b66ed04697..96b65ad935cdb1b8f3cb10d2b07dedfdab86aa2b 100644 (file)
@@ -562,11 +562,11 @@ export class LedgerWallet extends Wallet {
 \r
        get ledger () { return this.#ledger }\r
 \r
-       constructor (ledger: Ledger) {\r
+       constructor (ledger: Ledger, id?: string) {\r
                if (!LedgerWallet.#isInternal) {\r
                        throw new Error(`LedgerWallet cannot be instantiated directly. Use 'await LedgerWallet.create()' instead.`)\r
                }\r
-               super()\r
+               super(undefined, undefined, id)\r
                this.#ledger = ledger\r
                LedgerWallet.#isInternal = false\r
        }\r
@@ -584,6 +584,22 @@ export class LedgerWallet extends Wallet {
                return new this(l)\r
        }\r
 \r
+       /**\r
+       * Retrieves an existing Ledger wallet from session storage using its ID.\r
+       *\r
+       * @param {string} id - Generated when the wallet was initially created\r
+       * @returns {LedgerWallet} Restored LedgerWallet\r
+       */\r
+       static async restore (id: string): Promise<LedgerWallet> {\r
+               LedgerWallet.#isInternal = true\r
+               if (typeof id !== 'string' || id === '') {\r
+                       throw new TypeError('Wallet ID is required to restore')\r
+               }\r
+               const { Ledger } = await import('./ledger.js')\r
+               const l = await Ledger.init()\r
+               return new this(l, id)\r
+       }\r
+\r
        /**\r
        * Gets the public key for an account from the Ledger device.\r
        *\r