Use Bytes Primitive

This commit is contained in:
Jim Hamill
2025-07-23 00:27:41 +00:00
parent 9af5dc0087
commit e72c678785
10 changed files with 62 additions and 72 deletions
+8 -5
View File
@@ -23,15 +23,18 @@ import { hash256 } from '@bitauth/libauth';
* ```
*/
export class BlockHeader {
protected readonly bytes: Bytes;
// View of our data so that we can extract as numbers.
private view: DataView;
protected readonly view: DataView;
/**
* Creates a new BlockHeader instance.
* @param bytes The bytes representing our Block Header (Big Endian)
*/
protected constructor(private readonly bytes: Uint8Array) {
this.view = new DataView(bytes.buffer);
protected constructor(bytes: Bytes | Uint8Array) {
this.bytes = Bytes.from(bytes);
this.view = new DataView(this.bytes.buffer);
}
/**
@@ -58,7 +61,7 @@ export class BlockHeader {
* @returns A new BlockHeader instance
* @throws Error if the byte array length is not 80
*/
static fromBytes(bytes: Uint8Array): BlockHeader {
static fromBytes(bytes: Bytes | Uint8Array): BlockHeader {
if (bytes.length !== 80) {
throw new Error('Invalid block header length');
}
@@ -134,7 +137,7 @@ export class BlockHeader {
* Serializes the block header to a byte array.
* @returns 80-byte array containing the serialized block header
*/
toBytes(): Uint8Array {
toBytes(): Bytes {
return this.bytes;
}
+2 -2
View File
@@ -176,9 +176,9 @@ export class HDPrivateNode {
*
* @returns The Node's Private Key (as a Uint8Array).
*/
public toBytes(): Uint8Array {
public toBytes(): Bytes {
// Encode the XPriv from our node info.
return this.node.privateKey;
return Bytes.from(this.node.privateKey);
}
/**
+8 -8
View File
@@ -33,16 +33,16 @@ import {
*/
export class PrivateKey {
// The raw bytes representing the Private Key.
protected readonly bytes: Uint8Array;
protected readonly bytes: Bytes;
/**
* Construct a new Private Key.
*
* @throws {Error} If Private Key cannot be constructed.
*
* @param bytes {Uint8Array} The raw bytes (32) for the Private Key.
* @param bytes The raw bytes (32) for the Private Key.
*/
protected constructor(bytes: Uint8Array) {
protected constructor(bytes: Bytes | Uint8Array) {
// Ensure that the Private Key is valid.
if (!secp256k1.validatePrivateKey(bytes)) {
throw new Error(
@@ -50,7 +50,7 @@ export class PrivateKey {
);
}
this.bytes = bytes;
this.bytes = Bytes.from(bytes);
}
/**
@@ -62,7 +62,7 @@ export class PrivateKey {
*
* @returns The created Private Key Primitive.
*/
public static from(privateKey: Uint8Array | string) {
public static from(privateKey: Bytes | Uint8Array | string) {
if (privateKey instanceof Uint8Array) {
return this.fromBytes(privateKey);
}
@@ -83,7 +83,7 @@ export class PrivateKey {
*
* @returns {PrivateKey} The created Private Key Entity.
*/
public static fromBytes(privateKeyBytes: Uint8Array): PrivateKey {
public static fromBytes(privateKeyBytes: Bytes | Uint8Array): PrivateKey {
return new this(privateKeyBytes);
}
@@ -194,9 +194,9 @@ export class PrivateKey {
/**
* Convert this Private Key to a raw 32 byte representation.
*
* @returns {Uint8Array} The raw Private Key as a Uint8Array.
* @returns The raw Private Key as a Uint8Array.
*/
public toBytes(): Uint8Array {
public toBytes(): Bytes {
// Return the raw bytes for this Private Key.
return this.bytes;
}
+7 -7
View File
@@ -24,15 +24,15 @@ import { secp256k1, sha256, ripemd160, utf8ToBin } from '@bitauth/libauth';
*/
export class PublicKey {
// The raw bytes representing the Public Key.
protected readonly bytes: Uint8Array;
protected readonly bytes: Bytes;
/**
* Construct a new Public Key.
*
* @param bytes The raw bytes (33) for the Public Key.
*/
protected constructor(bytes: Uint8Array) {
this.bytes = bytes;
protected constructor(bytes: Bytes | Uint8Array) {
this.bytes = Bytes.from(bytes);
}
/**
@@ -44,7 +44,7 @@ export class PublicKey {
*
* @returns The created Public Key Primitive.
*/
public static from(publicKey: Uint8Array | string) {
public static from(publicKey: Bytes | Uint8Array | string) {
return publicKey instanceof Uint8Array
? this.fromBytes(publicKey)
: this.fromHex(publicKey);
@@ -59,7 +59,7 @@ export class PublicKey {
*
* @returns The created Public Key entity.
*/
public static fromBytes(publicKeyBytes: Uint8Array): PublicKey {
public static fromBytes(publicKeyBytes: Bytes | Uint8Array): PublicKey {
// Ensure that exactly 33 Bytes are given.
if (!secp256k1.validatePublicKey(publicKeyBytes)) {
throw new Error(`Invalid Public Key.`);
@@ -90,7 +90,7 @@ export class PublicKey {
*
* @returns A new instance of this primitive.
*/
public static fromRaw(bytes: Uint8Array) {
public static fromRaw(bytes: Bytes | Uint8Array) {
return new this(bytes);
}
@@ -108,7 +108,7 @@ export class PublicKey {
*
* @returns The raw bytes of the Public Key.
*/
public toBytes(): Uint8Array {
public toBytes(): Bytes {
// Return the raw bytes for this Public Key.
return this.bytes;
}
+6 -6
View File
@@ -28,15 +28,15 @@ import {
* ```
*/
export class Script {
protected readonly bytes: Uint8Array;
protected readonly bytes: Bytes;
/**
* Construct a new Script
*
* @param bytes {Uint8Array} The raw bytes of the Script.
*/
protected constructor(bytes: Uint8Array) {
this.bytes = bytes;
protected constructor(bytes: Bytes | Uint8Array) {
this.bytes = Bytes.from(bytes);
}
/**
@@ -48,7 +48,7 @@ export class Script {
*
* @returns The created Script Primitive.
*/
public static from(script: Uint8Array | string) {
public static from(script: Bytes | Uint8Array | string) {
return script instanceof Uint8Array
? this.fromBytes(script)
: this.fromHex(script);
@@ -63,7 +63,7 @@ export class Script {
*
* @returns {Script} The created Script Entity.
*/
public static fromBytes(bytes: Uint8Array): Script {
public static fromBytes(bytes: Bytes | Uint8Array): Script {
// Disassemble the bytecode.
const disassembledBytecode = disassembleBytecodeBCH(bytes);
@@ -213,7 +213,7 @@ export class Script {
*
* @returns {Uint8Array} The compiled bytes.
*/
public toBytes(): Uint8Array {
public toBytes(): Bytes {
return this.bytes;
}