From ec393b57f129aa7afdf301fb434af253fb311216 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 3 Jul 2025 14:15:08 -0700 Subject: [PATCH] Fix tests using random bytes since password gets zeroed out when used. --- test/test.lock-unlock-wallet.mjs | 38 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/test/test.lock-unlock-wallet.mjs b/test/test.lock-unlock-wallet.mjs index 2252f04..a22c5d9 100644 --- a/test/test.lock-unlock-wallet.mjs +++ b/test/test.lock-unlock-wallet.mjs @@ -31,16 +31,16 @@ await suite('Lock and unlock wallets', async () => { await test('locking and unlocking a Bip44Wallet with random bytes', async () => { const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const lockResult = await wallet.lock(key) + const key = globalThis.crypto.getRandomValues(new Uint8Array(64)) + const lockResult = await wallet.lock(new Uint8Array(key)) assert.ok(lockResult) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.equals(wallet.mnemonic, '') assert.equals(wallet.seed, '') - const unlockResult = await wallet.unlock(key) + const unlockResult = await wallet.unlock(new Uint8Array(key)) assert.equals(unlockResult, true) assert.ok('mnemonic' in wallet) @@ -69,11 +69,11 @@ await suite('Lock and unlock wallets', async () => { await test('fail to unlock a Bip44Wallet with different random bytes', async () => { const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const rightKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const wrongKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const lockResult = await wallet.lock(rightKey) + const rightKey = globalThis.crypto.getRandomValues(new Uint8Array(64)) + const wrongKey = globalThis.crypto.getRandomValues(new Uint8Array(64)) + const lockResult = await wallet.lock(new Uint8Array(rightKey)) - await assert.rejects(wallet.unlock(wrongKey), { message: 'Failed to unlock wallet' }) + await assert.rejects(wallet.unlock(new Uint8Array(wrongKey)), { message: 'Failed to unlock wallet' }) assert.equals(lockResult, true) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -85,9 +85,9 @@ await suite('Lock and unlock wallets', async () => { await test('fail to unlock a Bip44Wallet with different valid inputs', async () => { const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) - const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + const key = globalThis.crypto.getRandomValues(new Uint8Array(64)) - await assert.rejects(wallet.unlock(key), { message: 'Failed to unlock wallet' }) + await assert.rejects(wallet.unlock(new Uint8Array(key)), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC) @@ -161,11 +161,11 @@ await suite('Lock and unlock wallets', async () => { await wallet.destroy() }) - await test('locking and unlocking a Blake2bWallet with a random CryptoKey', async () => { + await test('locking and unlocking a Blake2bWallet with random bytes', async () => { const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const lockResult = await wallet.lock(key) + const key = globalThis.crypto.getRandomValues(new Uint8Array(64)) + const lockResult = await wallet.lock(new Uint8Array(key)) assert.equals(lockResult, true) assert.ok('mnemonic' in wallet) @@ -173,7 +173,7 @@ await suite('Lock and unlock wallets', async () => { assert.equals(wallet.mnemonic, '') assert.equals(wallet.seed, '') - const unlockResult = await wallet.unlock(key) + const unlockResult = await wallet.unlock(new Uint8Array(key)) assert.equals(lockResult, true) assert.equals(unlockResult, true) @@ -200,11 +200,11 @@ await suite('Lock and unlock wallets', async () => { await test('fail to unlock a Blake2bWallet with different keys', async () => { const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const rightKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const wrongKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) - const lockResult = await wallet.lock(rightKey) + const rightKey = globalThis.crypto.getRandomValues(new Uint8Array(64)) + const wrongKey = globalThis.crypto.getRandomValues(new Uint8Array(64)) + const lockResult = await wallet.lock(new Uint8Array(rightKey)) - await assert.rejects(wallet.unlock(wrongKey), { message: 'Failed to unlock wallet' }) + await assert.rejects(wallet.unlock(new Uint8Array(wrongKey)), { message: 'Failed to unlock wallet' }) assert.equals(lockResult, true) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -216,9 +216,9 @@ await suite('Lock and unlock wallets', async () => { await test('fail to unlock a Blake2bWallet with different valid inputs', async () => { const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) - const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + const key = globalThis.crypto.getRandomValues(new Uint8Array(64)) - await assert.rejects(wallet.unlock(key), { message: 'Failed to unlock wallet' }) + await assert.rejects(wallet.unlock(new Uint8Array(key)), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) -- 2.47.3