From: Chris Duncan Date: Thu, 7 Aug 2025 18:38:34 +0000 (-0700) Subject: Add documentation for block constructor. Remove signature from constructor and allow... X-Git-Tag: v0.10.5~43^2~56 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=87ddf87d6bb1d09b49e6a0416ae2587ff5d45f5c;p=libnemo.git Add documentation for block constructor. Remove signature from constructor and allow literal signature string to be passed to sign method. --- diff --git a/src/lib/block.ts b/src/lib/block.ts index 797b302..44b8be7 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -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 /** * 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 /** * 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 /** * 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') {