bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.85k stars 2.09k forks source link

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

Open bitjson opened 5 years ago

bitjson commented 5 years ago

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.

ljluestc commented 1 year ago

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')});