node-stratum-pool/blockTemplate.js
Matthew Little 7e5241a6e2 improvements
2014-01-06 23:15:27 -05:00

55 lines
1.7 KiB
JavaScript

var binpack = require('binpack');
var merkleTree = require('./merkleTree.js');
var transactions = require('./transactions.js');
var util = require('./util.js');
var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, publicKey, reward){
//private members
function getMerkleHashes(steps){
return steps.map(function(step){
return util.reverseBuffer(step).toString('hex');
});
}
function getTransactionBuffers(txs){
var txHashes = txs.map(function(tx){
return util.uint256BufferFromHash(tx.hash);
});
return [null].concat(txHashes);
}
//public members
this.rpcData = rpcData;
this.jobId = jobId;
this.merkleTree = new merkleTree(getTransactionBuffers(rpcData.transactions));
this.merkleBranch = getMerkleHashes(this.merkleTree.steps);
this.generationTransaction = new transactions.Generation(
rpcData,
publicKey,
reward
);
this.getJobParams = function(){
if (!this.jobParams){
this.jobParams = [
this.jobId,
util.reverseHex(this.rpcData.previousblockhash),
this.generationTransaction.coinbase[0].toString('hex'),
this.generationTransaction.coinbase[1].toString('hex'),
this.merkleBranch,
binpack.packInt32(this.rpcData.version, 'big').toString('hex'),
this.rpcData.bits,
binpack.packUInt32(this.rpcData.curtime, 'big').toString('hex'),
true
];
}
return this.jobParams;
}
}