* @param {unknown} address - Nano address to validate\r
* @throws Error if address is undefined, not a string, or an invalid format\r
*/\r
- static validate (address: unknown): asserts address is string {\r
+ static isValid (address: unknown): address is string {\r
return _validate(address)\r
}\r
}\r
const pattern = new RegExp(`^(${PREFIX}|${PREFIX_LEGACY})[13][${ALPHABET}]{59}$`)
-export function _validate (address: unknown): asserts address is string {
+export function _validate (address: unknown): address is string {
if (address === undefined) {
throw new ReferenceError('Address is undefined.')
}
actualChecksumBuf.reverse()
const actualChecksum = bytes.toBase32(actualChecksumBuf)
- if (expectedChecksum !== actualChecksum) {
- throw new Error('Incorrect address checksum')
- }
+ return expectedChecksum === actualChecksum
}
if ('errors' in body && body.errors != null && typeof body.errors === 'object') {
for (const [address, error] of Object.entries(body.errors) as [string, unknown][]) {
try {
- Account.validate(address)
+ if (!Account.isValid(address)) {
+ throw new RpcError('Invalid account address')
+ }
if (typeof error === 'string') {
- throw new Error(error)
+ throw new RpcError(error)
}
} catch (err: any) {
response.errors ??= {}
if ('balances' in body && body.balances != null && typeof body.balances === 'object') {
for (const [address, data] of Object.entries(body.balances) as [string, unknown][]) {
try {
- Account.validate(address)
+ if (!Account.isValid(address)) {
+ throw new RpcError('Invalid account address')
+ }
if (data == null || typeof data !== 'object') {
- throw new Error('Invalid account balance data')
+ throw new RpcError('Invalid account balance data')
}
response.balances ??= {}
response.balances[address] = account_balance(data)
import { Account } from '../account'
import { hex } from '../convert'
+import { RpcError } from '../errors'
-type AccountsFrontiers = {
+export type AccountsFrontiersResponse = {
frontiers?: {
[address: string]: Hex
}
}
}
-export function accounts_frontiers (body: unknown): AccountsFrontiers {
- const response: AccountsFrontiers = {}
+export function accounts_frontiers (body: unknown): AccountsFrontiersResponse {
+ const response: AccountsFrontiersResponse = {}
if (body == null || typeof body !== 'object') {
return response
}
if (typeof frontiers === 'object') {
for (const [address, frontier] of Object.entries(frontiers) as [string, unknown][]) {
try {
- Account.validate(address)
+ if (!Account.isValid(address)) {
+ throw new RpcError('Invalid account address')
+ }
if (hex.is(frontier, 64)) {
response.frontiers ??= {}
response.frontiers[address] = frontier
if (typeof errors === 'object') {
for (const [address, error] of Object.entries(errors) as [string, unknown][]) {
try {
- Account.validate(address)
+ if (!Account.isValid(address)) {
+ throw new RpcError('Invalid account address')
+ }
if (typeof error === 'string') {
throw new Error(error)
}
&& 'account' in body.contents && typeof body.contents.account === 'string'
&& 'balance' in body.contents && typeof body.contents.balance === 'string'
&& 'link' in body.contents && hex.is(body.contents.link, 64)
- && 'link_as_account' in body.contents && Account.validate(body.contents.link_as_account)
+ && 'link_as_account' in body.contents && Account.isValid(body.contents.link_as_account)
&& 'previous' in body.contents && hex.is(body.contents.previous, 64)
- && 'representative' in body.contents && typeof body.contents.representative === 'string'
+ && 'representative' in body.contents && Account.isValid(body.contents.representative)
&& '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'
import { RpcError } from '../errors'
import { BlockInfoResponse, block_info } from './block_info'
-type BlocksInfoResponse = {
+export type BlocksInfoResponse = {
blocks?: {
[hash: Hex]: BlockInfoResponse
}
import { Block } from '../block'
import { Rpc } from '../rpc'
import { AccountInfoRequest, AccountInfoResponse } from '../rpc/account_info'
+import { AccountsFrontiersResponse } from '../rpc/accounts_frontiers'
+import { BlocksInfoResponse } from '../rpc/blocks_info'
import { Wallet } from '../wallet'
export async function _refresh (wallet: Wallet, rpc: Rpc | string | URL, from: number, to: number): Promise<Map<number, Account>>
json_block: true,
accounts: addresses
}
- const { frontiers } = await rpc.post('accounts_frontiers', data)
- const { blocks } = await rpc.post('blocks_info', { json_block: true, include_not_found: true, hashes: Object.values(frontiers ?? {}) })
+ const { frontiers }: AccountsFrontiersResponse = await rpc.post('accounts_frontiers', data)
+ const { blocks }: BlocksInfoResponse = await rpc.post('blocks_info', { json_block: true, include_not_found: true, hashes: Object.values(frontiers ?? {}) })
for (const account of accounts.values()) {
const reqAccountInfo: AccountInfoRequest = {
if (typeof frontierBlock[subtype] !== 'function') {
throw new TypeError('Unknown frontier block subtype', { cause: subtype })
}
- const arg = subtype === 'epoch' ? account.version : subtype === 'change' ? representative : link
+ const arg = subtype === 'epoch' ? account_version : subtype === 'change' ? representative : link
frontierBlock[subtype](arg, 0).signature = signature
account.frontier_block = frontierBlock
}