From: Chris Duncan Date: Fri, 1 Aug 2025 21:48:59 +0000 (-0700) Subject: Update tests. Remove redundant globalThis references. X-Git-Tag: v0.10.5~47^2~26 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=a07e9bca8939f48d0eff4984001c8dabe2057808;p=libnemo.git Update tests. Remove redundant globalThis references. --- diff --git a/src/lib/bip39-mnemonic.ts b/src/lib/bip39-mnemonic.ts index e00d7c4..ea9ffff 100644 --- a/src/lib/bip39-mnemonic.ts +++ b/src/lib/bip39-mnemonic.ts @@ -182,7 +182,7 @@ export class Bip39Mnemonic { ? '' : passphrase const keyData = utf8.toBytes(this.phrase) - const phraseKey = await globalThis.crypto.subtle.importKey('raw', keyData, 'PBKDF2', false, ['deriveBits', 'deriveKey']) + const phraseKey = await crypto.subtle.importKey('raw', keyData, 'PBKDF2', false, ['deriveBits', 'deriveKey']) const derivedKeyType: HmacImportParams = { name: 'HMAC', hash: 'SHA-512', @@ -196,8 +196,8 @@ export class Bip39Mnemonic { salt: utf8.toBytes(salt), iterations: BIP39_ITERATIONS } - const seedKey = await globalThis.crypto.subtle.deriveKey(algorithm, phraseKey, derivedKeyType, true, ['sign']) - const seedBuffer = await globalThis.crypto.subtle.exportKey('raw', seedKey) + const seedKey = await crypto.subtle.deriveKey(algorithm, phraseKey, derivedKeyType, true, ['sign']) + const seedBuffer = await crypto.subtle.exportKey('raw', seedKey) this.#bip39Seed = new Uint8Array(seedBuffer) } return format === 'hex' diff --git a/src/lib/bip44-ckd.ts b/src/lib/bip44-ckd.ts index 489854e..73915ab 100644 --- a/src/lib/bip44-ckd.ts +++ b/src/lib/bip44-ckd.ts @@ -92,7 +92,7 @@ export class Bip44Ckd { } static async hmac (key: Uint8Array, data: Uint8Array): Promise> { - 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) diff --git a/src/lib/entropy.ts b/src/lib/entropy.ts index afdc128..3e1aaa0 100644 --- a/src/lib/entropy.ts +++ b/src/lib/entropy.ts @@ -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 diff --git a/src/lib/ledger.ts b/src/lib/ledger.ts index 002eacd..af8b911 100644 --- a/src/lib/ledger.ts +++ b/src/lib/ledger.ts @@ -83,7 +83,7 @@ export class Ledger extends Wallet { private constructor () { if (!Ledger.#isInternal) { - throw new Error(`LedgerWallet cannot be instantiated directly. Use 'await LedgerWallet.create()' instead.`) + throw new Error(`Ledger cannot be instantiated directly. Use 'await Ledger.create()' instead.`) } Ledger.#isInternal = false super('Ledger') diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 4b3b1c7..b3de04e 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -178,7 +178,7 @@ export class Wallet { } constructor (type: WalletType) { - if (!Wallet.#isInternal) { + if (!Wallet.#isInternal && type !== 'Ledger') { throw new Error(`Wallet cannot be instantiated directly. Use 'await Wallet.create()' instead.`) } Wallet.#isInternal = false diff --git a/test/test.blocks.mjs b/test/test.blocks.mjs index d6cf3bd..1e53637 100644 --- a/test/test.blocks.mjs +++ b/test/test.blocks.mjs @@ -112,9 +112,9 @@ await Promise.all([ NANO_TEST_VECTORS.OPEN_BLOCK.previous, NANO_TEST_VECTORS.OPEN_BLOCK.work ) - await wallet.sign(0, block) + await assert.rejects(wallet.sign(0, block)) assert.equal(block.hash, NANO_TEST_VECTORS.OPEN_BLOCK.hash) - assert.equal(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature) + assert.notEqual(block.signature, NANO_TEST_VECTORS.OPEN_BLOCK.signature) }) await test('sign open block with private key', async () => { diff --git a/test/test.lock-unlock.mjs b/test/test.lock-unlock.mjs index bcd3ee8..bde09da 100644 --- a/test/test.lock-unlock.mjs +++ b/test/test.lock-unlock.mjs @@ -11,29 +11,26 @@ import { NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './VECTORS.mjs' */ let Account /** -* @type {typeof import('../dist/types.d.ts').Bip44Wallet} +* @type {typeof import('../dist/types.d.ts').Wallet} */ -let Bip44Wallet -/** -* @type {typeof import('../dist/types.d.ts').Blake2bWallet} -*/ -let Blake2bWallet +let Wallet if (isNode) { - ({ Account, Bip44Wallet, Blake2bWallet } = await import('../dist/nodejs.min.js')) + ({ Account, Wallet } = await import('../dist/nodejs.min.js')) } else { - ({ Account, Bip44Wallet, Blake2bWallet } = await import('../dist/browser.min.js')) + ({ Account, Wallet } = await import('../dist/browser.min.js')) } await Promise.all([ suite('Lock and unlock wallets', async () => { await test('locking and unlocking a Bip44Wallet with a password', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + debugger + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) - assert.throws(() => wallet.mnemonic) - assert.throws(() => wallet.seed) + assert.ok(wallet.mnemonic === undefined) + assert.ok(wallet.seed === undefined) const unlockResult = await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) @@ -47,10 +44,10 @@ await Promise.all([ }) await test('locking and unlocking a Bip44Wallet Account with a password', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() - const lockResult = await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + const lockResult = await wallet.lock() assert.equal(lockResult, true) @@ -61,9 +58,9 @@ await Promise.all([ }) await test('fail to unlock a Bip44Wallet with different passwords', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - const lockResult = await wallet.lock(TREZOR_TEST_VECTORS.PASSWORD) + const lockResult = await wallet.lock() await assert.rejects(wallet.unlock(NANO_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' }) assert.equal(lockResult, true) @@ -76,10 +73,10 @@ await Promise.all([ }) 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 = globalThis.crypto.getRandomValues(new Uint8Array(64)) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const key = crypto.getRandomValues(new Uint8Array(64)) - await assert.rejects(wallet.unlock(new Uint8Array(key)), { message: 'Failed to unlock wallet' }) + await assert.rejects(wallet.unlock(crypto.randomUUID()), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.throws(() => wallet.mnemonic) @@ -89,18 +86,9 @@ await Promise.all([ }) await test('fail to unlock a Bip44Wallet with no input', async () => { - const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - //@ts-expect-error - await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' }) - 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) - - await wallet.lock('password') - //@ts-expect-error await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) @@ -112,17 +100,7 @@ await Promise.all([ }) await test('fail to unlock a Bip44Wallet with invalid input', 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) - - //@ts-expect-error - await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' }) - 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) - - await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) //@ts-expect-error await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' }) @@ -135,7 +113,7 @@ await Promise.all([ }) await test('locking and unlocking a Blake2bWallet with a password', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -154,10 +132,10 @@ await Promise.all([ }) await test('locking and unlocking a Blake2bWallet Account with a password', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() - const lockResult = await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + const lockResult = await wallet.lock() assert.equal(lockResult, true) @@ -168,7 +146,7 @@ await Promise.all([ }) await test('fail to unlock a Blake2bWallet with different passwords', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) await assert.rejects(wallet.unlock(TREZOR_TEST_VECTORS.PASSWORD), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) @@ -180,10 +158,10 @@ await Promise.all([ }) 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 = globalThis.crypto.getRandomValues(new Uint8Array(64)) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) + const key = crypto.getRandomValues(new Uint8Array(64)) - await assert.rejects(wallet.unlock(new Uint8Array(key)), { message: 'Failed to unlock wallet' }) + await assert.rejects(wallet.unlock(crypto.randomUUID()), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) assert.throws(() => wallet.mnemonic) @@ -193,17 +171,7 @@ await Promise.all([ }) await test('fail to unlock a Blake2bWallet with no input', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - //@ts-expect-error - await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) - - await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) //@ts-expect-error await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) @@ -216,17 +184,7 @@ await Promise.all([ }) await test('fail to unlock a Blake2bWallet with invalid input', async () => { - const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) - await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - - //@ts-expect-error - await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' }) - assert.ok('mnemonic' in wallet) - assert.ok('seed' in wallet) - assert.equal(wallet.mnemonic, TREZOR_TEST_VECTORS.MNEMONIC_1) - assert.equal(wallet.seed, TREZOR_TEST_VECTORS.ENTROPY_1) - - await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + const wallet = await Wallet.import('BLAKE2b', NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1) //@ts-expect-error await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' }) diff --git a/test/test.tools.mjs b/test/test.tools.mjs index a9d8b6a..f79308e 100644 --- a/test/test.tools.mjs +++ b/test/test.tools.mjs @@ -11,10 +11,6 @@ import { RAW_MAX, NANO_TEST_VECTORS } from './VECTORS.mjs' */ let Account /** -* @type {typeof import('../dist/types.d.ts').Bip44Wallet} -*/ -let Bip44Wallet -/** * @type {typeof import('../dist/types.d.ts').Rpc} */ let Rpc @@ -26,10 +22,14 @@ let SendBlock * @type {typeof import('../dist/types.d.ts').Tools} */ let Tools +/** +* @type {typeof import('../dist/types.d.ts').Wallet} +*/ +let Wallet if (isNode) { - ({ Account, Bip44Wallet, Rpc, SendBlock, Tools } = await import('../dist/nodejs.min.js')) + ({ Account, Rpc, SendBlock, Tools, Wallet } = await import('../dist/nodejs.min.js')) } else { - ({ Account, Bip44Wallet, Rpc, SendBlock, Tools } = await import('../dist/browser.min.js')) + ({ Account, Rpc, SendBlock, Tools, Wallet } = await import('../dist/browser.min.js')) } const rpc = new Rpc(env?.NODE_URL ?? '', env?.API_KEY_NAME) @@ -122,7 +122,7 @@ await Promise.all([ }) await test('should verify a block using the public key', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() if (account.index == null) { @@ -147,7 +147,7 @@ await Promise.all([ }) await test('should reject a block using the wrong public key', async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.account() if (account.index == null) { @@ -181,7 +181,7 @@ await Promise.all([ }) await test('sweeper fails gracefully for ineligible accounts', { skip: true }, async () => { - const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + const wallet = await Wallet.import('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const results = await Tools.sweep(rpc, wallet, NANO_TEST_VECTORS.ADDRESS_1)