From f4ff9bc8cb81b6c3d87131692b112a38375f2ee2 Mon Sep 17 00:00:00 2001 From: MoneroOcean Date: Tue, 30 Nov 2021 16:12:59 +0000 Subject: [PATCH] GR support --- index.js | 23 +++++++++++------- package.json | 2 +- rtm.js | 68 ++++++---------------------------------------------- 3 files changed, 22 insertions(+), 71 deletions(-) diff --git a/index.js b/index.js index 7e62b14..1c42acb 100644 --- a/index.js +++ b/index.js @@ -87,7 +87,7 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) { // will be used for our reserved_offset extra_nonce Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'), 0xFFFFFFFF, 0xFFFFFFFF, - Buffer.concat([serializedBlockHeight, Buffer('CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC', 'hex')]) // 17 bytes + Buffer.concat([serializedBlockHeight, Buffer.alloc(17, 0xCC)]) // 17 bytes ); txCoinbase.addOutput(scriptCompile(poolAddrHash), Math.floor(rpcData.coinbasevalue)); @@ -152,27 +152,28 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) { }; }; -function update_merkle_root_hash(blob_in, blob_out) { - let offset = 80 + 8 + 32; +function update_merkle_root_hash(offset, payload, blob_in, blob_out) { const nTransactions = varuint.decode(blob_in, offset); + console.log("2x" + nTransactions); offset += varuint.decode.bytes; let transactions = []; for (let i = 0; i < nTransactions; ++i) { - const tx = bitcoin.Transaction.fromBuffer(blob_in.slice(offset), true); + const tx = bitcoin.Transaction.fromBuffer(blob_in.slice(offset), true, payload && i == 0); transactions.push(tx); offset += tx.byteLength(); + console.log("2xx " + tx.byteLength()); } getMerkleRoot(transactions).copy(blob_out, 4 + 32); }; module.exports.convertRavenBlob = function(blobBuffer) { let header = blobBuffer.slice(0, 80); - update_merkle_root_hash(blobBuffer, header); + update_merkle_root_hash(80 + 8 + 32, false, blobBuffer, header); return reverseBuffer(hash256(header)); }; module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) { - update_merkle_root_hash(blockTemplate, blockTemplate); + update_merkle_root_hash(80 + 8 + 32, false, blockTemplate, blockTemplate); nonceBuff.copy (blockTemplate, 80, 0, 8); mixhashBuff.copy(blockTemplate, 88, 0, 32); return blockTemplate; @@ -207,10 +208,14 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) { return rtm.RtmBlockTemplate(rpcData, poolAddress); }; -module.exports.convertRtmBlob = function(buffer) { - return rtm.convertRtmBlob(buffer); +module.exports.convertRtmBlob = function(blobBuffer) { + let header = blobBuffer.slice(0, 80); + update_merkle_root_hash(80, true, blobBuffer, header); + return header; }; module.exports.constructNewRtmBlob = function(blockTemplate, nonceBuff) { - return rtm.constructNewRtmBlob(blockTemplate, nonceBuff); + update_merkle_root_hash(80, true, blockTemplate, blockTemplate); + nonceBuff.copy(blockTemplate, 76, 0, 4); + return blockTemplate; }; diff --git a/package.json b/package.json index f23edf8..a81ce7f 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "sha3": "*", "base58-native": "*", "varuint-bitcoin": "^1.0.4", - "bitcoinjs-lib": "git+https://github.com/bitcoinjs/bitcoinjs-lib.git#533d6c2e6d0aa4111f7948b1c12003cf6ef83137" + "bitcoinjs-lib": "git+https://github.com/MoneroOcean/bitcoinjs-lib.git" }, "keywords": [ "cryptonight", diff --git a/rtm.js b/rtm.js index 3758f42..dfead80 100644 --- a/rtm.js +++ b/rtm.js @@ -1,4 +1,3 @@ -const crypto = require('crypto'); const bignum = require('bignum'); const base58 = require('base58-native'); @@ -106,16 +105,6 @@ function serializeString(s) { } } -function sha256(buffer) { - let hash1 = crypto.createHash('sha256'); - hash1.update(buffer); - return hash1.digest(); -} - -function sha256d(buffer) { - return sha256(sha256(buffer)); -} - // An exact copy of python's range feature. Written by Tadeck: // http://stackoverflow.com/a/8273091 function range(start, stop, step) { @@ -136,36 +125,6 @@ function range(start, stop, step) { return result; } -function merkleJoin(h1, h2) { - let joined = Buffer.concat([h1, h2]); - let dhashed = sha256d(joined); - return dhashed; -} - -function merkleTreeBranches(data) { - let L = data; - let steps = []; - let PreL = [null]; - let StartL = 2; - let Ll = L.length; - - if (Ll > 1) { - while (true) { - if (Ll === 1) break; - steps.push(L[1]); - if (Ll % 2) L.push(L[L.length - 1]); - let Ld = []; - let r = range(StartL, Ll, 2); - r.forEach(function(i) { - Ld.push(merkleJoin(L[i], L[i + 1])); - }); - L = PreL.concat(Ld); - Ll = L.length; - } - } - return steps; -} - function uint256BufferFromHash(hex) { let fromHex = Buffer.from(hex, 'hex'); if (fromHex.length != 32) { @@ -255,7 +214,7 @@ function generateOutputTransactions(rpcData, poolAddress) { } module.exports.RtmBlockTemplate = function(rpcData, poolAddress) { - const extraNoncePlaceholderLength = 16; + const extraNoncePlaceholderLength = 17; const coinbaseVersion = Buffer.concat([packUInt16LE(3), packUInt16LE(5)]); const scriptSigPart1 = Buffer.concat([ @@ -294,29 +253,16 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) { let bits = Buffer.from(rpcData.bits, 'hex'); bits.writeUInt32LE(bits.readUInt32BE()); + console.log("1x" + (rpcData.transactions.length + 1) + " " + (blob1.length + extraNoncePlaceholderLength + blob2.length)); + return { difficulty: parseFloat((diff1 / bignum(rpcData.target, 16).toNumber()).toFixed(9)), height: rpcData.height, prev_hash: rpcData.previousblockhash, blocktemplate_blob: version + rpcData.previousblockhash + Buffer.alloc(32, 0).toString('hex') + curtime + bits.toString('hex') + Buffer.alloc(4, 0).toString('hex') + - packUInt32LE(blob1.length + extraNoncePlaceholderLength + blob2.length).toString('hex') + - blob1.toString('hex') + Buffer.alloc(extraNoncePlaceholderLength, 0).toString('hex') + blob2.toString('hex') + - Buffer.concat(merkleTreeBranches(getTransactionBuffers(rpcData.transactions))).toString('hex'), - reserved_offset: 80 + 4 + blob1.length + varIntBuffer(rpcData.transactions.length + 1).toString('hex') + + blob1.toString('hex') + Buffer.alloc(extraNoncePlaceholderLength, 0xCC).toString('hex') + blob2.toString('hex') + + Buffer.concat(rpcData.transactions.map(function(tx) { return Buffer.from(tx.data, 'hex'); })).toString('hex'), + reserved_offset: 80 + 4 + 1 + blob1.length } } - -module.exports.convertRtmBlob = function(buffer) { - let header = buffer.slice(0, 80); - const blobSize = buffer.slice(80, 80 + 4).readUInt32LE(); - let merkle_root = sha256d(buffer.slice(84, 84 + blobSize)); - for (let i = 84 + blobSize; i < buffer.length; i += 32) - merkle_root = merkleJoin(merkle_root, buffer.slice(i, i + 32)); - merkle_root.copy(header, 4 + 32, 0, 32); - return header; -} - -module.exports.constructNewRtmBlob = function(blockTemplate, nonceBuff) { - nonceBuff.copy(blockTemplate, 76, 0, 4); - return blockTemplate; -}