]> git.codecow.com Git - libnemo.git/commitdiff
Add documentation for block constructor. Remove signature from constructor and allow...
authorChris Duncan <chris@zoso.dev>
Thu, 7 Aug 2025 18:38:34 +0000 (11:38 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 7 Aug 2025 18:38:34 +0000 (11:38 -0700)
src/lib/block.ts

index 797b30220c9d7ab17b6443332770ffc519b8d58f..44b8be7707a6c352f00901f2c3ce225b5af2f1fc 100644 (file)
@@ -71,8 +71,21 @@ export class Block {
        signature?: string
        work?: string
 
-       constructor (account: string | Account, balance?: bigint | number | string, previous?: string, representative?: string | Account, signature?: string)
-       constructor (account: unknown, balance: unknown, previous: unknown, representative: unknown, signature: unknown) {
+       /**
+       * Initialize a block with the current state of an account so that it can
+       * subsequently be configured as a change, receive, or send transaction.
+       *
+       * All parameters are eventually required in order to initialize the block, but
+       * but if `account` is an Account class object, its properties can be used for
+       * the other parameters instead of passing them into the constructor.
+       *
+       * @param {(string|Account)} account - Target of the transaction; can include `balance`, `frontier`, `representative`
+       * @param {(bigint|number|string)} [balance] - Current balance of the target account
+       * @param {string} [previous] - Current frontier block hash of the target account
+       * @param {(string|Account)} [representative] - Current representative of the target account
+       */
+       constructor (account: string | Account, balance?: bigint | number | string, previous?: string, representative?: string | Account)
+       constructor (account: unknown, balance: unknown, previous: unknown, representative: unknown) {
                try {
                        if (typeof account === 'string') {
                                account = Account.import(account)
@@ -98,9 +111,6 @@ export class Block {
                        if (representative instanceof Account) {
                                this.representative = representative
                        }
-                       if (typeof signature === 'string') {
-                               this.signature = signature
-                       }
                } catch (err) {
                        throw new Error('Failed to initialize Block', { cause: err })
                }
@@ -383,16 +393,22 @@ export class Block {
                }
        }
 
+       /**
+       * Sets the `signature` property of the block to a precalculated value.
+       *
+       * @param {string} [key] - 64-byte hexadecimal signature
+       */
+       async sign (signature: string): Promise<Block>
        /**
        * Signs the block using a private key. If successful, the result is stored in
-       * the object's `signature` property.
+       * the `signature` property of the block.
        *
-       * @param {string} [key] - Hexadecimal-formatted private key to use for signing
+       * @param {string} [key] - 32-byte hexadecimal private key to use for signing
        */
        async sign (key: string): Promise<Block>
        /**
        * Signs the block using a Wallet. If successful, the result is stored in
-       * the object's `signature` property. The wallet must be unlocked prior to
+       * the `signature` property of the block. The wallet must be unlocked prior to
        * signing.
        *
        * @param {Wallet} wallet - Wallet to use for signing
@@ -401,10 +417,9 @@ export class Block {
        async sign (wallet: Wallet, index: number): Promise<Block>
        /**
        * Signs the block using a Ledger hardware wallet. If that fails, an error is
-       * thrown with the status code from the device.
-       *
-       * If successful, the result is stored in the object's `signature`
-       * property.
+       * thrown with the status code from the device. If successful, the result is
+       * stored in the `signature` property of the block. The wallet must be unlocked
+       * prior to signing.
        *
        * @param {number} index - Account index between 0x0 and 0x7fffffff
        * @param {object} [frontier] - JSON of frontier block for offline signing
@@ -416,8 +431,14 @@ export class Block {
                                throw new TypeError('Invalid input')
 
                        } else if (typeof input === 'string') {
-                               const sig = await NanoNaCl.detached(this.hash, hex.toBytes(input))
-                               this.signature = bytes.toHex(sig)
+                               if (/^[A-F0-9]{128}$/.test(input)) {
+                                       this.signature = input
+                               } else if (/^[A-F0-9]{64}$/.test(input)) {
+                                       const sig = await NanoNaCl.detached(this.hash, hex.toBytes(input))
+                                       this.signature = bytes.toHex(sig)
+                               } else {
+                                       throw new TypeError('Invalid signing input')
+                               }
                                return this
 
                        } else if (input instanceof Wallet && typeof param === 'number') {