Skip to content

viandwi24/blockchain-simulation

Repository files navigation

Blockchain Simulation

This is just a Web to simulate how Blockchain works in a short and simple way. Built on javascript (nodejs) and using Vuejs as Front End. (Just for education only)

Preview

Demo

To see directly, you can see via the following link : Live Demo

Loader Animation create in After Effect, if you wanna see :

Usage

You can see the base lib in /blockchain-simulation/lib

const Blockchain = require('./blockchain')
const Transaction = require('./transaction')
const { KeyGenerator } = require('./signature')

// Create a new blockchain
const mycoin = new Blockchain()

// Configure the blockchain
mycoin.blockProofOfWorkDifficulty = 4

// create a new wallet
const mywallet = (new KeyGenerator()).generate() // { publicKey: '...', privateKey: '...' }

// make default balance of wallet
const systemTransaction = (new Transaction(mycoin.system.publicKey, mywallet.publicKey, 1000)).sign(mycoin.system.privateKey)
mycoin.addTransaction(systemTransaction)
// mycoin.mine(mycoin.system.privateKey)

// create a new transaction
const destinationAddress = 'my-friend-address'
const mytransaction = new Transaction(mywallet.publicKey, destinationAddress, 10)

// sign the transaction with my wallet private key
mytransaction.sign(mywallet.privateKey)

// add the transaction to the blockchain
mycoin.addTransaction(mytransaction)

//
const mineAddress = 'miner-address'
mycoin.mine(mineAddress).then(() => {
  // mine was successful
  console.log({
    myballance: mycoin.getBalance(mywallet.publicKey),
    myfrinedballance: mycoin.getBalance(destinationAddress),
    minerballance: mycoin.getBalance(mineAddress),
  })
  // get last block
  const lastBlock = mycoin.getLastBlock()
  console.log(lastBlock)
  // check valid chain
  console.log(mycoin.isValid()) // true
})

This script you can see in /blockchain-simulation/lib/example.js

Contributors

You Can Contribute!!!

I will gladly accept your contribution for the better. You can follow the contributing guide listed here

Donate

You can support the development of this project at :

License

Blockchain Simulation is open-sourced software licensed under the MIT license.