scripts: gen.js.

This commit is contained in:
Christopher Jeffrey 2016-06-27 15:29:45 -07:00
parent aadac3d525
commit 4d6d526126
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -2,35 +2,36 @@
var bcoin = require('bcoin'); var bcoin = require('bcoin');
var constants = bcoin.protocol.constants; var constants = bcoin.protocol.constants;
var opcodes = constants.opcodes;
var network = bcoin.protocol.network; var network = bcoin.protocol.network;
var utils = bcoin.utils; var utils = bcoin.utils;
var bn = require('bn.js'); var bn = require('bn.js');
function createGenesisBlock(options) { function createGenesisBlock(options) {
var parser = bcoin.protocol.parser; var flags = options.flags;
var tx, block, txRaw, blockRaw; var script = options.script;
var reward = options.reward;
var tx, block;
if (!options.flags) { if (!flags) {
options.flags = new Buffer( flags = new Buffer(
'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks', 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks',
'ascii'); 'ascii');
} }
if (!options.script) { if (!script) {
options.script = { script = bcoin.script.fromArray([
code: [ new Buffer('04678afdb0fe5548271967f1a67130b7105cd6a828e039'
new Buffer('04678afdb0fe5548271967f1a67130b7105cd6a828e039' + '09a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c3'
+ '09a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c3' + '84df7ba0b8d578a4c702b6bf11d5f', 'hex'),
+ '84df7ba0b8d578a4c702b6bf11d5f', 'hex'), opcodes.OP_CHECKSIG
constants.opcodes.OP_CHECKSIG ]);
]
};
} }
if (!options.reward) if (!reward)
options.reward = 50 * constants.COIN; reward = 50 * constants.COIN;
tx = { tx = new bcoin.tx({
version: 1, version: 1,
flag: 1, flag: 1,
inputs: [{ inputs: [{
@ -38,55 +39,31 @@ function createGenesisBlock(options) {
hash: constants.NULL_HASH, hash: constants.NULL_HASH,
index: 0xffffffff index: 0xffffffff
}, },
script: { script: [
code: [ bcoin.opcode.fromNumber(new bn(486604799)),
new bn(486604799).toBuffer('le'), bcoin.opcode.fromPush(new Buffer([4])),
new bn(4).toBuffer('le'), bcoin.opcode.fromData(flags)
options.flags ],
]
},
sequence: 0xffffffff sequence: 0xffffffff
}], }],
outputs: [{ outputs: [{
value: options.reward, value: reward,
script: options.script script: script
}], }],
locktime: 0 locktime: 0
}; });
tx.inputs[0].script.code[1].opcode = 1; block = new bcoin.block({
txRaw = bcoin.protocol.framer.tx(tx);
tx._raw = txRaw;
tx._size = txRaw.length;
tx._witnessSize = 0;
block = {
version: options.version, version: options.version,
prevBlock: constants.NULL_HASH, prevBlock: constants.NULL_HASH,
merkleRoot: utils.dsha256(txRaw).toString('hex'), merkleRoot: tx.hash('hex'),
ts: options.ts, ts: options.ts,
bits: options.bits, bits: options.bits,
nonce: options.nonce, nonce: options.nonce,
txs: [tx] height: 0
}; });
blockRaw = bcoin.protocol.framer.block(block); block.addTX(tx);
block = parser.parseBlock(blockRaw);
block._hash = utils.dsha256(blockRaw.slice(0, 80));
block.hash = block._hash.toString('hex');
block._raw = blockRaw;
block._size = blockRaw.length;
block._witnessSize = 0;
block.height = 0;
tx = block.txs[0];
tx.height = 0;
tx.ts = block.ts;
tx._hash = block.merkleRoot;
tx.hash = tx._hash.toString('hex');
return block; return block;
} }
@ -115,31 +92,39 @@ var regtest = createGenesisBlock({
var segnet3 = createGenesisBlock({ var segnet3 = createGenesisBlock({
version: 1, version: 1,
ts: 1452831101, ts: 1452831101,
bits: 0x1d00ffff, bits: 486604799,
nonce: 0 nonce: 0
}); });
var segnet4 = createGenesisBlock({ var segnet4 = createGenesisBlock({
version: 1, version: 1,
ts: 1452831101, ts: 1452831101,
bits: utils.toCompact(network.segnet4.pow.limit), bits: 503447551,
nonce: 0 nonce: 0
}); });
utils.print(main); utils.print(main);
utils.print('');
utils.print(testnet); utils.print(testnet);
utils.print('');
utils.print(regtest); utils.print(regtest);
utils.print('');
utils.print(segnet3); utils.print(segnet3);
utils.print('main hash: %s', utils.revHex(main.hash));
utils.print('main raw: %s', main._raw.toString('hex'));
utils.print(''); utils.print('');
utils.print('testnet hash: %s', utils.revHex(testnet.hash));
utils.print('testnet raw: %s', testnet._raw.toString('hex'));
utils.print('');
utils.print('regtest hash: %s', utils.revHex(regtest.hash));
utils.print('regtest raw: %s', regtest._raw.toString('hex'));
utils.print('segnet3 hash: %s', utils.revHex(segnet3.hash));
utils.print('segnet3 raw: %s', segnet3._raw.toString('hex'));
utils.print('segnet4 hash: %s', utils.revHex(segnet4.hash));
utils.print('segnet4 raw: %s', segnet4._raw.toString('hex'));
utils.print(segnet4); utils.print(segnet4);
utils.print('');
utils.print('');
utils.print('main hash: %s', main.rhash);
utils.print('main raw: %s', main.toRaw().toString('hex'));
utils.print('');
utils.print('testnet hash: %s', testnet.rhash);
utils.print('testnet raw: %s', testnet.toRaw().toString('hex'));
utils.print('');
utils.print('regtest hash: %s', regtest.rhash);
utils.print('regtest raw: %s', regtest.toRaw().toString('hex'));
utils.print('');
utils.print('segnet3 hash: %s', segnet3.rhash);
utils.print('segnet3 raw: %s', segnet3.toRaw().toString('hex'));
utils.print('');
utils.print('segnet4 hash: %s', segnet4.rhash);
utils.print('segnet4 raw: %s', segnet4.toRaw().toString('hex'));