Fix isCoinbase

Signed-off-by: James Zuccon <zuccon@gmail.com>
This commit is contained in:
James Zuccon
2024-09-12 21:44:02 +10:00
parent a158505b5f
commit 68758140a8
2 changed files with 11 additions and 22 deletions
+11 -13
View File
@@ -108,24 +108,22 @@ export class Transaction {
}
public isCoinbase(): boolean {
// TODO: This definitely needs checking.
// TODO: You're a moron Jim. outpointTransactionHash is 0x00 x 32 for Coinbase.
// A coinbase transaction MUST only have one input.
if (this.getInputs().length > 1) {
// A coinbase transaction MUST only have one input
if (this.getInputs().length !== 1) {
return false;
}
// Get the input's unlocking bytecode.
const unlockingBytecode = this.getInputs()[0].unlockingBytecode;
const input = this.getInputs()[0];
// Get the firt byte.
const firstByte = unlockingBytecode[0];
// Check if the outpoint transaction hash is all zeros (32 bytes of 0x00)
const isOutpointZero = input.outpointTransactionHash.every(
(byte) => byte === 0x00
);
if (firstByte >= 0x01 && firstByte <= 0x4b) {
return true;
}
// Check if the outpoint index is 0xFFFFFFFF (or -1 in signed 32-bit integer representation)
const isOutpointIndexMax = input.outpointIndex === 0xffffffff;
return false;
// Both conditions must be true for a coinbase transaction
return isOutpointZero && isOutpointIndexMax;
}
}