]> git.codecow.com Git - libnemo.git/commitdiff
Reenable signature and work as block constructor args. Adjust error handling.
authorChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 17:55:15 +0000 (10:55 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 17:55:15 +0000 (10:55 -0700)
src/lib/block.ts

index bce9e1947d0bdbc0e1f49f4b7ff82e5e26329944..19dde64500da037dcbd35b7bd2a0dee947a2d34d 100644 (file)
@@ -73,33 +73,43 @@ class Block {
        signature?: string
        work?: string
 
-       constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account)
-       constructor (account: unknown, balance: unknown, previous: unknown, representative: unknown) {
-               if (typeof account === 'string') {
-                       account = Account.import(account)
-               }
-               if (typeof representative === 'string') {
-                       representative = Account.import(representative)
-               }
-               if (!(account instanceof Account)) {
-                       throw new TypeError('Invalid account')
-               }
-               balance ??= account.balance
-               previous ??= account.frontier
-               representative ??= account.representative
-               if (typeof balance !== 'bigint' && typeof balance !== 'string') {
-                       throw new TypeError('Account balance is unknown')
-               }
-               if (typeof previous !== 'string') {
-                       throw new TypeError('Account frontier is unknown')
-               }
-               if (!(representative instanceof Account)) {
-                       throw new TypeError('Account representative is unknown')
+       constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account, signature?: string, work?: string)
+       constructor (account: unknown, balance: unknown, previous: unknown, representative: unknown, signature: unknown, work: unknown) {
+               try {
+                       if (typeof account === 'string') {
+                               account = Account.import(account)
+                       }
+                       if (typeof representative === 'string') {
+                               representative = Account.import(representative)
+                       }
+                       if (!(account instanceof Account)) {
+                               throw new TypeError('Invalid account')
+                       }
+                       balance ??= account.balance
+                       previous ??= account.frontier
+                       representative ??= account.representative
+                       if (typeof balance !== 'bigint' && typeof balance !== 'string') {
+                               throw new TypeError('Account balance is unknown')
+                       }
+                       if (typeof previous !== 'string') {
+                               throw new TypeError('Account frontier is unknown')
+                       }
+                       if (!(representative instanceof Account)) {
+                               throw new TypeError('Account representative is unknown')
+                       }
+                       this.account = account
+                       this.balance = BigInt(balance)
+                       this.previous = previous
+                       this.representative = representative
+                       if (typeof signature === 'string') {
+                               this.signature = signature
+                       }
+                       if (typeof work === 'string') {
+                               this.work = work
+                       }
+               } catch (err) {
+                       throw new Error('Failed to initialize Block', { cause: err })
                }
-               this.account = account
-               this.balance = BigInt(balance)
-               this.previous = previous
-               this.representative = representative
        }
 
        /**