Amend Primitives/Tooling
This commit is contained in:
@@ -24,7 +24,7 @@ export class BlockHeader {
|
||||
* @param bits - Compact representation of the target threshold
|
||||
* @param nonce - Value modified during mining to make the block hash valid
|
||||
*/
|
||||
constructor(
|
||||
protected constructor(
|
||||
private readonly version: number,
|
||||
private readonly previousBlockHash: Uint8Array,
|
||||
private readonly merkleRoot: Uint8Array,
|
||||
@@ -33,6 +33,24 @@ export class BlockHeader {
|
||||
private readonly nonce: number,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Instantiates a blockheader from either:
|
||||
*
|
||||
* 1. Transaction bytecode
|
||||
* 2. Transaction bytecode (in hexadecimal format)
|
||||
*
|
||||
* @param blockHeader The block header in one of the above formats.
|
||||
*
|
||||
* @throws {Error} If the block header cannot be decoded.
|
||||
*
|
||||
* @returns The created BlockHeader Primitive.
|
||||
*/
|
||||
public static from(blockHeader: Uint8Array | string) {
|
||||
return blockHeader instanceof Uint8Array
|
||||
? BlockHeader.fromBytes(blockHeader)
|
||||
: BlockHeader.fromHex(blockHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a BlockHeader instance from a byte array.
|
||||
* @param bytes - 80-byte array containing the serialized block header
|
||||
@@ -72,6 +90,9 @@ export class BlockHeader {
|
||||
|
||||
/**
|
||||
* Creates a BlockHeader instance from a raw object representation.
|
||||
*
|
||||
* @remarks This method is UNSAFE and MUST only be used where input is already verified or trusted.
|
||||
*
|
||||
* @param obj - Object containing the block header fields
|
||||
* @returns A new BlockHeader instance
|
||||
*/
|
||||
@@ -86,6 +107,23 @@ export class BlockHeader {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given BlochHeader is of a valid structure.
|
||||
*
|
||||
* @param blockHeader The blockheader as hex or a Uint8Array.
|
||||
*
|
||||
* @returns boolean indicating whether the blockheader is of a valid structure.
|
||||
*/
|
||||
public static isValid(blockHeader: Uint8Array | string): boolean {
|
||||
try {
|
||||
BlockHeader.from(blockHeader);
|
||||
} catch (_error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes the block header to a byte array.
|
||||
* @returns 80-byte array containing the serialized block header
|
||||
@@ -106,7 +144,17 @@ export class BlockHeader {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the block header to a raw object representation.
|
||||
* Converts the block header to a hex string.
|
||||
*
|
||||
* @returns 160-character hex string representing the block header
|
||||
*/
|
||||
toHex(): string {
|
||||
return binToHex(this.toBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the block header to a serializable raw object representation.
|
||||
*
|
||||
* @returns Object containing the block header fields
|
||||
*/
|
||||
toRaw(): BlockHeaderRaw {
|
||||
@@ -120,14 +168,6 @@ export class BlockHeader {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the block header to a hex string.
|
||||
* @returns 160-character hex string representing the block header
|
||||
*/
|
||||
toHex(): string {
|
||||
return binToHex(this.toBytes());
|
||||
}
|
||||
|
||||
// Getters
|
||||
getVersion(): number {
|
||||
return this.version;
|
||||
|
||||
Reference in New Issue
Block a user