Browse Source

Added links to sources in documentation

master
Robby Zambito 2 years ago
parent
commit
d8a7071078
7 changed files with 56 additions and 16 deletions
  1. +8
    -0
      .idea/misc.xml
  2. +2
    -1
      src/main/scala/com/zambito/blockchain/Block.scala
  3. +1
    -1
      src/main/scala/com/zambito/blockchain/Blockchain.scala
  4. +4
    -2
      src/main/scala/com/zambito/blockchain/Main.scala
  5. +4
    -8
      src/main/scala/com/zambito/blockchain/Transaction.scala
  6. +3
    -2
      src/main/scala/com/zambito/blockchain/Wallet.scala
  7. +34
    -2
      src/main/scala/com/zambito/blockchain/package.scala

+ 8
- 0
.idea/misc.xml View File

@@ -3,4 +3,12 @@
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="ScaladocSettings">
<option name="outputDir" value="C:\Users\Zambi\IdeaProjects\SimpleScalaBlockchain\docs" />
<option name="additionalFlags" value="" />
<option name="maxHeapSize" value="" />
<option name="docTitle" value="SimpleScalaBlockchain documentation" />
<option name="verbose" value="false" />
<option name="openInBrowser" value="false" />
</component>
</project>

+ 2
- 1
src/main/scala/com/zambito/blockchain/Block.scala View File

