]> git.codecow.com Git - libnemo.git/commitdiff
Narrow account_info response validation.
authorChris Duncan <chris@codecow.com>
Sat, 8 Aug 2026 18:54:36 +0000 (11:54 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 8 Aug 2026 18:54:36 +0000 (11:54 -0700)
src/lib/rpc/account_info.ts
src/lib/rpc/accounts_frontiers.ts
src/lib/rpc/block_info.ts

index 8631100cfc56d3f822714cb2b74697dcb9ac6172..29875de3fd953042fc8574875523d409c0f5a2ca 100644 (file)
@@ -13,7 +13,7 @@ export type AccountInfoRequest = {
 }
 
 export type AccountInfoResponse = {
-       account_version: string
+       account_version: '1' | '2'
        balance: string
        block_count: string
        confirmation_height?: string
@@ -26,7 +26,7 @@ export type AccountInfoResponse = {
        confirmed_representative: string
        frontier: Hex
        modified_timestamp: string
-       open_block: string
+       open_block: Hex
        pending?: string
        receivable: string
        representative: string
@@ -36,17 +36,17 @@ export type AccountInfoResponse = {
 
 export function account_info (body: unknown): AccountInfoResponse {
        if (body != null && typeof body === 'object') {
-               if ('account_version' in body && typeof body.account_version === 'string'
+               if ('account_version' in body && (body.account_version === '1' || body.account_version === '2')
                        && 'balance' in body && typeof body.balance === 'string'
                        && 'block_count' in body && typeof body.block_count === 'string'
                        && 'confirmed_balance' in body && typeof body.confirmed_balance === 'string'
-                       && 'confirmed_frontier' in body && hex.is(body.confirmed_frontier)
+                       && 'confirmed_frontier' in body && hex.is(body.confirmed_frontier, 64)
                        && 'confirmed_height' in body && typeof body.confirmed_height === 'string'
                        && 'confirmed_receivable' in body && typeof body.confirmed_receivable === 'string'
                        && 'confirmed_representative' in body && typeof body.confirmed_representative === 'string'
-                       && 'frontier' in body && hex.is(body.frontier)
+                       && 'frontier' in body && hex.is(body.frontier, 64)
                        && 'modified_timestamp' in body && typeof body.modified_timestamp === 'string'
-                       && 'open_block' in body && typeof body.open_block === 'string'
+                       && 'open_block' in body && hex.is(body.open_block, 64)
                        && 'receivable' in body && typeof body.receivable === 'string'
                        && 'representative' in body && typeof body.representative === 'string'
                        && 'representative_block' in body && typeof body.representative_block === 'string'
index 538b15f20960b9d92996697c7e9362a5afec8a46..35fdb834cf6ffb46c42c6283a7afaa4a311de8e0 100644 (file)
@@ -26,7 +26,7 @@ export function accounts_frontiers (body: unknown): AccountsFrontiers {
                        for (const [address, frontier] of Object.entries(frontiers) as [string, unknown][]) {
                                try {
                                        Account.validate(address)
-                                       if (hex.is(frontier)) {
+                                       if (hex.is(frontier, 64)) {
                                                response.frontiers ??= {}
                                                response.frontiers[address] = frontier
                                        }
index 5344c7f66d7605892dcca97228c697a642520b55..eaa2e3ca4007afe14da80bd1deda14fdca216726 100644 (file)
@@ -1,6 +1,8 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
+import { Account } from '../account'
+import { hex } from '../convert'
 import { RpcError } from '../errors'
 
 export type BlockInfoRequest = {
@@ -48,11 +50,11 @@ export function block_info (body: unknown): BlockInfoResponse {
                        && 'contents' in body && body.contents != null && typeof body.contents === 'object'
                        && 'account' in body.contents && typeof body.contents.account === 'string'
                        && 'balance' in body.contents && typeof body.contents.balance === 'string'
-                       && 'link' in body.contents && typeof body.contents.link === 'string' && /^[0-9A-F]{64}$/i.test(body.contents.link)
-                       && 'link_as_account' in body.contents && typeof body.contents.link_as_account === 'string'
-                       && 'previous' in body.contents && typeof body.contents.previous === 'string' && /^[0-9A-F]{64}$/i.test(body.contents.previous)
+                       && 'link' in body.contents && hex.is(body.contents.link, 64)
+                       && 'link_as_account' in body.contents && Account.validate(body.contents.link_as_account)
+                       && 'previous' in body.contents && hex.is(body.contents.previous, 64)
                        && 'representative' in body.contents && typeof body.contents.representative === 'string'
-                       && 'signature' in body.contents && typeof body.contents.signature === 'string' && /^[0-9A-F]{128}$/i.test(body.contents.signature)
+                       && 'signature' in body.contents && hex.is(body.contents.signature, 128)
                        && 'type' in body.contents && body.contents.type === 'state'
                        && 'work' in body.contents && typeof body.contents.work === 'string'
                ) {