From 09efbc30857defdc4c3894157b77e738981f044d Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 18 May 2026 06:50:23 -0700 Subject: [PATCH] Extract cache update function. --- src/lib/ledger/cache.ts | 77 +++++++++++++++++++++-------------------- src/lib/ledger/index.ts | 6 +--- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/src/lib/ledger/cache.ts b/src/lib/ledger/cache.ts index fb32768..a58aadb 100644 --- a/src/lib/ledger/cache.ts +++ b/src/lib/ledger/cache.ts @@ -6,48 +6,49 @@ import { Account } from '../account' import { Block } from '../block' import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET } from '../constants' import { bytes, dec, hex } from '../convert' -import { queue } from './queue' export async function _cache (transport: LedgerTransport, index: number = 0, block: Block): Promise { - return queue(async () => { - try { - if (typeof index !== 'number' || index < 0 || index >= HARDENED_OFFSET) { - throw new TypeError('Invalid account index') - } - if (!(block instanceof Block)) { - throw new TypeError('Invalid block format') - } - if (!(block.link instanceof Uint8Array)) { - throw new TypeError('Invalid block link') - } - if (!(block.representative instanceof Account)) { - throw new TypeError('Invalid block link') - } - if (!block.signature) { - throw new ReferenceError('Cannot cache unsigned block') - } + try { + if (typeof index !== 'number' || index < 0 || index >= HARDENED_OFFSET) { + throw new TypeError('Invalid account index') + } + if (!(block instanceof Block)) { + throw new TypeError('Invalid block format') + } + if (!(block.link instanceof Uint8Array)) { + throw new TypeError('Invalid block link') + } + if (!(block.representative instanceof Account)) { + throw new TypeError('Invalid block link') + } + if (!block.signature) { + throw new ReferenceError('Cannot cache unsigned block') + } - const purpose = dec.toBytes(BIP44_PURPOSE + HARDENED_OFFSET, 4) - const coin = dec.toBytes(BIP44_COIN_NANO + HARDENED_OFFSET, 4) - const account = dec.toBytes(index + HARDENED_OFFSET, 4) - const previous = block.previous - const link = block.link - const representative = hex.toBytes(block.representative.publicKey, 32) - const balance = hex.toBytes(block.balance.toString(16), 16) - const signature = hex.toBytes(block.signature, 64) - const data = new Uint8Array([APDU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance, ...signature]) + const purpose = dec.toBytes(BIP44_PURPOSE + HARDENED_OFFSET, 4) + const coin = dec.toBytes(BIP44_COIN_NANO + HARDENED_OFFSET, 4) + const account = dec.toBytes(index + HARDENED_OFFSET, 4) + const previous = block.previous + const link = block.link + const representative = hex.toBytes(block.representative.publicKey, 32) + const balance = hex.toBytes(block.balance.toString(16), 16) + const signature = hex.toBytes(block.signature, 64) + const data = new Uint8Array([APDU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance, ...signature]) - const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT) - const response = await t - .send(APDU_CODES.class, APDU_CODES.cacheBlock, APDU_CODES.paramUnused, APDU_CODES.paramUnused, data as Buffer) - .then((res: Buffer) => bytes.toDec(res)) - .catch((err: any) => err.statusCode) - .finally(async () => await t.close()) as number + const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT) + const response = await t + .send(APDU_CODES.class, APDU_CODES.cacheBlock, APDU_CODES.paramUnused, APDU_CODES.paramUnused, data as Buffer) + .then((res: Buffer) => bytes.toDec(res)) + .catch((err: any) => err.statusCode) + .finally(async () => await t.close()) as number - return { status: STATUS_CODES[response] } - } catch (err: any) { - console.error('Ledger.#cacheBlock()', err) - return { status: err.message } + const status = STATUS_CODES[response] + if (status !== 'OK') { + throw new Error('failed to cache frontier block in ledger', { cause: status }) } - }) + return { status } + } catch (err: any) { + console.error('Ledger cache()', err) + return { status: err.message } + } } diff --git a/src/lib/ledger/index.ts b/src/lib/ledger/index.ts index 969c75f..238fc18 100644 --- a/src/lib/ledger/index.ts +++ b/src/lib/ledger/index.ts @@ -279,11 +279,7 @@ export class Ledger { } input = res.contents } - const { status } = await _cache(this.#transport, index, input) - if (status !== 'OK') { - throw new Error('failed to cache frontier block in ledger', { cause: status }) - } - return { status } + return queue(async () => _cache(this.#transport, index, input)) } /** -- 2.47.3