Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d405a871a4 | |||
| de78291246 | |||
| 04ba92e6fd | |||
| 9021066354 | |||
| 7c139874ce | |||
| 58f8aeb67b | |||
| 2c6f6e6dd2 | |||
| 8729782845 | |||
| df11d9c2bf | |||
| 9d722a83a3 | |||
| 16acb844d7 | |||
| 1647e8ccd6 | |||
| 95870f3f47 | |||
| f8941cbe83 | |||
| a0153d8eb3 | |||
| 3ddf060490 | |||
| 2805366502 | |||
| 8e2572248d | |||
| 0fc0e0ac9f | |||
| e4d961cd99 | |||
| 18eedca1e9 | |||
| a697f0fa32 | |||
| bc501fcb4d | |||
| fb0443bb96 |
@@ -6,8 +6,6 @@ const bitcoin = require('bitcoinjs-lib');
|
||||
const varuint = require('varuint-bitcoin');
|
||||
const crypto = require('crypto');
|
||||
const fastMerkleRoot = require('merkle-lib/fastRoot');
|
||||
const promise = require('promise');
|
||||
const merklebitcoin = promise.denodeify(require('merkle-bitcoin'));
|
||||
|
||||
function scriptCompile(addrHash) {
|
||||
return bitcoin.script.compile([
|
||||
@@ -25,15 +23,6 @@ function reverseBuffer(buff) {
|
||||
return reversed;
|
||||
}
|
||||
|
||||
function getMerkleRoot(rpcData, generateTxRaw) {
|
||||
hashes = [ reverseBuffer(new Buffer(generateTxRaw, 'hex')).toString('hex') ];
|
||||
rpcData.transactions.forEach(function (value) {
|
||||
hashes.push(value.txid ? value.txid : value.hash);
|
||||
});
|
||||
if (hashes.length === 1) return hashes[0];
|
||||
return Object.values(merklebitcoin(hashes))[2].root;
|
||||
}
|
||||
|
||||
function txesHaveWitnessCommit(transactions) {
|
||||
return (
|
||||
transactions instanceof Array &&
|
||||
@@ -47,27 +36,43 @@ function txesHaveWitnessCommit(transactions) {
|
||||
);
|
||||
}
|
||||
|
||||
function getMerkleRoot2(transactions) {
|
||||
if (transactions.length === 0) return null;
|
||||
function sha256(buffer) {
|
||||
return crypto.createHash('sha256').update(buffer).digest();
|
||||
};
|
||||
|
||||
function hash256(buffer) {
|
||||
return sha256(sha256(buffer));
|
||||
};
|
||||
|
||||
function getMerkleRoot(transactions) {
|
||||
if (transactions.length === 0) return new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
||||
const forWitness = txesHaveWitnessCommit(transactions);
|
||||
const hashes = transactions.map(transaction => transaction.getHash(forWitness));
|
||||
const hash256 = function (buffer) { return crypto.createHash('sha256').update(buffer).digest(); };
|
||||
const rootHash = fastMerkleRoot(hashes, hash256);
|
||||
return forWitness ? hash256(Buffer.concat([rootHash, transactions[0].ins[0].witness[0]])) : rootHash;
|
||||
}
|
||||
|
||||
let last_epoch_number;
|
||||
let last_seed_hash;
|
||||
const diff1 = 0x00000000ff000000000000000000000000000000000000000000000000000000;
|
||||
|
||||
module.exports.baseDiff = function() {
|
||||
return bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 16);
|
||||
};
|
||||
|
||||
module.exports.baseRavenDiff = function() {
|
||||
return parseInt('0x00000000ff000000000000000000000000000000000000000000000000000000');
|
||||
};
|
||||
|
||||
module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
|
||||
const poolAddrHash = bitcoin.address.fromBase58Check(poolAddress).hash;
|
||||
|
||||
let txCoinbase = new bitcoin.Transaction();
|
||||
let bytesHeight;
|
||||
{ // input for coinbase tx
|
||||
let blockHeightSerial = rpcData.height.toString(16).length % 2 === 0 ?
|
||||
rpcData.height.toString(16) :
|
||||
rpcData.height.toString(16) :
|
||||
'0' + rpcData.height.toString(16);
|
||||
const bytesHeight = Math.ceil((rpcData.height << 1).toString(2).length / 8);
|
||||
bytesHeight = Math.ceil((rpcData.height << 1).toString(2).length / 8);
|
||||
const lengthDiff = blockHeightSerial.length/2 - bytesHeight;
|
||||
for (let i = 0; i < lengthDiff; i++) blockHeightSerial = blockHeightSerial + '00';
|
||||
const serializedBlockHeight = new Buffer.concat([
|
||||
@@ -80,7 +85,7 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
|
||||
// will be used for our reserved_offset extra_nonce
|
||||
new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex'),
|
||||
0xFFFFFFFF, 0xFFFFFFFF,
|
||||
new Buffer.concat([serializedBlockHeight, Buffer('6b6177706f77', 'hex')])
|
||||
new Buffer.concat([serializedBlockHeight, Buffer('CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC', 'hex')]) // 17 bytes
|
||||
);
|
||||
|
||||
txCoinbase.addOutput(scriptCompile(poolAddrHash), Math.floor(rpcData.coinbasevalue));
|
||||
@@ -89,14 +94,13 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
|
||||
txCoinbase.addOutput(new Buffer(rpcData.default_witness_commitment, 'hex'), 0);
|
||||
}
|
||||
}
|
||||
const merkleRoot = getMerkleRoot(rpcData, txCoinbase.getHash().toString('hex'));
|
||||
|
||||
let header = new Buffer(80);
|
||||
{ let position = 0;
|
||||
header.writeUInt32BE(rpcData.height, position, 4); // height 42-46
|
||||
header.write(rpcData.bits, position += 4, 4, 'hex'); // bits 47-50
|
||||
header.writeUInt32BE(rpcData.curtime, position += 4, 4, 'hex'); // nTime 51-54
|
||||
header.write(merkleRoot, position += 4, 32, 'hex'); // merkelRoot 55-87
|
||||
header.write('DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD', position += 4, 32, 'hex'); // merkelRoot 55-87
|
||||
header.write(rpcData.previousblockhash, position += 32, 32, 'hex'); // prevblockhash 88-120
|
||||
header.writeUInt32BE(rpcData.version, position += 32, 4); // version 121-153
|
||||
header = reverseBuffer(header);
|
||||
@@ -104,8 +108,8 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
|
||||
|
||||
let blob = new Buffer.concat([
|
||||
header, // 80 bytes
|
||||
new Buffer('EEEEEEEEEEEEEEEE', 'hex'), // 8 bytes
|
||||
new Buffer('EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE', 'hex'), // 32 bytes
|
||||
new Buffer('AAAAAAAAAAAAAAAA', 'hex'), // 8 bytes
|
||||
new Buffer('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB', 'hex'), // 32 bytes
|
||||
varuint.encode(rpcData.transactions.length + 1, new Buffer(varuint.encodingLength(rpcData.transactions.length + 1)), 0)
|
||||
]);
|
||||
const offset1 = blob.length;
|
||||
@@ -131,34 +135,42 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
|
||||
last_epoch_number = epoch_number;
|
||||
}
|
||||
|
||||
const difficulty = parseFloat((diff1 / bignum(rpcData.target, 16).toNumber()).toFixed(9));
|
||||
const difficulty = parseFloat((module.exports.baseRavenDiff() / bignum(rpcData.target, 16).toNumber()).toFixed(9));
|
||||
|
||||
return {
|
||||
blocktemplate_blob: blob.toString('hex'),
|
||||
reserved_offset: offset1 + 4 /* txCoinbase.version */ + 1 /* txCoinbase.vinLen */,
|
||||
// reserved_offset to CCCCCC....
|
||||
reserved_offset: offset1 + 4 /* txCoinbase.version */ + 1 /* vinLen */ + 32 /* hash */ + 4 /* index */ +
|
||||
1 /* vScript len */ + 1 /* coinbase height len */ + bytesHeight + 1 /* trailing zero byte */,
|
||||
seed_hash: last_seed_hash.toString('hex'),
|
||||
difficulty: difficulty,
|
||||
height: rpcData.height,
|
||||
bits: rpcData.bits,
|
||||
prev_hash: rpcData.previousblockhash,
|
||||
};
|
||||
};
|
||||
|
||||
module.exports.convertRavenBlob = function(blobBuffer) {
|
||||
let header = blobBuffer.slice(0, 80); // header
|
||||
function update_merkle_root_hash(blob_in, blob_out) {
|
||||
let offset = 80 + 8 + 32;
|
||||
const nTransactions = varuint.decode(blobBuffer, offset);
|
||||
const nTransactions = varuint.decode(blob_in, offset);
|
||||
offset += varuint.decode.bytes;
|
||||
let transactions = [];
|
||||
for (let i = 0; i < nTransactions; ++i) {
|
||||
const tx = bitcoin.Transaction.fromBuffer(blobBuffer.slice(offset), true);
|
||||
const tx = bitcoin.Transaction.fromBuffer(blob_in.slice(offset), true);
|
||||
transactions.push(tx);
|
||||
offset += tx.byteLength();
|
||||
}
|
||||
getMerkleRoot2(transactions).copy(header, 4 + 32);
|
||||
return reverseBuffer(crypto.createHash('sha256').update(header).digest());
|
||||
getMerkleRoot(transactions).copy(blob_out, 4 + 32);
|
||||
};
|
||||
|
||||
module.exports.convertRavenBlob = function(blobBuffer) {
|
||||
let header = blobBuffer.slice(0, 80);
|
||||
update_merkle_root_hash(blobBuffer, header);
|
||||
return reverseBuffer(hash256(header));
|
||||
};
|
||||
|
||||
module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) {
|
||||
update_merkle_root_hash(blockTemplate, blockTemplate);
|
||||
nonceBuff.copy (blockTemplate, 80, 0, 8);
|
||||
mixhashBuff.copy(blockTemplate, 88, 0, 32);
|
||||
return blockTemplate;
|
||||
@@ -167,4 +179,14 @@ module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhas
|
||||
module.exports.constructNewDeroBlob = function(blockTemplate, nonceBuff) {
|
||||
nonceBuff.copy(blockTemplate, 39, 0, 4);
|
||||
return blockTemplate;
|
||||
};
|
||||
|
||||
module.exports.EthBlockTemplate = function(rpcData) {
|
||||
const difficulty = (module.exports.baseDiff() / bignum(rpcData[2].substr(2), 16)).toNumber();
|
||||
return {
|
||||
hash: rpcData[0].substr(2),
|
||||
seed_hash: rpcData[1].substr(2),
|
||||
difficulty: difficulty,
|
||||
height: parseInt(rpcData[3])
|
||||
};
|
||||
};
|
||||
+2
-3
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "cryptoforknote-util",
|
||||
"version": "9.0.1",
|
||||
"version": "9.1.3",
|
||||
"main": "cryptoforknote-util",
|
||||
"author": {
|
||||
"name": "LucasJones",
|
||||
@@ -17,8 +17,7 @@
|
||||
"bignum": "^0.13.1",
|
||||
"sha3": "*",
|
||||
"varuint-bitcoin": "^1.0.4",
|
||||
"bitcoinjs-lib": "git+https://github.com/bitcoinjs/bitcoinjs-lib.git#533d6c2e6d0aa4111f7948b1c12003cf6ef83137",
|
||||
"merkle-bitcoin": "git+https://github.com/joshuayabut/merkle-bitcoin.git#ec00ae20ba60d444e150ead03c747695ddaa83a1"
|
||||
"bitcoinjs-lib": "git+https://github.com/bitcoinjs/bitcoinjs-lib.git#533d6c2e6d0aa4111f7948b1c12003cf6ef83137"
|
||||
},
|
||||
"keywords": [
|
||||
"cryptonight",
|
||||
|
||||
Reference in New Issue
Block a user