From 66dc70993d373cecd9a6b38da2c8f1876dfd92d8 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 6 Aug 2025 10:31:01 -0700 Subject: [PATCH] Move block validation. --- src/lib/block.ts | 98 ++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/src/lib/block.ts b/src/lib/block.ts index 6e51e60..1e38fbf 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -16,6 +16,54 @@ import { Wallet } from './wallet' * of three derived classes: SendBlock, ReceiveBlock, ChangeBlock. */ class Block { + /** + * Validates block data. + * + * @param {Block} block - SendBlock, ReceiveBlock, or ChangeBlock to validate + */ + static #validate (block: unknown): asserts block is Block { + if (typeof block !== 'object') { + throw new TypeError('Invalid block') + } + const b = block as { [key: string]: unknown } + if (b.account == null) { + throw new Error('Account missing') + } + if (b.previous == null || b.previous === '') { + throw new Error('Frontier missing') + } + if (b.representative == null) { + throw new Error('Representative missing') + } + if (b.balance == null) { + throw new Error('Balance missing') + } + if (typeof b.balance !== 'number' && typeof b.balance !== 'bigint') { + throw new TypeError('Balance must be number or bigint') + } + if (b.balance < 0) { + throw new Error('Negative balance') + } + if (b.subtype === 'send') { + if (b.link == null || b.link === '') { + throw new Error('Recipient missing') + } + } + if (b.subtype === 'receive') { + if (b.link == null) { + throw new Error('Origin send block hash missing') + } + } + if (b.subtype === 'change') { + if (b.link == null) { + throw new Error('Change block link missing') + } + if (+(b.link) !== 0) { + throw new Error('Invalid change block link') + } + } + } + subtype?: 'send' | 'receive' | 'change' account: Account balance: bigint @@ -183,7 +231,7 @@ class Block { * @returns {Promise} Hash of the processed block */ async process (rpc: Rpc): Promise { - validate(this) + Block.#validate(this) if (!this.signature) { throw new Error('Block is missing signature. Use sign() and try again.') } @@ -441,51 +489,3 @@ class Block { } } } - -/** - * Validates block data. - * - * @param {Block} block - SendBlock, ReceiveBlock, or ChangeBlock to validate - */ -function validate (block: unknown): asserts block is Block { - if (typeof block !== 'object') { - throw new TypeError('Invalid block') - } - const b = block as { [key: string]: unknown } - if (b.account == null) { - throw new Error('Account missing') - } - if (b.previous == null || b.previous === '') { - throw new Error('Frontier missing') - } - if (b.representative == null) { - throw new Error('Representative missing') - } - if (b.balance == null) { - throw new Error('Balance missing') - } - if (typeof b.balance !== 'number' && typeof b.balance !== 'bigint') { - throw new TypeError('Balance must be number or bigint') - } - if (b.balance < 0) { - throw new Error('Negative balance') - } - if (b.subtype === 'send') { - if (b.link == null || b.link === '') { - throw new Error('Recipient missing') - } - } - if (b.subtype === 'receive') { - if (b.link == null) { - throw new Error('Origin send block hash missing') - } - } - if (b.subtype === 'change') { - if (b.link == null) { - throw new Error('Change block link missing') - } - if (+(b.link) !== 0) { - throw new Error('Invalid change block link') - } - } -} -- 2.47.3