diff --git a/src/models/MiningJob.ts b/src/models/MiningJob.ts index 041556a..4064a38 100644 --- a/src/models/MiningJob.ts +++ b/src/models/MiningJob.ts @@ -32,13 +32,17 @@ export class MiningJob { this.block.transactions = blockTemplate.transactions.map(t => bitcoinjs.Transaction.fromHex(t.data)); - const coinbaseTransaction = this.createCoinbaseTransaction(payoutInformation, this.blockTemplate.height, this.blockTemplate.coinbasevalue); + const coinbaseTransaction = this.createCoinbaseTransaction(payoutInformation, this.blockTemplate.coinbasevalue); this.block.transactions.unshift(coinbaseTransaction); this.block.witnessCommit = bitcoinjs.Block.calculateMerkleRoot(this.block.transactions, true); this.block.merkleRoot = bitcoinjs.Block.calculateMerkleRoot(this.block.transactions, false); + + // https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki + const blockHeightScript = Buffer.from(`03${this.blockTemplate.height.toString(16).padStart(8, '0')}`, 'hex'); + //The commitment is recorded in a scriptPubKey of the coinbase transaction. It must be at least 38 bytes, with the first 6-byte of 0x6a24aa21a9ed, that is: // 1-byte - OP_RETURN (0x6a) // 1-byte - Push the following 36 bytes (0x24) @@ -47,7 +51,7 @@ export class MiningJob { // 32-byte - Commitment hash: Double-SHA256(witness root hash|witness reserved value) const commitmentHash = this.sha256(this.sha256(this.block.witnessCommit)); // 39th byte onwards: Optional data with no consensus meaning - coinbaseTransaction.ins[0].script = bitcoinjs.script.compile([bitcoinjs.opcodes.OP_RETURN, Buffer.concat([segwitMagicBits, commitmentHash, Buffer.from('00000000' + '00000000', 'hex')])]); + coinbaseTransaction.ins[0].script = bitcoinjs.script.compile([bitcoinjs.opcodes.OP_RETURN, Buffer.concat([blockHeightScript, segwitMagicBits, commitmentHash, Buffer.from('00000000' + '00000000', 'hex')])]); // get the non-witness coinbase tx //@ts-ignore @@ -55,7 +59,6 @@ export class MiningJob { const inputScript = coinbaseTransaction.ins[0].script.toString('hex'); - const partOneIndex = serializedCoinbaseTx.indexOf(inputScript) + inputScript.length; const coinbasePart1 = serializedCoinbaseTx.slice(0, partOneIndex); @@ -119,7 +122,7 @@ export class MiningJob { } - private createCoinbaseTransaction(addresses: AddressObject[], blockHeight: number, reward: number): bitcoinjs.Transaction { + private createCoinbaseTransaction(addresses: AddressObject[], reward: number): bitcoinjs.Transaction { // Part 1 const coinbaseTransaction = new bitcoinjs.Transaction(); @@ -127,7 +130,6 @@ export class MiningJob { coinbaseTransaction.version = 2; - // const inputScript = bitcoinjs.script.compile([bitcoinjs.opcodes.OP_RETURN, Buffer.from('00000000' + '00000000', 'hex')]) // Add the coinbase input (input with no previous output) coinbaseTransaction.addInput(Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'), 0xffffffff, 0xffffffff);