]> git.codecow.com Git - libnemo.git/commitdiff
Move block validation.
authorChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 17:31:01 +0000 (10:31 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 17:31:01 +0000 (10:31 -0700)
src/lib/block.ts

index 6e51e601fa7e6283e6c533bbee29c2a12de8e9e8..1e38fbf721f9b5fcfa5ecba254af66c7d1156f1e 100644 (file)
@@ -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<string>} Hash of the processed block
        */
        async process (rpc: Rpc): Promise<string> {
-               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')
-               }
-       }
-}