const response: AccountsBalancesResponse = Object.create(null)
if ('errors' in body && body.errors != null && typeof body.errors === 'object') {
+ const errors: Record<string, string> = 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<string, AccountBalanceResponse> = 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
}
}
}
}
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')
}
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<Hex, BlockInfoResponse> = 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)
}
}
}