From: Chris Duncan Date: Mon, 7 Jul 2025 16:14:05 +0000 (-0700) Subject: Skip Ledger connection in Node environments. Expand Ledger account derivation tests... X-Git-Tag: v0.10.5~92 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=fc07d2208b8369a74832659612a7ed4375fd9fcc;p=libnemo.git Skip Ledger connection in Node environments. Expand Ledger account derivation tests. Refactor process polyfills for browser. Add sample environment files. --- diff --git a/sample.env b/sample.env new file mode 100644 index 0000000..86e8955 --- /dev/null +++ b/sample.env @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: 2025 Chris Duncan +# 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 index 0000000..73f8611 --- /dev/null +++ b/sample.env.mjs @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: 2025 Chris Duncan +// 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 } diff --git a/test/GLOBALS.mjs b/test/GLOBALS.mjs index 7356b28..f0b6b56 100644 --- a/test/GLOBALS.mjs +++ b/test/GLOBALS.mjs @@ -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 diff --git a/test/test.create-wallet.mjs b/test/test.create-wallet.mjs index 16822af..d06e34e 100644 --- a/test/test.create-wallet.mjs +++ b/test/test.create-wallet.mjs @@ -3,7 +3,7 @@ 'use strict' -import { assert, suite, test } from './GLOBALS.mjs' +import { assert, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS } from './VECTORS.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.min.js' @@ -57,7 +57,7 @@ await suite('Create wallets', async () => { await assert.rejects(Blake2bWallet.create()) }) - await test('connect to a Ledger device', { skip: false }, async () => { + await test('connect to a Ledger device', { skip: false || isNode }, async () => { const wallet = await LedgerWallet.create() let status = await new Promise(resolve => { const button = document.createElement('button') diff --git a/test/test.derive-accounts.mjs b/test/test.derive-accounts.mjs index d6f48c9..7626976 100644 --- a/test/test.derive-accounts.mjs +++ b/test/test.derive-accounts.mjs @@ -3,7 +3,7 @@ 'use strict' -import { assert, suite, test } from './GLOBALS.mjs' +import { assert, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS } from './VECTORS.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.min.js' @@ -86,15 +86,27 @@ await suite('Account derivation', async () => { await wallet.destroy() }) +}) - await test('fetch the first account from a Ledger device', { skip: true }, async () => { - const wallet = await LedgerWallet.create() - const accounts = await wallet.accounts() +/** +* This suite requires a connected unlocked Ledger device to execute tests. +*/ +await suite('should derive accounts for a Ledger device wallet', { skip: false || isNode }, async () => { + const wallet = await LedgerWallet.create() + await wallet.connect() - assert.equals(accounts.length, 1) - assert.exists(accounts[0].publicKey) - assert.exists(accounts[0].address) + await test('fetch the first account from a Ledger device', async () => { + const account = await wallet.account() - await wallet.destroy() + assert.exists(account.publicKey) + assert.exists(account.address) + assert.exists(account.privateKey) + assert.equals(account.privateKey, '0000000000000000000000000000000000000000000000000000000000000000') }) + + await test('fail to return multiple accounts from a Ledger device', async () => { + assert.rejects(wallet.accounts()) + }) + + await wallet.destroy() }) diff --git a/test/test.refresh-accounts.mjs b/test/test.refresh-accounts.mjs index d325f09..54063f4 100644 --- a/test/test.refresh-accounts.mjs +++ b/test/test.refresh-accounts.mjs @@ -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 () => {