From: Chris Duncan Date: Mon, 10 Aug 2026 04:49:49 +0000 (-0700) Subject: Simplify multi-account RPC response parsing. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=a280917ff0199746ee7b558321418fe477b76436;p=libnemo.git Simplify multi-account RPC response parsing. --- diff --git a/src/lib/rpc/accounts_balances.ts b/src/lib/rpc/accounts_balances.ts index bce825a..5b18218 100644 --- a/src/lib/rpc/accounts_balances.ts +++ b/src/lib/rpc/accounts_balances.ts @@ -19,38 +19,21 @@ export function accounts_balances (body: unknown): AccountsBalancesResponse { const response: AccountsBalancesResponse = Object.create(null) if ('errors' in body && body.errors != null && typeof body.errors === 'object') { + const errors: Record = response.errors ?? Object.create(null) + response.errors = errors for (const [address, error] of Object.entries(body.errors) as [string, unknown][]) { - try { - if (!Account.isValid(address)) { - throw new RpcError('Invalid account address') - } - if (typeof error === 'string') { - throw new RpcError(error) - } - } catch (err: any) { - const errors = response.errors ?? Object.create(null) - response.errors = errors - errors[address] = err?.message + if (Account.isValid(address) && typeof error === 'string') { + errors[address] = error } } } if ('balances' in body && body.balances != null && typeof body.balances === 'object') { + const balances: Record = response.balances ?? Object.create(null) + response.balances = balances for (const [address, data] of Object.entries(body.balances) as [string, unknown][]) { - try { - if (!Account.isValid(address)) { - throw new RpcError('Invalid account address') - } - if (data == null || typeof data !== 'object') { - throw new RpcError('Invalid account balance data') - } - const balances = response.balances ?? Object.create(null) - response.balances = balances + if (Account.isValid(address)) { balances[address] = account_balance(data) - } catch (err: any) { - const errors = response.errors ?? Object.create(null) - response.errors = errors - errors[address] = err?.message } } } diff --git a/src/lib/rpc/accounts_frontiers.ts b/src/lib/rpc/accounts_frontiers.ts index 173eae4..0bf7469 100644 --- a/src/lib/rpc/accounts_frontiers.ts +++ b/src/lib/rpc/accounts_frontiers.ts @@ -15,52 +15,30 @@ export type AccountsFrontiersResponse = { } export function accounts_frontiers (body: unknown): AccountsFrontiersResponse { - const response: AccountsFrontiersResponse = Object.create(null) - if (body == null || typeof body !== 'object') { - return response - } - const frontiers = 'frontiers' in body ? body.frontiers : null - const errors = 'errors' in body ? body.errors : null + if (body != null && typeof body === 'object') { + const response: AccountsFrontiersResponse = Object.create(null) - if (frontiers != null) { - if (typeof frontiers === 'object') { - for (const [address, frontier] of Object.entries(frontiers) as [string, unknown][]) { - try { - if (!Account.isValid(address)) { - throw new RpcError('Invalid account address') - } - if (hex.is(frontier, 64)) { - const frontiers = response.frontiers ?? Object.create(null) - response.frontiers = frontiers - frontiers[address] = frontier - } - } catch (err: any) { - const errors = response.errors ?? Object.create(null) - response.errors = errors - errors[address] = err?.message + if ('errors' in body && body.errors != null && typeof body.errors === 'object') { + const errors = response.errors ?? Object.create(null) + response.errors = errors + for (const [address, error] of Object.entries(body.errors) as [string, unknown][]) { + if (Account.isValid(address) && typeof error === 'string') { + errors[address] = error } } } - } - if (errors != null) { - if (typeof errors === 'object') { - for (const [address, error] of Object.entries(errors) as [string, unknown][]) { - try { - if (!Account.isValid(address)) { - throw new RpcError('Invalid account address') - } - if (typeof error === 'string') { - throw new Error(error) - } - } catch (err: any) { - const errors = response.errors ?? Object.create(null) - response.errors = errors - errors[address] = err?.message + if ('frontiers' in body && body.frontiers != null && typeof body.frontiers === 'object') { + const frontiers = response.frontiers ?? Object.create(null) + response.frontiers = frontiers + for (const [address, frontier] of Object.entries(frontiers) as [string, unknown][]) { + if (Account.isValid(address) && hex.is(frontier, 64)) { + frontiers[address] = frontier } } } - } - return response + return response + } + throw new RpcError('Invalid accounts_balance response') } diff --git a/src/lib/rpc/blocks_info.ts b/src/lib/rpc/blocks_info.ts index 2d44f90..7f354b8 100644 --- a/src/lib/rpc/blocks_info.ts +++ b/src/lib/rpc/blocks_info.ts @@ -20,35 +20,21 @@ export function blocks_info (body: unknown): BlocksInfoResponse { const response: BlocksInfoResponse = Object.create(null) if ('errors' in body && body.errors != null && typeof body.errors === 'object') { + const errors = response.errors ?? Object.create(null) + response.errors = errors for (const [hash, error] of Object.entries(body.errors) as [string, unknown][]) { - try { - if (hex.is(hash) && typeof error === 'string') { - throw new Error(error) - } else { - throw new Error('Invalid blocks_info error') - } - } catch (err: any) { - const errors = response.errors ?? Object.create(null) - response.errors = errors - errors[hash] = err?.message + if (hex.is(hash, 64) && typeof error === 'string') { + errors[hash] = error } } } if ('blocks' in body && body.blocks != null && typeof body.blocks === 'object') { + const blocks: Record = response.blocks ?? Object.create(null) + response.blocks = blocks for (const [hash, data] of Object.entries(body.blocks) as [string, unknown][]) { - try { - if (hex.is(hash)) { - const blocks = response.blocks ?? Object.create(null) - response.blocks = blocks - blocks[hash] = block_info(data) - } else { - throw new Error('Invalid blocks_info hash') - } - } catch (err: any) { - const errors = response.errors ?? Object.create(null) - response.errors = errors - errors[hash] = err?.message + if (hex.is(hash, 64)) { + blocks[hash] = block_info(data) } } }