@@ -11,7 +11,8 @@ import scala.xml.Elem
* @param nonce Random number that must be found to validate the hash of this [[Block]]
*
* @author Robby Zambito
* @see [[https://github.com/CryptoKass/NoobChain-Tutorial-Part-2/blob/master/src/noobchain/Block.java `Block.java`]]
* @see [[https://github.com/Zambito1/SimpleScalaBlockchain/blob/master/src/main/scala/com/zambito/blockchain/Block.scala `Source`]]
* @see [[https://github.com/CryptoKass/NoobChain-Tutorial-Part-2/blob/master/src/noobchain/Block.java Block.java]]
*/
case class Block(transactions: Seq[Transaction],
previousHash: String,


+ 1
- 1
src/main/scala/com/zambito/blockchain/Blockchain.scala View File

@@ -1,7 +1,7 @@
package com.zambito.blockchain

case class Blockchain(blocks: Seq[Block] = Seq(),
UTXOs: Map[String, TransactionOutput]) {
UTXOs: Map[String, TransactionOutput] = Map()) {

def isValidChain(blocks: Seq[Block] = blocks): Boolean = {



+ 4
- 2
src/main/scala/com/zambito/blockchain/Main.scala View File

@@ -26,7 +26,7 @@ object Main extends App {
alice.publicKey,
bob.publicKey,
33.33f,
Seq(TransactionInput(genesisTransaction.outputs.head.id))
Seq(TransactionInput(genesisTransaction.outputs.head.id, Some(genesisTransaction.outputs.head)))
).signedWith(alice.privateKey)
), "0").mined

@@ -46,9 +46,11 @@ object Main extends App {
}



printBlockchain(blockchain)

println(s"Bobs public key: ${bob.publicKey.getStringFromKey}")
println(s"Bobs private key: ${bob.privateKey.getStringFromKey}")

println(s"Is valid: ${blockchain.isValidChain()}")

println(s"Alice balance: ${alice.getBalance(blockchain)}")


+ 4
- 8
src/main/scala/com/zambito/blockchain/Transaction.scala View File

@@ -1,7 +1,7 @@
package com.zambito.blockchain

import java.security._
import java.util.concurrent.atomic.AtomicInteger
import scala.compat.Platform

/**
* A transaction of currency from one [[Wallet]] to another.
@@ -12,7 +12,8 @@ import java.util.concurrent.atomic.AtomicInteger
* @param inputs Proof the sender recieved to funds at some point.
* @param signature Approval of the sender.
* @author Robby Zambito
* @see [[https://github.com/CryptoKass/NoobChain-Tutorial-Part-2/blob/master/src/noobchain/Transaction.java `Transaction.java`]]
* @see [[https://github.com/Zambito1/SimpleScalaBlockchain/blob/master/src/main/scala/com/zambito/blockchain/Transaction.scala `Source`]]
* @see [[https://github.com/CryptoKass/NoobChain-Tutorial-Part-2/blob/master/src/noobchain/Transaction.java Transaction.java]]
*/
case class Transaction(sender: PublicKey,
recipient: PublicKey,
@@ -23,7 +24,7 @@ case class Transaction(sender: PublicKey,
(sender.getStringFromKey +
recipient.getStringFromKey +
value.toString +
Transaction.number.incrementAndGet().toString).encrypted()
Platform.currentTime.toString).encrypted()
}

val outputs: Seq[TransactionOutput] = Seq(
@@ -54,11 +55,6 @@ case class Transaction(sender: PublicKey,

}

/** Simply used to avoid hash collisions between separate [[Transaction]]s */
object Transaction {
private val number = new AtomicInteger(0)
}

/**
* Represents the previous transaction which the sender recieved the funds to create a transaction.
* @param transactionOutputId Hash of the [[TransactionOutput]] that put the sender in control of the funds.


+ 3
- 2
src/main/scala/com/zambito/blockchain/Wallet.scala View File

@@ -6,10 +6,11 @@ import scala.annotation.tailrec
import scala.collection.Map

/**
* A wallet represents an entity which can own funds.
* A wallet represents an entity which can control funds.
*
* @author Robby Zambito
* @see [[https://github.com/CryptoKass/NoobChain-Tutorial-Part-2/blob/master/src/noobchain/Wallet.java `Wallet.java`]]
* @see [[https://github.com/Zambito1/SimpleScalaBlockchain/blob/master/src/main/scala/com/zambito/blockchain/Wallet.scala `Source`]]
* @see [[https://github.com/CryptoKass/NoobChain-Tutorial-Part-2/blob/master/src/noobchain/Wallet.java Wallet.java]]
*/
class Wallet {
val (privateKey, publicKey) = {


+ 34
- 2
src/main/scala/com/zambito/blockchain/package.scala View File

@@ -2,13 +2,43 @@ package com.zambito

import java.security._
import java.util.Base64
import com.zambito.blockchain._

/**
* Simple blockchain built using Scala
*
* This blockchain features:
*
* 1. Proof-of-work block mining.
* 1. [[Block]]s which represent a set of valid transactions that occurred since the last block was mined.
* 1. [[Wallet]]s which can create transactions within the bounds of their funds.
* 1. A [[Blockchain]] which can be validated by checking the hashes of the blocks to make sure no changes have
* occurred, and checking the validity of the transactions on each block.
* 1. A collection of UTXOs (unspent transaction outputs) to quickly check the funds of a wallet, and for various
* other operations.
*
* All operations are done in a functional style. There are is no use of the keyword `var` in this project. This
* implementation would not be good for a long term solution, because append operations on immutable collections are
* highly inefficient when they grow to a large size. A more reasonable solution would choose to use a mutable
* collection to represent the blockchain, where race conditions are a non-issue due to the nature of the
* tamper-resistance. This project was created purely for practice reasons.
*
* Built following along with [[https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3cb03fa this tutorial]].
*
* Found from in [[https://github.com/openblockchains/awesome-blockchains this repo]].
*
* @author Robby Zambito
* @see [[https://github.com/Zambito1/SimpleScalaBlockchain/blob/master/src/main/scala/com/zambito/blockchain/package.scala `Source`]]
*/
package object blockchain {
val DIFFICULTY = 3
/** Represents the number of leading 0s required for a valid block hash */
val DIFFICULTY = 5

/** Minimum value of a valid transaction */
val MIN_TRANSACTION = 0.0f

/** Implicit functions which extend strings. Allows for strings to encrypt themselves. */
implicit class StringUtil(str: String) {

def encrypted(algorithm: String = "SHA-256"): String = {
val digest = MessageDigest.getInstance(algorithm)
val hash = digest.digest(str.getBytes("UTF-8"))
@@ -26,11 +56,13 @@ package object blockchain {
}
}

/** Allows Keys to be printed in hex. */
implicit class KeyUtil(key: Key) {
def getStringFromKey: String =
Base64.getEncoder.encodeToString(key.getEncoded)
}

/** Allows third parties to verify the signature on some data given a publicKey */
implicit class PublicKeyUtil(pk: PublicKey) {
def verifySig(data: String, signature: Array[Byte]): Boolean = {
try {


Loading…
Cancel
Save