Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6bb5e00c17 | |||
| 7bbb0cf80e | |||
| 16f9569d0c | |||
| e6143eb9c0 | |||
| 1b2f6af8f8 | |||
| eb61aefe8b | |||
| 1d0ada1c82 | |||
| 0bb1785826 | |||
| 1c48ad7e46 | |||
| f0c26e6d5b | |||
| 7a1d7271a1 | |||
| 30e051fa46 | |||
| 4ccd4fdca7 | |||
| 71bda2c8bb | |||
| 261c518133 | |||
| 2a1741ac52 | |||
| 1f59698bda | |||
| 3238964d2a | |||
| 85260f0281 | |||
| 8c944e469c | |||
| dae35d962a | |||
| 89fc132363 | |||
| af3cc3e902 |
@@ -46,10 +46,28 @@ function hash256(buffer) {
|
||||
return sha256(sha256(buffer));
|
||||
};
|
||||
|
||||
function getMerkleRoot(transactions) {
|
||||
function sha256_3(buffer) {
|
||||
return crypto.createHash('sha3-256').update(buffer).digest();
|
||||
};
|
||||
|
||||
function hash256_3(buffer) {
|
||||
return sha256_3(sha256_3(buffer));
|
||||
};
|
||||
|
||||
function transaction_hash(transaction, forWitness) {
|
||||
if (forWitness && transaction.isCoinbase()) return Buffer.alloc(32, 0);
|
||||
return hash256(transaction.__toBuffer(undefined, undefined, forWitness));
|
||||
}
|
||||
|
||||
function transaction_hash3(transaction, forWitness) {
|
||||
if (forWitness && transaction.isCoinbase()) return Buffer.alloc(32, 0);
|
||||
return hash256_3(transaction.__toBuffer(undefined, undefined, forWitness));
|
||||
}
|
||||
|
||||
function getMerkleRoot(transactions, transaction_hash_func, detectWitness) {
|
||||
if (transactions.length === 0) return Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
||||
const forWitness = txesHaveWitnessCommit(transactions);
|
||||
const hashes = transactions.map(transaction => transaction.getHash(forWitness));
|
||||
const forWitness = detectWitness ? txesHaveWitnessCommit(transactions) : false;
|
||||
const hashes = transactions.map(transaction => transaction_hash_func(transaction, forWitness));
|
||||
const rootHash = fastMerkleRoot(hashes, hash256);
|
||||
return forWitness ? hash256(Buffer.concat([rootHash, transactions[0].ins[0].witness[0]])) : rootHash;
|
||||
}
|
||||
@@ -157,7 +175,7 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
|
||||
};
|
||||
};
|
||||
|
||||
function update_merkle_root_hash(offset, payload, blob_in, blob_out) {
|
||||
function update_merkle_root_hash(offset, payload, blob_in, blob_out, transaction_hash_func, detectWitness) {
|
||||
const nTransactions = varuint.decode(blob_in, offset);
|
||||
offset += varuint.decode.bytes;
|
||||
let transactions = [];
|
||||
@@ -166,21 +184,25 @@ function update_merkle_root_hash(offset, payload, blob_in, blob_out) {
|
||||
transactions.push(tx);
|
||||
offset += tx.byteLength();
|
||||
}
|
||||
getMerkleRoot(transactions).copy(blob_out, 4 + 32);
|
||||
getMerkleRoot(transactions, transaction_hash_func, detectWitness).copy(blob_out, 4 + 32);
|
||||
};
|
||||
|
||||
module.exports.blockHashBuff = function(blobBuffer) {
|
||||
return reverseBuffer(hash256(blobBuffer));
|
||||
};
|
||||
|
||||
module.exports.blockHashBuff3 = function(blobBuffer) {
|
||||
return reverseBuffer(hash256_3(blobBuffer));
|
||||
};
|
||||
|
||||
module.exports.convertRavenBlob = function(blobBuffer) {
|
||||
let header = blobBuffer.slice(0, 80);
|
||||
update_merkle_root_hash(80 + 8 + 32, false, blobBuffer, header);
|
||||
update_merkle_root_hash(80 + 8 + 32, false, blobBuffer, header, transaction_hash, true);
|
||||
return module.exports.blockHashBuff(header);
|
||||
};
|
||||
|
||||
module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) {
|
||||
update_merkle_root_hash(80 + 8 + 32, false, blockTemplate, blockTemplate);
|
||||
update_merkle_root_hash(80 + 8 + 32, false, blockTemplate, blockTemplate, transaction_hash, true);
|
||||
nonceBuff.copy (blockTemplate, 80, 0, 8);
|
||||
mixhashBuff.copy(blockTemplate, 88, 0, 32);
|
||||
return blockTemplate;
|
||||
@@ -217,12 +239,24 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
|
||||
|
||||
module.exports.convertRtmBlob = function(blobBuffer) {
|
||||
let header = blobBuffer.slice(0, 80);
|
||||
update_merkle_root_hash(80, true, blobBuffer, header);
|
||||
update_merkle_root_hash(80, true, blobBuffer, header, transaction_hash, true);
|
||||
return header;
|
||||
};
|
||||
|
||||
module.exports.convertKcnBlob = function(blobBuffer) {
|
||||
let header = blobBuffer.slice(0, 80);
|
||||
update_merkle_root_hash(80, false, blobBuffer, header, transaction_hash3, false);
|
||||
return header;
|
||||
};
|
||||
|
||||
module.exports.constructNewRtmBlob = function(blockTemplate, nonceBuff) {
|
||||
update_merkle_root_hash(80, true, blockTemplate, blockTemplate);
|
||||
update_merkle_root_hash(80, true, blockTemplate, blockTemplate, transaction_hash, true);
|
||||
nonceBuff.copy(blockTemplate, 76, 0, 4);
|
||||
return blockTemplate;
|
||||
};
|
||||
|
||||
module.exports.constructNewKcnBlob = function(blockTemplate, nonceBuff) {
|
||||
update_merkle_root_hash(80, false, blockTemplate, blockTemplate, transaction_hash3, false);
|
||||
nonceBuff.copy(blockTemplate, 76, 0, 4);
|
||||
return blockTemplate;
|
||||
};
|
||||
|
||||
+2
-2
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"name": "cryptoforknote-util",
|
||||
"version": "15.2.1",
|
||||
"main": "cryptoforknote-util",
|
||||
"version": "15.3.20",
|
||||
"author": {
|
||||
"name": "LucasJones",
|
||||
"email": "lucasjonesdev@hotmail.co.uk"
|
||||
@@ -17,6 +16,7 @@
|
||||
"bignum": "^0.13.1",
|
||||
"sha3": "*",
|
||||
"base58-native": "*",
|
||||
"bech32": "*",
|
||||
"varuint-bitcoin": "^1.0.4",
|
||||
"merkle-lib": "^2.0.10",
|
||||
"bitcoinjs-lib": "git+https://github.com/MoneroOcean/bitcoinjs-lib.git"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const bignum = require('bignum');
|
||||
const base58 = require('base58-native');
|
||||
const bech32 = require('bech32');
|
||||
const bitcoin = require('bitcoinjs-lib');
|
||||
|
||||
const diff1 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
|
||||
@@ -148,14 +149,20 @@ function getTransactionBuffers(txs) {
|
||||
}
|
||||
|
||||
function addressToScript(addr) {
|
||||
const decoded = base58.decode(addr);
|
||||
if (decoded.length != 25) throw new Error('Invalid address length for ' + addr);
|
||||
if (!decoded) throw new Error('Base58 decode failed for ' + addr);
|
||||
let decoded;
|
||||
try {
|
||||
decoded = base58.decode(addr);
|
||||
} catch(err) {}
|
||||
if (!decoded || decoded.length != 25) {
|
||||
const decoded2 = Buffer.from(bech32.bech32.fromWords(bech32.bech32.decode(addr).words.slice(1)));
|
||||
if (decoded2.length != 20) throw new Error('Invalid address ' + addr);
|
||||
return Buffer.concat([Buffer.from([0x0, 0x14]), decoded2]);
|
||||
}
|
||||
const pubkey = decoded.slice(1, -4);
|
||||
return Buffer.concat([Buffer.from([0x76, 0xa9, 0x14]), pubkey, Buffer.from([0x88, 0xac])]);
|
||||
}
|
||||
|
||||
function createOutputTransaction(amount, payee, rewardToPool, reward, txOutputBuffers, payeeScript) {
|
||||
function createTransactionOutput(amount, payee, rewardToPool, reward, txOutputBuffers, payeeScript) {
|
||||
const payeeReward = amount;
|
||||
if (!payeeScript) payeeScript = addressToScript(payee);
|
||||
txOutputBuffers.push(Buffer.concat([
|
||||
@@ -166,19 +173,25 @@ function createOutputTransaction(amount, payee, rewardToPool, reward, txOutputBu
|
||||
return { reward: reward - amount, rewardToPool: rewardToPool - amount };
|
||||
}
|
||||
|
||||
function generateOutputTransactions(rpcData, poolAddress) {
|
||||
let reward = rpcData.coinbasevalue;
|
||||
function generateTransactionOutputs(rpcData, poolAddress) {
|
||||
let reward = rpcData.coinbasevalue + (rpcData.coinbasedevreward ? rpcData.coinbasedevreward.value : 0);
|
||||
let rewardToPool = reward;
|
||||
let txOutputBuffers = [];
|
||||
|
||||
if (rpcData.coinbasedevreward) {
|
||||
const rewards = createTransactionOutput(rpcData.coinbasedevreward.value, null, rewardToPool, reward, txOutputBuffers, Buffer.from(rpcData.coinbasedevreward.scriptpubkey, 'hex'));
|
||||
reward = rewards.reward;
|
||||
rewardToPool = rewards.rewardToPool;
|
||||
}
|
||||
|
||||
if (rpcData.smartnode) {
|
||||
if (rpcData.smartnode.payee) {
|
||||
const rewards = createOutputTransaction(rpcData.smartnode.amount, rpcData.smartnode.payee, rewardToPool, reward, txOutputBuffers);
|
||||
const rewards = createTransactionOutput(rpcData.smartnode.amount, rpcData.smartnode.payee, rewardToPool, reward, txOutputBuffers);
|
||||
reward = rewards.reward;
|
||||
rewardToPool = rewards.rewardToPool;
|
||||
} else if (Array.isArray(rpcData.smartnode)) {
|
||||
for (let i in rpcData.smartnode) {
|
||||
const rewards = createOutputTransaction(rpcData.smartnode[i].amount, rpcData.smartnode[i].payee, rewardToPool, reward, txOutputBuffers);
|
||||
const rewards = createTransactionOutput(rpcData.smartnode[i].amount, rpcData.smartnode[i].payee, rewardToPool, reward, txOutputBuffers);
|
||||
reward = rewards.reward;
|
||||
rewardToPool = rewards.rewardToPool;
|
||||
}
|
||||
@@ -187,7 +200,7 @@ function generateOutputTransactions(rpcData, poolAddress) {
|
||||
|
||||
if (rpcData.superblock) {
|
||||
for (let i in rpcData.superblock) {
|
||||
const rewards = createOutputTransaction(rpcData.superblock[i].amount, rpcData.superblock[i].payee, rewardToPool, reward, txOutputBuffers);
|
||||
const rewards = createTransactionOutput(rpcData.superblock[i].amount, rpcData.superblock[i].payee, rewardToPool, reward, txOutputBuffers);
|
||||
reward = rewards.reward;
|
||||
rewardToPool = rewards.rewardToPool;
|
||||
}
|
||||
@@ -195,41 +208,44 @@ function generateOutputTransactions(rpcData, poolAddress) {
|
||||
|
||||
if (rpcData.founder_payments_started && rpcData.founder) {
|
||||
const founderReward = rpcData.founder.amount || 0;
|
||||
const rewards = createOutputTransaction(founderReward, rpcData.founder.payee, rewardToPool, reward, txOutputBuffers);
|
||||
const rewards = createTransactionOutput(founderReward, rpcData.founder.payee, rewardToPool, reward, txOutputBuffers);
|
||||
reward = rewards.reward;
|
||||
rewardToPool = rewards.rewardToPool;
|
||||
}
|
||||
|
||||
createOutputTransaction(rewardToPool, null, rewardToPool, reward, txOutputBuffers, Buffer.from(addressToScript(poolAddress), "hex"));
|
||||
createTransactionOutput(rewardToPool, null, rewardToPool, reward, txOutputBuffers, Buffer.from(addressToScript(poolAddress), "hex"));
|
||||
|
||||
if (rpcData.default_witness_commitment !== undefined) {
|
||||
const witness_commitment = Buffer.from(rpcData.default_witness_commitment, 'hex');
|
||||
txOutputBuffers.unshift(Buffer.concat([
|
||||
packInt64LE(0),
|
||||
varIntBuffer(witness_commitment.length),
|
||||
witness_commitment
|
||||
if (rpcData.default_witness_commitment) {
|
||||
createTransactionOutput(0, null, rewardToPool, reward, txOutputBuffers, Buffer.from(rpcData.default_witness_commitment, 'hex'));
|
||||
txOutputBuffers.push(Buffer.concat([
|
||||
varIntBuffer(1),
|
||||
varIntBuffer(32),
|
||||
Buffer.alloc(32, 0)
|
||||
]));
|
||||
}
|
||||
|
||||
return Buffer.concat([ varIntBuffer(txOutputBuffers.length), Buffer.concat(txOutputBuffers)]);
|
||||
return Buffer.concat([ varIntBuffer(rpcData.default_witness_commitment ? txOutputBuffers.length - 1 : txOutputBuffers.length), Buffer.concat(txOutputBuffers)]);
|
||||
}
|
||||
|
||||
module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
|
||||
const extraNoncePlaceholderLength = 17;
|
||||
const coinbaseVersion = Buffer.concat([packUInt16LE(3), packUInt16LE(5)]);
|
||||
const coinbaseVersion = rpcData.coinbasedevreward ? Buffer.concat([packUInt16LE(1), packUInt16LE(0)]) : Buffer.concat([packUInt16LE(3), packUInt16LE(5)]);
|
||||
|
||||
const scriptSigPart1 = Buffer.concat([
|
||||
serializeNumber(rpcData.height),
|
||||
Buffer.from(rpcData.coinbaseaux.flags, 'hex'),
|
||||
Buffer.from(rpcData.coinbaseaux.flags ? rpcData.coinbaseaux.flags : "", 'hex'),
|
||||
serializeNumber(Date.now() / 1000 | 0),
|
||||
Buffer.from([extraNoncePlaceholderLength])
|
||||
]);
|
||||
|
||||
const scriptSigPart2 = serializeString('/nodeStratum/');
|
||||
|
||||
const is_witness = rpcData.default_witness_commitment !== undefined;
|
||||
|
||||
const blob1 = Buffer.concat([
|
||||
coinbaseVersion,
|
||||
// transaction input
|
||||
Buffer.from(is_witness ? "0001" : "", 'hex'),
|
||||
varIntBuffer(1), // txInputsCount
|
||||
uint256BufferFromHash(""), // txInPrevOutHash
|
||||
packUInt32LE(Math.pow(2, 32) - 1), // txInPrevOutIndex
|
||||
@@ -242,13 +258,19 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
|
||||
packUInt32LE(0), // txInSequence
|
||||
// end transaction input
|
||||
// transaction output
|
||||
generateOutputTransactions(rpcData, poolAddress),
|
||||
generateTransactionOutputs(rpcData, poolAddress, is_witness),
|
||||
// end transaction ouput
|
||||
packUInt32LE(0), // txLockTime
|
||||
varIntBuffer(rpcData.coinbase_payload.length / 2),
|
||||
Buffer.from(rpcData.coinbase_payload, 'hex')
|
||||
packUInt32LE(0) // txLockTime
|
||||
]);
|
||||
|
||||
if (rpcData.coinbase_payload) {
|
||||
blob2 = Buffer.concat([
|
||||
blob2,
|
||||
varIntBuffer(rpcData.coinbase_payload.length / 2),
|
||||
Buffer.from(rpcData.coinbase_payload, 'hex')
|
||||
]);
|
||||
}
|
||||
|
||||
const prev_hash = reverseBuffer(Buffer.from(rpcData.previousblockhash, 'hex')).toString('hex');
|
||||
const version = packInt32LE(rpcData.version).toString('hex');
|
||||
const curtime = packUInt32LE(rpcData.curtime).toString('hex');
|
||||
|
||||
Reference in New Issue
Block a user