]> git.codecow.com Git - libnemo.git/commitdiff
Update tests. Remove redundant globalThis references.
authorChris Duncan <chris@zoso.dev>
Fri, 1 Aug 2025 21:48:59 +0000 (14:48 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 1 Aug 2025 21:48:59 +0000 (14:48 -0700)
src/lib/bip39-mnemonic.ts
src/lib/bip44-ckd.ts
src/lib/entropy.ts
src/lib/ledger.ts
src/lib/wallet.ts
test/test.blocks.mjs
test/test.lock-unlock.mjs
test/test.tools.mjs

index e00d7c44f1abe145f8885c46b82630cde37e95ce..ea9ffffe9378a21e0fd3a5827f7ddfe80e28d4cf 100644 (file)
@@ -182,7 +182,7 @@ export class Bip39Mnemonic {
                                ? ''\r
                                : passphrase\r
                        const keyData = utf8.toBytes(this.phrase)\r
-                       const phraseKey = await globalThis.crypto.subtle.importKey('raw', keyData, 'PBKDF2', false, ['deriveBits', 'deriveKey'])\r
+                       const phraseKey = await crypto.subtle.importKey('raw', keyData, 'PBKDF2', false, ['deriveBits', 'deriveKey'])\r
                        const derivedKeyType: HmacImportParams = {\r
                                name: 'HMAC',\r
                                hash: 'SHA-512',\r
@@ -196,8 +196,8 @@ export class Bip39Mnemonic {
                                salt: utf8.toBytes(salt),\r
                                iterations: BIP39_ITERATIONS\r
                        }\r
-                       const seedKey = await globalThis.crypto.subtle.deriveKey(algorithm, phraseKey, derivedKeyType, true, ['sign'])\r
-                       const seedBuffer = await globalThis.crypto.subtle.exportKey('raw', seedKey)\r
+                       const seedKey = await crypto.subtle.deriveKey(algorithm, phraseKey, derivedKeyType, true, ['sign'])\r
+                       const seedBuffer = await crypto.subtle.exportKey('raw', seedKey)\r
                        this.#bip39Seed = new Uint8Array(seedBuffer)\r
                }\r
                return format === 'hex'\r
index 489854eb6f666a64d68b6b48f0321078c90480ec..73915ab91bc5422a55321c72586f8b73ce36b5c3 100644 (file)
@@ -92,7 +92,7 @@ export class Bip44Ckd {
        }
 
        static async hmac (key: Uint8Array, data: Uint8Array): Promise<Uint8Array<ArrayBuffer>> {
-               const { subtle } = globalThis.crypto
+               const { subtle } = crypto
                const pk = await subtle.importKey('raw', key, { name: 'HMAC', hash: 'SHA-512' }, false, ['sign'])
                const signature = await subtle.sign('HMAC', pk, data)
                return new Uint8Array(signature)
index afdc128d2d3c9420e5c1f3447c04647ff70a51a9..3e1aaa049aa76c5b3df530acb15e4e1a0dc50f9d 100644 (file)
@@ -55,7 +55,7 @@ export class Entropy {
                                }
                        }
                        Entropy.#isInternal = true
-                       resolve(new this(globalThis.crypto.getRandomValues(new Uint8Array(size ?? this.MAX))))
+                       resolve(new this(crypto.getRandomValues(new Uint8Array(size ?? this.MAX))))
                })
        }
 
