]> git.codecow.com Git - libnemo.git/commitdiff
Expand options for block constructor arguments, using block account as fallback.
authorChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 17:52:40 +0000 (10:52 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 17:52:40 +0000 (10:52 -0700)
src/lib/block.ts

index 7258ef1ca3f788cf9aad52a5356768f72717cbc9..bce9e1947d0bdbc0e1f49f4b7ff82e5e26329944 100644 (file)
@@ -73,24 +73,33 @@ class Block {
        signature?: string
        work?: string
 
-       constructor (account: Account)
-       constructor (account: unknown) {
+       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')
                }
-               if (account.balance == null) {
+               balance ??= account.balance
+               previous ??= account.frontier
+               representative ??= account.representative
+               if (typeof balance !== 'bigint' && typeof balance !== 'string') {
                        throw new TypeError('Account balance is unknown')
                }
-               if (account.frontier == null) {
+               if (typeof previous !== 'string') {
                        throw new TypeError('Account frontier is unknown')
                }
-               if (account.representative == null) {
+               if (!(representative instanceof Account)) {
                        throw new TypeError('Account representative is unknown')
                }
                this.account = account
-               this.balance = account.balance
-               this.previous = account.frontier
-               this.representative = account.representative
+               this.balance = BigInt(balance)
+               this.previous = previous
+               this.representative = representative
        }
 
        /**