Amend Primitives/Tooling

This commit is contained in:
Jim Hamill
2025-01-13 09:43:11 +00:00
parent 06c9d52b56
commit f53fc4b456
29 changed files with 817 additions and 463 deletions
+68 -3
View File
@@ -11,7 +11,35 @@ export class TransactionHash {
*
* @param bytes {Uint8Array} The Transaction Hash's bytes.
*/
constructor(bytes: Uint8Array) {
protected constructor(bytes: Uint8Array) {
this.bytes = bytes;
}
/**
* Instantiates a TransactionHash from either the Hash Hex or Hash Bytes.
*
* @param hash The Transaction Hash as either hex or bytes.
*
* @throws {Error} If the hash is invalid.
*
* @returns The created Public Key Primitive.
*/
public static from(hash: Uint8Array | string) {
return hash instanceof Uint8Array
? TransactionHash.fromBytes(hash)
: TransactionHash.fromHex(hash);
}
/**
* Instantiates TransactionHash from Hash Bytes.
*
* @param bytes The hash as bytes.
*
* @throws {Error} If hash is invalid.
*
* @returns {TransactionHash} Instance of TransactionHash.
*/
public static fromBytes(bytes: Uint8Array): TransactionHash {
// If bytes is not exactly 32, then this is an invalid transaction hash...
if (bytes.length !== 32) {
throw new Error(
@@ -19,7 +47,7 @@ export class TransactionHash {
);
}
this.bytes = bytes;
return new TransactionHash(bytes);
}
/**
@@ -38,7 +66,22 @@ export class TransactionHash {
const bytes = hexToBin(hex);
return new TransactionHash(bytes);
return this.fromBytes(bytes);
}
/**
* Instantiates a TransactionHash from either the Transaction Hex or Transaction Hash.
*
* @param hash The Transaction as either hex or bytes.
*
* @throws {Error} If the public key cannot be decoded.
*
* @returns The created Public Key Primitive.
*/
public static fromTransaction(transaction: Uint8Array | string) {
return transaction instanceof Uint8Array
? TransactionHash.fromTransactionBytes(transaction)
: TransactionHash.fromTransactionHex(transaction);
}
/**
@@ -75,6 +118,19 @@ export class TransactionHash {
return TransactionHash.fromTransactionBytes(bytes);
}
/**
* Creates an instance from serialized data.
*
* @remarks This method is UNSAFE and MUST only be used where input is already verified or trusted.
*
* @param bytes - Serialized Primitive Data
*
* @returns A new instance of this primitive.
*/
public static fromRaw(bytes: Uint8Array) {
return new this(bytes);
}
/**
* Returns transaction hash as a hex string.
*
@@ -92,4 +148,13 @@ export class TransactionHash {
public toBytes(): Uint8Array {
return this.bytes;
}
/**
* Converts this primitive to a serializable representation.
*
* @returns Serialzied primitive data.
*/
toRaw() {
return this.bytes;
}
}