//! SPDX-License-Identifier: GPL-3.0-or-later
import { NanoPow } from 'nano-pow'
-import { derive as nano25519_derive, sign as nano25519_sign, verify as nano25519_verify } from 'nano25519'
+import { derive as nano25519_derive, sign as nano25519_sign, verify as nano25519_verify } from 'nano25519/sync'
import { Account } from './account'
import { BURN_PUBLIC_KEY, DIFFICULTY_RECEIVE, DIFFICULTY_SEND, PREAMBLE, UNITS } from './constants'
import { bytes, dec, hex } from './convert'
import { Wallet } from './wallet'
/**
-* Represents a block as defined by the Nano cryptocurrency protocol.
-*/
+ * Represents a block as defined by the Nano cryptocurrency protocol.
+ */
export class Block {
[key: string]: bigint | string | Account | Function | Uint8Array<ArrayBuffer> | 'send' | 'receive' | 'change' | undefined
+
/**
- * Validates block data.
+ * Validates the format of Block properties.
*
* @param {Block} block - SendBlock, ReceiveBlock, or ChangeBlock to validate
*/
work?: string
/**
- * 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 in raw
- * @param {string} [previous] - Current frontier block hash of the target account
- * @param {(string|Account)} [representative] - Current representative of the target account
- */
+ * 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 in raw
+ * @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 {
}
/**
- * Calculates the block hash using Blake2b.
- *
- * @returns {string} Hexadecimal representation of 32-byte hash of block data
- */
+ * Uses BLAKE2b to generate a 32-byte hash of the block data.
+ *
+ * @returns {string} Hexadecimal representation of hash
+ */
get hash (): string {
try {
if (this.link == null) {
}
/**
- * Converts the block to JSON format as expected by the `process` RPC.
- *
- * @returns {string} JSON representation of the block
- */
+ * Converts the block to JSON format as expected by the `process` RPC.
+ *
+ * @returns {string} JSON representation of the block
+ */
toJSON (): Record<string, string> {
try {
if (this.link == null) {
}
/**
- * Set the subtype, link, and target account to configure this as a change
- * representative block.
- *
- * @param {(string|Account)} account - Account to choose as representative, or its address or public key
- * @returns {Block} This block with link, representative, and subtype configured
- */
+ * Set the subtype, link, and target account to configure this as a change
+ * representative block.
+ *
+ * @param {(string|Account)} account - Account to choose as representative, or its address or public key
+ * @returns {Block} This block with link, representative, and subtype configured
+ */
change (representative: string | Account): Block
change (representative: unknown): Block {
const { link, representative: rep, subtype } = this
}
/**
- * Calculates proof-of-work using a pool of Web Workers.
- *
- * A successful response sets the `work` property.
- */
+ * Calculates proof-of-work using NanoPow.
+ *
+ * @returns {Block} This block with `work` set
+ */
async pow (work?: string): Promise<Block>
async pow (work: unknown): Promise<Block> {
const difficulty: bigint = (this.subtype === 'send' || this.subtype === 'change')
}
/**
- * Sends the block to a node for processing on the network.
- *
- * The block must already be signed (see `sign()` for more information).
- *
- * If the block has no `work` value, `pow()` will be called automatically.
- *
- * @param {Rpc} rpc - RPC node information required to call `process`
- * @returns {Promise<string>} Hash of the processed block
- */
+ * Sends the block to a node for processing on the network.
+ *
+ * The block must already be signed (see `sign()` for more information).
+ *
+ * If the block has no `work` value, `pow()` will be called automatically.
+ *
+ * @param {Rpc} rpc - RPC node information required to call `process`
+ * @returns {Promise<string>} Hash of the processed block
+ */
async process (rpc: Rpc): Promise<string> {
const b: typeof Block = this.constructor as typeof Block
b.validate(this)
}
/**
- * Set the amount of nano that this block will receive from a corresponding
- * send block.
- *
- * @param {(string|Block)} sendBlock - Corresponding send block or its hash
- * @param {(bigint|number|string)} amount - Amount to be received from sender
- * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
- * @returns {Block} This block with balance, link, and subtype configured
+ * Set the amount of nano that this block will receive from a corresponding
+ * send block.
+ *
+ * @param {(string|Block)} sendBlock - Corresponding send block or its hash
+ * @param {(bigint|number|string)} amount - Amount to be received from sender
+ * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
+ * @returns {Block} This block with balance, link, and subtype configured
*/
receive (sendBlock: string | Block, amount: bigint | number | string, unit?: string): Block
receive (sendBlock: unknown, amount: unknown, unit: unknown): Block {
}
/**
- * Set the amount of nano that this block will send to a recipient account.
- *
- * @param {(string|Account)} account - Account to target or its address or public key
- * @param {(bigint|number|string)} amount - Amount to send to recipient
- * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
- * @returns {Block} This block with balance, link, and subtype configured
- */
+ * Set the amount of nano that this block will send to a recipient account.
+ *
+ * @param {(string|Account)} account - Account to target or its address or public key
+ * @param {(bigint|number|string)} amount - Amount to send to recipient
+ * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
+ * @returns {Block} This block with balance, link, and subtype configured
+ */
send (account: string | Account, amount: bigint | number | string, unit?: string): Block
send (account: unknown, amount: unknown, unit: unknown): Block {
const { balance, link, subtype } = this
}
/**
- * Sets the `signature` property of the block to a precalculated value.
- *
- * @param {string} signature - 64-byte hexadecimal signature
- * @returns Block with `signature` value set
- */
+ * Sets the `signature` property of the block to a precalculated value.
+ *
+ * @param {string} signature - 64-byte hexadecimal signature
+ * @returns Block with `signature` value set
+ */
sign (signature: string): Block
/**
- * Signs the block using a private key. If successful, the result is stored in
- * the `signature` property of the block.
- *
- * @param {string} [key] - 32-byte hexadecimal private key to use for signing
- * @returns Block with `signature` value set
- */
+ * Signs the block using a private key. If successful, the result is stored in
+ * the `signature` property of the block.
+ *
+ * @param {string} [key] - 32-byte hexadecimal private key to use for signing
+ * @returns Block with `signature` value set
+ */
async sign (key: string): Promise<Block>
/**
- * Signs the block using a Wallet. If successful, the result is stored in
- * the `signature` property of the block. The wallet must be unlocked prior to
- * signing.
- *
- * @param {Wallet} wallet - Wallet to use for signing
- * @param {number} index - Account in wallet to use for signing
- * @returns Block with `signature` value set
- */
+ * Signs the block using a Wallet. If successful, the result is stored in
+ * the `signature` property of the block. The wallet must be unlocked prior to
+ * signing.
+ *
+ * @param {Wallet} wallet - Wallet to use for signing
+ * @param {number} index - Account in wallet to use for signing
+ * @returns Block with `signature` value set
+ */
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 `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
- * @returns Block with `signature` value set
- */
+ * 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 `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
+ * @returns Block with `signature` value set
+ */
async sign (wallet: Wallet, index: number, frontier?: Block): Promise<Block>
sign (input: unknown, index?: unknown, frontier?: unknown): Block | Promise<Block> {
if (typeof input === 'string' && /^[A-F0-9]{128}$/i.test(input)) {
}
/**
- * Verifies the signature of the block. If a key is not provided, the public
- * key of the block's account will be used if it exists.
- *
- * @param {string} [key] - Hexadecimal-formatted public key to use for verification
- * @returns {boolean} True if block was signed by the matching private key
- */
+ * Verifies the signature of the block. If a key is not provided, the public
+ * key of the block's account will be used if it exists.
+ *
+ * @param {string} [key] - Hexadecimal-formatted public key to use for verification
+ * @returns {boolean} True if block was signed by the matching private key
+ */
async verify (key?: string): Promise<boolean>
async verify (key: unknown): Promise<boolean> {
try {