Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.security.MessageDigest
- import java.math.BigInteger
- import scala.io.StdIn.readLine
- import scala.util.control.Breaks
- object ScalaBlockchain {
- def main(args: Array[String]): Unit = {
- println("Scala Blockchain v.0.1")
- println("Welcome to your ScalaBlockchain Account!\nTo Sign Up please enter your name.")
- val name: String = readLine("Name: ")
- val id: Int = 0
- val user = new UserAcc(name, id)
- val bob = new UserAcc("bob", id+1)
- val loop = new Breaks
- loop.breakable {
- while (true) {
- println("Commands:\nbalance - check your account balance\nreturn - return Scalablockchain\ntransact - new transaction\nquit - quit program")
- val prompt: String = readLine("Enter Command:")
- if (prompt == "balance") {
- user.check_balance()
- }
- if (prompt == "return") {
- Chain.returnChain()
- }
- if (prompt == "transact") {
- println("Please enter amount, recipient name, and recipient ID.")
- val amount = readLine("Amount: ")
- val recipientName: String = readLine("Recipient name: ")
- val recipientID = readLine("Recipient ID: ")
- user.newTransaction(amount.toInt, recipientName, recipientID.toInt)
- }
- if (prompt == "quit") {
- loop.break()
- }
- else {
- println("Unknown command!")
- }
- }
- }
- bob.check_balance()
- user.check_balance()
- user.newTransaction(23, "bob", 1)
- Chain.returnChain()
- }
- }
- object Chain {
- var genesis: Block = new Block(0,"time", List(100, "bob", 1), "0")
- var Chain: Array[Block] = Array(genesis)
- def add_block( timestamp: String, blockdata: List[Any]): Unit = {
- println("Adding Block to ScalaBlockchain...")
- val ChainLength : Int = Chain.length
- val newBlock : Block = new Block(ChainLength, timestamp, blockdata, Chain(ChainLength-1).hash)
- Chain :+= newBlock
- }
- def returnChain(): Array[Block] = {
- for (n <- Chain) {
- println(n.returnBlock())
- }
- Chain
- }
- }
- class Block (index: Int, timestamp: String, blockdata: List[Any], prevhash: String) {
- val hash : String = calc_hash()
- var nonce: Int = 0
- val dataList: List[Any] = List(this.index, this.timestamp, this.blockdata, this.prevhash, hash, nonce)
- def calc_hash(): String = {
- println("Calculating hash...")
- val data : String = index + timestamp + blockdata + prevhash + nonce
- val hash : String = String.format(
- "%064x",
- new BigInteger(1, MessageDigest.getInstance("SHA-256").digest(data.getBytes("UTF-8")))
- )
- hash
- }
- def validate(): Unit ={
- while (! hash.startsWith("00000")) {
- nonce += 1
- calc_hash()
- }
- }
- def returnBlock(): Any ={
- for (n <- dataList) {
- println(n)
- }
- }
- }
- class UserAcc (name: String, id: Int) {
- val username: String = name
- var balance: Int = 100
- val userID: Int = id
- def newTransaction( amount: Int, recipient: String, recipientID: Int ): List[Any] = {
- val blockdata: List[Any] = List(amount, recipient, recipientID, username, userID)
- Chain.add_block("13/04/21:09:56", blockdata)
- balance = balance - amount
- blockdata
- }
- def check_balance(): Int = {
- println("Current balance: " + balance)
- balance
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement