\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
\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
\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
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> {
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 = {
}
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') {
}
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') {
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'
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()
//! 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')
+}
type AccountsFrontiers = {
frontiers?: {
- [address: string]: string
+ [address: string]: Hex
}
errors?: {
[address: string]: string
import { BlockInfoContents } from './block_info'
export type ProcessRequest = {
- action: 'process'
block: BlockInfoContents
async: boolean
force: boolean
export const Schema = Object.freeze({
account_balance,
- // account_info,
+ account_info,
// account_representative,
accounts_balances,
accounts_frontiers,