From: Chris Duncan Date: Wed, 24 Sep 2025 15:09:01 +0000 (-0700) Subject: Fix restore of Ledger wallets. X-Git-Tag: v0.10.5~10 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=a2d78a06a2fb5c43253c4131eeef55eea2f97475;p=libnemo.git Fix restore of Ledger wallets. --- diff --git a/src/lib/wallet/get.ts b/src/lib/wallet/get.ts index 3e47f24..2d8d352 100644 --- a/src/lib/wallet/get.ts +++ b/src/lib/wallet/get.ts @@ -12,17 +12,19 @@ export async function _get (recordId: string) { if (typeof id !== 'string') { throw new TypeError('Retrieved invalid ID', { cause: id }) } - if (type !== 'BIP-44' && type !== 'BLAKE2b') { + if (type !== 'BIP-44' && type !== 'BLAKE2b' && type !== 'Ledger') { throw new TypeError('Retrieved invalid type', { cause: type }) } - if (!(iv instanceof ArrayBuffer)) { - throw new TypeError('Retrieved invalid iv', { cause: iv }) - } - if (!(salt instanceof ArrayBuffer)) { - throw new TypeError('Retrieved invalid salt', { cause: salt }) - } - if (!(encrypted instanceof ArrayBuffer)) { - throw new TypeError('Retrieved invalid encrypted data', { cause: encrypted }) + if (type !== 'Ledger') { + if (!(iv instanceof ArrayBuffer)) { + throw new TypeError('Retrieved invalid iv', { cause: iv }) + } + if (!(salt instanceof ArrayBuffer)) { + throw new TypeError('Retrieved invalid salt', { cause: salt }) + } + if (!(encrypted instanceof ArrayBuffer)) { + throw new TypeError('Retrieved invalid encrypted data', { cause: encrypted }) + } } return { id, type, iv, salt, encrypted } as const } catch (err) { diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index d116e43..574f9c5 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -14,7 +14,6 @@ import { _backup } from './backup' import { _config } from './config' import { _create } from './create' import { _destroy } from './destroy' -import { _get } from './get' import { _load } from './load' import { _lock } from './lock' import { _refresh } from './refresh' diff --git a/src/lib/wallet/load.ts b/src/lib/wallet/load.ts index 4d6fb60..dee3b57 100644 --- a/src/lib/wallet/load.ts +++ b/src/lib/wallet/load.ts @@ -17,7 +17,6 @@ export async function _load (wallet: Wallet, vault: Vault, password: unknown, se type: wallet.type } if (wallet.type === 'Ledger') { - if (Ledger.isUnsupported) { throw new Error('Failed to initialize Ledger wallet', { cause: 'Browser is unsupported' }) } diff --git a/src/lib/wallet/update.ts b/src/lib/wallet/update.ts index 50d3f92..1199c16 100644 --- a/src/lib/wallet/update.ts +++ b/src/lib/wallet/update.ts @@ -6,7 +6,6 @@ import { utf8 } from '../convert' import { Database } from '../database' import { Vault } from '../vault' import { Wallet } from '../wallet' -import { _get } from './get' export async function _update (wallet: Wallet, vault: Vault, password?: string): Promise export async function _update (wallet: Wallet, vault: Vault, password: unknown): Promise { diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index c1a2759..0a64da4 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -31,7 +31,7 @@ await Promise.all([ 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 + let wallet, account, openBlock, sendBlock, receiveBlock, restored try { wallet = await Wallet.create('Ledger') } catch { @@ -170,6 +170,10 @@ await Promise.all([ }) await test('verify mnemonic', async () => { + await click( + 'Open Nano app, then click to continue', + async () => wallet.unlock() + ) const isVerified = await wallet.verify(LEDGER_MNEMONIC) assert.exists(isVerified) @@ -302,7 +306,13 @@ await Promise.all([ await assert.rejects(sendBlock.sign(wallet, 0)) }) - await test('destroy wallet', async () => { + await test('restore from db then destroy', async () => { + restored = await Wallet.restore(wallet.id) + + assert.exists(restored) + await assert.resolves(restored.unlock()) + assert.equal(await restored.verify(LEDGER_MNEMONIC), true) + await click( 'Click to finish Ledger tests by destroying wallet', async () => new Promise(r => setTimeout(r)) @@ -310,6 +320,7 @@ await Promise.all([ await assert.resolves(wallet.destroy()) await assert.rejects(wallet.unlock()) await wallet.destroy() + await restored.destroy() }) }) ]) diff --git a/test/test.lock-unlock.mjs b/test/test.lock-unlock.mjs index e86a5d8..b42edfa 100644 --- a/test/test.lock-unlock.mjs +++ b/test/test.lock-unlock.mjs @@ -253,6 +253,8 @@ await Promise.all([ await assert.rejects(wallet.config({ timeout: 0 })) await assert.rejects(wallet.config({ timeout: 9 })) await assert.rejects(wallet.config({ timeout: 601 })) + + await assert.resolves(wallet.destroy()) }) }) ])