From: Chris Duncan Date: Mon, 18 May 2026 19:53:32 +0000 (-0700) Subject: Extract verify function. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=11776990739d445b8023b320509008d48bded4a9;p=libnemo.git Extract verify function. --- diff --git a/src/lib/ledger/index.ts b/src/lib/ledger/index.ts index 07a5f5e..2b49f67 100644 --- a/src/lib/ledger/index.ts +++ b/src/lib/ledger/index.ts @@ -7,9 +7,8 @@ import { default as TransportHID } from '@ledgerhq/hw-transport-webhid' import { default as TransportUSB } from '@ledgerhq/hw-transport-webusb' import { Block } from '../block' import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET } from '../constants' -import { dec, utf8 } from '../convert' +import { dec } from '../convert' import { Rpc } from '../rpc' -import { Wallet } from '../wallet' import { _account } from './account' import { _cache } from './cache' import { _close } from './close' @@ -19,6 +18,7 @@ import { LedgerEvent } from './event' import { _open } from './open' import { queue } from './queue' import { _sign } from './sign' +import { _verify } from './verify' export type LedgerStatus = 'UNSUPPORTED' | 'DISCONNECTED' | 'BUSY' | 'LOCKED' | 'CONNECTED' export type LedgerTransport = typeof TransportHID | typeof TransportBLE | typeof TransportUSB @@ -263,21 +263,7 @@ export class Ledger { */ static async verify (mnemonic: string): Promise static async verify (secret: string): Promise { - const testWallet = await Wallet.load('BIP-44', '', secret) - secret = '' - const nonce = crypto.randomUUID().slice(0, 16) - try { - await testWallet.unlock('') - const testSignature = await testWallet.sign(0, `Nano Signed Nonce:\n${utf8.toHex(nonce)}`) - try { - const ledgerSignature = await this.sign(0, nonce) - return ledgerSignature === testSignature - } catch (err) { - throw new Error('Failed to verify wallet', { cause: err }) - } - } finally { - await testWallet.destroy() - } + return _verify(secret) } /** diff --git a/src/lib/ledger/verify.ts b/src/lib/ledger/verify.ts new file mode 100644 index 0000000..ba0e891 --- /dev/null +++ b/src/lib/ledger/verify.ts @@ -0,0 +1,23 @@ +//! SPDX-FileCopyrightText: 2025 Chris Duncan +//! SPDX-License-Identifier: GPL-3.0-or-later +import { Ledger } from '.' +import { utf8 } from '../convert' +import { Wallet } from '../wallet' + +export async function _verify (secret: string): Promise { + const testWallet = await Wallet.load('BIP-44', '', secret) + secret = '' + const nonce = crypto.randomUUID().slice(0, 16) + try { + await testWallet.unlock('') + const testSignature = await testWallet.sign(0, `Nano Signed Nonce:\n${utf8.toHex(nonce)}`) + try { + const ledgerSignature = await Ledger.sign(0, nonce) + return ledgerSignature === testSignature + } catch (err) { + throw new Error('Failed to verify wallet', { cause: err }) + } + } finally { + await testWallet.destroy() + } +}