From 72a3a04583809e29b9a33bb6a2656825cdc74143 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 3 Aug 2026 12:02:36 -0700 Subject: [PATCH] Validate frontier block for Ledger cache. --- src/lib/ledger/index.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib/ledger/index.ts b/src/lib/ledger/index.ts index c7f80a3..9c175cf 100644 --- a/src/lib/ledger/index.ts +++ b/src/lib/ledger/index.ts @@ -9,6 +9,7 @@ import { Block } from '../block' import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET } from '../constants' import { dec } from '../convert' import { Rpc } from '../rpc' +import { SCHEMA } from '../rpc/schema' import { _account } from './account' import { _cache } from './cache' import { _close } from './close' @@ -234,10 +235,20 @@ export class Ledger { 'hash': input } const res = await node.post('block_info', data) - if (!res || !res.ok || res.error) { - throw new Error(`Unable to fetch block info`, res) + if (!res || typeof res !== 'object' || !('ok' in res) || 'error' in res) { + throw new Error('Unable to fetch block info', { cause: res }) } - const { contents: { account, balance, link, previous, representative, signature } } = res + const blockInfo = res as Record & Record<'ok', unknown> + if (!(blockInfo.contents != null && typeof blockInfo.contents === 'object')) { + throw new Error('Block info missing contents', { cause: blockInfo }) + } + const contents = {} as Record + for (const [k, v] of Object.entries(blockInfo.contents)) { + if (SCHEMA.block_info.fields.includes(k)) { + contents[k] = v + } + } + const { account, balance, link, previous, representative, signature } = contents const block = new Block(account, balance, previous, representative).receive(link, 0) block.signature = signature input = block -- 2.52.0