const crypto = require('crypto')
// change these with values from the provably fair info
const SECRET = 'fb659b4738748514374d98dccc4ee0ec17dc7a3dcbba5b88f2659fbe4a382ad8';
const GAME_ID = 'jcopep59k3gr4imiqz7nwzmowepja95n';
const BLOCK_HASH = '17ac4fe87407bb4a7a66f6daf193299d2917f7499effd303975ab5e8c6d52434';
const ROUND_COUNT = 7
const PLAYERS = [
"76561199063276068",
"botK9mr7E8kExmj6xkp",
"botYW7MTG5D1yiDxR10",
"bot5RkobYid61DB9qBJ"
]
const MAX_TICKETS = 100e3;
const chunk = (arr, n) => arr.slice(0, ((arr.length + n - 1) / n) | 0).map((c, i) => arr.slice(n * i, n * i + n));
function* byteGenerator({ serverSeed, entropy, nonce, cursor }) {
// Setup curser variables
let currentRound = Math.floor(cursor / 32);
let currentRoundCursor = cursor;
currentRoundCursor -= currentRound * 32;
// Generate outputs until cursor requirement fullfilled
while (true) {
// HMAC function used to output provided inputs into bytes
const hmac = crypto.createHmac('sha256', serverSeed);
hmac.update(`${entropy}:${nonce}:${currentRound}`);
const buffer = hmac.digest();
// Update curser for next iteration of loop
while (currentRoundCursor < 32) {
yield Number(buffer[currentRoundCursor]);
currentRoundCursor += 1;
}
currentRoundCursor = 0;
currentRound += 1;
}
}
const generateFloats = ({
serverSeed, entropy, nonce, cursor, count,
}) => {
// Random number generator function
const rng = byteGenerator({ serverSeed, entropy, nonce, cursor });
// Declare bytes as empty array
const bytes = [];
// Populate bytes array with sets of 4 from RNG output
while (bytes.length < count * 4) {
bytes.push(rng.next().value);
}
// Return bytes as floats using lodash reduce function
return chunk(bytes, 4).map(bytesChunk => bytesChunk.reduce((result, value, i) => {
const divider = 256 ** (i + 1);
const partialResult = value / divider;
return result + partialResult;
}, 0));
};
function getRoll(privateKey, blockHash, gameId, playerId, round) {
const [roll] = generateFloats({
serverSeed: privateKey,
entropy: `${gameId}-${blockHash}-${playerId}`,
nonce: round,
cursor: 0,
count: 1,
});
return roll;
}
for (let round = 0; round < ROUND_COUNT; round++) {
for (const userId of PLAYERS) {
const roll = getRoll(SECRET, BLOCK_HASH, GAME_ID, userId, round + 1)
console.log(`Round ${round + 1} - ${userId} rolled ${roll * 100}`)
}
}