]> git.codecow.com Git - libnemo.git/commitdiff
Move restore wallet method to parent class and implement type in wallet ID for easy...
authorChris Duncan <chris@zoso.dev>
Sat, 26 Jul 2025 05:58:20 +0000 (22:58 -0700)
committerChris Duncan <chris@zoso.dev>
Sat, 26 Jul 2025 05:58:20 +0000 (22:58 -0700)
src/lib/wallets/bip44-wallet.ts
src/lib/wallets/blake2b-wallet.ts
src/lib/wallets/ledger-wallet.ts
src/lib/wallets/wallet.ts

index a3dff4f090c7f645ff18ae86bfe8eb9a387bcbad..2d9e125a77bfd3f21e16540fc69afdaae1f81015 100644 (file)
@@ -190,20 +190,6 @@ export class Bip44Wallet extends Wallet {
                return wallet\r
        }\r
 \r
-       /**\r
-       * Retrieves an existing HD wallet from storage using its ID.\r
-       *\r
-       * @param {string} id - Generated when the wallet was initially created\r
-       * @returns {Bip44Wallet} Restored locked Bip44Wallet\r
-       */\r
-       static async restore (id: string): Promise<Bip44Wallet> {\r
-               if (typeof id !== 'string' || id === '') {\r
-                       throw new TypeError('Wallet ID is required to restore')\r
-               }\r
-               Bip44Wallet.#isInternal = true\r
-               return new this(await Entropy.import(id))\r
-       }\r
-\r
        /**\r
        * Derives BIP-44 Nano account private keys.\r
        *\r
index 9bd6f74f1caca0ae4cd673cf42b15fc3f7fb23ed..8dd93a4d2d447d20195d2953bdca51506d38deae 100644 (file)
@@ -139,20 +139,6 @@ export class Blake2bWallet extends Wallet {
                return wallet\r
        }\r
 \r
-       /**\r
-       * Retrieves an existing BLAKE2b wallet from storage using its ID.\r
-       *\r
-       * @param {string} id - Generated when the wallet was initially created\r
-       * @returns {Blake2bWallet} Restored locked Blake2bWallet\r
-       */\r
-       static async restore (id: string): Promise<Blake2bWallet> {\r
-               if (typeof id !== 'string' || id === '') {\r
-                       throw new TypeError('Wallet ID is required to restore')\r
-               }\r
-               Blake2bWallet.#isInternal = true\r
-               return new this(await Entropy.import(id))\r
-       }\r
-\r
        /**\r
        * Derives BLAKE2b account private keys.\r
        *\r
index 87e25e67bb16d2ab87155c2649b6fddebaa6290c..839294efa58e617fd50a8f9fad41ed26313c3d0b 100644 (file)
@@ -169,28 +169,6 @@ export class LedgerWallet extends Wallet {
                }\r
        }\r
 \r
-       /**\r
-       * Retrieves an existing Ledger wallet from 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
-               if (typeof id !== 'string' || id === '') {\r
-                       throw new TypeError('Wallet ID is required to restore')\r
-               }\r
-               try {\r
-                       const transport = LedgerWallet.checkBrowserSupport()\r
-                       LedgerWallet.#isInternal = true\r
-                       const wallet = new this(await Entropy.import(id))\r
-                       wallet.DynamicTransport = transport\r
-                       return wallet\r
-               } catch (err) {\r
-                       console.error(err)\r
-                       throw new Error('failed to restore wallet', { cause: err })\r
-               }\r
-       }\r
-\r
        /**\r
        * Sign a block with the Ledger device.\r
        *\r
index 010d51d497a4b5e57ba2f6fbb8d79f5e5cf3fe30..a483134763cf67b503a09268ac18efe986ca8ee1 100644 (file)
@@ -39,6 +39,33 @@ export abstract class Wallet {
                }\r
        }\r
 \r
+       /**\r
+       * Reinitializes a wallet from storage using its ID.\r
+       *\r
+       * @param {string} id - Generated when the wallet was initially created\r
+       * @returns {Wallet} Wallet locked with password used when initially created\r
+       */\r
+       static async restore (id: string): Promise<Wallet> {\r
+               if (typeof id !== 'string' || id === '') {\r
+                       throw new TypeError('Wallet ID is required to restore')\r
+               }\r
+               const idSplit = id.split('_')\r
+               if (idSplit.length !== 2) {\r
+                       throw new Error('Invalid wallet ID')\r
+               }\r
+               const idType = idSplit[0]\r
+               if (idType !== 'BIP-44' && idType !== 'BLAKE2b' && idType !== 'Ledger') {\r
+                       throw new Error('Invalid wallet ID')\r
+               }\r
+               try {\r
+                       const idEntropy = await Entropy.import(idSplit[1])\r
+                       return new this(idEntropy, idType)\r
+               } catch (err) {\r
+                       console.error(err)\r
+                       throw new Error('Failed to restore wallet', { cause: err })\r
+               }\r
+       }\r
+\r
        #accounts: AccountList\r
        #id: Entropy\r
        #locked: boolean\r
@@ -46,7 +73,7 @@ export abstract class Wallet {
        #s?: Uint8Array<ArrayBuffer>\r
        #type: WalletType\r
 \r
-       get id () { return this.#id.hex }\r
+       get id () { return `${this.type}_${this.#id.hex}` }\r
        get isLocked () { return this.#locked }\r
        get isUnlocked () { return !this.#locked }\r
        get mnemonic () {\r