From 4c22f971e6d13ed2c29e14bbee9fb5b8f5fea228 Mon Sep 17 00:00:00 2001 From: Nasa Nguyen Date: Tue, 20 Mar 2018 16:41:59 +0700 Subject: [PATCH] first commit --- .gitignore | 19 +++ README.md | 111 +++++++++++++ genesis.js | 162 +++++++++++++++++++ package-lock.json | 399 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 27 ++++ utils.js | 71 +++++++++ 6 files changed, 789 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 genesis.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 utils.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..abf8dad --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +.DS_Store +node_modules/ +dist/* +dist +build +.vscode +*.log +*.rar +*.zip +.svn/* +obj/* +*.bak +config.js +**/env/prod.js +note.txt +term.txt +/coverage +coverage.json +temp \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2193fe2 --- /dev/null +++ b/README.md @@ -0,0 +1,111 @@ +## Create Genesis Block Proof with Node.js + + +## setup + +```js +git clone https://github.com/nasa8x/node-genesis-block.git genesis-block +cd genesis-block +npm install +``` + +## help + +```js + Options: + -t TIME, --time=TIME the (unix) time when the genesisblock is created + -z TIMESTAMP, --timestamp=TIMESTAMP + the pszTimestamp found in the coinbase of the genesisblock + -n NONCE, --nonce=NONCE + the first value of the nonce that will be incremented + when searching the genesis hash + -a ALGORITHM, --algorithm=ALGORITHM + the PoW algorithm: [x11|neoscrypt|quark|qubit|keccak|lyra2re] + -p PUBKEY, --pubkey=PUBKEY + the pubkey found in the output script + -v VALUE, --value=VALUE + the value in coins for the output, full value (exp. in bitcoin 5000000000 - To get other coins value: Block Value * 100000000) + -b BITS, --bits=BITS + the target in compact representation, associated to a difficulty of 1 +``` + + +## algorithms +---------- +* x11 +* x13 +* x15 +* x16r +* nist5 +* neoscrypt +* scrypt +* keccak +* quark +* bcrypt +* skein +* groestl +* groestlmyriad +* blake +* fugue +* qubit +* hefty1 +* shavite3 +* cryptonight +* boolberry +* yescrypt +* fresh + + +## x11 + +```js +node genesis -a x11 -z "Don't work for weekends, work for our goals - 18/Jan/2018." -p "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" + +// result: +algorithm: x11 +pzTimestamp: Don't work for weekends, work for our goals - 18/Jan/2018. +pubkey: 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f +bits: 504365040 +time: 1521537891 +merkle root hash: 77fc7d6cfbc4ec91703444a515955092d4b7c04dbb8f23be59deb42a39ec0057 +Searching for genesis hash... +nonce: 1827816 +genesis hash: 0000084e98003628c45719136940cf7068805f4024419a51d6259fb676c299da + +``` + + +## Genesis Block Proof of Work for Keccak Algorithms. + +```js +python gen.py -a keccak -t 1517542208 -z "Don't work for weekends, work for our goals - 18/Jan/2018." -p "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" + +``` + +## Genesis Block Proof of Work for NeoScrypt Algorithms. + +```js +python gen.py -a neoscript -t 1517542208 -z "Don't work for weekends, work for our goals - 18/Jan/2018." -p "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" + +``` + +## Genesis Block Proof of Work for Qubit Algorithms. + +```js +python gen.py -a qubit -t 1517542208 -z "Don't work for weekends, work for our goals - 18/Jan/2018." -p "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" + +``` + +## Genesis Block Proof of Work for Quark Algorithms. + +```js +python gen.py -a quark -t 1517542208 -z "Don't work for weekends, work for our goals - 18/Jan/2018." -p "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" + +``` + +## Genesis Block Proof of Work for Lyra2re Algorithms. + +```js +python gen.py -a lyra2re -t 1517542208 -z "Don't work for weekends, work for our goals - 18/Jan/2018." -p "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" + +``` diff --git a/genesis.js b/genesis.js new file mode 100644 index 0000000..d8718f5 --- /dev/null +++ b/genesis.js @@ -0,0 +1,162 @@ + + +var $ = require("./utils"); +var Hash = require('multi-hashing'); + +var defaults = { + //the (unix) time when the genesisblock is created + time: Math.round((new Date()).getTime() / 1000), + //the pszTimestamp found in the coinbase of the genesisblock + timestamp: "Don't work for weekends, work for our goals.", + //the first value of the nonce that will be incremented when searching the genesis hash + nonce: 1, + //the PoW algorithm: [x11|x13|x15|geek|quark|keccak|qubit|neoscrypt|lyra2re...] + algorithm: 'geek', + //the pubkey found in the output script + pubkey: '04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f', + //the value in coins for the output, full value (exp. in bitcoin 5000000000 - To get other coins value: Block Value * 100000000) + value: 5000000000, + //the target in compact representation, associated to a difficulty of 1 + bits: 0x1e0ffff0, + locktime: 0 +} + +const argv = require('yargs') + .alias('t', 'time') + .alias('z', 'timestamp') + .alias('n', 'nonce') + .alias('a', 'algorithm') + .alias('p', 'pubkey') + .alias('v', 'value') + .alias('b', 'bits') + .alias('l', 'locktime') + .alias('h', 'help') + .help() + .command('*', 'create genesis block', () => { }, (argv) => { + //console.log(argv); + var options = Object.assign({}, defaults, argv); + //console.log(options); + + var merkle_root = $.sha256d(createTx(options)); + var genesisblock = createBlock(merkle_root, options); + console.log("---------------"); + console.log("algorithm: %s", options.algorithm); + console.log("pzTimestamp: %s", options.timestamp); + console.log("pubkey: %s", options.pubkey); + console.log("bits: %s", options.bits); + console.log("time: %s", options.time); + console.log("merkle root hash: %s", $.reverseBuffer(merkle_root).toString('hex')); + + PoW(genesisblock, options); + //console.log("genesis block: %s", genesisblock.toString('hex')); + // console.log($.reverseBuffer(hash_merkle_root).toString('hex')); + + }) + .argv; + + +function createInputScript(options) { + var tz = options.timestamp; + var psz_prefix = tz.length > 76 ? '4c' : ''; + var script_prefix = '04ffff001d0104' + psz_prefix + Buffer.from(String.fromCharCode(tz.length)).toString('hex'); + return Buffer.from(script_prefix + Buffer.from(tz).toString('hex'), 'hex'); +} + +function createOutputScript(options) { + return Buffer.from('41' + options.pubkey + 'ac', 'hex'); +} + + +function createTx(options) { + + var input = createInputScript(options); + var out = createOutputScript(options); + + var size = 4 // tx version + + 1 // number of inputs + + 32 // hash of previous output + + 4 // previous output's index + + 1 // 1 byte for the size of scriptSig + + input.length + + 4 // size of sequence + + 1 // number of outputs + + 8 // 8 bytes for coin value + + 1 // 1 byte to represent size of the pubkey Script + + out.length + + 4; // 4 bytes for lock time + + + var tx = Buffer.alloc(size); + var position = 0; + tx.writeIntLE(1, position, true); + tx.writeIntLE(1, position += 4, true); + tx.write(new Buffer(32).toString('hex'), position += 1, 32, 'hex'); + tx.writeInt32LE(0xFFFFFFFF, position += 32, true); + tx.writeIntLE(input.length, position += 4, true); + tx.write(input.toString('hex'), position += 1, input.length, "hex"); + tx.writeInt32LE(0xFFFFFFFF, position += input.length, true); + tx.writeIntLE(1, position += 4); + tx.write(Buffer.from($.numToBytes(options.value)).toString('hex'), position += 1, 8, 'hex'); // 50 * coin + tx.writeInt32LE(0x43, position += 8); + //tx.write(input.toString('hex'), position += 1, input.length, "hex"); + tx.write(out.toString('hex'), position += 1, out.length, "hex"); + tx.writeIntLE(options.locktime, position += out.length); + + + //console.log(tx.toString('hex')); + + return tx; + +}; + + +function createBlock(merkleRoot, options) { + + var block = Buffer.alloc(80); + var position = 0; + + block.writeIntLE(1, position); //version + block.write(new Buffer(32).toString('hex'), position += 4, 32, 'hex'); //previousblockhash + block.write(merkleRoot.toString('hex'), position += 32, 32, 'hex'); + block.writeInt32LE(options.time, position += 32); + //block.write(Buffer.from($.numToBytes(options.time)).toString('hex'), position += 32, 4, 'hex'); + + block.writeInt32LE(options.bits, position += 4); + //block.write(Buffer.from($.numToBytes(options.bits)).toString('hex'), position += 4, 4, 'hex'); + + block.writeIntLE(options.nonce, position += 4); + + return block; + +}; + +function PoW(data, options) { + + console.log('Searching for genesis hash...'); + var nonce = options.nonce; + + //var target = $.numToBytes((options.bits & 0xffffff) * 2 ** (8 * ((options.bits >> 24) - 3))); + // console.log('' + target.toString('hex')); + + while (true) { + + var hash = $.reverseBuffer(Hash[options.algorithm](data)).toString('hex'); + // var hash2 = $.reverseBuffer(hash).toString('hex'); + // var val = parseInt(hash2, 16); + + if (hash.match(/^00000/)) { + console.log("nonce: %s", nonce); + console.log("genesis hash: %s", hash); + return; + } else { + + nonce += 1; + // if (nonce % 2000 == 0) { + // console.log("nonce: %s | hash: %s ", nonce, hash.toString('hex')); + // } + data.writeInt32LE(nonce, data.length - 4); + } + + } + +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..bbfef9f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,399 @@ +{ + "name": "genesis-block", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ansi-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", + "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=" + }, + "bignumber.js": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-6.0.0.tgz", + "integrity": "sha512-x247jIuy60/+FtMRvscqfxtVHQf8AGx2hm9c6btkgC0x/hp9yt+teISNhvF8WlwRkCc5yF2fDECH8SIMe8j+GA==" + }, + "bindings": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.3.0.tgz", + "integrity": "sha512-DpLh5EzMR2kzvX1KIlVC0VkC3iZtHKTgdtZ0a3pglBZdaQFjt5S9g9xd1lE+YvXyfd6mtCeRnrUfOLYiTMlNSw==" + }, + "bn.js": { + "version": "4.11.8", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.8.tgz", + "integrity": "sha512-ItfYfPLkWHUjckQCk8xC+LwxgK8NYcXywGigJgSwOP8Y2iyWT4f2vsZnoOXTTbo+o5yXmIUJ4gn5538SO5S3gA==" + }, + "bytebuffer": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/bytebuffer/-/bytebuffer-5.0.1.tgz", + "integrity": "sha1-WC7qSxqHO20CCkjVjfhfC7ps/d0=", + "requires": { + "long": "3.2.0" + } + }, + "camelcase": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-4.1.0.tgz", + "integrity": "sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0=" + }, + "cliui": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-4.0.0.tgz", + "integrity": "sha512-nY3W5Gu2racvdDk//ELReY+dHjb9PlIcVDFXP72nVIhq2Gy3LuVXYwJoPVudwQnv1shtohpgkdCKT2YaKY0CKw==", + "requires": { + "string-width": "2.1.1", + "strip-ansi": "4.0.0", + "wrap-ansi": "2.1.0" + } + }, + "code-point-at": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", + "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=" + }, + "cross-spawn": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz", + "integrity": "sha1-6L0O/uWPz/b4+UUQoKVUu/ojVEk=", + "requires": { + "lru-cache": "4.1.2", + "shebang-command": "1.2.0", + "which": "1.3.0" + } + }, + "crypto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz", + "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==" + }, + "decamelize": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", + "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=" + }, + "execa": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-0.7.0.tgz", + "integrity": "sha1-lEvs00zEHuMqY6n68nrVpl/Fl3c=", + "requires": { + "cross-spawn": "5.1.0", + "get-stream": "3.0.0", + "is-stream": "1.1.0", + "npm-run-path": "2.0.2", + "p-finally": "1.0.0", + "signal-exit": "3.0.2", + "strip-eof": "1.0.0" + } + }, + "find-up": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", + "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", + "requires": { + "locate-path": "2.0.0" + } + }, + "get-caller-file": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.2.tgz", + "integrity": "sha1-9wLmMSfn4jHBYKgMFVSstw1QR+U=" + }, + "get-stream": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz", + "integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ=" + }, + "invert-kv": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-1.0.0.tgz", + "integrity": "sha1-EEqOSqym09jNFXqO+L+rLXo//bY=" + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=" + }, + "is-stream": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", + "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" + }, + "isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" + }, + "lcid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-1.0.0.tgz", + "integrity": "sha1-MIrMr6C8SDo4Z7S28rlQYlHRuDU=", + "requires": { + "invert-kv": "1.0.0" + } + }, + "locate-path": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-2.0.0.tgz", + "integrity": "sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=", + "requires": { + "p-locate": "2.0.0", + "path-exists": "3.0.0" + } + }, + "long": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/long/-/long-3.2.0.tgz", + "integrity": "sha1-2CG3E4yhy1gcFymQ7xTbIAtcR0s=" + }, + "lru-cache": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.2.tgz", + "integrity": "sha512-wgeVXhrDwAWnIF/yZARsFnMBtdFXOg1b8RIrhilp+0iDYN4mdQcNZElDZ0e4B64BhaxeQ5zN7PMyvu7we1kPeQ==", + "requires": { + "pseudomap": "1.0.2", + "yallist": "2.1.2" + } + }, + "mem": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz", + "integrity": "sha1-Xt1StIXKHZAP5kiVUFOZoN+kX3Y=", + "requires": { + "mimic-fn": "1.2.0" + } + }, + "mimic-fn": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-1.2.0.tgz", + "integrity": "sha512-jf84uxzwiuiIVKiOLpfYk7N46TSy8ubTonmneY9vrpHNAnp0QBt2BxWV9dO3/j+BoVAb+a5G6YDPW3M5HOdMWQ==" + }, + "multi-hashing": { + "version": "git+https://github.com/nasa8x/node-multi-hashing.git#d04972dd1dfa014f18a6c032199bf7239ed98d5f", + "requires": { + "bignumber.js": "6.0.0", + "bindings": "1.3.0", + "bn.js": "4.11.8", + "bytebuffer": "5.0.1", + "nan": "2.10.0", + "yargs": "11.0.0" + } + }, + "nan": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.10.0.tgz", + "integrity": "sha512-bAdJv7fBLhWC+/Bls0Oza+mvTaNQtP+1RyhhhvD95pgUJz6XM5IzgmxOkItJ9tkoCiplvAnXI1tNmmUD/eScyA==" + }, + "npm-run-path": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz", + "integrity": "sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=", + "requires": { + "path-key": "2.0.1" + } + }, + "number-is-nan": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", + "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" + }, + "os-locale": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-2.1.0.tgz", + "integrity": "sha512-3sslG3zJbEYcaC4YVAvDorjGxc7tv6KVATnLPZONiljsUncvihe9BQoVCEs0RZ1kmf4Hk9OBqlZfJZWI4GanKA==", + "requires": { + "execa": "0.7.0", + "lcid": "1.0.0", + "mem": "1.1.0" + } + }, + "p-finally": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", + "integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=" + }, + "p-limit": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-1.2.0.tgz", + "integrity": "sha512-Y/OtIaXtUPr4/YpMv1pCL5L5ed0rumAaAeBSj12F+bSlMdys7i8oQF/GUJmfpTS/QoaRrS/k6pma29haJpsMng==", + "requires": { + "p-try": "1.0.0" + } + }, + "p-locate": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-2.0.0.tgz", + "integrity": "sha1-IKAQOyIqcMj9OcwuWAaA893l7EM=", + "requires": { + "p-limit": "1.2.0" + } + }, + "p-try": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-1.0.0.tgz", + "integrity": "sha1-y8ec26+P1CKOE/Yh8rGiN8GyB7M=" + }, + "path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" + }, + "path-key": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", + "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=" + }, + "pseudomap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" + }, + "require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=" + }, + "require-main-filename": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-1.0.1.tgz", + "integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE=" + }, + "set-blocking": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", + "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=" + }, + "shebang-command": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", + "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", + "requires": { + "shebang-regex": "1.0.0" + } + }, + "shebang-regex": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", + "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=" + }, + "signal-exit": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", + "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=" + }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "requires": { + "is-fullwidth-code-point": "2.0.0", + "strip-ansi": "4.0.0" + } + }, + "strip-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", + "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", + "requires": { + "ansi-regex": "3.0.0" + } + }, + "strip-eof": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", + "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=" + }, + "which": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz", + "integrity": "sha512-xcJpopdamTuY5duC/KnTTNBraPK54YwpenP4lzxU8H91GudWpFv38u0CKjclE1Wi2EH2EDz5LRcHcKbCIzqGyg==", + "requires": { + "isexe": "2.0.0" + } + }, + "which-module": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz", + "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=" + }, + "wrap-ansi": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", + "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", + "requires": { + "string-width": "1.0.2", + "strip-ansi": "3.0.1" + }, + "dependencies": { + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", + "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", + "requires": { + "number-is-nan": "1.0.1" + } + }, + "string-width": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", + "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", + "requires": { + "code-point-at": "1.1.0", + "is-fullwidth-code-point": "1.0.0", + "strip-ansi": "3.0.1" + } + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "requires": { + "ansi-regex": "2.1.1" + } + } + } + }, + "y18n": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-3.2.1.tgz", + "integrity": "sha1-bRX7qITAhnnA136I53WegR4H+kE=" + }, + "yallist": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", + "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" + }, + "yargs": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-11.0.0.tgz", + "integrity": "sha512-Rjp+lMYQOWtgqojx1dEWorjCofi1YN7AoFvYV7b1gx/7dAAeuI4kN5SZiEvr0ZmsZTOpDRcCqrpI10L31tFkBw==", + "requires": { + "cliui": "4.0.0", + "decamelize": "1.2.0", + "find-up": "2.1.0", + "get-caller-file": "1.0.2", + "os-locale": "2.1.0", + "require-directory": "2.1.1", + "require-main-filename": "1.0.1", + "set-blocking": "2.0.0", + "string-width": "2.1.1", + "which-module": "2.0.0", + "y18n": "3.2.1", + "yargs-parser": "9.0.2" + } + }, + "yargs-parser": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-9.0.2.tgz", + "integrity": "sha1-nM9qQ0YP5O1Aqbto9I1DuKaMwHc=", + "requires": { + "camelcase": "4.1.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..e46adca --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "genesis-block", + "version": "1.0.0", + "description": "Create Genesis Block Proof of Work for Bitcoin, Dash, Litecoin, Zcash...", + "main": "genesis.js", + "scripts": { + + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/nasa8x/node-genesis-block.git" + }, + "keywords": [ + "genesisblock" + ], + "author": "Nasa8x", + "license": "MIT", + "bugs": { + "url": "https://github.com/nasa8x/node-genesis-block/issues" + }, + "homepage": "https://github.com/nasa8x/node-genesis-block#readme", + "dependencies": { + "multi-hashing": "https://github.com/nasa8x/node-multi-hashing.git", + "crypto": "^1.0.1" + } +} diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..2cf5d13 --- /dev/null +++ b/utils.js @@ -0,0 +1,71 @@ +"use strict"; +var crypto = require('crypto'); + +function swapHex(value) { + let s = value.toString(16); + s = s.replace(/^(.(..)*)$/, "0$1"); + var a = s.match(/../g); + a.reverse(); + var s2 = a.join(""); + return s2; +} + +function numToBytes(num, bytes) { + if (bytes === undefined) bytes = 8; + if (bytes == 0) return []; + else return [num % 256].concat(numToBytes(Math.floor(num / 256), bytes - 1)); +} + +function numToVarInt(num) { + if (num < 253) return [num]; + else if (num < 65536) return [253].concat(numToBytes(num, 2)); + else if (num < 4294967296) return [254].concat(numToBytes(num, 4)); + else return [253].concat(numToBytes(num, 8)); +} + +function hexToBytes(hex) { + for (var bytes = [], c = 0; c < hex.length; c += 2) + bytes.push(parseInt(hex.substr(c, 2), 16)); + return bytes; +} + +function bytesToHex(bytes) { + for (var hex = [], i = 0; i < bytes.length; i++) { + hex.push((bytes[i] >>> 4).toString(16)); + hex.push((bytes[i] & 0xf).toString(16)); + } + return hex.join(""); +} +function bytesLen(num) { + return Math.ceil(num.toString(2).length / 8); +} + + +function sha256(buffer) { + var hash1 = crypto.createHash('sha256'); + hash1.update(buffer); + return hash1.digest(); +}; + +function sha256d(buffer) { + return sha256(sha256(buffer)); +}; + +function reverseBuffer(buff) { + var reversed = Buffer.alloc(buff.length); + for (var i = buff.length - 1; i >= 0; i--) + reversed[buff.length - i - 1] = buff[i]; + return reversed; +}; + +module.exports = { + swapHex, + numToBytes, + numToVarInt, + hexToBytes, + bytesToHex, + bytesLen, + sha256, + sha256d, + reverseBuffer +}; \ No newline at end of file