From 68758140a8b59b97fbefca495d9d2a778c36efe0 Mon Sep 17 00:00:00 2001 From: James Zuccon Date: Thu, 12 Sep 2024 21:44:02 +1000 Subject: [PATCH] Fix isCoinbase Signed-off-by: James Zuccon --- packages/primitives/src/transaction.ts | 24 +++++++++---------- .../wallet/src/blockchain/blockchain-local.ts | 9 ------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/packages/primitives/src/transaction.ts b/packages/primitives/src/transaction.ts index 399e9ff..7734233 100644 --- a/packages/primitives/src/transaction.ts +++ b/packages/primitives/src/transaction.ts @@ -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; } } diff --git a/packages/wallet/src/blockchain/blockchain-local.ts b/packages/wallet/src/blockchain/blockchain-local.ts index 65d71aa..e0a980f 100644 --- a/packages/wallet/src/blockchain/blockchain-local.ts +++ b/packages/wallet/src/blockchain/blockchain-local.ts @@ -81,21 +81,15 @@ export class BlockchainLocal implements IBlockchain { const utxoSet = this.deriveUtxoSet(); if (!opts.skipValidation) { - console.log('utxoSet', utxoSet); - // Find the source outputs from our UTXO set so that we can make sure the transaction is valid. const sourceOutputs = tx.getInputs().map((input) => { const utxoId = `${binToHex(input.outpointTransactionHash)}:${ input.outpointIndex }`; - console.log(utxoId); - return utxoSet[utxoId]; }); - console.log('sourceOutputs', sourceOutputs); - // Validate the transaction. BlockchainLocal.validateTransaction(tx, sourceOutputs); } @@ -333,9 +327,6 @@ export class BlockchainLocal implements IBlockchain { * @todo Consider moving this to Transaction Primitive. Trade-off is that it's probably big. Maybe standalone util? */ static validateTransaction(tx: Transaction, sourceOutputs: Array) { - console.log(tx); - console.log(sourceOutputs); - const virtualMachine = createVirtualMachineBCH(); const virtualMachineResult = virtualMachine.verify({ transaction: {