]> git.codecow.com Git - libnemo.git/commitdiff
More RPC refactoring.
authorChris Duncan <chris@codecow.com>
Fri, 7 Aug 2026 16:23:17 +0000 (09:23 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 7 Aug 2026 16:23:17 +0000 (09:23 -0700)
src/lib/account/index.ts
src/lib/account/refresh.ts
src/lib/block/index.ts
src/lib/rpc/account_info.ts
src/lib/rpc/accounts_frontiers.ts
src/lib/rpc/process.ts
src/lib/rpc/schema.ts

index 2247b9ac1071aad32456866b757449d6e60b4fa9..adcd9013256ac572fd816212b2e25951a179c63d 100644 (file)
@@ -34,7 +34,7 @@ export class Account {
 \r
        #balance?: bigint\r
        #block_count?: number\r
-       #frontier?: string\r
+       #frontier?: Hex\r
        #frontier_block?: Block\r
        #open_block?: string\r
        #receivable?: bigint\r
@@ -59,7 +59,7 @@ export class Account {
 \r
        get balance (): bigint | undefined { return this.#balance }\r
        get block_count (): number | undefined { return this.#block_count }\r
-       get frontier (): string | undefined { return this.#frontier }\r
+       get frontier (): Hex | undefined { return this.#frontier }\r
        get frontier_block (): Block | undefined { return this.#frontier_block }\r
        get open_block (): string | undefined { return this.#open_block }\r
        get receivable (): bigint | undefined { return this.#receivable }\r
@@ -84,7 +84,7 @@ export class Account {
 \r
        set balance (v: bigint | number | string) { this.#balance = BigInt(v) }\r
        set block_count (v: number | undefined) { if (v !== undefined) this.#block_count = v | 0 }\r
-       set frontier (v: string | undefined) { this.#frontier = v }\r
+       set frontier (v: Hex | undefined) { this.#frontier = v }\r
        set frontier_block (v: Block | undefined) { this.#frontier_block = v }\r
        set open_block (v: string | undefined) { this.#open_block = v }\r
        set receivable (v: bigint | number | string) { this.#receivable = BigInt(v) }\r
index 3fc558c196d7c03d2f56f72d54aae2efd48837e6..09dbc2acfe250b0f031714287534a1babd09356e 100644 (file)
@@ -3,7 +3,8 @@
 
 import { Account } from '.'
 import { Block } from '../block'
-import { Rpc, Schema } from '../rpc'
+import { Rpc } from '../rpc'
+import { AccountInfoRequest } from '../rpc/account_info'
 
 export async function _refresh (account: Account, rpc: Rpc | string | URL): Promise<void>
 export async function _refresh (account: Account, rpc: unknown): Promise<void> {
@@ -13,25 +14,16 @@ export async function _refresh (account: Account, rpc: unknown): Promise<void> {
        if (!(rpc instanceof Rpc)) {
                throw new TypeError('RPC must be a valid node')
        }
-       const reqAccountInfo = {
+       const reqAccountInfo: AccountInfoRequest = {
                account: account.address,
                include_confirmed: true,
                receivable: true,
                representative: true,
                weight: true
        }
-       const resAccountInfo = await rpc.post('account_info', reqAccountInfo)
-       if (resAccountInfo == null || typeof resAccountInfo !== 'object') {
-               throw new TypeError('Invalid account_info response')
-       }
-       const accountInfo = resAccountInfo as Record<string, unknown>
-       if (accountInfo.frontier == null) {
-               throw new Error('Account not found')
-       }
+       const accountInfo = await rpc.post('account_info', reqAccountInfo)
        for (const [k, v] of Object.entries(accountInfo)) {
-               if (Schema.account_info.fields.includes(k) && typeof v === 'string') {
-                       account[k] = v
-               }
+               account[k] = v
        }
 
        const reqConfirmedFrontierBlockInfo = {
@@ -54,9 +46,7 @@ export async function _refresh (account: Account, rpc: unknown): Promise<void> {
        }
        const confirmedFrontierContents: Record<string, string> = {}
        for (const [k, v] of Object.entries(resConfirmedFrontierBlockInfo.contents)) {
-               if (Schema.block_info.contents.fields.includes(k) && typeof v === 'string') {
-                       confirmedFrontierContents[k] = v
-               }
+               confirmedFrontierContents[k] = v
        }
        const confirmedFrontierBlock = new Block(confirmedFrontierContents.account, confirmedFrontierContents.balance, confirmedFrontierContents.previous, confirmedFrontierContents.representative)
        if (typeof confirmedFrontierBlock[confirmedFrontierSubtype] !== 'function') {
@@ -85,9 +75,7 @@ export async function _refresh (account: Account, rpc: unknown): Promise<void> {
        }
        const frontierContents: Record<string, string> = {}
        for (const [k, v] of Object.entries(resFrontierBlockInfo.contents)) {
-               if (Schema.block_info.contents.fields.includes(k) && typeof v === 'string') {
-                       frontierContents[k] = v
-               }
+               frontierContents[k] = v
        }
        const frontierBlock = new Block(frontierContents.account, frontierContents.balance, frontierContents.previous, frontierContents.representative)
        if (typeof frontierBlock[frontierSubtype] !== 'function') {
index a214faf0bec84931b5e6d3c1d56a0fcea1a0f7fb..85deeddf40211a87405404ebf357eb9026beace3 100644 (file)
@@ -7,6 +7,7 @@ import { DIFFICULTY_RECEIVE, DIFFICULTY_SEND, PREAMBLE } from '../constants'
 import { bytes, dec, hex } from '../convert'
 import { Blake2b } from '../crypto'
 import { Rpc } from '../rpc'
+import { ProcessRequest } from '../rpc/process'
 import { convert } from '../tools'
 import { Wallet } from '../wallet'
 import { _change } from './change'
@@ -204,7 +205,7 @@ export class Block {
                        throw new Error('Block is missing signature. Use sign() and try again.')
                }
                if (this.work == null) await this.pow()
-               const data = {
+               const data: ProcessRequest = {
                        subtype: this.subtype,
                        json_block: true,
                        block: this.toJSON()
index df63a2e42f2fa913c9d26e0984b9ddf73abc977e..6931cd142152247e823a368ada253523d842c788 100644 (file)
@@ -1,24 +1,88 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-export const account_info = Object.freeze({
-       fields: [
-               'account_version',
-               'balance',
-               'block_count',
-               'confirmation_height',
-               'confirmation_height_frontier',
-               'confirmed_balance',
-               'confirmed_frontier',
-               'confirmed_height',
-               'confirmed_receivable',
-               'confirmed_representative',
-               'frontier',
-               'modified_timestamp',
-               'open_block',
-               'receivable',
-               'representative',
-               'representative_block',
-               'weight',
-       ],
-})
+import { RpcError } from '../errors'
+
+export type AccountInfoRequest = {
+       account: string
+       include_confirmed: true
+       receivable: true
+       representative: true
+       weight: true
+}
+
+export type AccountInfoResponse = {
+       account_version: string
+       balance: string
+       block_count: string
+       confirmation_height: string
+       confirmation_height_frontier: string
+       confirmed_balance: string
+       confirmed_frontier: string
+       confirmed_height: string
+       confirmed_pending?: string
+       confirmed_receivable: string
+       confirmed_representative: string
+       frontier: string
+       modified_timestamp: string
+       open_block: string
+       pending?: string
+       receivable: string
+       representative: string
+       representative_block: string
+       weight: string
+}
+
+export function account_info (body: unknown): AccountInfoResponse {
+       if (body != null && typeof body === 'object') {
+               if ('account_version' in body && typeof body.account_version === 'string'
+                       && 'balance' in body && typeof body.balance === 'string'
+                       && 'block_count' in body && typeof body.block_count === 'string'
+                       && 'confirmation_height' in body && typeof body.confirmation_height === 'string'
+                       && 'confirmation_height_frontier' in body && typeof body.confirmation_height_frontier === 'string'
+                       && 'confirmed_balance' in body && typeof body.confirmed_balance === 'string'
+                       && 'confirmed_frontier' in body && typeof body.confirmed_frontier === 'string'
+                       && '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 && typeof body.frontier === 'string'
+                       && 'modified_timestamp' in body && typeof body.modified_timestamp === 'string'
+                       && 'open_block' in body && typeof body.open_block === 'string'
+                       && 'receivable' in body && typeof body.receivable === 'string'
+                       && 'representative' in body && typeof body.representative === 'string'
+                       && 'representative_block' in body && typeof body.representative_block === 'string'
+                       && 'weight' in body && typeof body.weight === 'string'
+               ) {
+                       const response: AccountInfoResponse = {
+                               account_version: body.account_version,
+                               balance: body.balance,
+                               block_count: body.block_count,
+                               confirmation_height: body.confirmation_height,
+                               confirmation_height_frontier: body.confirmation_height_frontier,
+                               confirmed_balance: body.confirmed_balance,
+                               confirmed_frontier: body.confirmed_frontier,
+                               confirmed_height: body.confirmed_height,
+                               confirmed_receivable: body.confirmed_receivable,
+                               confirmed_representative: body.confirmed_representative,
+                               frontier: body.frontier,
+                               modified_timestamp: body.modified_timestamp,
+                               open_block: body.open_block,
+                               receivable: body.receivable,
+                               representative: body.representative,
+                               representative_block: body.representative_block,
+                               weight: body.weight,
+                       }
+                       if ('confirmed_pending' in body && typeof body.confirmed_pending === 'string') {
+                               response.confirmed_pending = body.confirmed_pending
+                       }
+                       if ('pending' in body && typeof body.pending === 'string') {
+                               response.pending = body.pending
+                       }
+                       return response
+               }
+               if ('error' in body && typeof body.error === 'string') {
+                       throw new RpcError(body.error)
+               }
+       }
+       throw new RpcError('Invalid account_info response')
+}
index dce1ee06899e8d61c129c291f461dbeea3831d77..01df9bae5147e9a132078612c15e155404e510ae 100644 (file)
@@ -8,7 +8,7 @@ import { hex } from '../convert'
 
 type AccountsFrontiers = {
        frontiers?: {
-               [address: string]: string
+               [address: string]: Hex
        }
        errors?: {
                [address: string]: string
index 99b62ba89f1ef2cb2f4214680a5ca1b34e6f1d2f..450399a4fd26a44134201a51fd4899778e930a85 100644 (file)
@@ -6,7 +6,6 @@ import { RpcError } from '../errors'
 import { BlockInfoContents } from './block_info'
 
 export type ProcessRequest = {
-       action: 'process'
        block: BlockInfoContents
        async: boolean
        force: boolean
index 295a92b75bafab65e0758382e5f049aaa86e0e2b..a3b46f60a91e518cb4f34d3be8b8cff36cddee56 100644 (file)
@@ -12,7 +12,7 @@ import { process } from './process'
 
 export const Schema = Object.freeze({
        account_balance,
-       // account_info,
+       account_info,
        // account_representative,
        accounts_balances,
        accounts_frontiers,