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)
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 })
}
}
}
+ /**
+ * 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
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
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') {