From: Chris Duncan Date: Mon, 18 May 2026 21:27:37 +0000 (-0700) Subject: Improve type checking on cache update input. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=b600e7b1eb2c0535f89f1f029866b9230fbb53c5;p=libnemo.git Improve type checking on cache update input. --- diff --git a/src/lib/ledger/cache.ts b/src/lib/ledger/cache.ts index a58aadb..8ffc885 100644 --- a/src/lib/ledger/cache.ts +++ b/src/lib/ledger/cache.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { APDU_CODES, LedgerResponse, LedgerTransport, STATUS_CODES, LISTEN_TIMEOUT, OPEN_TIMEOUT } from '.' +import { APDU_CODES, LedgerResponse, LedgerTransport, LISTEN_TIMEOUT, OPEN_TIMEOUT, STATUS_CODES } from '.' import { Account } from '../account' import { Block } from '../block' import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET } from '../constants' @@ -12,9 +12,6 @@ export async function _cache (transport: LedgerTransport, index: number = 0, blo 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') } diff --git a/src/lib/ledger/index.ts b/src/lib/ledger/index.ts index 487f2b1..23d4a4d 100644 --- a/src/lib/ledger/index.ts +++ b/src/lib/ledger/index.ts @@ -229,7 +229,7 @@ export class Ledger { * @param {Rpc} rpc - Rpc class object with a node URL */ static async updateCache (index: number, hash: string, rpc: Rpc): Promise - static async updateCache (index: number, input: any, node?: Rpc): Promise { + static async updateCache (index: number, input: unknown, node?: Rpc): Promise { if (typeof input === 'string' && node instanceof Rpc) { const data = { 'json_block': 'true', @@ -239,7 +239,13 @@ export class Ledger { if (!res || !res.ok || res.error) { throw new Error(`Unable to fetch block info`, res) } - input = res.contents + const { contents: { account, balance, link, previous, representative, signature } } = res + const block = new Block(account, balance, previous, representative).receive(link, 0) + block.signature = signature + input = block + } + if (!(input instanceof Block)) { + throw new TypeError('Invalid block format') } return queue(async () => _cache(this.#transport, index, input)) }