From: Chris Duncan Date: Tue, 4 Aug 2026 07:23:35 +0000 (-0700) Subject: Start fixing click functionality. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=b6869cc407be17312851c8c3dcb8f8db58295159;p=libnemo.git Start fixing click functionality. --- diff --git a/src/lib/rpc/index.ts b/src/lib/rpc/index.ts index 27edc3f..2038b4e 100644 --- a/src/lib/rpc/index.ts +++ b/src/lib/rpc/index.ts @@ -47,7 +47,7 @@ export class Rpc { } const body = (data as Record) ?? {} if ('action' in body && (typeof body.action !== 'string' || body.action.toLowerCase() !== action)) { - throw new RangeError(`RPC post data contains 'action' property and does not match 'action' parameter argument. Do not include 'action' in request data.`, { cause: body }) + throw new RangeError(`RPC post data contains 'action' property and does not match 'action' parameter argument. Do not include 'action' in request data.`, { cause: JSON.stringify({ data, body }) }) } body.action = action diff --git a/test/GLOBALS.mjs b/test/GLOBALS.mjs index 77a6649..5a17bab 100644 --- a/test/GLOBALS.mjs +++ b/test/GLOBALS.mjs @@ -73,29 +73,51 @@ function pass (name) { console.log(`%cPASS `, 'color:green', name) } -export async function click (text, fn) { - if (isNode) return await fn() - return new Promise((resolve, reject) => { - const button = document.createElement('button') - const hourglass = document.createTextNode('⏳') - button.innerText = text - button.onclick = async () => { - button.disabled = true - button.innerText = 'Waiting for device...' - button.after(hourglass) - try { - const result = await fn() - resolve(result) - } catch (err) { - reject(err) - } finally { - hourglass.remove() - button.remove() +/** + * + * @param {string} text + * @param {AsyncFunction} fn + * @param {object} [opt] + * @param {number} [opt.timeout] + * @returns {Promise} + */ +export async function click (text, fn, opt) { + async function wait (timeout) { + if (typeof timeout === 'number' && timeout > 0) { + if (timeout > 300) { + throw new RangeError('Click test should not wait more than 5 minutes', { cause: timeout }) } + console.log(`Click done, waiting ${timeout} seconds to reset transient user activation timer...`) + return await new Promise(r => setTimeout(r, timeout * 1000)) } - document.body.appendChild(button) - window?.scrollTo(0, document.body.scrollHeight) - }) + if (isNode) { + await wait(opt?.timeout) + return await fn() + } + const { timeout } = opt + return new Promise((resolve, reject) => { + const button = document.createElement('button') + const hourglass = document.createTextNode('⏳') + button.innerText = text + button.onclick = async () => { + button.disabled = true + button.innerText = 'Waiting for device...' + button.after(hourglass) + try { + const result = await fn() + resolve(result) + } catch (err) { + reject(err) + } finally { + hourglass.remove() + button.remove() + await wait(opt?.timeout) + } + } + document.body.appendChild(button) + window?.scrollTo(0, document.body.scrollHeight) + }) + } } export function stats (times) { diff --git a/test/test.blocks.mjs b/test/test.blocks.mjs index 0ec641a..7313996 100644 --- a/test/test.blocks.mjs +++ b/test/test.blocks.mjs @@ -16,10 +16,9 @@ await Promise.all([ await assert.resolves(click( 'Sign and autogen PoW when processing', - async () => block.sign(OPEN_BLOCK.key) + async () => block.sign(OPEN_BLOCK.key), + { timeout: 6 } )) - 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)) @@ -113,10 +112,9 @@ await Promise.all([ await assert.resolves(click( 'Sign with BLAKE2b', - async () => wallet.sign(1, block) + async () => wallet.sign(1, block), + { timeout: 6 } )) - 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()) @@ -130,10 +128,9 @@ await Promise.all([ await assert.resolves(click( 'Sign with BIP-44', - async () => wallet.sign(0, block) + async () => wallet.sign(0, block), + { timeout: 6 } )) - 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()) @@ -147,10 +144,9 @@ await Promise.all([ await assert.resolves(click( 'Sign with Exodus', - async () => wallet.sign(0, block) + async () => wallet.sign(0, block), + { timeout: 6 } )) - 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()) @@ -175,10 +171,9 @@ await Promise.all([ await assert.rejects(click( 'Fail to sign while locked', - async () => wallet.sign(0, block) + async () => wallet.sign(0, block), + { timeout: 6 } )) - 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() @@ -191,10 +186,9 @@ await Promise.all([ await assert.resolves(click( 'Sign open block with key', - async () => block.sign(OPEN_BLOCK.key) + async () => block.sign(OPEN_BLOCK.key), + { timeout: 6 } )) - 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) }) @@ -206,10 +200,9 @@ await Promise.all([ await assert.resolves(click( 'Sign receive block with key', - async () => block.sign(RECEIVE_BLOCK.key) + async () => block.sign(RECEIVE_BLOCK.key), + { timeout: 6 } )) - 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) @@ -221,10 +214,9 @@ await Promise.all([ await assert.resolves(click( 'Sign receive block without work', - async () => block.sign(RECEIVE_BLOCK.key) + async () => block.sign(RECEIVE_BLOCK.key), + { timeout: 6 } )) - 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) @@ -243,10 +235,9 @@ await Promise.all([ await assert.resolves(click( 'Sign Ledger-derived block using BIP-44 wallet', - async () => wallet.sign(0, block) + async () => wallet.sign(0, block), + { timeout: 6 } )) - 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)) @@ -261,10 +252,9 @@ await Promise.all([ await assert.resolves(click( 'Sign send block with key', - async () => block.sign(SEND_BLOCK.key) + async () => block.sign(SEND_BLOCK.key), + { timeout: 6 } )) - 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) @@ -276,10 +266,9 @@ await Promise.all([ await assert.resolves(click( 'Sign send block without work', - async () => block.sign(SEND_BLOCK.key) + async () => block.sign(SEND_BLOCK.key), + { timeout: 6 } )) - 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) @@ -295,10 +284,9 @@ await Promise.all([ await assert.resolves(click( 'Sign change block with key', - async () => block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3') // Did not find a private key at nano docs for this address + async () => block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3'), // Did not find a private key at nano docs for this address + { timeout: 6 } )) - 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) @@ -310,10 +298,9 @@ await Promise.all([ await assert.resolves(click( 'Sign change block without work', - async () => block.sign(PRIVATE_0) + async () => block.sign(PRIVATE_0), + { timeout: 6 } )) - 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) @@ -327,35 +314,31 @@ await Promise.all([ await assert.rejects(click( 'fail to sign without args', //@ts-expect-error - async () => block.sign() + async () => block.sign(), + { timeout: 6 } )) - 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 null arg', //@ts-expect-error - async () => block.sign(null) + async () => block.sign(null), + { timeout: 6 } )) - 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') + async () => block.sign('1'), + { timeout: 6 } )) - 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') + async () => block.sign('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'), + { timeout: 6 } )) - 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.import-wallet.mjs b/test/test.import-wallet.mjs index ec4ac65..9eb538d 100644 --- a/test/test.import-wallet.mjs +++ b/test/test.import-wallet.mjs @@ -167,8 +167,8 @@ await Promise.all([ assert.ok(await wallet.verify(TREZOR_TEST_VECTORS.MNEMONIC_2)) assert.ok(await wallet.verify(TREZOR_TEST_VECTORS.ENTROPY_2)) - const imported = await Wallet.load('BLAKE2b', TREZOR_TEST_VECTORS.MNEMONIC_SALT, TREZOR_TEST_VECTORS.MNEMONIC_2) - await imported.unlock(TREZOR_TEST_VECTORS.MNEMONIC_SALT) + const imported = await Wallet.load('BLAKE2b', TEST_PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_2) + await imported.unlock(TEST_PASSWORD) const importedAccount = await imported.account() assert.ok(await imported.verify(TREZOR_TEST_VECTORS.MNEMONIC_2))