Misc Primitive Fixes
This commit is contained in:
@@ -29,8 +29,8 @@ export class Transaction {
|
||||
*/
|
||||
public static from(transaction: Uint8Array | string) {
|
||||
return transaction instanceof Uint8Array
|
||||
? Transaction.fromBytes(transaction)
|
||||
: Transaction.fromHex(transaction);
|
||||
? this.fromBytes(transaction)
|
||||
: this.fromHex(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +52,7 @@ export class Transaction {
|
||||
}
|
||||
|
||||
// Return a new instance of the Transaction Primitive created from the decode result.
|
||||
return new Transaction(decodeResult);
|
||||
return new this(decodeResult);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +74,7 @@ export class Transaction {
|
||||
const bytes = hexToBin(hex);
|
||||
|
||||
// Return the transaction constructed from our bytes.
|
||||
return Transaction.fromBytes(bytes);
|
||||
return this.fromBytes(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +99,7 @@ export class Transaction {
|
||||
*/
|
||||
public static isValid(transaction: Uint8Array | string): boolean {
|
||||
try {
|
||||
Transaction.from(transaction);
|
||||
this.from(transaction);
|
||||
} catch (_error) {
|
||||
return false;
|
||||
}
|
||||
@@ -159,35 +159,6 @@ export class 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) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const input = this.getInputs()[0];
|
||||
|
||||
// Check if the outpoint transaction hash is all zeros (32 bytes of 0x00)
|
||||
const isOutpointZero = input.outpointTransactionHash.every(
|
||||
(byte) => byte === 0x00,
|
||||
);
|
||||
|
||||
// Check if the outpoint index is 0xFFFFFFFF (or -1 in signed 32-bit integer representation)
|
||||
const isOutpointIndexMax = input.outpointIndex === 0xffffffff;
|
||||
|
||||
// Both conditions must be true for a coinbase transaction
|
||||
return isOutpointZero && isOutpointIndexMax;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the version for this transaction.
|
||||
*
|
||||
@@ -231,4 +202,33 @@ export class Transaction {
|
||||
public getLocktime() {
|
||||
return this.transaction.locktime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const input = this.getInputs()[0];
|
||||
|
||||
// Check if the outpoint transaction hash is all zeros (32 bytes of 0x00)
|
||||
const isOutpointZero = input.outpointTransactionHash.every(
|
||||
(byte) => byte === 0x00,
|
||||
);
|
||||
|
||||
// Check if the outpoint index is 0xFFFFFFFF (or -1 in signed 32-bit integer representation)
|
||||
const isOutpointIndexMax = input.outpointIndex === 0xffffffff;
|
||||
|
||||
// Both conditions must be true for a coinbase transaction
|
||||
return isOutpointZero && isOutpointIndexMax;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user