From e0a354b362e9cf9e77dd0760673b7fa605be5239 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 20 Aug 2025 13:54:31 -0700 Subject: [PATCH] Refactor Ledger tests to work with agnostic wallet. --- test/test.ledger.mjs | 104 ++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 60 deletions(-) diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index cb043ea..8a88044 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -15,18 +15,18 @@ let Account */ let Block /** -* @type {typeof import('../dist/types.d.ts').Ledger} -*/ -let Ledger -/** * @type {typeof import('../dist/types.d.ts').Rpc} */ let Rpc +/** +* @type {typeof import('../dist/types.d.ts').Wallet} +*/ +let Wallet if (isNode) { - ({ Account, Block, Ledger, Rpc } = await import('../dist/nodejs.min.js')) + ({ Account, Block, Rpc, Wallet } = await import('../dist/nodejs.min.js')) } else { - ({ Account, Block, Ledger, Rpc } = await import('../dist/browser.min.js')) + ({ Account, Block, Rpc, Wallet } = await import('../dist/browser.min.js')) } const rpc = new Rpc(env.NODE_URL ?? '', env.API_KEY_NAME) @@ -37,46 +37,39 @@ const rpc = new Rpc(env.NODE_URL ?? '', env.API_KEY_NAME) */ await Promise.all([ /* node:coverage disable */ - suite('Ledger hardware wallet', { skip: false || isNode || Ledger.isUnsupported }, async () => { + suite('Ledger hardware wallet', { skip: false || isNode }, async () => { const { OPEN_BLOCK, RECEIVE_BLOCK, SEND_BLOCK } = NANO_TEST_VECTORS - let wallet, account, openBlock, sendBlock, receiveBlock + try { + wallet = await Wallet.create('Ledger') + } catch { + return + } await test('request permissions', async () => { - wallet = await Ledger.create() - let status = wallet.status - assert.equal(status, 'DISCONNECTED') - assert.equal(status, wallet.status) - - status = await click( - 'Reset permissions, unlock device, quit Nano app, then click to continue', - async () => wallet.connect() - ) - assert.equal(status, 'BUSY') - assert.equal(status, wallet.status) - - status = await click( - 'Open Nano app on device, allow device to auto-lock, then click to continue', - async () => wallet.connect() - ) - assert.equal(status, 'LOCKED') - assert.equal(status, wallet.status) - - status = await click( - 'Unlock device, verify Nano app is open, then click to continue', - async () => wallet.connect() - ) - assert.equal(status, 'CONNECTED') - assert.equal(status, wallet.status) - }) - - await test('get version', async () => { - const { status, name, version } = await wallet.version() - - assert.equal(status, 'OK') - assert.equal(name, 'Nano') - assert.equal(version, '1.2.6') + await assert.rejects(wallet.unlock(), 'expect DISCONNECTED') + + await assert.rejects(async () => { + await click( + 'Reset permissions, unlock device, quit Nano app, then click to continue', + async () => wallet.unlock() + ) + }, 'expect BUSY') + + await assert.rejects(async () => { + await click( + 'Open Nano app on device, allow device to auto-lock, then click to continue', + async () => wallet.unlock() + ) + }, 'expect LOCKED') + + await assert.resolves(async () => { + await click( + 'Unlock device, verify Nano app is open, then click to continue', + async () => wallet.unlock() + ) + }, 'expect CONNECTED') }) await test('get first account', async () => { @@ -109,7 +102,7 @@ await Promise.all([ assert.exists(account.address) assert.exists(account.publicKey) assert.exists(account.balance) - assert.ok(account.balance >= 0) + assert.ok(account.balance ?? -1 >= 0) } }) @@ -121,21 +114,14 @@ await Promise.all([ assert.nullish(openBlock.signature) assert.equal(openBlock.account.publicKey, account.publicKey) - const signature = await wallet.sign(0, openBlock) + await wallet.sign(0, openBlock) - assert.ok(/^[A-F0-9]{128}$/i.test(signature)) + assert.ok(/^[A-F0-9]{128}$/i.test(openBlock.signature ?? '')) - await openBlock.sign(0) + await openBlock.sign(wallet, 0) assert.exists(openBlock.signature) assert.ok(/^[A-F0-9]{128}$/i.test(openBlock.signature ?? '')) - assert.equal(signature, openBlock.signature) - }) - - await test('cache open block', async () => { - const { status } = await wallet.updateCache(0, openBlock) - - assert.equal(status, 'OK') }) await test('sign send block from wallet which requires cache to be up-to-date', async () => { @@ -146,10 +132,9 @@ await Promise.all([ assert.nullish(sendBlock.signature) assert.equal(sendBlock.account.publicKey, account.publicKey) - const signature = await wallet.sign(0, sendBlock, 'hex') + await wallet.sign(0, sendBlock, openBlock) - assert.ok(/^[A-F0-9]{128}$/i.test(signature)) - assert.equal(signature, sendBlock.signature) + assert.ok(/^[A-F0-9]{128}$/i.test(sendBlock.signature ?? '')) }) await test('sign a receive block from block object which can accept previous block for cache', async () => { @@ -160,7 +145,7 @@ await Promise.all([ assert.nullish(receiveBlock.signature) assert.equal(receiveBlock.account.publicKey, account.publicKey) - await receiveBlock.sign(0, sendBlock) + await receiveBlock.sign(wallet, 0, sendBlock) assert.exists(receiveBlock.signature) assert.ok(/^[A-F0-9]{128}$/i.test(receiveBlock.signature ?? '')) @@ -174,7 +159,7 @@ await Promise.all([ assert.nullish(sendBlock.signature) assert.equal(sendBlock.account.publicKey, account.publicKey) - await assert.rejects(wallet.sign(0, sendBlock, 'hex')) + await assert.rejects(wallet.sign(0, sendBlock)) }) await test('sign a send block from wallet including frontier block for cache', async () => { @@ -185,16 +170,15 @@ await Promise.all([ assert.nullish(sendBlock.signature) assert.equal(sendBlock.account.publicKey, account.publicKey) - const signature = await wallet.sign(0, sendBlock, 'hex', receiveBlock) + await wallet.sign(0, sendBlock, receiveBlock) assert.exists(sendBlock.signature) assert.ok(/^[A-F0-9]{128}$/i.test(sendBlock.signature ?? '')) - assert.equal(signature, sendBlock.signature) }) await test('destroy wallet', async () => { await wallet.destroy() - await assert.rejects(wallet.version()) + await assert.rejects(wallet.unlock()) }) await test('fail when using new', async () => { -- 2.47.3