]> git.codecow.com Git - libnemo.git/commitdiff
Extract cache update function.
authorChris Duncan <chris@codecow.com>
Mon, 18 May 2026 13:50:23 +0000 (06:50 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 18 May 2026 13:50:23 +0000 (06:50 -0700)
src/lib/ledger/cache.ts
src/lib/ledger/index.ts

index fb3276819f54a85feb6d528d0332edbe80800d0e..a58aadb45a0512919638fbfcee3f2d311b7d2cf6 100644 (file)
@@ -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<LedgerResponse> {
-       return queue<LedgerResponse>(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 }
+       }
 }
index 969c75fd88651c147d8aec7dda410faff250af9c..238fc185222a4bef7c3b2a6ab9b230e00ecfcbdc 100644 (file)
@@ -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<LedgerResponse>(async () => _cache(this.#transport, index, input))
        }
 
        /**