Mike.MN
Full Stack PHP Developer / Systems Administrator / DevOps
Minneapolis, Minnesota, USA|

Crypto Dice

Created: Q1 2014
Language: PHP, SQL, CodeIgniter, Bootstrap.css
Software: Bitcoin

A Bitcoin dice game. A clone of the ever-popular SatoshiDice gambling website. In 2014 Bitcoin version 0.6(?) introduced a config option called walletnotify that executes a script each time a transaction is received by the client. You could use this such as walletnotify=/home/bitcoin/dice/wrapper.sh %s to do neat things with the incoming transaction if someone sends you coins.

How it works
This service was 98% backend and 2% frontend. All the website had to do was display a static list of game addresses and query a single table to get the most recent games. The interesting stuff happened under-the-hood in the wrapper script executed from within the Bitcoin client. The script will do some math to generate a winning ticket number based on the incoming transaction id and a public secret hash. If the user guessed correctly that his number was "less than [GameID]", he wins the multiplier of his original bet. The script would immediately send the winnings back to the address that sent the bet. Or in the case of losing, it sends a small consolation prize - dust (0.00000001) to acknowledge the receipt of the bet.

The game is designed to be cheat-proof. Both from the player, and by the house. Cheating is prevented by publishing a list of public secrets that get updated daily. The incoming Transaction ID is hashed with a public secret, so at midnight you can confirm if your random losing number was mathematically proven to be the real random number. All transactions are performed on the blockchain, ensuring an accurate log of every game ever played forever.

How to verify the roll
The winning ticket number range is 0-65535 (65536 total) We use the first 4 bytes of the hashed (incoming txid+daily secret) and convert it from hex to decimal.

public_hash = sha256(secret_hash)
txid_in_hashed = hash_hmac(sha256, txid_in, secret_hash)
winning_ticket = base_convert(first4bytes(txid_in_hashed), hex, decimal)
Yesterday's secret hash is published each day at 00:00 UTC. Ideally the pre-computed list of future public hashes would be distributed by third party hosters and pastebins, to prevent cheating.

Features

  • Fully automated, zero maintenance.
  • Wallet scanner to prevent dropped games in case of inability to process live transactions.
  • Config options for text / branding, able to quickly deploy to new alt-coins.
  • HTTP betting API so you can play the game with bots or with specific strategies like Martingale.
  • Provably random

TODO

  • Marketing
  • Better accounting reports / game analysis

Screenshots

Home Page

Shows all the different game addresses and the most recent games.

Public Secret Hashes

Public list of daily secrets.

Search Game

Simple search form to find a transaction id of a pervious game.

API Examples

HTTP API for app developers.

About

Rules of the game, how the winning number is calculated in a provable way.