Blockchain-based Provably Fair Betting

Our progress is far better than expected in developing a workaround for random number generation under blockchain environment! Here is the brief description how it works.

If you have installed MetaMask in Chrome or Firefox, welcome to have a try in this technical demo by switch to Ropsten testnet:

https://eth888.io/game1-ropsten/


You may get some Ethers from this faucet:


http://faucet.ropsten.be:3001/



There are three major concerns in design:


  1. convince the players that the decision made by the platform was before them
  2. players can contribute their inputs to affect number generation
  3. players are unable to control the result
For point 1, we provide a SHA256 encrypted hash to the players before any bets placed. The key to this hash actually is a random server-side seed (a long random string) generated from a centralized world (a private client node in Ethereum network). The seed is already stored in a private database (no public end-point). Once result available, the server-side seed will be available too. They can check all consistencies with an open validator.

For point 2 and 3, the players are allowed to place their bet coupled with their user-side seed. The smart contract will take the betting information, hash given and also user-side seed, broadcasting an event to our private client node by providing these information as well. The private node has the access to the private database, so it has the server-side seed, and calculates the final result. When done, it will callback an owner function in the smart contract, most importantly, providing the server-side seed which consists with the hash under SHA256 algorithm. 

The demo generates 3 numbers, each ranging from 1 - 6. The player can place a bet (1 - 6) and win if any numbers matching with the selected bet. For example, if 3 is selected at $1 bet, the result is 1,4,3, so the player wins $1 and $1 + $1 (principal) totally $2 will be deposited to her wallet address. If the result is 1,4,6, she loses $1. If 1,3,3 comes out, she wins $2 and $2 + $1 = $3 will be deposited, similarly for 3,3,3, with x3 multiplier. 

The algorithm for generating a number from two seeds is very simple. By splitting a long string into 3 pieces, each length is a factor of 3, trimming the tails, for example, hs73hnd8j36dh2hgnsoq, becoming "hs73hn", "d8j36d" and "h2hgns", which represent 3 numbers individually. By adding up the ASCII codes of each character in the segment, here has a positive integer. To have a number within 1 - 6, we take the remainder of 6 from this positive integer. As the result is not only affected by the server-side seed, we blend it with the user-side seed as well. The concept here is chopping each seed into 3 individual pieces, getting 3 positive integers per seed, thus adding the respective components together. Below is the JS code for the calculation:


function calculateResult(serverSeed, playerSeed) {

    var results = [-1, -1, -1];
    
    if(serverSeed.length >= 3)
    {      
      var seedLength = serverSeed.length;
      var segmentLength1 = (seedLength - (seedLength % 3)) / 3;
      var str1 = serverSeed.substr(0, segmentLength1);
      var str2 = serverSeed.substr(segmentLength1, segmentLength1);
      var str3 = serverSeed.substr(segmentLength1 * 2, segmentLength1);

      var s1 = 0;
      for(var i = 0; i < segmentLength1; i++)
        s1 += str1.charCodeAt(i);      

      var s2 = 0;
      for(var i = 0; i < segmentLength1; i++)
        s2 += str2.charCodeAt(i);      

      var s3 = 0;
      for(var i = 0; i < segmentLength1; i++)
        s3 += str3.charCodeAt(i);
      
      if(playerSeed.length >= 3)
      {
        seedLength = playerSeed.length;
        var segmentLength2 = (seedLength - (seedLength % 3)) / 3;
        str1 = playerSeed.substr(0, segmentLength2);
        str2 = playerSeed.substr(segmentLength2, segmentLength2);
        str3 = playerSeed.substr(segmentLength2 * 2, segmentLength2);

        for(var i = 0; i < segmentLength2; i++)
          s1 += str1.charCodeAt(i); 

        for(var i = 0; i < segmentLength2; i++)
          s2 += str2.charCodeAt(i);

        for(var i = 0; i < segmentLength2; i++)
          s3 += str3.charCodeAt(i);
      }

      results[0] = s1 % 6;
      results[1] = s2 % 6;
      results[2] = s3 % 6;
    }

    return results;
}




留言

這個網誌中的熱門文章

Smart Contract and Random Number

Adding Vanil tokens to your wallet