Skip to content

Instantly share code, notes, and snippets.

@tacyarg
Created June 12, 2021 17:51
Show Gist options
  • Save tacyarg/908be9fbcc380d40fbe63cd80949fe86 to your computer and use it in GitHub Desktop.
Save tacyarg/908be9fbcc380d40fbe63cd80949fe86 to your computer and use it in GitHub Desktop.
Provable Example // source https://jsbin.com/kiqiguf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Provable Example</title>
<script src="https://rawgit.com/daywiss/provable/master/dist/provable.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css"></script>
</head>
<body>
<div class="container">
<div>
This shows an example of third party provably fair verification. If the last game played
was game 100, a user can verify all previous games before that by looking at
the outcomes generated here, and comparing them to the outcomes of the original application.
For more information see the <a target="_blank" href='https://github.com/daywiss/provable'>Provable source code</a>
</div>
<table class="u-full-width">
<thead>
<tr>
<th colspan='3'>Provable Hashes</th>
</tr>
<tr>
<th>Round</th>
<th>Hash</th>
<th>Win?</th>
</tr>
</thead>
<tbody id='table'></tbody>
</table>
</div>
<script id="jsbin-javascript">
//see https://github.com/daywiss/provable for full documentation
function refreshTable(hashes,round){
var tableBody = document.getElementById("table");
tableBody.innerHTML = "";
tableBody.innerHTML = hashes.map(function(hash,index){
var result = calcOutcome(hash)
return "<tr><td>"+(round-index)+"</td><td>"+hash+"</td><td>"+result+"</td></tr>";
}).join('')
}
function calcOutcome(hash){
//rehash with public seed if you need to
//hash = Provable.rehash(hash,publicSeed)
return Provable.toBool(hash)
}
//get query parameters from the url
function getParams(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)")
var results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function run(){
//this is just some hash you want to verify. It can be passed in as url params
var currentHash = getParams('hash') || 'bea3d13023a3f99f16553c6bc9e02f78b09de773d2d226b93afffe16022be98f'
//our current round, must be > 0 to be able to do any verification
var currentRound = getParams('round') || 100
//limit the rounds we are checking to 20
var hashCount = Math.min(currentRound,20)
//initialize provable
var provable = Provable({
seed:currentHash, //the current hash we are checking
count:hashCount, //the number of previous hashes we want to list with it
})
//here we get all hashes (20 or hashCount), including seed hash, and reverse them so seed is first.
//see docs for full function specification
var hashes = provable.hashes(null,null,true,true)
refreshTable(hashes,currentRound)
}
run()
</script>
<script id="jsbin-source-javascript" type="text/javascript">//see https://github.com/daywiss/provable for full documentation
function refreshTable(hashes,round){
var tableBody = document.getElementById("table");
tableBody.innerHTML = "";
tableBody.innerHTML = hashes.map(function(hash,index){
var result = calcOutcome(hash)
return "<tr><td>"+(round-index)+"</td><td>"+hash+"</td><td>"+result+"</td></tr>";
}).join('')
}
function calcOutcome(hash){
//rehash with public seed if you need to
//hash = Provable.rehash(hash,publicSeed)
return Provable.toBool(hash)
}
//get query parameters from the url
function getParams(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)")
var results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function run(){
//this is just some hash you want to verify. It can be passed in as url params
var currentHash = getParams('hash') || 'bea3d13023a3f99f16553c6bc9e02f78b09de773d2d226b93afffe16022be98f'
//our current round, must be > 0 to be able to do any verification
var currentRound = getParams('round') || 100
//limit the rounds we are checking to 20
var hashCount = Math.min(currentRound,20)
//initialize provable
var provable = Provable({
seed:currentHash, //the current hash we are checking
count:hashCount, //the number of previous hashes we want to list with it
})
//here we get all hashes (20 or hashCount), including seed hash, and reverse them so seed is first.
//see docs for full function specification
var hashes = provable.hashes(null,null,true,true)
refreshTable(hashes,currentRound)
}
run()</script></body>
</html>
//see https://github.com/daywiss/provable for full documentation
function refreshTable(hashes,round){
var tableBody = document.getElementById("table");
tableBody.innerHTML = "";
tableBody.innerHTML = hashes.map(function(hash,index){
var result = calcOutcome(hash)
return "<tr><td>"+(round-index)+"</td><td>"+hash+"</td><td>"+result+"</td></tr>";
}).join('')
}
function calcOutcome(hash){
//rehash with public seed if you need to
//hash = Provable.rehash(hash,publicSeed)
return Provable.toBool(hash)
}
//get query parameters from the url
function getParams(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)")
var results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function run(){
//this is just some hash you want to verify. It can be passed in as url params
var currentHash = getParams('hash') || 'bea3d13023a3f99f16553c6bc9e02f78b09de773d2d226b93afffe16022be98f'
//our current round, must be > 0 to be able to do any verification
var currentRound = getParams('round') || 100
//limit the rounds we are checking to 20
var hashCount = Math.min(currentRound,20)
//initialize provable
var provable = Provable({
seed:currentHash, //the current hash we are checking
count:hashCount, //the number of previous hashes we want to list with it
})
//here we get all hashes (20 or hashCount), including seed hash, and reverse them so seed is first.
//see docs for full function specification
var hashes = provable.hashes(null,null,true,true)
refreshTable(hashes,currentRound)
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment