]> git.codecow.com Git - libnemo.git/commitdiff
Fix tests using random bytes since password gets zeroed out when used.
authorChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 21:15:08 +0000 (14:15 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 21:15:08 +0000 (14:15 -0700)
test/test.lock-unlock-wallet.mjs

index 2252f046c3829a37620deda0deb19d592d17b19b..a22c5d980c27301976761c56a57b4758a9b98bdc 100644 (file)
@@ -31,16 +31,16 @@ await suite('Lock and unlock wallets', async () => {
        await test('locking and unlocking a Bip44Wallet with random bytes', async () => {\r
                const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
-               const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt'])\r
-               const lockResult = await wallet.lock(key)\r
+               const key = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
 \r
+               const lockResult = await wallet.lock(new Uint8Array(key))\r
                assert.ok(lockResult)\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
                assert.equals(wallet.mnemonic, '')\r
                assert.equals(wallet.seed, '')\r
 \r
-               const unlockResult = await wallet.unlock(key)\r
+               const unlockResult = await wallet.unlock(new Uint8Array(key))\r
 \r
                assert.equals(unlockResult, true)\r
                assert.ok('mnemonic' in wallet)\r
@@ -69,11 +69,11 @@ await suite('Lock and unlock wallets', async () => {
        await test('fail to unlock a Bip44Wallet with different random bytes', async () => {\r
                const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
-               const rightKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt'])\r
-               const wrongKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt'])\r
-               const lockResult = await wallet.lock(rightKey)\r
+               const rightKey = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
+               const wrongKey = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
+               const lockResult = await wallet.lock(new Uint8Array(rightKey))\r
 \r
-               await assert.rejects(wallet.unlock(wrongKey), { message: 'Failed to unlock wallet' })\r
+               await assert.rejects(wallet.unlock(new Uint8Array(wrongKey)), { message: 'Failed to unlock wallet' })\r
                assert.equals(lockResult, true)\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
@@ -85,9 +85,9 @@ await suite('Lock and unlock wallets', async () => {
 \r
        await test('fail to unlock a Bip44Wallet with different valid inputs', async () => {\r
                const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
-               const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt'])\r
+               const key = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
 \r
-               await assert.rejects(wallet.unlock(key), { message: 'Failed to unlock wallet' })\r
+               await assert.rejects(wallet.unlock(new Uint8Array(key)), { message: 'Failed to unlock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
                assert.notEqual(wallet.mnemonic, NANO_TEST_VECTORS.MNEMONIC)\r
@@ -161,11 +161,11 @@ await suite('Lock and unlock wallets', async () => {
                await wallet.destroy()\r
        })\r
 \r
-       await test('locking and unlocking a Blake2bWallet with a random CryptoKey', async () => {\r
+       await test('locking and unlocking a Blake2bWallet with random bytes', async () => {\r
                const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
-               const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt'])\r
-               const lockResult = await wallet.lock(key)\r
+               const key = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
+               const lockResult = await wallet.lock(new Uint8Array(key))\r
 \r
                assert.equals(lockResult, true)\r
                assert.ok('mnemonic' in wallet)\r
@@ -173,7 +173,7 @@ await suite('Lock and unlock wallets', async () => {
                assert.equals(wallet.mnemonic, '')\r
                assert.equals(wallet.seed, '')\r
 \r
-               const unlockResult = await wallet.unlock(key)\r
+               const unlockResult = await wallet.unlock(new Uint8Array(key))\r
 \r
                assert.equals(lockResult, true)\r
                assert.equals(unlockResult, true)\r
@@ -200,11 +200,11 @@ await suite('Lock and unlock wallets', async () => {
        await test('fail to unlock a Blake2bWallet with different keys', async () => {\r
                const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
-               const rightKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt'])\r
-               const wrongKey = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt'])\r
-               const lockResult = await wallet.lock(rightKey)\r
+               const rightKey = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
+               const wrongKey = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
+               const lockResult = await wallet.lock(new Uint8Array(rightKey))\r
 \r
-               await assert.rejects(wallet.unlock(wrongKey), { message: 'Failed to unlock wallet' })\r
+               await assert.rejects(wallet.unlock(new Uint8Array(wrongKey)), { message: 'Failed to unlock wallet' })\r
                assert.equals(lockResult, true)\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
@@ -216,9 +216,9 @@ await suite('Lock and unlock wallets', async () => {
 \r
        await test('fail to unlock a Blake2bWallet with different valid inputs', async () => {\r
                const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
-               const key = await globalThis.crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt'])\r
+               const key = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
 \r
-               await assert.rejects(wallet.unlock(key), { message: 'Failed to unlock wallet' })\r
+               await assert.rejects(wallet.unlock(new Uint8Array(key)), { message: 'Failed to unlock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
                assert.notEqual(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1)\r