UNPKG

libnemo

Version:

Nano cryptocurrency wallet library.

139 lines (138 loc) 6.03 kB
import { Account } from './account'; import { Rpc } from './rpc'; import { Wallet } from './wallet'; /** * Represents a block as defined by the Nano cryptocurrency protocol. */ export declare class Block { [key: string]: bigint | string | Account | Function | Uint8Array<ArrayBuffer> | 'send' | 'receive' | 'change' | undefined; /** * Validates block data. * * @param {Block} block - SendBlock, ReceiveBlock, or ChangeBlock to validate */ static validate(block: unknown): asserts block is Block; subtype?: 'send' | 'receive' | 'change'; account: Account; balance: bigint; previous: Uint8Array<ArrayBuffer>; representative?: Account; link?: Uint8Array<ArrayBuffer>; signature?: string; 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 */ constructor(account: string | Account, balance?: bigint | number | string, previous?: string, representative?: string | Account); /** * Calculates the block hash using Blake2b. * * @returns {string} Hexadecimal representation of 32-byte hash of block data */ get hash(): string; /** * Converts the block to JSON format as expected by the `process` RPC. * * @returns {string} JSON representation of the block */ toJSON(): { [key: string]: string; }; /** * 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; /** * Calculates proof-of-work using a pool of Web Workers. * * A successful response sets the `work` property. */ pow(work?: string): Promise<Block>; /** * Sends the block to a node for processing on the network. * * The block must already be signed (see `sign()` for more information). * The block must also have a `work` value. * * @param {Rpc} rpc - RPC node information required to call `process` * @returns {Promise<string>} Hash of the processed block */ process(rpc: Rpc): Promise<string>; /** * 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; /** * 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; /** * 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 */ 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 */ 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 */ sign(wallet: Wallet, index: number, frontier?: Block): Promise<Block>; /** * 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 */ verify(key?: string): Promise<boolean>; }