Advertisement
f0xtrade

Scala-Blockchain

Apr 21st, 2021
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.31 KB | None | 0 0
  1. import java.security.MessageDigest
  2. import java.math.BigInteger
  3. import scala.io.StdIn.readLine
  4. import scala.util.control.Breaks
  5.  
  6. object ScalaBlockchain {
  7.   def main(args: Array[String]): Unit = {
  8.  
  9.     println("Scala Blockchain v.0.1")
  10.     println("Welcome to your ScalaBlockchain Account!\nTo Sign Up please enter your name.")
  11.     val name: String = readLine("Name: ")
  12.     val id: Int = 0
  13.     val user = new UserAcc(name, id)
  14.     val bob = new UserAcc("bob", id+1)
  15.     val loop = new Breaks
  16.     loop.breakable {
  17.       while (true) {
  18.         println("Commands:\nbalance - check your account balance\nreturn - return Scalablockchain\ntransact - new transaction\nquit - quit program")
  19.         val prompt: String = readLine("Enter Command:")
  20.         if (prompt == "balance") {
  21.           user.check_balance()
  22.         }
  23.         if (prompt == "return") {
  24.           Chain.returnChain()
  25.         }
  26.         if (prompt == "transact") {
  27.           println("Please enter amount, recipient name, and recipient ID.")
  28.           val amount = readLine("Amount: ")
  29.           val recipientName: String = readLine("Recipient name: ")
  30.           val recipientID = readLine("Recipient ID: ")
  31.           user.newTransaction(amount.toInt, recipientName, recipientID.toInt)
  32.         }
  33.         if (prompt == "quit") {
  34.           loop.break()
  35.         }
  36.         else {
  37.           println("Unknown command!")
  38.         }
  39.  
  40.       }
  41.     }
  42.     bob.check_balance()
  43.     user.check_balance()
  44.     user.newTransaction(23, "bob", 1)
  45.     Chain.returnChain()
  46.  
  47.   }
  48. }
  49.  
  50. object Chain {
  51.   var genesis: Block = new Block(0,"time", List(100, "bob", 1), "0")
  52.   var Chain: Array[Block] = Array(genesis)
  53.   def add_block( timestamp: String, blockdata: List[Any]): Unit = {
  54.     println("Adding Block to ScalaBlockchain...")
  55.     val ChainLength : Int = Chain.length
  56.     val newBlock : Block = new Block(ChainLength, timestamp, blockdata, Chain(ChainLength-1).hash)
  57.  
  58.     Chain :+= newBlock
  59.   }
  60.  
  61.   def returnChain(): Array[Block] = {
  62.     for (n <- Chain) {
  63.       println(n.returnBlock())
  64.     }
  65.     Chain
  66.   }
  67. }
  68.  
  69. class Block (index: Int, timestamp: String, blockdata: List[Any], prevhash: String) {
  70.   val hash : String = calc_hash()
  71.   var nonce: Int = 0
  72.   val dataList: List[Any] = List(this.index, this.timestamp, this.blockdata, this.prevhash, hash, nonce)
  73.  
  74.   def calc_hash(): String = {
  75.     println("Calculating hash...")
  76.     val data : String = index + timestamp + blockdata + prevhash + nonce
  77.     val hash : String = String.format(
  78.       "%064x",
  79.       new BigInteger(1, MessageDigest.getInstance("SHA-256").digest(data.getBytes("UTF-8")))
  80.     )
  81.     hash
  82.   }
  83.  
  84.   def validate(): Unit ={
  85.     while (! hash.startsWith("00000")) {
  86.       nonce += 1
  87.       calc_hash()
  88.     }
  89.   }
  90.  
  91.   def returnBlock(): Any ={
  92.     for (n <- dataList) {
  93.       println(n)
  94.     }
  95.   }
  96. }
  97.  
  98. class UserAcc (name: String, id: Int) {
  99.   val username: String = name
  100.   var balance: Int = 100
  101.   val userID: Int = id
  102.  
  103.   def newTransaction( amount: Int, recipient: String, recipientID: Int ): List[Any] = {
  104.     val blockdata: List[Any] = List(amount, recipient, recipientID, username, userID)
  105.     Chain.add_block("13/04/21:09:56", blockdata)
  106.     balance = balance - amount
  107.     blockdata
  108.   }
  109.  
  110.   def check_balance(): Int = {
  111.     println("Current balance: " + balance)
  112.     balance
  113.   }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement