From ffe1b794fd77c010b286692e40e717290e3fe026 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 12 Sep 2025 22:27:22 -0700 Subject: [PATCH] Fix restored wallets with undefined type. --- src/lib/vault/vault-worker.ts | 3 ++- test/test.derive-accounts.mjs | 36 +++++++++++++++++++++++++++++++++++ test/test.lock-unlock.mjs | 10 +++++----- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/lib/vault/vault-worker.ts b/src/lib/vault/vault-worker.ts index e4b7c0a..45dabcf 100644 --- a/src/lib/vault/vault-worker.ts +++ b/src/lib/vault/vault-worker.ts @@ -222,7 +222,7 @@ export class VaultWorker { /** * Decrypts the input and sets the seed and, if it is included, the mnemonic. */ - unlock (type?: string, key?: CryptoKey, iv?: ArrayBuffer, encrypted?: ArrayBuffer): Promise> { + unlock (type?: 'BIP-44' | 'BLAKE2b', key?: CryptoKey, iv?: ArrayBuffer, encrypted?: ArrayBuffer): Promise> { if (type == null) { throw new TypeError('Wallet type is required') } @@ -244,6 +244,7 @@ export class VaultWorker { if (mnemonic != null && !(mnemonic instanceof ArrayBuffer)) { throw new TypeError('Invalid mnemonic') } + this.#type = type this.#seed = seed this.#mnemonic = mnemonic this.#locked = false diff --git a/test/test.derive-accounts.mjs b/test/test.derive-accounts.mjs index 1fee8fd..b84e84e 100644 --- a/test/test.derive-accounts.mjs +++ b/test/test.derive-accounts.mjs @@ -67,6 +67,24 @@ await Promise.all([ await assert.resolves(wallet.destroy()) }) + + await test('derive from restored BIP-44 wallet', async () => { + const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const restored = await Wallet.restore(wallet.id) + await restored.unlock(NANO_TEST_VECTORS.PASSWORD) + const account = await restored.account() + + assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_0) + assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_0) + assert.equal(account.index, 0) + + const accounts = await restored.accounts() + assert.exists(accounts[0]) + assert.equal(account, accounts[0]) + + await assert.resolves(wallet.destroy()) + await assert.resolves(restored.destroy()) + }) }), suite('Derive accounts from BLAKE2b wallet', async () => { @@ -121,5 +139,23 @@ await Promise.all([ await assert.resolves(wallet.destroy()) }) + + await test('derive from restored BLAKE2b wallet', async () => { + const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BLAKE2B_SEED) + const restored = await Wallet.restore(wallet.id) + await restored.unlock(NANO_TEST_VECTORS.PASSWORD) + const account = await restored.account(1) + + assert.equal(account.publicKey, NANO_TEST_VECTORS.BLAKE2B_PUBLIC_1) + assert.equal(account.address, NANO_TEST_VECTORS.BLAKE2B_ADDRESS_1) + assert.equal(account.index, 1) + + const accounts = await restored.accounts(1) + assert.exists(accounts[1]) + assert.equal(account, accounts[1]) + + await assert.resolves(wallet.destroy()) + await assert.resolves(restored.destroy()) + }) }) ]) diff --git a/test/test.lock-unlock.mjs b/test/test.lock-unlock.mjs index 14386df..a6ce21c 100644 --- a/test/test.lock-unlock.mjs +++ b/test/test.lock-unlock.mjs @@ -64,7 +64,7 @@ await Promise.all([ await assert.resolves(wallet.destroy()) }) - await test('fail to unlock a Bip44Wallet with different passwords', async () => { + await test('fail to unlock a BIP-44 wallet with different passwords', async () => { const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) wallet.lock() @@ -87,7 +87,7 @@ await Promise.all([ await assert.resolves(wallet.destroy()) }) - await test('fail to unlock a Bip44Wallet with invalid input', async () => { + await test('fail to unlock a BIP-44 wallet with invalid input', async () => { const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) @@ -98,7 +98,7 @@ await Promise.all([ await assert.resolves(wallet.destroy()) }) - await test('locking and unlocking a Blake2bWallet with a password', async () => { + await test('locking and unlocking a BLAKE2b wallet with a password', async () => { const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) assert.ok('mnemonic' in wallet) @@ -114,7 +114,7 @@ await Promise.all([ await assert.resolves(wallet.destroy()) }) - await test('fail to unlock a Blake2bWallet with different passwords', async () => { + await test('fail to unlock a BLAKE2b wallet with different passwords', async () => { const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) await assert.rejects(wallet.unlock(TREZOR_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' }) @@ -128,7 +128,7 @@ await Promise.all([ await assert.resolves(wallet.destroy()) }) - await test('fail to unlock a Blake2bWallet with no input', async () => { + await test('fail to unlock a BLAKE2b wallet with no input', async () => { const wallet = await Wallet.load('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) -- 2.47.3