Keep’s random beacon

One of the first components of the Keep Network that we have been working on is the random beacon. Because this component can be a great boon to other projects, we would like to encourage developers and those working on dApps that require randomness to have a glimpse into how Keep’s random beacon works.

What’s hard about random numbers on the blockchain?

You can trust a blockchain, but you can not get trusted randomness since it’s a deterministic system where you still have to rely on a 3rd party for the random source. It’s not ideal, of course, and you want to be sure that no one has the ability to even slightly swing the odds to their favor. The good news is that with the recent advances in cryptography it’s possible to use decentralized networks to swirl random processes and create a trusted source of randomness on a blockchain without relying on a centralized source.

Generating random avatars on the blockchain

As part of our alpha API announcement, we would like to present you with a fun little web app that you can run on your computer and play with the concept of a random beacon on blockchain.

What are the most popular use cases for randomness, you might ask? The first that comes to mind is games, but it turns out there are more: cryptography, of course, science experiments and simulation, statistical analysis and surprisingly art!

Let’s have some fun with the art concept and generate a random avatar for you. We’ve developed a simple web app that will call the Keep Random Beacon smart contract, get a random number from it, and generate a unique avatar by selecting random values from the list of features such as hair color, clothes, eyebrow expression and so on.

Random avatar on blockchain? There is an app for that.

We’re using a great avatar library https://avataaars.com created by Pablo Stanley. Our repo is essentially a react web app with truffle framework under the hood. Since we’re still at the pre-launch state, we’re going to use a “stub” Random Beacon contract on a local testnet. “Stub” contract, in our case, is a demo contract that doesn’t require the live network but can demonstrate our alpha API defined methods and events from the real contract that will be live on our forthcoming mainnet launch.

The first step you need to do is to install and run a local Ethereum testnet. Ganache is the best option we’ve tried.

The best solution for local Ethereum testnet we’ve tried so far.

For easier integration, please change the default 7545 port to 8545 by clicking the gear icon on the right side:

You will also need node.js installed. This can be easily done with Brew if you’re on a Mac. Please follow instructions here and then run:

brew install node

We assume you are familiar with the Metamask browser extension. You can learn more about getting up and running with Metamask with this great introductory video.

Once you have all those set up, you can download our repository from here. Extract and navigate to the folder using your terminal:

cd ~/Downloads/random-avatar-master

Install the required dependency packages:

npm install

Make sure your Ganache app is live and then run:

npm run demo

This will deploy the contracts, set configuration and start the web app. You should be able to see it by opening http://localhost:3000

Now the fun part! Switch your metamask network to local Ethereum testnet by selecting http://localhost:8545 as your custom RPC:

Setting up a custom network on Metamask

Get the private key for the first account on Ganache app (the key icon on the right hand side):

Copy the key and import it in to Metamask:

Reload the page and click the ‘Get random avatar!’ button. This should bring up Metamask confirmation dialog, accept the transaction and you will get your avatar!

Check out the second part on how to build with the alpha API.