bitcore icon indicating copy to clipboard operation
bitcore copied to clipboard

Bitcore: block objects should include a `coinbaseInputScript` or similar

Open bitjson opened this issue 6 years ago • 1 comments

The coinbase input script includes a committed block height (since block v2), any BIP 9 or similar block signaling, and often the claimed name of the pool which mined the block. Clients (like Insight) can consume this script to display a lot of additional useful data.

bitjson avatar Oct 19 '18 21:10 bitjson

const bitcoin = require('bitcoinjs-lib'); const crypto = require('crypto');

const network = bitcoin.networks.bitcoin;

// Construct your coinbase message const coinbaseMessage = Buffer.from('Hello, miners!', 'utf8');

// Create the coinbase input const coinbaseInput = new bitcoin.TransactionInput({ script: bitcoin.script.compile([ bitcoin.opcodes.OP_RETURN, coinbaseMessage ]), prevOut: new bitcoin.Outpoint(Buffer.alloc(32, 0), 0xffffffff), sequence: 0xffffffff });

// Construct the coinbase transaction const coinbaseTransaction = new bitcoin.Transaction({ version: 2, // Set the block version inputs: [coinbaseInput], outputs: [ { script: bitcoin.script.witnessPubKeyHash.output.encode(Buffer.alloc(20, 0x00)), value: 1250000000 // Example coinbase reward } ] });

// Calculate transaction ID (double SHA-256) const txid = crypto .createHash('sha256') .update(coinbaseTransaction.toBuffer()) .digest();

console.log(Transaction ID: ${txid.toString('hex')});

ljluestc avatar Sep 03 '23 16:09 ljluestc