@@ -124,7 +124,7 @@ export class Entropy {
        */
        destroy (): boolean {
                try {
-                       globalThis.crypto.getRandomValues(this.#bytes)
+                       crypto.getRandomValues(this.#bytes)
                        return true
                } catch (err) {
                        return false
index 002eacd0c5c1014137e741f338ea91dc9a29dc54..af8b91181a6ba352f7ddfbd95234a413b610522b 100644 (file)
@@ -83,7 +83,7 @@ export class Ledger extends Wallet {
 \r
        private constructor () {\r
                if (!Ledger.#isInternal) {\r
-                       throw new Error(`LedgerWallet cannot be instantiated directly. Use 'await LedgerWallet.create()' instead.`)\r
+                       throw new Error(`Ledger cannot be instantiated directly. Use 'await Ledger.create()' instead.`)\r
                }\r
                Ledger.#isInternal = false\r
                super('Ledger')\r
index 4b3b1c70801e1da8fe51108e053c8bfd816efb67..b3de04e74639da3697913cc8809890534e585a2e 100644 (file)
@@ -178,7 +178,7 @@ export class Wallet {
        }\r
 \r
        constructor (type: WalletType) {\r
-               if (!Wallet.#isInternal) {\r
+               if (!Wallet.#isInternal && type !== 'Ledger') {\r
                        throw new Error(`Wallet cannot be instantiated directly. Use 'await Wallet.create()' instead.`)\r
                }\r
                Wallet.#isInternal = false\r
index d6cf3bd2704e7ca7d15418aa216192c0e277655e..1e536377233c55db2834c7741a1b74d543681f50 100644 (file)
@@ -112,9 +112,9 @@ await Promise.all([
                                NANO_TEST_VECTORS.OPEN_BLOCK.previous,\r
                                NANO_TEST_VECTORS.OPEN_BLOCK.work\r
                        )\r
-                       await wallet.sign(0, block)\r
+                       await assert.rejects(wallet.sign(0, block))\r
                        assert.equal(block.hash, NANO_TEST_VECTORS.OPEN_BLOCK.hash)\r
-                       assert.equal(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature)\r
+                       assert.notEqual(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature)\r
                })\r
 \r
                await test('sign open block with private key', async () => {\r
index bcd3ee86a5f485a1dad801d89e76439d5dba9741..bde09da961df8e4cc698c896448cea22e955a281 100644 (file)
@@ -11,29 +11,26 @@ import { NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './VECTORS.mjs'
 */\r
 let Account\r
 /**\r
-* @type {typeof import('../dist/types.d.ts').Bip44Wallet}\r
+* @type {typeof import('../dist/types.d.ts').Wallet}\r
 */\r
-let Bip44Wallet\r
-/**\r
-* @type {typeof import('../dist/types.d.ts').Blake2bWallet}\r
-*/\r
-let Blake2bWallet\r
+let Wallet\r
 if (isNode) {\r
-       ({ Account, Bip44Wallet, Blake2bWallet } = await import('../dist/nodejs.min.js'))\r
+       ({ Account, Wallet } = await import('../dist/nodejs.min.js'))\r
 } else {\r
-       ({ Account, Bip44Wallet, Blake2bWallet } = await import('../dist/browser.min.js'))\r
+       ({ Account, Wallet } = await import('../dist/browser.min.js'))\r
 }\r
 \r
 await Promise.all([\r
        suite('Lock and unlock wallets', async () => {\r
 \r
                await test('locking and unlocking a Bip44Wallet with a password', async () => {\r
-                       const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
+                       debugger\r
+                       const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
 \r
                        assert.ok('mnemonic' in wallet)\r
                        assert.ok('seed' in wallet)\r
-                       assert.throws(() => wallet.mnemonic)\r
-                       assert.throws(() => wallet.seed)\r
+                       assert.ok(wallet.mnemonic === undefined)\r
+                       assert.ok(wallet.seed === undefined)\r
 \r
                        const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
 \r
@@ -47,10 +44,10 @@ await Promise.all([
                })\r
 \r
                await test('locking and unlocking a Bip44Wallet Account with a password', async () => {\r
-                       const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
+                       const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                        const account = await wallet.account()\r
-                       const lockResult = await wallet.lock(NANO_TEST_VECTORS.PASSWORD)\r
+                       const lockResult = await wallet.lock()\r
 \r
                        assert.equal(lockResult, true)\r
 \r
@@ -61,9 +58,9 @@ await Promise.all([
                })\r
 \r
                await test('fail to unlock a Bip44Wallet with different passwords', async () => {\r
-                       const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
+                       const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
-                       const lockResult = await wallet.lock(TREZOR_TEST_VECTORS.PASSWORD)\r
+                       const lockResult = await wallet.lock()\r
 \r
                        await assert.rejects(wallet.unlock(NANO_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' })\r
                        assert.equal(lockResult, true)\r
@@ -76,10 +73,10 @@ await Promise.all([
                })\r
 \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 = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
+                       const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
+                       const key = crypto.getRandomValues(new Uint8Array(64))\r
 \r
-                       await assert.rejects(wallet.unlock(new Uint8Array(key)), { message: 'Failed to unlock wallet' })\r
+                       await assert.rejects(wallet.unlock(crypto.randomUUID()), { message: 'Failed to unlock wallet' })\r
                        assert.ok('mnemonic' in wallet)\r
                        assert.ok('seed' in wallet)\r
                        assert.throws(() => wallet.mnemonic)\r
@@ -89,18 +86,9 @@ await Promise.all([
                })\r
 \r
                await test('fail to unlock a Bip44Wallet with no input', async () => {\r
-                       const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
+                       const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
 \r
-                       //@ts-expect-error\r
-                       await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' })\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
-                       await wallet.lock('password')\r
-\r
                        //@ts-expect-error\r
                        await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' })\r
                        assert.ok('mnemonic' in wallet)\r
@@ -112,17 +100,7 @@ await Promise.all([
                })\r
 \r
                await test('fail to unlock a Bip44Wallet with invalid input', 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
-\r
-                       //@ts-expect-error\r
-                       await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' })\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
-                       await wallet.lock(NANO_TEST_VECTORS.PASSWORD)\r
+                       const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
 \r
                        //@ts-expect-error\r
                        await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' })\r
@@ -135,7 +113,7 @@ await Promise.all([
                })\r
 \r
                await test('locking and unlocking a Blake2bWallet with a password', async () => {\r
-                       const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)\r
+                       const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)\r
 \r
                        assert.ok('mnemonic' in wallet)\r
                        assert.ok('seed' in wallet)\r
@@ -154,10 +132,10 @@ await Promise.all([
                })\r
 \r
                await test('locking and unlocking a Blake2bWallet Account with a password', async () => {\r
-                       const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)\r
+                       const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                        const account = await wallet.account()\r
-                       const lockResult = await wallet.lock(NANO_TEST_VECTORS.PASSWORD)\r
+                       const lockResult = await wallet.lock()\r
 \r
                        assert.equal(lockResult, true)\r
 \r
@@ -168,7 +146,7 @@ await Promise.all([
                })\r
 \r
                await test('fail to unlock a Blake2bWallet with different passwords', async () => {\r
-                       const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
+                       const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
 \r
                        await assert.rejects(wallet.unlock(TREZOR_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' })\r
                        assert.ok('mnemonic' in wallet)\r
@@ -180,10 +158,10 @@ await Promise.all([
                })\r
 \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 = globalThis.crypto.getRandomValues(new Uint8Array(64))\r
+                       const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
+                       const key = crypto.getRandomValues(new Uint8Array(64))\r
 \r
-                       await assert.rejects(wallet.unlock(new Uint8Array(key)), { message: 'Failed to unlock wallet' })\r
+                       await assert.rejects(wallet.unlock(crypto.randomUUID()), { message: 'Failed to unlock wallet' })\r
                        assert.ok('mnemonic' in wallet)\r
                        assert.ok('seed' in wallet)\r
                        assert.throws(() => wallet.mnemonic)\r
@@ -193,17 +171,7 @@ await Promise.all([
                })\r
 \r
                await test('fail to unlock a Blake2bWallet with no input', 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
-\r
-                       //@ts-expect-error\r
-                       await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' })\r
-                       assert.ok('mnemonic' in wallet)\r
-                       assert.ok('seed' in wallet)\r
-                       assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1)\r
-                       assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1)\r
-\r
-                       await wallet.lock(NANO_TEST_VECTORS.PASSWORD)\r
+                       const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
 \r
                        //@ts-expect-error\r
                        await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' })\r
@@ -216,17 +184,7 @@ await Promise.all([
                })\r
 \r
                await test('fail to unlock a Blake2bWallet with invalid input', 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
-\r
-                       //@ts-expect-error\r
-                       await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' })\r
-                       assert.ok('mnemonic' in wallet)\r
-                       assert.ok('seed' in wallet)\r
-                       assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1)\r
-                       assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1)\r
-\r
-                       await wallet.lock(NANO_TEST_VECTORS.PASSWORD)\r
+                       const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
 \r
                        //@ts-expect-error\r
                        await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' })\r
index a9d8b6a3c81b24988856cced9fcabb36b6f789f8..f79308ec381ce23cb003cee37d623671196a946d 100644 (file)
@@ -11,10 +11,6 @@ import { RAW_MAX, NANO_TEST_VECTORS } from './VECTORS.mjs'
 */\r
 let Account\r
 /**\r
-* @type {typeof import('../dist/types.d.ts').Bip44Wallet}\r
-*/\r
-let Bip44Wallet\r
-/**\r
 * @type {typeof import('../dist/types.d.ts').Rpc}\r
 */\r
 let Rpc\r
@@ -26,10 +22,14 @@ let SendBlock
 * @type {typeof import('../dist/types.d.ts').Tools}\r
 */\r
 let Tools\r
+/**\r
+* @type {typeof import('../dist/types.d.ts').Wallet}\r
+*/\r
+let Wallet\r
 if (isNode) {\r
-       ({ Account, Bip44Wallet, Rpc, SendBlock, Tools } = await import('../dist/nodejs.min.js'))\r
+       ({ Account, Rpc, SendBlock, Tools, Wallet } = await import('../dist/nodejs.min.js'))\r
 } else {\r
-       ({ Account, Bip44Wallet, Rpc, SendBlock, Tools } = await import('../dist/browser.min.js'))\r
+       ({ Account, Rpc, SendBlock, Tools, Wallet } = await import('../dist/browser.min.js'))\r
 }\r
 \r
 const rpc = new Rpc(env?.NODE_URL ?? '', env?.API_KEY_NAME)\r
@@ -122,7 +122,7 @@ await Promise.all([
                })\r
 \r
                await test('should verify a block using the public key', async () => {\r
-                       const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
+                       const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                        const account = await wallet.account()\r
                        if (account.index == null) {\r
@@ -147,7 +147,7 @@ await Promise.all([
                })\r
 \r
                await test('should reject a block using the wrong public key', async () => {\r
-                       const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
+                       const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                        const account = await wallet.account()\r
                        if (account.index == null) {\r
@@ -181,7 +181,7 @@ await Promise.all([
                })\r
 \r
                await test('sweeper fails gracefully for ineligible accounts', { skip: true }, async () => {\r
-                       const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
+                       const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
                        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                        const results = await Tools.sweep(rpc, wallet, NANO_TEST_VECTORS.ADDRESS_1)\r
 \r