This commit is contained in:
Jim Hamill
2025-02-02 06:21:25 +00:00
parent 2def0383ec
commit 2e4e6a2761
5 changed files with 94 additions and 156 deletions
+57 -71
View File
@@ -2,15 +2,6 @@ import { Bytes } from './bytes.js';
import { hash256 } from '@bitauth/libauth';
export interface BlockHeaderRaw {
version: number;
previousBlockHash: Uint8Array;
merkleRoot: Uint8Array;
timestamp: number;
bits: number;
nonce: number;
}
/**
* Represents a Bitcoin Cash block header.
* This class handles parsing and serialization of BCH block headers between different formats
@@ -32,23 +23,16 @@ export interface BlockHeaderRaw {
* ```
*/
export class BlockHeader {
// View of our data so that we can extract as numbers.
private view: DataView;
/**
* Creates a new BlockHeader instance.
* @param version - The block version
* @param previousBlockHash - Hash of the previous block (32 bytes)
* @param merkleRoot - Merkle root of the block's transactions (32 bytes)
* @param timestamp - Block timestamp in seconds since Unix epoch
* @param bits - Compact representation of the target threshold
* @param nonce - Value modified during mining to make the block hash valid
* @param bytes The bytes representing our Block Header (Big Endian)
*/
protected constructor(
private readonly version: number,
private readonly previousBlockHash: Uint8Array,
private readonly merkleRoot: Uint8Array,
private readonly timestamp: number,
private readonly bits: number,
private readonly nonce: number,
) {}
protected constructor(private readonly bytes: Uint8Array) {
this.view = new DataView(bytes.buffer);
}
/**
* Instantiates a blockheader from either:
@@ -79,15 +63,7 @@ export class BlockHeader {
throw new Error('Invalid block header length');
}
const view = new DataView(bytes.buffer);
return new BlockHeader(
view.getInt32(0, true),
bytes.slice(4, 36),
bytes.slice(36, 68),
view.getUint32(68, true),
view.getUint32(72, true),
view.getUint32(76, true),
);
return new BlockHeader(bytes);
}
/**
@@ -108,15 +84,8 @@ export class BlockHeader {
* @param obj - Object containing the block header fields
* @returns A new BlockHeader instance
*/
static fromRaw(obj: BlockHeaderRaw): BlockHeader {
return new this(
obj.version,
obj.previousBlockHash,
obj.merkleRoot,
obj.timestamp,
obj.bits,
obj.nonce,
);
static fromRaw(bytes: Uint8Array): BlockHeader {
return new this(bytes);
}
/**
@@ -141,18 +110,7 @@ export class BlockHeader {
* @returns 80-byte array containing the serialized block header
*/
toBytes(): Uint8Array {
const buffer = new ArrayBuffer(80);
const view = new DataView(buffer);
const bytes = new Uint8Array(buffer);
view.setInt32(0, this.version, true);
bytes.set(this.previousBlockHash, 4);
bytes.set(this.merkleRoot, 36);
view.setUint32(68, this.timestamp, true);
view.setUint32(72, this.bits, true);
view.setUint32(76, this.nonce, true);
return bytes;
return this.bytes;
}
/**
@@ -169,35 +127,63 @@ export class BlockHeader {
*
* @returns Object containing the block header fields
*/
toRaw(): BlockHeaderRaw {
return {
version: this.version,
previousBlockHash: this.previousBlockHash,
merkleRoot: this.merkleRoot,
timestamp: this.timestamp,
bits: this.bits,
nonce: this.nonce,
};
toRaw() {
return this.bytes;
}
// Getters
getVersion(): number {
return this.version;
return this.view.getUint32(0, true);
}
getVersionBytes(): Uint8Array {
return this.bytes.slice(0, 4).toReversed();
}
getVersionHex(): string {
return Bytes.fromBytes(this.getVersionBytes()).toHex();
}
getPreviousBlockHash(): Uint8Array {
return this.previousBlockHash;
// NOTE: We want to return this as a Big Endian, so we reverse it.
return this.bytes.slice(4, 36).toReversed();
}
getMerkleRoot(): Uint8Array {
return this.merkleRoot;
// NOTE: We want to return this as a Big Endian, so we reverse it.
return this.bytes.slice(36, 68).toReversed();
}
getTimestamp(): number {
return this.timestamp;
return this.view.getUint32(68, true);
}
getTimestampDate(): Date {
return new Date(Math.round(this.getTimestamp() * 1000));
}
getBits(): number {
return this.bits;
return this.view.getUint32(72, true);
}
getBitsBytes(): Uint8Array {
return this.bytes.slice(72, 76).toReversed();
}
getBitsHex(): string {
return Bytes.fromBytes(this.getBitsBytes()).toHex();
}
getNonce(): number {
return this.nonce;
return this.view.getUint32(76, true);
}
getNonceBytes(): Uint8Array {
return this.bytes.slice(76, 80).toReversed();
}
getNonceHex(): string {
return Bytes.fromBytes(this.getNonceBytes()).toHex();
}
/**
@@ -205,7 +191,7 @@ export class BlockHeader {
* @returns 64-character hex string
*/
getPreviousBlockHashHex(): string {
return Bytes.fromBytes(this.previousBlockHash).toHex();
return Bytes.fromBytes(this.getPreviousBlockHash()).toHex();
}
/**
@@ -213,7 +199,7 @@ export class BlockHeader {
* @returns 64-character hex string
*/
getMerkleRootHex(): string {
return Bytes.fromBytes(this.merkleRoot).toHex();
return Bytes.fromBytes(this.getMerkleRoot()).toHex();
}
/**
@@ -221,7 +207,7 @@ export class BlockHeader {
* @returns 32-byte array containing the block hash
*/
getHash(): Uint8Array {
return hash256(this.toBytes());
return hash256(this.toBytes()).toReversed();
}
/**
+4
View File
@@ -152,4 +152,8 @@ export class Bytes {
public toBytes(): Uint8Array {
return this.bytes;
}
public invert(): Bytes {
return new Bytes(Uint8Array.from([...this.bytes].reverse()));
}
}