From 337c4ed36b254b2161fcca3dd7f6eaa076f6823f Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 15 Sep 2025 08:24:47 -0700 Subject: [PATCH] Fix Ledger verify method. --- src/lib/wallet/ledger.ts | 9 +++++---- test/GLOBALS.mjs | 2 +- test/VECTORS.mjs | 1 + test/test.ledger.mjs | 28 ++++++++++++++++++++++++++-- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/lib/wallet/ledger.ts b/src/lib/wallet/ledger.ts index 311faaa..bed221c 100644 --- a/src/lib/wallet/ledger.ts +++ b/src/lib/wallet/ledger.ts @@ -271,12 +271,13 @@ export class Ledger { const testOpenBlock = await new Block(testAccount.address, '0', testAccount.publicKey, testAccount.address) .receive(testAccount.publicKey, 0) .sign(testWallet, 0) - const testSendBlock = new Block(testAccount.address, '0', testOpenBlock.hash, testAccount.address) + const testSendBlock = await new Block(testAccount.address, '0', testOpenBlock.hash, testAccount.address) .send(testAccount.address, 0) - await testWallet.sign(0, testOpenBlock) + .sign(testWallet, 0) + const testSignature = testSendBlock.signature try { - await Ledger.sign(0, testSendBlock, testOpenBlock) - return testSendBlock.signature === testOpenBlock.signature + const ledgerSignature = await Ledger.sign(0, testSendBlock, testOpenBlock) + return ledgerSignature === testSignature } catch (err) { throw new Error('Failed to verify wallet', { cause: err }) } diff --git a/test/GLOBALS.mjs b/test/GLOBALS.mjs index 8388c0c..f8a284b 100644 --- a/test/GLOBALS.mjs +++ b/test/GLOBALS.mjs @@ -293,7 +293,7 @@ export class assert { /** * @param {unknown} fn - * @param {any} msg + * @param {any} [msg] * @returns {asserts fn is Function} */ static throws (fn, msg) { diff --git a/test/VECTORS.mjs b/test/VECTORS.mjs index e788a63..a98a137 100644 --- a/test/VECTORS.mjs +++ b/test/VECTORS.mjs @@ -222,6 +222,7 @@ export const CUSTOM_TEST_VECTORS = Object.freeze({ // derived using BIP-44 from actual Ledger Nano S hardware wallet designated for dedicated testing LEDGER_MNEMONIC: "slight govern pepper assist escape target bread repair long faith maid bread donor situate swamp toss garage master stage gift vendor jacket client upset", + LEDGER_SEED: "1913E77BCFD9AA95BCB5171B1E781EC0EA3531F1FC427686D8BBA3D655BEFA43FDED6985B5E5806199A33539332DF8780426EDAD17E7BC2D95324D678BEEEE34", LEDGER_PRIVATE_0: "174A5E324A107D66E1AB17D9BCFC676D89F2940A4B90CB64A6656005756EBDA1", LEDGER_PUBLIC_0: "4423C456B9AF40A82807114BA3F77822E5CE1E5B4C2885296C6BBE7B9B377D48", LEDGER_ADDRESS_0: "nano_1j35rjddmdt1o1n1g6cdnhuqiaq7srh7pm3ainnprtxyhgfmgzcat494fyrr", diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index ab87f59..a2177a1 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -4,7 +4,7 @@ 'use strict' import { assert, click, env, isNode, suite, test } from './GLOBALS.mjs' -import { NANO_TEST_VECTORS } from './VECTORS.mjs' +import { CUSTOM_TEST_VECTORS, NANO_TEST_VECTORS } from './VECTORS.mjs' /** * @type {typeof import('../dist/types.d.ts').Account} @@ -34,11 +34,17 @@ const rpc = new Rpc(env.NODE_URL ?? '', env.API_KEY_NAME) /** * HID interactions require user gestures, so to reduce clicks, the variables * shared among tests like wallet and account are declared at the top-level. +* +* Some of these tests depend on a specific Ledger wallet owned by the developer +* and dedicated to testing. Project clones or forks will need to modify the test +* vectors to use the mnemonics, seeds, private keys, public keys, and addresses +* from their own Ledger hardware wallets. */ await Promise.all([ /* node:coverage disable */ suite('Ledger hardware wallet', { skip: false || isNode || navigator?.usb == null }, async () => { + const { LEDGER_MNEMONIC, LEDGER_SEED, LEDGER_PUBLIC_0, LEDGER_ADDRESS_0 } = CUSTOM_TEST_VECTORS const { OPEN_BLOCK, RECEIVE_BLOCK, SEND_BLOCK } = NANO_TEST_VECTORS let wallet, account, openBlock, sendBlock, receiveBlock try { @@ -76,13 +82,31 @@ await Promise.all([ assert.equal(wallet.isLocked, false) }) + await test('verify mnemonic', async () => { + const isVerified = await wallet.verify(LEDGER_MNEMONIC) + + assert.exists(isVerified) + assert.equal(typeof isVerified, 'boolean') + assert.equal(isVerified, true) + }) + + await test('verify seed', async () => { + const isVerified = await wallet.verify(LEDGER_SEED) + + assert.exists(isVerified) + assert.equal(typeof isVerified, 'boolean') + assert.equal(isVerified, true) + }) + await test('get first account', async () => { account = await wallet.account() assert.exists(account) assert.ok(account instanceof Account) - assert.exists(account.address) assert.exists(account.publicKey) + assert.equal(account.publicKey, LEDGER_PUBLIC_0) + assert.exists(account.address) + assert.equal(account.address, LEDGER_ADDRESS_0) }) await test('get second and third accounts', async () => { -- 2.47.3