Amend Primitives/Tooling
This commit is contained in:
@@ -13,7 +13,7 @@ import type { TransactionBCH } from '@bitauth/libauth';
|
||||
* Transaction Primitive.
|
||||
*/
|
||||
export class Transaction {
|
||||
constructor(public transaction: TransactionBCH) {}
|
||||
protected constructor(public transaction: TransactionBCH) {}
|
||||
|
||||
/**
|
||||
* Instantiates a transaction from either:
|
||||
@@ -28,19 +28,9 @@ export class Transaction {
|
||||
* @returns The created Transaction Primitive.
|
||||
*/
|
||||
public static from(transaction: Uint8Array | string) {
|
||||
// Handle string (hex) case
|
||||
if (typeof transaction === 'string') {
|
||||
return Transaction.fromHex(transaction);
|
||||
}
|
||||
|
||||
// Handle Uint8Array case
|
||||
if (transaction instanceof Uint8Array) {
|
||||
return Transaction.fromBytes(transaction);
|
||||
}
|
||||
|
||||
throw new TypeError(
|
||||
'Invalid transaction input type. Expected Uint8Array, or hex string',
|
||||
);
|
||||
return transaction instanceof Uint8Array
|
||||
? Transaction.fromBytes(transaction)
|
||||
: Transaction.fromHex(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +80,7 @@ export class Transaction {
|
||||
/**
|
||||
* Create a Transaction Primitive from LibAuth's TransactionBCH type.
|
||||
*
|
||||
* @remarks This method is UNSAFE and should only be used where input is trusted.
|
||||
* @remarks This method is UNSAFE and MUST only be used where input is already verified or trusted.
|
||||
*
|
||||
* @param transaction The transaction object (as per LibAuth).
|
||||
*
|
||||
@@ -104,6 +94,7 @@ export class Transaction {
|
||||
* Validates the structure of a transaction.
|
||||
*
|
||||
* @param transaction The transaction (as hex or bytes).
|
||||
*
|
||||
* @returns boolean indicating whether the transaction structure is valid.
|
||||
*/
|
||||
public static isValid(transaction: Uint8Array | string): boolean {
|
||||
@@ -116,34 +107,67 @@ export class Transaction {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes the transaction to a byte array.
|
||||
* @returns The transaction serialized as a Uint8Array
|
||||
*/
|
||||
public toBytes(): Uint8Array {
|
||||
const bytes = encodeTransaction(this.transaction);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes the transaction to a byte array (represented as a hex string).
|
||||
* @returns The transaction serialized in hex
|
||||
*/
|
||||
public toHex(): string {
|
||||
const bytes = this.toBytes();
|
||||
|
||||
return binToHex(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes the transaction and returns a TransactionHash primitive.
|
||||
* @returns The transaction hash primitive.
|
||||
*/
|
||||
public toHash(): TransactionHash {
|
||||
return TransactionHash.fromTransactionBytes(this.toBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes the transaction and returns the hash as hexadecimal.
|
||||
* @returns The transaction hash hex.
|
||||
*/
|
||||
public toHashHex(): string {
|
||||
return this.toHash().toHex();
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes the transaction and returns the hash as a Uint8Array.
|
||||
* @returns The transaction hash bytes.
|
||||
*/
|
||||
public toHashBytes(): Uint8Array {
|
||||
return this.toHash().toBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts transaction to a raw object representation.
|
||||
* @returns A TransactionBCH LibAuth object.
|
||||
*/
|
||||
public toRaw(): TransactionBCH {
|
||||
return this.transaction;
|
||||
return { ...this.transaction };
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this transaction is a Coinbase Transaction.
|
||||
*
|
||||
* @remarks UNTESTED
|
||||
*
|
||||
* @experimental
|
||||
*
|
||||
* @returns True if this is a Coinbase Transaction
|
||||
*/
|
||||
public isCoinbase(): boolean {
|
||||
// A coinbase transaction MUST only have one input
|
||||
if (this.getInputs().length !== 1) {
|
||||
@@ -164,18 +188,46 @@ export class Transaction {
|
||||
return isOutpointZero && isOutpointIndexMax;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the version for this transaction.
|
||||
*
|
||||
* @experimental
|
||||
*
|
||||
* @returns The version of this transaction
|
||||
*/
|
||||
public getVersion() {
|
||||
return this.transaction.version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the inputs in this transaction.
|
||||
*
|
||||
* @experimental
|
||||
*
|
||||
* @returns The inputs in this transaction
|
||||
*/
|
||||
public getInputs() {
|
||||
return this.transaction.inputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the outputs in this transaction.
|
||||
*
|
||||
* @experimental
|
||||
*
|
||||
* @returns The outputs in this transaction
|
||||
*/
|
||||
public getOutputs() {
|
||||
return this.transaction.outputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the locktime for this transaction.
|
||||
*
|
||||
* @experimental
|
||||
*
|
||||
* @returns The locktime of this transaction
|
||||
*/
|
||||
public getLocktime() {
|
||||
return this.transaction.locktime;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user