]> git.codecow.com Git - libnemo.git/commitdiff
Improve type checking on cache update input.
authorChris Duncan <chris@codecow.com>
Mon, 18 May 2026 21:27:37 +0000 (14:27 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 18 May 2026 21:27:37 +0000 (14:27 -0700)
src/lib/ledger/cache.ts
src/lib/ledger/index.ts

index a58aadb45a0512919638fbfcee3f2d311b7d2cf6..8ffc885a53c14af98f2fe459433f594009628d26 100644 (file)
@@ -1,7 +1,7 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! 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')
                }
index 487f2b1b507525badf7201f93b13c841f7fe1599..23d4a4d736498335eec236bcf5b9623daeb0f931 100644 (file)
@@ -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<LedgerResponse>
-       static async updateCache (index: number, input: any, node?: Rpc): Promise<LedgerResponse> {
+       static async updateCache (index: number, input: unknown, node?: Rpc): Promise<LedgerResponse> {
                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<LedgerResponse>(async () => _cache(this.#transport, index, input))
        }