]> git.codecow.com Git - libnemo.git/commitdiff
Skip Ledger connection in Node environments. Expand Ledger account derivation tests...
authorChris Duncan <chris@zoso.dev>
Mon, 7 Jul 2025 16:14:05 +0000 (09:14 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 7 Jul 2025 16:14:05 +0000 (09:14 -0700)
sample.env [new file with mode: 0644]
sample.env.mjs [new file with mode: 0644]
test/GLOBALS.mjs
test/test.create-wallet.mjs
test/test.derive-accounts.mjs
test/test.refresh-accounts.mjs

diff --git a/sample.env b/sample.env
new file mode 100644 (file)
index 0000000..86e8955
--- /dev/null
@@ -0,0 +1,7 @@
+# SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+# Save this file as `.env` and replace the following with real values
+NODE_URL="https://rpc.example.com"
+API_KEY_NAME="api_key"
+LIBNEMO_RPC_API_KEY="fedcba9876543210fedcba9876543210"
diff --git a/sample.env.mjs b/sample.env.mjs
new file mode 100644 (file)
index 0000000..73f8611
--- /dev/null
@@ -0,0 +1,19 @@
+// SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+'use strict'
+
+// Save this file as `env.mjs` and replace the following with real values
+const env = {
+       NODE_URL: "https://rpc.example.com",
+       API_KEY_NAME: "api_key",
+       LIBNEMO_RPC_API_KEY: "fedcba9876543210fedcba9876543210"
+}
+
+var process = process || { env }
+process.env ??= env
+process.NODE_URL ??= env.NODE_URL
+process.API_KEY_NAME ??= env.API_KEY_NAME
+process.LIBNEMO_RPC_API_KEY ??= env.LIBNEMO_RPC_API_KEY
+
+export { process }
index 7356b2886bf4b032e89044b4e491506c2d998436..f0b6b56ba9da6a43db734e045b2f8958660004c8 100644 (file)
@@ -2,6 +2,7 @@
 // SPDX-License-Identifier: GPL-3.0-or-later
 
 import { Queue } from './QUEUE.mjs'
+import { process } from '../env.mjs'
 
 const queue = new Queue()
 
@@ -19,6 +20,9 @@ if (globalThis.sessionStorage == null) {
                enumerable: true
        })
 }
+export { process }
+
+export const isNode = process.versions?.node != null
 
 export function stats (times) {
        if (times == null || times.length === 0) return null
index 16822afef23219fda21be88f530d6367d3df9ce4..d06e34e1fe9ad97c2d145300f90aa596dad1d90a 100644 (file)
@@ -3,7 +3,7 @@
 \r
 'use strict'\r
 \r
-import { assert, suite, test } from './GLOBALS.mjs'\r
+import { assert, isNode, suite, test } from './GLOBALS.mjs'\r
 import { NANO_TEST_VECTORS } from './VECTORS.js'\r
 import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.min.js'\r
 \r
@@ -57,7 +57,7 @@ await suite('Create wallets', async () => {
                await assert.rejects(Blake2bWallet.create())\r
        })\r
 \r
-       await test('connect to a Ledger device', { skip: false }, async () => {\r
+       await test('connect to a Ledger device', { skip: false || isNode }, async () => {\r
                const wallet = await LedgerWallet.create()\r
                let status = await new Promise(resolve => {\r
                        const button = document.createElement('button')\r
index d6f48c9c096adcbe3ee70c76655a19701c76f1fc..762697619cabbb48c9428aa00060b4afb86df253 100644 (file)
@@ -3,7 +3,7 @@
 \r
 'use strict'\r
 \r
-import { assert, suite, test } from './GLOBALS.mjs'\r
+import { assert, isNode, suite, test } from './GLOBALS.mjs'\r
 import { NANO_TEST_VECTORS } from './VECTORS.js'\r
 import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.min.js'\r
 \r
@@ -86,15 +86,27 @@ await suite('Account derivation', async () => {
 \r
                await wallet.destroy()\r
        })\r
+})\r
 \r
-       await test('fetch the first account from a Ledger device', { skip: true }, async () => {\r
-               const wallet = await LedgerWallet.create()\r
-               const accounts = await wallet.accounts()\r
+/**\r
+* This suite requires a connected unlocked Ledger device to execute tests.\r
+*/\r
+await suite('should derive accounts for a Ledger device wallet', { skip: false || isNode }, async () => {\r
+       const wallet = await LedgerWallet.create()\r
+       await wallet.connect()\r
 \r
-               assert.equals(accounts.length, 1)\r
-               assert.exists(accounts[0].publicKey)\r
-               assert.exists(accounts[0].address)\r
+       await test('fetch the first account from a Ledger device', async () => {\r
+               const account = await wallet.account()\r
 \r
-               await wallet.destroy()\r
+               assert.exists(account.publicKey)\r
+               assert.exists(account.address)\r
+               assert.exists(account.privateKey)\r
+               assert.equals(account.privateKey, '0000000000000000000000000000000000000000000000000000000000000000')\r
        })\r
+\r
+       await test('fail to return multiple accounts from a Ledger device', async () => {\r
+               assert.rejects(wallet.accounts())\r
+       })\r
+\r
+       await wallet.destroy()\r
 })\r
index d325f090c04a6a47a9c42e4a000df3d2c104dfe5..54063f4307467f89cd57e57b7885d8bc3ec128e3 100644 (file)
@@ -3,14 +3,11 @@
 
 'use strict'
 
-import { env } from '../env.mjs'
-import { assert, suite, test } from './GLOBALS.mjs'
+import { assert, process, suite, test } from './GLOBALS.mjs'
 import { NANO_TEST_VECTORS } from './VECTORS.js'
 import { Account, Bip44Wallet, Rpc } from '../dist/main.min.js'
 
-let rpc
-var process = process || env || null
-rpc = new Rpc(process?.env?.NODE_URL ?? '', process?.env?.API_KEY_NAME)
+const rpc = new Rpc(process.env.NODE_URL ?? '', process.env.API_KEY_NAME)
 
 await suite('refreshing account info', { skip: false }, async () => {