From: Chris Duncan Date: Mon, 14 Oct 2024 09:25:13 +0000 (-0700) Subject: Rename wallet import to restore which better reflects that it is being retrieved... X-Git-Tag: v0.0.10~2 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=f0bea675f7d7ee01e963fb78d2af6154de217683;p=libnemo.git Rename wallet import to restore which better reflects that it is being retrieved from existing storage. Add tests for wallet restore. --- diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index f0dabd5..fafc6d1 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -376,9 +376,19 @@ export class Bip44Wallet extends Wallet { return wallet } - static async import (id: string) { + /** + * Retrieves an existing HD wallet from session storage using its ID. + * + * @param {string} id - Generated when the wallet was initially created + * @returns {Bip44Wallet} Restored locked Bip44Wallet + */ + static async restore (id: string): Promise { Bip44Wallet.#isInternal = true + if (typeof id !== 'string' || id === '') { + throw new TypeError('Wallet ID is required to restore') + } const wallet = new this('', undefined, id) + return wallet } /** @@ -508,9 +518,19 @@ export class Blake2bWallet extends Wallet { } } - static async import (id: string) { + /** + * Retrieves an existing BLAKE2b wallet from session storage using its ID. + * + * @param {string} id - Generated when the wallet was initially created + * @returns {Blake2bWallet} Restored locked Blake2bWallet + */ + static async restore (id: string): Promise { Blake2bWallet.#isInternal = true + if (typeof id !== 'string' || id === '') { + throw new TypeError('Wallet ID is required to restore') + } const wallet = new this('', undefined, id) + return wallet } /** diff --git a/test/import-wallet.test.mjs b/test/import-wallet.test.mjs index 1835f7c..9aaa067 100644 --- a/test/import-wallet.test.mjs +++ b/test/import-wallet.test.mjs @@ -173,3 +173,41 @@ describe('invalid wallet', async () => { { message: 'Seed contains invalid hexadecimal characters.' }) }) }) + +describe('import from storage', async () => { + it('should retrieve a Bip44Wallet from storage using an ID', async () => { + const id = (await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)).id + const wallet = await Bip44Wallet.restore(id) + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equal(wallet.mnemonic, '') + assert.equal(wallet.seed, '') + + const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + assert.equal(unlockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equal(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) + assert.equal(wallet.seed, NANO_TEST_VECTORS.BIP39_SEED) + }) + + it('should retrieve a Blake2bWallet from storage using an ID', async () => { + const id = (await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)).id + const wallet = await Blake2bWallet.restore(id) + + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equal(wallet.mnemonic, '') + assert.equal(wallet.seed, '') + + const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + + assert.equal(unlockResult, true) + assert.ok('mnemonic' in wallet) + assert.ok('seed' in wallet) + assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_0) + assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_0) + }) +})