161 lines
4.1 KiB
TypeScript
161 lines
4.1 KiB
TypeScript
import { binToHex, hexToBin, hashTransaction, isHex } from '@bitauth/libauth';
|
|
|
|
/**
|
|
* Transaction Hash Primitive.
|
|
*/
|
|
export class TransactionHash {
|
|
public readonly bytes: Uint8Array;
|
|
|
|
/**
|
|
* Instantiates TransactionHash from a Transaction Hash's bytes.
|
|
*
|
|
* @param bytes {Uint8Array} The Transaction Hash's bytes.
|
|
*/
|
|
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(
|
|
`Invalid Transaction Hash: Expected 32 bytes but received ${bytes.length}`,
|
|
);
|
|
}
|
|
|
|
return new TransactionHash(bytes);
|
|
}
|
|
|
|
/**
|
|
* Instantiates TransactionHash from a hex string.
|
|
*
|
|
* @param hex {string} The hex string.
|
|
*
|
|
* @throws {Error} If hex is invalid.
|
|
*
|
|
* @returns {TransactionHash} Instance of TransactionHash.
|
|
*/
|
|
public static fromHex(hex: string): TransactionHash {
|
|
if (!isHex(hex)) {
|
|
throw new Error('Invalid hex provided');
|
|
}
|
|
|
|
const bytes = hexToBin(hex);
|
|
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* Instantiates TransactionHash from a Transaction's bytes.
|
|
*
|
|
* @param transactionBytes {Uint8Array} The Transaction's bytes.
|
|
*
|
|
* @returns {TransactionHash} Instance of TransactionHash.
|
|
*/
|
|
public static fromTransactionBytes(
|
|
transactionBytes: Uint8Array,
|
|
): TransactionHash {
|
|
const transactionHash = hashTransaction(transactionBytes);
|
|
|
|
return new TransactionHash(hexToBin(transactionHash));
|
|
}
|
|
|
|
/**
|
|
* Instantiates TransactionHash from a Transaction's bytes in hex.
|
|
*
|
|
* @param transactionHex {Uint8Array} The Transaction's bytes in hex.
|
|
*
|
|
* @throws {Error} If hex is invalid.
|
|
*
|
|
* @returns {TransactionHash} Instance of TransactionHash.
|
|
*/
|
|
public static fromTransactionHex(transactionHex: string): TransactionHash {
|
|
if (!isHex(transactionHex)) {
|
|
throw new Error('Invalid hex provided');
|
|
}
|
|
|
|
const bytes = hexToBin(transactionHex);
|
|
|
|
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.
|
|
*
|
|
* @returns {string} Hex string of the Transaction Hash.
|
|
*/
|
|
public toHex(): string {
|
|
return binToHex(this.bytes);
|
|
}
|
|
|
|
/**
|
|
* Returns transaction hash as a Uint8Array.
|
|
*
|
|
* @returns {Uint8Array} Uint8Array of the Transaction Hash.
|
|
*/
|
|
public toBytes(): Uint8Array {
|
|
return this.bytes;
|
|
}
|
|
|
|
/**
|
|
* Converts this primitive to a serializable representation.
|
|
*
|
|
* @returns Serialzied primitive data.
|
|
*/
|
|
toRaw() {
|
|
return this.bytes;
|
|
}
|
|
}
|