From b4d4049859505a4a19d091c6dabd30cd6406c1d1 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 3 May 2026 06:28:29 -0700 Subject: [PATCH] Consolidate user activation timeouts into click generator. --- test/GLOBALS.mjs | 2 + test/test.blocks.mjs | 34 ---------- test/test.ledger.mjs | 122 ++++++++++++++++------------------- test/test.manage-rolodex.mjs | 4 -- test/test.tools.mjs | 2 - test/test.wallet-sign.mjs | 6 -- 6 files changed, 56 insertions(+), 114 deletions(-) diff --git a/test/GLOBALS.mjs b/test/GLOBALS.mjs index 7f21fb9..cd638ff 100644 --- a/test/GLOBALS.mjs +++ b/test/GLOBALS.mjs @@ -85,6 +85,8 @@ export async function click (text, fn) { button.after(hourglass) try { const result = await fn() + console.log('Click done, waiting 6 seconds to reset transient user activation timer...') + await new Promise(r => setTimeout(r, 6000)) resolve(result) } catch (err) { reject(err) diff --git a/test/test.blocks.mjs b/test/test.blocks.mjs index 7f1e64f..12caf6b 100644 --- a/test/test.blocks.mjs +++ b/test/test.blocks.mjs @@ -20,8 +20,6 @@ await Promise.all([ 'Sign and autogen PoW when processing', async () => block.sign(OPEN_BLOCK.key) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.nullish(block.work) const result = await assert.rejects(block.process(rpc)) @@ -117,8 +115,6 @@ await Promise.all([ 'Sign with BLAKE2b', async () => wallet.sign(1, block) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.ok(await block.verify(BLAKE2B_PUBLIC_1)) await assert.resolves(wallet.destroy()) @@ -134,8 +130,6 @@ await Promise.all([ 'Sign with BIP-44', async () => wallet.sign(0, block) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.ok(await block.verify(PUBLIC_0)) await assert.resolves(wallet.destroy()) @@ -151,8 +145,6 @@ await Promise.all([ 'Sign with Exodus', async () => wallet.sign(0, block) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.ok(await block.verify(EXODUS.PUBLIC_0)) await assert.resolves(wallet.destroy()) @@ -179,8 +171,6 @@ await Promise.all([ 'Fail to sign while locked', async () => wallet.sign(0, block) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.ok(block.signature === undefined) await wallet.destroy() @@ -195,8 +185,6 @@ await Promise.all([ 'Sign open block with key', async () => block.sign(OPEN_BLOCK.key) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.equal(block.hash, OPEN_BLOCK.hash) assert.equal(block.signature, OPEN_BLOCK.signature) }) @@ -210,8 +198,6 @@ await Promise.all([ 'Sign receive block with key', async () => block.sign(RECEIVE_BLOCK.key) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.equal(block.hash, RECEIVE_BLOCK.hash) assert.equal(block.signature, RECEIVE_BLOCK.signature) @@ -225,8 +211,6 @@ await Promise.all([ 'Sign receive block without work', async () => block.sign(RECEIVE_BLOCK.key) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.equal(block.hash, RECEIVE_BLOCK.hash) assert.equal(block.signature, RECEIVE_BLOCK.signature) @@ -247,8 +231,6 @@ await Promise.all([ 'Sign Ledger-derived block using BIP-44 wallet', async () => wallet.sign(0, block) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.equal(block.signature, LEDGER_NANOS.OPEN_BLOCK.signature) assert.ok(await block.verify(account.publicKey)) @@ -265,8 +247,6 @@ await Promise.all([ 'Sign send block with key', async () => block.sign(SEND_BLOCK.key) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.equal(block.hash, SEND_BLOCK.hash) assert.equal(block.signature, SEND_BLOCK.signature) @@ -280,8 +260,6 @@ await Promise.all([ 'Sign send block without work', async () => block.sign(SEND_BLOCK.key) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.equal(block.hash, SEND_BLOCK.hash) assert.equal(block.signature, SEND_BLOCK.signature) @@ -299,8 +277,6 @@ await Promise.all([ 'Sign change block with key', async () => block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3') // Did not find a private key at nano docs for this address )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.equal(block.signature?.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D') assert.equal(block.work, work) @@ -314,8 +290,6 @@ await Promise.all([ 'Sign change block without work', async () => block.sign(PRIVATE_0) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.equal(block.signature?.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305') assert.nullish(block.work) @@ -331,8 +305,6 @@ await Promise.all([ //@ts-expect-error async () => block.sign() )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.nullish(block.signature) await assert.rejects(click( @@ -340,24 +312,18 @@ await Promise.all([ //@ts-expect-error async () => block.sign(null) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.nullish(block.signature) await assert.rejects(click( 'fail to sign with invalid string length', async () => block.sign('1') )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.nullish(block.signature) await assert.rejects(click( 'fail to sign with invalid string characters', async () => block.sign('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.nullish(block.signature) }) }) diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index e90957c..0a1ce88 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -36,7 +36,7 @@ await Promise.all([ /** @type {Block} */ openBlock, /** @type {Block} */ sendBlock, /** @type {Block} */ receiveBlock, - restored + /** @type {Wallet} */ restored try { wallet = await Wallet.create('Ledger') } catch { @@ -53,8 +53,9 @@ await Promise.all([ await click( 'Reset permissions, then click to continue', - async () => new Promise(r => setTimeout(r, 5000)) + async () => { } ) + // should initially be locked and disconnected after 6s timeout in click() await assert.rejects(wallet.unlock()) assert.equal(wallet.isLocked, true) assert.equal(Ledger.status, 'DISCONNECTED') @@ -62,72 +63,48 @@ await Promise.all([ await assert.rejects(click( 'Unlock device, quit Nano app, then click to continue', - async () => wallet.unlock() + async () => { + wallet.unlock() + assert.equal(wallet.isLocked, true) + assert.equal(Ledger.status, 'BUSY') + assert.equal(status, 'BUSY') + } )) + + // should still be locked and busy after 6s timeout in click() assert.equal(wallet.isLocked, true) assert.equal(Ledger.status, 'BUSY') assert.equal(status, 'BUSY') - await new Promise(async (resolve, reject) => { - console.log('Waiting 6 seconds...') - setTimeout(async () => { - try { - // should still be locked and busy - assert.equal(wallet.isLocked, true) - assert.equal(Ledger.status, 'BUSY') - assert.equal(status, 'BUSY') - resolve(null) - } catch (err) { - reject(err) - } - }, 6000) - }) - await assert.rejects(click( 'Open Nano app on device, allow device to auto-lock, then click to continue', - async () => wallet.unlock() + async () => { + wallet.unlock() + assert.equal(wallet.isLocked, true) + assert.equal(Ledger.status, 'LOCKED') + } )) + + // should still be locked after 6s timeout in click() assert.equal(wallet.isLocked, true) assert.equal(Ledger.status, 'LOCKED') - - await new Promise(async (resolve, reject) => { - console.log('Waiting 6 seconds...') - setTimeout(async () => { - try { - // should still be locked - assert.equal(wallet.isLocked, true) - assert.equal(Ledger.status, 'LOCKED') - assert.equal(status, 'LOCKED') - resolve(null) - } catch (err) { - reject(err) - } - }, 6000) - }) + assert.equal(status, 'LOCKED') await assert.resolves(click( 'Unlock device, verify Nano app is open, then click to continue', - async () => wallet.unlock() + async () => { + wallet.unlock() + assert.equal(wallet.isLocked, false) + assert.equal(Ledger.status, 'CONNECTED') + assert.equal(status, 'CONNECTED') + } )) + + // should still be unlocked after 6s timeout in click() assert.equal(wallet.isLocked, false) assert.equal(Ledger.status, 'CONNECTED') assert.equal(status, 'CONNECTED') - await new Promise(async (resolve, reject) => { - console.log('Waiting 6 seconds...') - setTimeout(async () => { - try { - // should still be unlocked - assert.equal(wallet.isLocked, false) - assert.equal(Ledger.status, 'CONNECTED') - assert.equal(status, 'CONNECTED') - resolve(null) - } catch (err) { - reject(err) - } - }, 6000) - }) - await new Promise(async (resolve, reject) => { console.log('Waiting 90 seconds...') setTimeout(async () => { @@ -145,34 +122,41 @@ await Promise.all([ await assert.resolves(click( 'Unlock device again, then click to continue', - async () => { } + async () => { + assert.equal(wallet.isLocked, false) + assert.equal(Ledger.status, 'CONNECTED') + assert.equal(status, 'CONNECTED') + } )) - assert.equal(wallet.isLocked, false) - assert.equal(Ledger.status, 'CONNECTED') - assert.equal(status, 'CONNECTED') }) await test('switch between interfaces', { skip: false || isNode || navigator?.usb == null }, async () => { await assert.resolves(click( 'Verify current interface is HID, switch to unlocked Bluetooth device, then click to continue', - async () => wallet.config({ connection: 'ble' }) + async () => { + wallet.config({ connection: 'ble' }) + assert.equal(wallet.isLocked, true) + assert.equal(Ledger.status, 'BUSY') + } )) - assert.equal(wallet.isLocked, true) - assert.equal(Ledger.status, 'BUSY') await assert.resolves(click( 'Verify current interface is BLE, switch back to unlocked USB device, then click to continue', - async () => wallet.config({ connection: 'usb' }) + async () => { + wallet.config({ connection: 'usb' }) + assert.equal(wallet.isLocked, false) + assert.equal(Ledger.status, 'CONNECTED') + } )) - assert.equal(wallet.isLocked, false) - assert.equal(Ledger.status, 'CONNECTED') await assert.resolves(click( 'Verify current interface is USB, then click to continue', - async () => wallet.config({ connection: 'hid' }) + async () => { + wallet.config({ connection: 'hid' }) + assert.equal(wallet.isLocked, false) + assert.equal(Ledger.status, 'CONNECTED') + } )) - assert.equal(wallet.isLocked, false) - assert.equal(Ledger.status, 'CONNECTED') }) await test('verify mnemonic', async () => { @@ -343,12 +327,14 @@ await Promise.all([ await click( 'Click to finish Ledger tests by destroying wallet', - async () => new Promise(r => setTimeout(r)) + async () => { + await new Promise(r => setTimeout(r)) + await assert.resolves(wallet.destroy()) + await assert.rejects(wallet.unlock()) + await wallet.destroy() + await restored.destroy() + } ) - await assert.resolves(wallet.destroy()) - await assert.rejects(wallet.unlock()) - await wallet.destroy() - await restored.destroy() }) }) ]) diff --git a/test/test.manage-rolodex.mjs b/test/test.manage-rolodex.mjs index 5e0de97..a9c2d16 100644 --- a/test/test.manage-rolodex.mjs +++ b/test/test.manage-rolodex.mjs @@ -193,8 +193,6 @@ await Promise.all([ signature = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0 + NANO_TEST_VECTORS.PUBLIC_0, data) } )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) await assert.resolves(Rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0)) const result = await Rolodex.verify('JohnDoe', signature, data) @@ -212,8 +210,6 @@ await Promise.all([ signature = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0 + NANO_TEST_VECTORS.PUBLIC_0, data) } )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) await assert.resolves(Rolodex.add('JaneSmith', NANO_TEST_VECTORS.ADDRESS_1)) const result = await Rolodex.verify('JaneSmith', signature, data) diff --git a/test/test.tools.mjs b/test/test.tools.mjs index ed40572..8420179 100644 --- a/test/test.tools.mjs +++ b/test/test.tools.mjs @@ -110,8 +110,6 @@ await Promise.all([ result = Tools.sign(NANO_TEST_VECTORS.PRIVATE_0 + NANO_TEST_VECTORS.PUBLIC_0, m) } )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.equal(result.toLowerCase(), '0ceebbc0b9b3a270a30bad1eef4eddc0931effd8d5c0b7cab007fcc61f30d3ee9760cd93d7eeb5b654ecc0d9d17bc7a6d53bee54aa163a8272f425fb1184e30e') }) diff --git a/test/test.wallet-sign.mjs b/test/test.wallet-sign.mjs index c6f1d6a..ba038b6 100644 --- a/test/test.wallet-sign.mjs +++ b/test/test.wallet-sign.mjs @@ -24,8 +24,6 @@ await Promise.all([ signature = await wallet.sign(0, data) } )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.ok(await Tools.verify(account.publicKey, signature, data)) await assert.resolves(wallet.destroy()) }) @@ -41,8 +39,6 @@ await Promise.all([ 'Sign to test subsequent verification success', async () => sendBlock.sign(wallet, 0) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.ok(await sendBlock.verify(account.publicKey)) await assert.resolves(wallet.destroy()) @@ -59,8 +55,6 @@ await Promise.all([ 'Sign to test subsequent verification failure', async () => sendBlock.sign(wallet, 0) )) - console.log('Click done, waiting 6 seconds to reset transient user activation timer...') - await new Promise(r => setTimeout(r, 6000)) assert.ok(await sendBlock.verify(account.publicKey)) -- 2.47.3