]> git.codecow.com Git - libnemo.git/commitdiff
Start fixing click functionality.
authorChris Duncan <chris@codecow.com>
Tue, 4 Aug 2026 07:23:35 +0000 (00:23 -0700)
committerChris Duncan <chris@codecow.com>
Tue, 4 Aug 2026 07:23:35 +0000 (00:23 -0700)
src/lib/rpc/index.ts
test/GLOBALS.mjs
test/test.blocks.mjs
test/test.import-wallet.mjs

index 27edc3f6fd26e03e2a8e2b7f71cbecd6a7450af7..2038b4e83f57dd5dc3aa7b689f142efce56e0fef 100644 (file)
@@ -47,7 +47,7 @@ export class Rpc {
                }
                const body = (data as Record<string, unknown>) ?? {}
                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
 
index 77a66498333ad1c3dbcbf69d3b59ff5b48db79a1..5a17bab1557929aef6813fbbca9cbb9b5d1f0912 100644 (file)
@@ -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<any>}
+ */
+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) {
index 0ec641a4980ccd85b7c7168a8f094fe350106cf8..731399657d042afe329a4fd74ec174fb606330cf 100644 (file)
@@ -16,10 +16,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign and autogen PoW when processing',\r
-                               async () => block.sign(OPEN_BLOCK.key)\r
+                               async () => block.sign(OPEN_BLOCK.key),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
 \r
                        assert.nullish(block.work)\r
                        const result = await assert.rejects(block.process(rpc))\r
@@ -113,10 +112,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign with BLAKE2b',\r
-                               async () => wallet.sign(1, block)\r
+                               async () => wallet.sign(1, block),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
                        assert.ok(await block.verify(BLAKE2B_PUBLIC_1))\r
 \r
                        await assert.resolves(wallet.destroy())\r
@@ -130,10 +128,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign with BIP-44',\r
-                               async () => wallet.sign(0, block)\r
+                               async () => wallet.sign(0, block),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
                        assert.ok(await block.verify(PUBLIC_0))\r
 \r
                        await assert.resolves(wallet.destroy())\r
@@ -147,10 +144,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign with Exodus',\r
-                               async () => wallet.sign(0, block)\r
+                               async () => wallet.sign(0, block),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
                        assert.ok(await block.verify(EXODUS.PUBLIC_0))\r
 \r
                        await assert.resolves(wallet.destroy())\r
@@ -175,10 +171,9 @@ await Promise.all([
 \r
                        await assert.rejects(click(\r
                                'Fail to sign while locked',\r
-                               async () => wallet.sign(0, block)\r
+                               async () => wallet.sign(0, block),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
                        assert.ok(block.signature === undefined)\r
 \r
                        await wallet.destroy()\r
@@ -191,10 +186,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign open block with key',\r
-                               async () => block.sign(OPEN_BLOCK.key)\r
+                               async () => block.sign(OPEN_BLOCK.key),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
                        assert.equal(block.hash, OPEN_BLOCK.hash)\r
                        assert.equal(block.signature, OPEN_BLOCK.signature)\r
                })\r
@@ -206,10 +200,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign receive block with key',\r
-                               async () => block.sign(RECEIVE_BLOCK.key)\r
+                               async () => block.sign(RECEIVE_BLOCK.key),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
 \r
                        assert.equal(block.hash, RECEIVE_BLOCK.hash)\r
                        assert.equal(block.signature, RECEIVE_BLOCK.signature)\r
@@ -221,10 +214,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign receive block without work',\r
-                               async () => block.sign(RECEIVE_BLOCK.key)\r
+                               async () => block.sign(RECEIVE_BLOCK.key),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
 \r
                        assert.equal(block.hash, RECEIVE_BLOCK.hash)\r
                        assert.equal(block.signature, RECEIVE_BLOCK.signature)\r
@@ -243,10 +235,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign Ledger-derived block using BIP-44 wallet',\r
-                               async () => wallet.sign(0, block)\r
+                               async () => wallet.sign(0, block),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
 \r
                        assert.equal(block.signature, LEDGER_NANOS.OPEN_BLOCK.signature)\r
                        assert.ok(await block.verify(account.publicKey))\r
@@ -261,10 +252,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign send block with key',\r
-                               async () => block.sign(SEND_BLOCK.key)\r
+                               async () => block.sign(SEND_BLOCK.key),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
 \r
                        assert.equal(block.hash, SEND_BLOCK.hash)\r
                        assert.equal(block.signature, SEND_BLOCK.signature)\r
@@ -276,10 +266,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign send block without work',\r
-                               async () => block.sign(SEND_BLOCK.key)\r
+                               async () => block.sign(SEND_BLOCK.key),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
 \r
                        assert.equal(block.hash, SEND_BLOCK.hash)\r
                        assert.equal(block.signature, SEND_BLOCK.signature)\r
@@ -295,10 +284,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign change block with key',\r
-                               async () => block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3') // Did not find a private key at nano docs for this address\r
+                               async () => block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3'), // Did not find a private key at nano docs for this address\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
 \r
                        assert.equal(block.signature?.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D')\r
                        assert.equal(block.work, work)\r
@@ -310,10 +298,9 @@ await Promise.all([
 \r
                        await assert.resolves(click(\r
                                'Sign change block without work',\r
-                               async () => block.sign(PRIVATE_0)\r
+                               async () => block.sign(PRIVATE_0),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
 \r
                        assert.equal(block.signature?.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305')\r
                        assert.nullish(block.work)\r
@@ -327,35 +314,31 @@ await Promise.all([
                        await assert.rejects(click(\r
                                'fail to sign without args',\r
                                //@ts-expect-error\r
-                               async () => block.sign()\r
+                               async () => block.sign(),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
                        assert.nullish(block.signature)\r
 \r
                        await assert.rejects(click(\r
                                'fail to sign with null arg',\r
                                //@ts-expect-error\r
-                               async () => block.sign(null)\r
+                               async () => block.sign(null),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
                        assert.nullish(block.signature)\r
 \r
                        await assert.rejects(click(\r
                                'fail to sign with invalid string length',\r
-                               async () => block.sign('1')\r
+                               async () => block.sign('1'),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
                        assert.nullish(block.signature)\r
 \r
                        await assert.rejects(click(\r
                                'fail to sign with invalid string characters',\r
-                               async () => block.sign('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')\r
+                               async () => block.sign('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'),\r
+                               { timeout: 6 }\r
                        ))\r
-                       console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
-                       await new Promise(r => setTimeout(r, 6000))\r
                        assert.nullish(block.signature)\r
                })\r
        })\r
index ec4ac65e91b12e5cf1745ef66058e51d8ed63262..9eb538ddccd591ea8270a66c4c5c27d4464d4d8b 100644 (file)
@@ -167,8 +167,8 @@ await Promise.all([
                        assert.ok(await wallet.verify(TREZOR_TEST_VECTORS.MNEMONIC_2))\r
                        assert.ok(await wallet.verify(TREZOR_TEST_VECTORS.ENTROPY_2))\r
 \r
-                       const imported = await Wallet.load('BLAKE2b', TREZOR_TEST_VECTORS.MNEMONIC_SALT, TREZOR_TEST_VECTORS.MNEMONIC_2)\r
-                       await imported.unlock(TREZOR_TEST_VECTORS.MNEMONIC_SALT)\r
+                       const imported = await Wallet.load('BLAKE2b', TEST_PASSWORD, TREZOR_TEST_VECTORS.MNEMONIC_2)\r
+                       await imported.unlock(TEST_PASSWORD)\r
                        const importedAccount = await imported.account()\r
 \r
                        assert.ok(await imported.verify(TREZOR_TEST_VECTORS.MNEMONIC_2))\r