]> zoso.dev Git - libnemo.git/commitdiff
Rename wallet import to restore which better reflects that it is being retrieved...
authorChris Duncan <chris@zoso.dev>
Mon, 14 Oct 2024 09:25:13 +0000 (02:25 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 14 Oct 2024 09:25:13 +0000 (02:25 -0700)
src/lib/wallet.ts
test/import-wallet.test.mjs

index f0dabd5fe74ded42685f82a499e17f85298cc739..fafc6d1193306378daffd327cbca39c6b9d5ec36 100644 (file)
@@ -376,9 +376,19 @@ export class Bip44Wallet extends Wallet {
                return wallet\r
        }\r
 \r
-       static async import (id: string) {\r
+       /**\r
+       * Retrieves an existing HD wallet from session 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
                Bip44Wallet.#isInternal = true\r
+               if (typeof id !== 'string' || id === '') {\r
+                       throw new TypeError('Wallet ID is required to restore')\r
+               }\r
                const wallet = new this('', undefined, id)\r
+               return wallet\r
        }\r
 \r
        /**\r
@@ -508,9 +518,19 @@ export class Blake2bWallet extends Wallet {
                }\r
        }\r
 \r
-       static async import (id: string) {\r
+       /**\r
+       * Retrieves an existing BLAKE2b wallet from session 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
                Blake2bWallet.#isInternal = true\r
+               if (typeof id !== 'string' || id === '') {\r
+                       throw new TypeError('Wallet ID is required to restore')\r
+               }\r
                const wallet = new this('', undefined, id)\r
+               return wallet\r
        }\r
 \r
        /**\r
index 1835f7c0422ba7bbf9174fec2a5f5b67c1672958..9aaa0672357e250478d8cd22be120c00180ef063 100644 (file)
@@ -173,3 +173,41 @@ describe('invalid wallet', async () => {
                        { message: 'Seed contains invalid hexadecimal characters.' })\r
        })\r
 })\r
+\r
+describe('import from storage', async () => {\r
+       it('should retrieve a Bip44Wallet from storage using an ID', async () => {\r
+               const id = (await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)).id\r
+               const wallet = await Bip44Wallet.restore(id)\r
+\r
+               assert.ok('mnemonic' in wallet)\r
+               assert.ok('seed' in wallet)\r
+               assert.equal(wallet.mnemonic, '')\r
+               assert.equal(wallet.seed, '')\r
+\r
+               const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
+\r
+               assert.equal(unlockResult, true)\r
+               assert.ok('mnemonic' in wallet)\r
+               assert.ok('seed' in wallet)\r
+               assert.equal(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC)\r
+               assert.equal(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED)\r
+       })\r
+\r
+       it('should retrieve a Blake2bWallet from storage using an ID', async () => {\r
+               const id = (await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)).id\r
+               const wallet = await Blake2bWallet.restore(id)\r
+\r
+               assert.ok('mnemonic' in wallet)\r
+               assert.ok('seed' in wallet)\r
+               assert.equal(wallet.mnemonic, '')\r
+               assert.equal(wallet.seed, '')\r
+\r
+               const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
+\r
+               assert.equal(unlockResult, true)\r
+               assert.ok('mnemonic' in wallet)\r
+               assert.ok('seed' in wallet)\r
+               assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0)\r
+               assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0)\r
+       })\r
+})\r