Roulette Provably FairTo prove that roulette rolls are fair, we use 3 different, predetermined inputs to calculate each roulette round's outcome.

Generating the Initial HashTo start the process, a base 'gameHash' is used as the source for all subsequent hashes. This hash serves as the initial state from which all future rounds' results are derived. The code uses the SHA-256 cryptographic hash function to ensure that the results are consistent and cannot be easily tampered with.

Generating Previous HashesTo obtain results from previous rounds, the 'getPreviousHash' function is used. This function applies the SHA-256 hash function to the current 'gameHash', producing a new hash that will be used to determine the results of the previous round. This chaining allows for backward tracking through a series of game rounds.

Determining the Game Result from a SeedThe 'saltHash' function combines a given hash with an optional 'salt' and returns a new SHA-256 hash. This new hash, referred to as the "seed", is used to generate the game result. The 'gameResultFromSeed' function takes a portion of this seed and converts it into a numerical value, which is then used to determine the outcome of the roulette game. The modulo operation creates a slight bias due to the division's remainder, which is why the game results might exhibit some patterns.

Mapping Results to Game ColorsThe roulette game uses colors to represent different outcomes. The 'gameResultToColor' function maps a numerical result to a specific color. For example, if the result is 0, the color is "green." If the result is between 1 and 7, it is "purple," and if the result is between 8 and 15, it is "black." This mapping system determines the final outcome of each round, which can then be used to decide winners and payouts in a game context.

Extracting Game InformationThe 'getGameInformation' function takes a hash and generates the corresponding seed, game result, and color. This function provides all the necessary information for a single round of the roulette game. By calling this function with a specific hash, you can determine the outcome of that round.

Golden Round CalculationThe golden round is calculated by generating a random integer based on the hash.

```
function isGoldenRound(hash) {
return new chance(hash).integer({ min: 1, max: 100 }) <= 1;
}
```

Retrieving Previous Game ResultsTo retrieve multiple previous rounds, the 'getPreviousResults' function is used. This function accepts a 'gameHash' and the number of previous rounds to retrieve (count). It repeatedly calls 'getPreviousHash' to generate a chain of hashes, then retrieves the corresponding game information for each hash. This allows you to create a sequence of roulette rounds with consistent and reliable results.

```
const sha256 = require("crypto-js/sha256");
import { createHmac } from "crypto";
function getPreviousHash(gameHash) {
return sha256(gameHash).toString();
}
function gameResultToColor(bet) {
if (bet === 0) return "green";
if (1 <= bet && bet <= 7) return "purple";
if (8 <= bet && bet <= 15) return "black";
}
const salt = '0000000000000000000126e13e7225feff5fd0d9a8cf1229d1ee718601491a49';
function saltHash(hash) {
return createHmac("sha256", hash).update(salt).digest("hex");
}
function gameResultFromSeed(seed) {
// warning: slightly biased because of modulo!
const num = parseInt(seed.substr(0, 52 / 4), 16);
return num % 15;
}
function getGameInformation(hash) {
const seed = saltHash(hash),
result = gameResultFromSeed(seed);
return {
result,
hash,
seed,
color: gameResultToColor(result)
};
}
function getPreviousResults(gameHash, count) {
const results = [];
for (let i = 0; i < count; i++) {
results.push(getGameInformation(gameHash));
gameHash = getPreviousHash(gameHash);
}
return results;
}
```