From: Chris Duncan Date: Mon, 3 Aug 2026 19:37:27 +0000 (-0700) Subject: Rename RPC schema object and fix unit conversion lookups. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=01fe36162850f9b7ee113657db14a6f4ccda602d;p=libnemo.git Rename RPC schema object and fix unit conversion lookups. --- diff --git a/src/lib/account/refresh.ts b/src/lib/account/refresh.ts index 9888674..ae964f3 100644 --- a/src/lib/account/refresh.ts +++ b/src/lib/account/refresh.ts @@ -3,8 +3,7 @@ import { Account } from '.' import { Block } from '../block' -import { SCHEMA } from '../constants' -import { Rpc } from '../rpc' +import { Rpc, Schema } from '../rpc' export async function _refresh (account: Account, rpc: Rpc | string | URL): Promise export async function _refresh (account: Account, rpc: unknown): Promise { @@ -30,7 +29,7 @@ export async function _refresh (account: Account, rpc: unknown): Promise { throw new Error('Account not found') } for (const [k, v] of Object.entries(accountInfo)) { - if (SCHEMA.account_info.fields.includes(k) && typeof v === 'string') { + if (Schema.account_info.fields.includes(k) && typeof v === 'string') { account[k] = v } } @@ -52,7 +51,7 @@ export async function _refresh (account: Account, rpc: unknown): Promise { } const confirmedFrontierContents: Record = {} for (const [k, v] of Object.entries(resConfirmedFrontierBlockInfo.contents)) { - if (SCHEMA.block_info.contents.fields.includes(k) && typeof v === 'string') { + if (Schema.block_info.contents.fields.includes(k) && typeof v === 'string') { confirmedFrontierContents[k] = v } } @@ -80,7 +79,7 @@ export async function _refresh (account: Account, rpc: unknown): Promise { } const frontierContents: Record = {} for (const [k, v] of Object.entries(resFrontierBlockInfo.contents)) { - if (SCHEMA.block_info.contents.fields.includes(k) && typeof v === 'string') { + if (Schema.block_info.contents.fields.includes(k) && typeof v === 'string') { frontierContents[k] = v } } diff --git a/src/lib/block/receive.ts b/src/lib/block/receive.ts index bfb5229..89044cd 100644 --- a/src/lib/block/receive.ts +++ b/src/lib/block/receive.ts @@ -24,7 +24,7 @@ export function _receive (block: Block, sendBlock: unknown, amount: unknown, uni block.subtype = 'receive' unit ??= 'RAW' - if (typeof unit !== 'string' || typeof UNITS[unit] !== 'number') { + if (typeof unit !== 'string' || typeof UNITS[unit as keyof typeof UNITS] !== 'number') { throw new TypeError('Invalid unit') } diff --git a/src/lib/block/send.ts b/src/lib/block/send.ts index 00552af..5d42a76 100644 --- a/src/lib/block/send.ts +++ b/src/lib/block/send.ts @@ -23,7 +23,7 @@ export function _send (block: Block, account: unknown, amount: unknown, unit: un block.subtype = 'send' unit ??= 'RAW' - if (typeof unit !== 'string' || typeof UNITS[unit] !== 'number') { + if (typeof unit !== 'string' || typeof UNITS[unit as keyof typeof UNITS] !== 'number') { throw new TypeError('Invalid unit', { cause: unit }) } diff --git a/src/lib/ledger/index.ts b/src/lib/ledger/index.ts index 9c175cf..032af12 100644 --- a/src/lib/ledger/index.ts +++ b/src/lib/ledger/index.ts @@ -8,8 +8,7 @@ import TransportUSB from '@ledgerhq/hw-transport-webusb' import { Block } from '../block' import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET } from '../constants' import { dec } from '../convert' -import { Rpc } from '../rpc' -import { SCHEMA } from '../rpc/schema' +import { Rpc, Schema } from '../rpc' import { _account } from './account' import { _cache } from './cache' import { _close } from './close' @@ -244,7 +243,7 @@ export class Ledger { } const contents = {} as Record for (const [k, v] of Object.entries(blockInfo.contents)) { - if (SCHEMA.block_info.fields.includes(k)) { + if (Schema.block_info.fields.includes(k)) { contents[k] = v } } diff --git a/src/lib/rpc/index.ts b/src/lib/rpc/index.ts index bb1881f..2d1c1f4 100644 --- a/src/lib/rpc/index.ts +++ b/src/lib/rpc/index.ts @@ -1,6 +1,8 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { SCHEMA } from './schema' +import { Schema } from './schema' + +export { Schema } /** * Represents a Nano network node. It primarily consists of a URL which will @@ -91,7 +93,7 @@ export class Rpc { if (!/^[A-Za-z]+(_[A-Za-z]+)*$/.test(action)) { throw new TypeError('RPC action contains invalid characters') } - if (!(action in SCHEMA)) { + if (!(action in Schema)) { throw new TypeError('Unknown RPC action') } } diff --git a/src/lib/rpc/schema.ts b/src/lib/rpc/schema.ts index 6a7cf51..7af2f2e 100644 --- a/src/lib/rpc/schema.ts +++ b/src/lib/rpc/schema.ts @@ -3,7 +3,7 @@ import { account_info } from './account_info' import { block_info } from './block_info' -export const SCHEMA = Object.freeze({ +export const Schema = Object.freeze({ account_info, block_info, })