From 6a503bfbe6c76f72682e1b6ea03ae0b301c7ae7a Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 10 Aug 2026 21:54:05 -0700 Subject: [PATCH] Build out more of blocks_info req/res. --- src/lib/rpc/block_info.ts | 4 ++++ src/lib/rpc/blocks_info.ts | 24 +++++++++++++++++++++++- src/lib/wallet/refresh.ts | 19 +++++++++++++++---- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/lib/rpc/block_info.ts b/src/lib/rpc/block_info.ts index 35e064e..18bdcb6 100644 --- a/src/lib/rpc/block_info.ts +++ b/src/lib/rpc/block_info.ts @@ -32,6 +32,7 @@ export type BlockInfoResponse = { height: string linked_account: string local_timestamp: string + receive_hash?: Hex | '0' subtype: 'change' | 'epoch' | 'open' | 'receive' | 'send' successor: string } @@ -72,6 +73,9 @@ export function block_info (body: unknown): BlockInfoResponse { response.local_timestamp = body.local_timestamp response.subtype = body.subtype response.successor = body.successor + if ('receive_hash' in body && (hex.is(body.receive_hash, 64) || body.receive_hash === '0')) { + response.receive_hash = body.receive_hash + } const contents: BlockInfoContents = Object.create(null) contents.account = body.contents.account diff --git a/src/lib/rpc/blocks_info.ts b/src/lib/rpc/blocks_info.ts index 7f354b8..0df6aea 100644 --- a/src/lib/rpc/blocks_info.ts +++ b/src/lib/rpc/blocks_info.ts @@ -6,12 +6,24 @@ import { hex } from '../convert' import { RpcError } from '../errors' import { BlockInfoResponse, block_info } from './block_info' +export type BlocksInfoRequest = { + hashes: Hex[] + include_linked_account: true + include_not_found: true + json_block: true + pending: true + receivable: true + receive_hash: true + source: true +} + export type BlocksInfoResponse = { blocks?: { [hash: Hex]: BlockInfoResponse } + blocks_not_found?: Hex[] errors?: { - [hash: string]: string + [hash: Hex]: string } } @@ -29,6 +41,16 @@ export function blocks_info (body: unknown): BlocksInfoResponse { } } + if ('blocks_not_found' in body && Array.isArray(body.blocks_not_found)) { + const blocks_not_found = response.blocks_not_found ?? Object.create(null) + response.blocks_not_found = blocks_not_found + for (const hash of body.blocks_not_found as unknown[]) { + if (hex.is(hash, 64)) { + blocks_not_found.push(hash) + } + } + } + if ('blocks' in body && body.blocks != null && typeof body.blocks === 'object') { const blocks: Record = response.blocks ?? Object.create(null) response.blocks = blocks diff --git a/src/lib/wallet/refresh.ts b/src/lib/wallet/refresh.ts index 0ced060..8eab352 100644 --- a/src/lib/wallet/refresh.ts +++ b/src/lib/wallet/refresh.ts @@ -6,7 +6,7 @@ import { Block } from '../block' import { Rpc } from '../rpc' import { AccountInfoRequest, AccountInfoResponse } from '../rpc/account_info' import { AccountsFrontiersResponse } from '../rpc/accounts_frontiers' -import { BlocksInfoResponse } from '../rpc/blocks_info' +import { BlocksInfoRequest, BlocksInfoResponse } from '../rpc/blocks_info' import { Wallet } from '../wallet' export async function _refresh (wallet: Wallet, rpc: Rpc | string | URL, from: number, to: number): Promise> @@ -32,9 +32,20 @@ export async function _refresh (wallet: Wallet, rpc: unknown, from: unknown, to: accounts: addresses } const resAccountsFrontiers: AccountsFrontiersResponse = await rpc.post('accounts_frontiers', data) - const frontiers = Object.values(resAccountsFrontiers.frontiers ?? Object.create(null)) + const frontiers = resAccountsFrontiers.frontiers ? Object.values(resAccountsFrontiers.frontiers) : [] + + const reqBlocksInfo: BlocksInfoRequest = { + hashes: frontiers, + include_not_found: true, + json_block: true, + include_linked_account: true, + pending: true, + receivable: true, + receive_hash: true, + source: true + } const { blocks }: BlocksInfoResponse = frontiers.length > 0 - ? await rpc.post('blocks_info', { json_block: true, include_not_found: true, hashes: frontiers }) + ? await rpc.post('blocks_info', reqBlocksInfo) : Object.create(null) for (const account of accounts.values()) { @@ -74,7 +85,7 @@ export async function _refresh (wallet: Wallet, rpc: unknown, from: unknown, to: frontierBlock[subtype](link, 0) } else { throw new TypeError('Invalid frontier block subtype', { cause: subtype }) - } + } frontierBlock.signature = signature account.frontier_block = frontierBlock } -- 2.52.0