UNPKG

ibctminer

Version:

```js const IntMiner = require('./src'); const Debug = require('./src/log')(); const fs = require('fs'); const COMP = '[SIPC]';

131 lines (117 loc) 3.82 kB
const EventEmitter = require('events'); const Debug = require('../log')(); const COMP = '[sipc]'; class Sipc extends EventEmitter { constructor({ }) { super(); } stratum_notify(data) { var packet = { job_id: null, powhash: null, minNonce: null, maxNonce: null, difficulty: null, timestamp: null, clean: null }; packet.job_id = data[0]; packet.powhash = data[1]; // matpool recevice 32bytes if (packet.powhash.length === 64) { packet.powhash += data[1] + '0'.repeat(32); } packet.minNonce = data[2]; packet.maxNonce = data[3]; packet.difficulty = data[4]; packet.timestamp = data[5]; packet.clean = data[6]; Debug.IbctLogInfo(COMP, 'sipc', JSON.stringify(packet)); return packet; } getCryptoName() { return 'sipc'; } JobtoWork(Job, Work) { var temp; if (!Job || !Work) { return false; } Work.job_id = Job.job_id; Work.snonce = parseInt('0x' + Job.minNonce, 16); Work.enonce = parseInt('0x' + Job.maxNonce, 16); Work.difficulty = parseInt('0x' + Job.difficulty, 16); temp = Math.floor(0xffffffffffffffff / Work.difficulty).toString(16); Work.target = Buffer.from('0'.repeat(16 - temp.length) + temp + 'f'.repeat(48), 'hex'); Work.data = Buffer.from(Job.powhash, 'hex'); Work.ntime = Buffer.from(Job.timestamp, 'hex'); Work.clean = Job.clean; Debug.IbctLogInfo(COMP, JSON.stringify(Work)); return true; } targetToDiff(target) { var Ntarget = parseInt('0x' + target, 16); var difficulty = Math.round(26959946667150639794667015087019630673637144422540572481103610249215.0 / Ntarget * 0xffffffff); return difficulty; } diffToTarget(difficulty) { var temp = Math.floor(0xffffffffffffffff / difficulty).toString(16); var target = Buffer.from('0'.repeat(16 - temp.length) + temp + 'f'.repeat(48), 'hex'); return target; } setWorkData(work, mode, data) { Debug.IbctLogInfo(COMP, 'setWorkData: ', data.toString('16')); if (mode === 'start nonce') { Debug.IbctLogInfo(COMP, ((data & 0xffffffff) >>> 0).toString('16')); // work.data.writeUIntBE((data & 0xffffffff), 76, 4); Debug.IbctLogInfo(COMP, (Math.floor(data / 0x100000000) >>> 0).toString('16')); // work.data.writeUIntBE(Math.floor(data / 0x100000000), 72, 4); } } checkHash(target, hash) { return Buffer.compare(target, hash) > 0; } reverseBuffer(src) { const buffer = Buffer.alloc(src.length); for (let i = 0, j = src.length - 1; i <= j; ++i, --j) { buffer[i] = src[j]; buffer[j] = src[i]; } return buffer; } calHash(Device, nonce) { var work = Device.dev.work; var data = Buffer.from(work.data); var algo = Device.dev.algorithm; var data = Buffer.from(work.data); var result = null; if (!algo) { return false; } nonce.copy(data, 72); result = algo.genHash(data, data.length, 0x30); return this.reverseBuffer(Buffer.from(result, 'hex')); } getSubmitParams(Device, nonce, hash) { var submit = { job_id: null, params: { id: null, submit: [] } }; var ntime = new Date(); submit.job_id = Device.dev.work.job_id; submit.params = { id: Device.dev.poolId, submit: [ // Device.dev.pool.user, submit.job_id, "0", Math.floor(ntime.getTime() / 1000).toString('16'), nonce.toString('16') Device.dev.pool.user, submit.job_id, submit.job_id, nonce.toString('16'), nonce.toString('16') ] } return submit; } }; module.exports = function GetSipc(options = {}) { return new Sipc(options); };