// SPDX-FileCopyrightText: © 2026 LEDGER SAS // SPDX-License-Identifier: Apache-2.0 import type { StellarContext } from '../config' import type { BlockInfo } from '@ledgerhq/coin-module-framework/api/index' import { fetchLedgerRecord } from '../network' /** * Returns lightweight metadata for a Stellar closed ledger at the given sequence. * * Fetches the ledger once. When `height > 1` and Horizon provides `prev_hash`, * `BlockInfo.parent` is derived without a second request; otherwise the parent * ledger is loaded explicitly. Genesis ledger sequence 1 has no parent. */ export async function getBlockInfo(context: StellarContext, height: number): Promise { if (!Number.isSafeInteger(height) || height <= 0) { throw new Error(`getBlockInfo: height must be a positive integer, got ${height}`) } const config = await context.config() const ledger = await fetchLedgerRecord(config, height) const info: BlockInfo = { height: ledger.sequence, hash: ledger.hash, time: new Date(ledger.closed_at), } let parent: BlockInfo['parent'] if (height > 1) { const prevHash = 'prev_hash' in ledger && typeof ledger.prev_hash === 'string' ? ledger.prev_hash : undefined if (prevHash) { parent = { height: height - 1, hash: prevHash } } else { const parentLedger = await fetchLedgerRecord(config, height - 1) parent = { height: parentLedger.sequence, hash: parentLedger.hash } } info.parent = parent } return info }