Overview
Implementation
Games
Verify
Implementation

Implementation

How do we generate results?

We generate random 64 characters long strings as our server seeds (SHA256 hash algorithm the create the hashed version) and rely on the EOS block hash when needed for certain multiplayer games. The code below shows how we transform available hashes into float values that are later converted into usable game actions.

import crypto from 'crypto';

function* getBytesChunks(
  serverSeed: string,
  clientSeed: string,
  nonce: number | string
): Generator<number[]> {
  let step = 0;
  let counter = 0;

  while (true) {
    const hmac = crypto.createHmac('sha512', serverSeed);
    hmac.update(`${clientSeed}:${nonce}:${step}`);
    const hashValue = hmac.digest('hex');

    while (counter < 16) {
      const hashBytes: number[] = [];

      for (let i = 0; i < 4; i++) {
        hashBytes.push(parseInt(hashValue.substring(i * 2 + counter * 8, i * 2 + 2 + counter * 8), 16));
      }

      yield hashBytes;
      counter += 1;
    }

    counter = 0;
    step += 1;
  }
}

function getFloats(
  serverSeed: string,
  clientSeed: string,
  nonce: number | string,
  count: number
): number[] {
  const bytes = getBytesChunks(serverSeed, clientSeed, nonce);
  const ints: number[][] = [];

  while (ints.length < count) {
    ints.push(bytes.next().value);
  }

  return ints.map((bytes) =>
    bytes.reduce((acc, value, i) => acc + value / Math.pow(256, i + 1), 0)
  );
}