Skip to content

Instantly share code, notes, and snippets.

@zefirka
Created September 22, 2017 16:47
Show Gist options
  • Save zefirka/9990720ed33ba63d524d163ebb88abf6 to your computer and use it in GitHub Desktop.
Save zefirka/9990720ed33ba63d524d163ebb88abf6 to your computer and use it in GitHub Desktop.
ExtraNonceGenerator
function packUInt32BE(num) {
const buff = new Buffer(4);
buff.writeUInt32BE(num, 0);
return buff;
}
function pad(n, v, i = '0') {
return v.length < n ? new Array(n - v.length).fill(i).join('') + v : v
}
function mask32(num, [i, j]) {
const mask = ['00', '00', '00', '00'];
num = leftPad(4, num);
mask[i] = num.slice(0, 2)
mask[j] = num.slice(2)
return parseInt(mask.join(''), 16)
}
function ExtraNonceGenerator(instanceId) {
const map = [
[0, 1],
[0, 2],
[0, 3],
[0, 4],
[1, 2],
[1, 3],
[1, 4],
[2, 3],
[2, 4],
[3, 4]
]
this.prev = 1
this.bytes = map[instanceId % map.length]
this.next = function extraNonce() {
var value = mask32(this.prev.toString(16), this.bytes)
this.prev ++
return packUInt32BE(value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment