In this TME, we will build a blockchain for cryptocurrencies from scratch. We’ll go step by step to get a functionnal blockchain. This TME can be made in any language, but all instructions will be given in JavaScript with Node.js support.
Follow the installation instructions on the home page.
npm install -g yarn
yarn # Install the dependencies
yarn start # Start the program
First, we need to be able to create blocks. A block is composed of a hash, identifying it uniquely, the pointer of the previous block, a nonce (we’ll see why later), a content (right now a string), an array of transactions and a timestamp.
- Propose a data structure for a block creation.
- Write a function to compute the hash of a block. The hash of a block is the result of
SHA256(previousBlockHash + timestamp + content)
. The SHA256 can be found in the Node.jscrypto
package withcreateHash
. - Write a really simple main which we’ll handle a list of blocks and add a new one periodically (every minute for example) without transactions. Keep in mind you’ll need a genesis block to get started. (So write a function to create genesis block.)
- Log the operation to see the blockchain running.
Now that we have a running blockchain, we need to let users use it. We need a way to identify a user (i.e. they should have a wallet). We should generate a new RSA keypair for every user. Use node-rsa
to generate a new keyPair of RSA keys. You then will have to create an object data structure to manage Wallets. They should have the keys, a balance of coins, an array of transactions, an array of pending transactions and an address.
- Create a data structure for wallet creation. At first everything is empty and the balance is 0. The address is the result of
SHA256(publicKey)
. - Check you can generate new wallets easily.
Now we need to create transactions to send coins from one user to another. A transaction should be identified by an ID, a sender address, a recipient address, a balance and a signature. It should also contain the public key of the sender.
- Create a data structure for transaction creation.
- Create the ID of a transaction. It should be the result of
SHA256(sender + recipient + balance)
. - Write a function to generate the signature of a transaction. This should take the id as input.
In your main, create two Wallets, and issue a transaction from one wallet to another.
- Add some money to first wallet to be able to send coins from first to second. To determine the value of the wallet at start, the wallet should query the blockchain and check the balance in achieved transactions.
- Modify the genesis block to accept initial recipients balance.
- Update the wallet to update the balance according to the transactions in blockchain.
- Creates and sign the transaction.
- Put the transaction in pending state in the wallet and in the blockchain.
- When the timer passed, the blockchain should resolve the transaction and check that signature is correct for the issuer Wallet and that the wallet has still enough money.
- If it’s right, the transaction pass and the blockchain should update all wallets. If it’s false, it’s rejected.
We will change a little bit the rules. We need to add difficulties to be sure no one will try to cheat. In the block generation, the hash should start with 6 0
. To do it, add a nonce (an integer) and add it to the hash SHA256(previousHash + timestamp + content + nonce)
. If the hash doesn’t start with 6 0
, increment the nonce and restart the process.
- Remove the timer.
- Generate as much block as possible the quickest possible. When a new block has been mined, another one will be mined too. If no transactions are pending, then the block is produced without transactions.
- To be correct, the id of the block must start with 6
0
. - Watch your computer burn to hell to find the correct hash. If your computer is too fast, try to increase the difficulty to 8 or 10
0
.