itns-sidechain/scripts/gen.js

149 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-06-13 01:06:01 -07:00
'use strict';
2017-06-29 20:54:07 -07:00
const consensus = require('../lib/protocol/consensus');
const TX = require('../lib/primitives/tx');
const Block = require('../lib/primitives/block');
2018-01-02 20:24:56 -08:00
const Address = require('../lib/primitives/address');
const Witness = require('../lib/script/witness');
const util = require('../lib/utils/util');
2017-06-29 20:54:07 -07:00
2018-01-04 20:00:55 -08:00
const secp256k1 = require('bcrypto/lib/secp256k1');
const hash160 = require('bcrypto/lib/hash160');
const hex = ''
+ '0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5c'
+ 'b2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3';
const uncompressed = Buffer.from(hex, 'hex');
const key = secp256k1.publicKeyConvert(uncompressed, true);
const keyHash = hash160.digest(key);
const ZERO_ROOT =
'03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314';
2018-01-04 20:00:55 -08:00
2016-02-23 20:38:25 -08:00
function createGenesisBlock(options) {
2017-06-29 20:54:07 -07:00
let flags = options.flags;
2018-01-02 20:24:56 -08:00
let addr = options.address;
let nonce = options.nonce;
let sol = options.solution;
2016-02-23 20:38:25 -08:00
2016-06-27 15:29:45 -07:00
if (!flags) {
flags = Buffer.from(
2018-01-02 20:24:56 -08:00
'01/Nov/2017 EFF to ICANN: Don\'t Pick Up the Censor\'s Pen',
2016-02-23 20:38:25 -08:00
'ascii');
}
2018-01-02 20:24:56 -08:00
if (!addr)
2018-01-04 20:00:55 -08:00
addr = Address.fromHash(keyHash, 0);
2018-01-02 20:24:56 -08:00
if (!nonce)
nonce = Buffer.alloc(16, 0x00);
2016-02-23 20:38:25 -08:00
2018-01-02 20:24:56 -08:00
if (!sol)
sol = new Uint32Array(2);
2016-02-23 20:38:25 -08:00
const tx = new TX({
2018-01-02 20:24:56 -08:00
version: 0,
2016-02-23 20:38:25 -08:00
inputs: [{
prevout: {
hash: consensus.NULL_HASH,
2016-02-23 20:38:25 -08:00
index: 0xffffffff
},
2018-01-02 20:24:56 -08:00
witness: new Witness([flags]),
2016-02-23 20:38:25 -08:00
sequence: 0xffffffff
}],
outputs: [{
2018-01-05 03:17:44 -08:00
value: consensus.BASE_REWARD,
2018-01-02 20:24:56 -08:00
address: addr
2016-02-23 20:38:25 -08:00
}],
locktime: 0
2016-06-27 15:29:45 -07:00
});
2016-02-23 20:38:25 -08:00
const block = new Block({
2017-12-31 15:08:01 -08:00
version: 0,
prevBlock: consensus.NULL_HASH,
2016-06-27 15:29:45 -07:00
merkleRoot: tx.hash('hex'),
2017-12-31 15:08:01 -08:00
witnessRoot: tx.witnessHash('hex'),
trieRoot: ZERO_ROOT,
2017-07-25 14:23:10 -07:00
time: options.time,
2016-02-23 20:38:25 -08:00
bits: options.bits,
2018-01-02 20:24:56 -08:00
nonce: nonce,
solution: sol
2016-06-27 15:29:45 -07:00
});
2016-02-23 20:38:25 -08:00
2017-07-25 01:53:58 -07:00
block.txs.push(tx);
2016-02-23 20:38:25 -08:00
return block;
}
const main = createGenesisBlock({
2017-12-31 15:08:01 -08:00
time: 1514765688,
2018-01-02 20:24:56 -08:00
bits: 0x207fffff,
2017-12-31 15:08:01 -08:00
solution: new Uint32Array(42)
2017-06-29 20:54:07 -07:00
});
2016-02-23 20:38:25 -08:00
const testnet = createGenesisBlock({
2017-12-31 15:08:01 -08:00
time: 1514765689,
2018-01-02 20:24:56 -08:00
bits: 0x207fffff,
solution: new Uint32Array(18)
2017-06-29 20:54:07 -07:00
});
2016-02-23 20:38:25 -08:00
const regtest = createGenesisBlock({
2017-12-31 15:08:01 -08:00
time: 1514765690,
2018-01-02 20:24:56 -08:00
bits: 0x207fffff,
solution: new Uint32Array(18)
2017-06-29 20:54:07 -07:00
});
2016-02-23 20:38:25 -08:00
2017-12-31 15:08:01 -08:00
const simnet = createGenesisBlock({
time: 1514765691,
2018-01-02 20:24:56 -08:00
bits: 0x207fffff,
solution: new Uint32Array(18)
2017-06-29 20:54:07 -07:00
});
2018-01-02 20:24:56 -08:00
function formatBlock(name, block) {
return `${name}.genesis = {
version: ${block.version},
hash: '${block.hash('hex')}',
prevBlock: '${block.prevBlock}',
merkleRoot:
'${block.merkleRoot}',
witnessRoot:
'${block.witnessRoot}',
trieRoot:
'${block.trieRoot}',
2018-01-02 20:24:56 -08:00
time: ${block.time},
bits: 0x${util.hex32(block.bits)},
nonce: Buffer.from('${block.nonce.toString('hex')}', 'hex'),
solution: new Uint32Array(${block.solution.size()}),
height: 0
};`;
}
function formatRaw(name, block) {
2017-12-31 15:08:01 -08:00
const str = block.toRaw().toString('hex');
2016-03-30 19:10:59 -07:00
2018-01-02 20:24:56 -08:00
let out = `${name}.genesisBlock = ''\n`;
2017-12-31 15:08:01 -08:00
for (let i = 0; i < str.length; i += 64)
out += ` + '${str.slice(i, i + 64)}'\n`;
2018-01-02 20:24:56 -08:00
out = out.slice(0, -1) + ';';
2017-12-31 15:08:01 -08:00
return out;
}
2016-09-08 17:43:58 -07:00
2018-01-02 20:24:56 -08:00
function dump(name, block) {
const blk = formatBlock(name, block);
const raw = formatRaw(name, block);
console.log(blk);
console.log('');
console.log(raw);
console.log('');
}
console.log('');
2018-01-02 20:24:56 -08:00
dump('main', main);
dump('testnet', testnet);
dump('regtest', regtest);
dump('simnet', simnet);
console.log(JSON.stringify(testnet.toJSON(), null, 2));