-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathkrb.js
More file actions
151 lines (129 loc) · 6.68 KB
/
krb.js
File metadata and controls
151 lines (129 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";
const bignum = require('bignum');
const cnUtil = require('cryptonote-util');
const multiHashing = require('multi-hashing');
const crypto = require('crypto');
const debug = require('debug')('coinFuncs');
let hexChars = new RegExp("[0-9a-f]+");
function handleRpcError(err, body, callback) {
console.error(JSON.stringify(body));
callback(new Error(`${err} RPC: ${JSON.stringify(body)}`))
}
function Coin(data){
this.bestExchange = global.config.payout.bestExchange;
this.data = data;
let instanceId = crypto.randomBytes(4);
this.coinDevAddress = "Kdev1L9V5ow3cdKNqDpLcFFxZCqu5W2GE9xMKewsB2pUXWxcXvJaUWHcSrHuZw91eYfQFzRtGfTemReSSMN4kE445i6Etb3"; // Developer Address
this.poolDevAddress = "KgseWakG2bMXHGJSsAUfzL1HykCyvD4m8gd9qgcuyZ1ufy8PqUCKRxEfAv3nahfdTrCjZByiWoCiRiohxq4u2rf2RgQ1pcJ"; // Snipa Address
this.blockedAddresses = [
this.coinDevAddress,
this.poolDevAddress,
];
this.exchangeAddresses = [
]; // These are addresses that MUST have a paymentID to perform logins with.
this.prefix = 111;
this.supportsAutoExchange = false;
this.niceHashDiff = 200000;
this.getBlockHeaderByHeight = (height, callback) => { // unused
global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) =>
(body && body.result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getblockheaderbyheight' to krb daemon failed to return the block header.`, body, callback)
);
};
this.getBlockHeaderByHash = (hash, callback) => {
global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) =>
(body && body.result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getblockheaderbyhash' to krb daemon failed to return the block header.`, body, callback)
);
};
this.getLastBlockHeader = (callback) => {
global.support.rpcDaemon('getlastblockheader', {}, (body) =>
(body && body.result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getlastblockheader' to krb daemon failed to return the block header.`, body, callback)
);
};
this.getBlockTemplate = function(walletAddress, callback){
global.support.rpcDaemon('getblocktemplate', {
reserve_size: 17,
wallet_address: walletAddress
}, function(body){
return callback(body);
});
};
this.baseDiff = function(){
return bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 16);
};
this.validateAddress = function(address){
// This function should be able to be called from the async library, as we need to BLOCK ever so slightly to verify the address.
address = new Buffer(address);
if (cnUtil.address_decode(address) === this.prefix){
return true;
}
return cnUtil.address_decode_integrated(address) === this.intPrefix;
};
this.convertBlob = function(blobBuffer){
return cnUtil.convert_blob(blobBuffer);
};
this.constructNewBlob = function(blockTemplate, NonceBuffer){
return cnUtil.construct_block_blob(blockTemplate, NonceBuffer);
};
this.getBlockID = function(blockBuffer){
return cnUtil.get_block_id(blockBuffer);
};
this.BlockTemplate = function(template) {
/*
Generating a block template is a simple thing. Ask for a boatload of information, and go from there.
Important things to consider.
The reserved space is 13 bytes long now in the following format:
Assuming that the extraNonce starts at byte 130:
|130-133|134-137|138-141|142-145|
|minerNonce/extraNonce - 4 bytes|instanceId - 4 bytes|clientPoolNonce - 4 bytes|clientNonce - 4 bytes|
This is designed to allow a single block template to be used on up to 4 billion poolSlaves (clientPoolNonce)
Each with 4 billion clients. (clientNonce)
While being unique to this particular pool thread (instanceId)
With up to 4 billion clients (minerNonce/extraNonce)
Overkill? Sure. But that's what we do here. Overkill.
*/
// Set this.blob equal to the BT blob that we get from upstream.
this.blob = template.blocktemplate_blob;
this.idHash = crypto.createHash('md5').update(template.blocktemplate_blob).digest('hex');
// Set this.diff equal to the known diff for this block.
this.difficulty = template.difficulty;
// Set this.height equal to the known height for this block.
this.height = template.height;
// Set this.reserveOffset to the byte location of the reserved offset.
this.reserveOffset = template.reserved_offset;
// Set this.buffer to the binary decoded version of the BT blob.
this.buffer = new Buffer(this.blob, 'hex');
// Copy the Instance ID to the reserve offset + 4 bytes deeper. Copy in 4 bytes.
instanceId.copy(this.buffer, this.reserveOffset + 4, 0, 3);
// Generate a clean, shiny new buffer.
this.previous_hash = new Buffer(32);
// Copy in bytes 7 through 39 to this.previous_hash from the current BT.
this.buffer.copy(this.previous_hash, 0, 7, 39);
// Reset the Nonce. - This is the per-miner/pool nonce
this.extraNonce = 0;
// The clientNonceLocation is the location at which the client pools should set the nonces for each of their clients.
this.clientNonceLocation = this.reserveOffset + 12;
// The clientPoolLocation is for multi-thread/multi-server pools to handle the nonce for each of their tiers.
this.clientPoolLocation = this.reserveOffset + 8;
this.nextBlob = function () {
// Write a 32 bit integer, big-endian style to the 0 byte of the reserve offset.
this.buffer.writeUInt32BE(++this.extraNonce, this.reserveOffset);
// Convert the blob into something hashable.
return global.coinFuncs.convertBlob(this.buffer).toString('hex');
};
// Make it so you can get the raw block blob out.
this.nextBlobWithChildNonce = function () {
// Write a 32 bit integer, big-endian style to the 0 byte of the reserve offset.
this.buffer.writeUInt32BE(++this.extraNonce, this.reserveOffset);
// Don't convert the blob to something hashable. You bad.
return this.buffer.toString('hex');
};
};
this.cryptoNight = multiHashing.cryptonight;
}
module.exports = Coin;