UNPKG

casper-js-sdk

Version:
509 lines (508 loc) 18.6 kB
import { Hash } from './key'; import { Timestamp } from './Time'; import { Proposer } from './BlockProposer'; import { EraEnd, EraEndV1, EraEndV2 } from './EraEnd'; import { TransactionCategory, TransactionVersion } from './Transaction'; import { PublicKey } from './keypair'; import { HexBytes } from './HexBytes'; /** * Represents a proof containing a public key and a signature, used for validating the authenticity of data. */ export declare class Proof { /** * The public key associated with the proof. * This key is used to verify the signature's validity. */ publicKey: PublicKey; /** * The signature generated by the associated public key. * The signature proves the authenticity of the data or transaction. */ signature: HexBytes; } /** * Represents a block in the blockchain containing metadata, transactions, and proof. * A block includes information like the hash, height, parent block, transactions, and more. */ export declare class Block { /** * The unique hash of the block. */ hash: Hash; /** * The height of the block in the blockchain. */ height: number; /** * The hash of the state root associated with the block. */ stateRootHash: Hash; /** * The hash of the block that switched the current state, if available. * This can be `null` if not applicable. */ lastSwitchBlockHash: Hash | null; /** * The hash of the parent block in the blockchain. */ parentHash: Hash; /** * The era ID in which this block was created. */ eraID: number; /** * The timestamp indicating when the block was created. */ timestamp: Timestamp; /** * The accumulated seed for randomness in the block generation. */ accumulatedSeed?: Hash; /** * A random bit used for proof-of-stake consensus. */ randomBit: boolean; /** * The current gas price for transactions included in the block. */ currentGasPrice: number; /** * The proposer who created this block. */ proposer: Proposer; /** * The protocol version used for this block. */ protocolVersion?: string; /** * The era end details, if available, for the block's era. */ eraEnd?: EraEnd; /** * The list of transactions included in this block. */ transactions: BlockTransaction[]; /** * A list of signature IDs that were rewarded in this block. */ rewardedSignatures: number[][]; /** * A list of proofs associated with this block. */ proofs: Proof[]; /** * The origin block in V1 format, if available. */ originBlockV1?: BlockV1; /** * The origin block in V2 format, if available. */ originBlockV2?: BlockV2; /** * Constructs a new `Block` instance. * @param hash - The unique hash of the block. * @param height - The height of the block in the blockchain. * @param stateRootHash - The hash of the state root for the block. * @param lastSwitchBlockHash - The hash of the block that switched the state, if available. * @param parentHash - The hash of the parent block. * @param eraID - The era ID in which the block was created. * @param timestamp - The timestamp of the block creation. * @param accumulatedSeed - The accumulated seed for randomness in the block. * @param randomBit - A random bit used for consensus. * @param currentGasPrice - The current gas price for transactions in the block. * @param proposer - The proposer of the block. * @param protocolVersion - The protocol version of the block. * @param eraEnd - The era end details for the block's era, if available. * @param transactions - The list of transactions included in the block. * @param rewardedSignatures - The list of rewarded signatures. * @param proofs - The list of proofs associated with the block. * @param originBlockV1 - The origin block in V1 format, if available. * @param originBlockV2 - The origin block in V2 format, if available. */ constructor(hash: Hash, height: number, stateRootHash: Hash, lastSwitchBlockHash: Hash | null, parentHash: Hash, eraID: number, timestamp: Timestamp, accumulatedSeed: Hash | undefined, randomBit: boolean, currentGasPrice: number, proposer: Proposer, protocolVersion: string | undefined, eraEnd: EraEnd | undefined, transactions: BlockTransaction[], rewardedSignatures: number[][], proofs: Proof[], originBlockV1?: BlockV1, originBlockV2?: BlockV2); /** * Retrieves the V1 format of the origin block, if available. * @returns The origin block in V1 format, or `undefined` if not available. */ getBlockV1(): BlockV1 | undefined; /** * Retrieves the V2 format of the origin block, if available. * @returns The origin block in V2 format, or `undefined` if not available. */ getBlockV2(): BlockV2 | undefined; /** * Creates a new `Block` instance from a `BlockWrapper` object, which may contain either V1 or V2 block format. * @param blockWrapper - The `BlockWrapper` containing either `blockV1` or `blockV2`. * @param proofs - The list of proofs associated with the block. * @returns A new `Block` instance based on the provided `blockWrapper` and `proofs`. * @throws Will throw an error if the `blockWrapper` does not contain a valid `blockV1` or `blockV2`. */ static newBlockFromBlockWrapper(blockWrapper: BlockWrapper, proofs: Proof[]): Block; /** * Creates a new `Block` instance from a V1 block format. * @param blockV1 - The V1 block format. * @returns A new `Block` instance created from the provided V1 block format. */ static newBlockFromBlockV1(blockV1: BlockV1): Block; } /** * Represents a transaction within a block, identified by a category, version, and hash. * Each `BlockTransaction` stores the category of the transaction, its version, and a unique hash. */ export declare class BlockTransaction { /** * The category of the transaction, indicating the type of transaction. * This could be one of the predefined categories such as Mint, Auction, InstallUpgrade, etc. */ category: TransactionCategory; /** * The version of the transaction, specifying the version of the transaction protocol. */ version: TransactionVersion; /** * The unique hash identifying the transaction. * This hash serves as a reference to the transaction. */ hash: Hash; /** * Constructs a new `BlockTransaction` instance. * @param category - The category of the transaction (e.g., Mint, Auction). * @param version - The version of the transaction protocol. * @param hash - The unique hash identifying the transaction. */ constructor(category: TransactionCategory, version: TransactionVersion, hash: Hash); /** * Deserializes a JSON object into an array of `BlockTransaction` instances. * The input `data` is expected to contain transaction hashes categorized by their types. * * @param data - The serialized data representing transactions categorized by type. * @returns An array of `BlockTransaction` instances. * * @example * const jsonData = { '0': ['hash1', 'hash2'], '1': ['hash3'] }; * const transactions = BlockTransaction.fromJSON(jsonData); * console.log(transactions); // Outputs an array of BlockTransaction instances. */ static fromJSON(data: any): BlockTransaction[]; toJSON(): string; } /** * Parses a JSON string representing block transactions and converts them into an array of `BlockTransaction` instances. * The input data is expected to be a JSON object with categories (e.g., '0', '1', etc.) mapping to arrays of `TransactionHash` objects. * Each category corresponds to a specific type of transaction, such as Mint, Auction, etc. * * @param data - A JSON string containing transaction categories as keys ('0', '1', etc.), with each key pointing to an array of `TransactionHash` objects. * @returns An array of `BlockTransaction` instances representing the transactions in the block. * * @throws {Error} If the input `data` is not valid JSON or does not conform to the expected structure. * * @example * const jsonData = '{"0": [{"transactionV1": "hash1"}], "1": [{"transactionV1": "hash2"}]}'; * const transactions = parseBlockTransactions(jsonData); * console.log(transactions); // Outputs an array of BlockTransaction instances for Mint and Auction transactions. */ export declare function parseBlockTransactions(data: string): BlockTransaction[]; /** * Represents the header of a version 1 block in the blockchain. * The header contains metadata about the block, such as its height, parent hash, protocol version, and timestamps. */ export declare class BlockHeaderV1 { /** * The accumulated seed used for randomness in block generation. * This seed is generated from previous blocks and influences the consensus. */ accumulatedSeed?: Hash; /** * The hash of the block's body, which contains the transactions and other block data. * This is used to verify the integrity of the block's contents. */ bodyHash: Hash; /** * The era ID in which this block was created. * The era ID represents the block's position within a specific era in the blockchain. */ eraID: number; /** * The height of the block in the blockchain. * The block height indicates its position relative to the first block in the chain (genesis block). */ height: number; /** * The hash of the parent block, linking this block to its predecessor in the chain. * The parent hash is used to verify the continuity of the blockchain. */ parentHash: Hash; /** * The protocol version used for this block, indicating the version of the consensus rules. */ protocolVersion?: string; /** * A random bit used for consensus, which may play a role in the block's selection or generation. */ randomBit: boolean; /** * The hash of the state root, representing the state of the blockchain after this block is applied. * The state root hash is used to verify the consistency of the blockchain's state. */ stateRootHash: Hash; /** * The timestamp indicating when the block was created. * This timestamp helps to determine the block's position in time within the blockchain. */ timestamp: Timestamp; /** * The era end details for this block's era, if available. * The `eraEnd` may include information about the transition from one era to another. */ eraEnd?: EraEndV1; } /** * Represents the body of a version 1 block in the blockchain. * The body contains essential data related to transactions, deploys, and other actions within the block. */ export declare class BlockBodyV1 { /** * The list of deploy hashes included in the block. * Deploys represent the transactions that are being executed within this block. */ deployHashes: Hash[]; /** * The proposer of the block, which represents the entity that created or proposed the block. */ proposer: Proposer; /** * The list of transfer hashes included in the block. * Transfers represent the transactions related to the movement of tokens within the blockchain. */ transferHashes: Hash[]; } /** * Represents a version 1 block in the blockchain, including the block's metadata (header), body, and proofs. * A `BlockV1` contains the block's unique hash, header (which includes metadata like height and era ID), body (which includes transactions), * and an array of proofs related to the block. */ export declare class BlockV1 { /** * The unique hash of the block, used to identify and verify the block in the blockchain. */ hash: Hash; /** * The header of the block, containing metadata such as block height, era ID, and timestamp. */ header: BlockHeaderV1; /** * The body of the block, which contains the transactions and related data for the block. */ body: BlockBodyV1; /** * An array of proofs associated with the block, used to verify the authenticity of the block. */ proofs: Proof[]; } /** * Represents the header of a version 2 block in the blockchain. * Contains metadata such as the block's height, parent hash, timestamp, gas price, and more. */ export declare class BlockHeaderV2 { /** * The accumulated seed used for randomness in block generation. * This seed is generated from previous blocks and influences the consensus. */ accumulatedSeed?: Hash; /** * The hash of the block's body, which contains the transactions and other data. * This is used to verify the integrity of the block's contents. */ bodyHash: Hash; /** * The era ID in which the block was created, indicating its position within a blockchain era. */ eraID: number; /** * The current gas price for transactions within this block. */ currentGasPrice: number; /** * The height of the block in the blockchain, representing its position in the chain. */ height: number; /** * The hash of the parent block, linking this block to its predecessor in the chain. */ parentHash: Hash; /** * The proposer who created this block. */ proposer: Proposer; /** * The protocol version used for this block. */ protocolVersion?: string; /** * A random bit used for consensus, which may play a role in block selection or generation. */ randomBit: boolean; /** * The hash of the state root, representing the state of the blockchain after this block is applied. */ stateRootHash: Hash; /** * The hash of the block that switched the state, marking the end of an era or other significant event. */ lastSwitchBlockHash: Hash; /** * The timestamp when the block was created. */ timestamp: Timestamp; /** * The era end details for this block's era, if available. */ eraEnd?: EraEndV2; } /** * Represents the body of a version 2 block, including the transactions and rewarded signatures. */ export declare class BlockBodyV2 { /** * The list of transactions included in the block. */ transactions: BlockTransaction[]; /** * The list of signature IDs that were rewarded in this block. */ rewardedSignatures: number[][]; } /** * Represents a version 2 block in the blockchain. * A `BlockV2` includes a hash, a header containing metadata, and the body containing the block's transactions and related data. */ export declare class BlockV2 { /** * The unique hash of the block, used to identify and verify the block in the blockchain. */ hash: Hash; /** * The header of the block, which contains metadata such as the block's height, parent hash, and protocol version. */ header: BlockHeaderV2; /** * The body of the block, which contains the transactions, deploys, and other data related to the block's content. */ body: BlockBodyV2; } /** * A wrapper for blocks that can contain either a version 1 or version 2 block. */ export declare class BlockWrapper { /** * A version 1 block, if available. */ blockV1?: BlockV1; /** * A version 2 block, if available. */ blockV2?: BlockV2; } /** * A block with associated proofs, which include cryptographic signatures for verification. */ export declare class BlockWithSignatures { /** * The block object containing either a version 1 or version 2 block. */ block: BlockWrapper; /** * The list of proofs associated with the block, used for validating its authenticity. */ proofs: Proof[]; } /** * A wrapper for block headers, which can contain either a version 1 or version 2 block header. */ export declare class BlockHeaderWrapper { /** * A version 1 block header, if available. */ blockHeaderV1?: BlockHeaderV1; /** * A version 2 block header, if available. */ blockHeaderV2?: BlockHeaderV2; } /** * Represents a block header that can be either version 1 or version 2, with various metadata about the block. */ export declare class BlockHeader { /** * The accumulated seed used for randomness in block generation. */ accumulatedSeed?: Hash; /** * The hash of the block's body, used to verify the block's contents. */ bodyHash: Hash; /** * The era ID in which the block was created. */ eraID: number; /** * The current gas price for transactions within this block. */ currentGasPrice: number; /** * The height of the block in the blockchain. */ height: number; /** * The hash of the parent block. */ parentHash: Hash; /** * The proposer who created this block. */ proposer: Proposer; /** * The protocol version used for this block. */ protocolVersion?: string; /** * A random bit used for consensus. */ randomBit: boolean; /** * The hash of the state root. */ stateRootHash: Hash; /** * The timestamp indicating when the block was created. */ timestamp: Timestamp; /** * The era end details, if available, for this block's era. */ eraEnd?: EraEnd | null; private originBlockHeaderV1?; private originBlockHeaderV2?; /** * Retrieves the version 1 block header, if available. * @returns The version 1 block header, or `undefined` if not available. */ getBlockHeaderV1(): BlockHeaderV1 | undefined; /** * Retrieves the version 2 block header, if available. * @returns The version 2 block header, or `undefined` if not available. */ getBlockHeaderV2(): BlockHeaderV2 | undefined; /** * Creates a new `BlockHeader` instance from a version 1 block header. * @param header - The version 1 block header. * @returns A new `BlockHeader` instance. */ static newBlockHeaderFromV1(header: BlockHeaderV1): BlockHeader; /** * Creates a new `BlockHeader` instance from a version 2 block header. * @param header - The version 2 block header. * @returns A new `BlockHeader` instance. */ static newBlockHeaderFromV2(header: BlockHeaderV2): BlockHeader; }