Skip to content

Commit

Permalink
Added signature validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Robby Zambito committed Jun 13, 2018
1 parent a476a5e commit d457d24
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions .idea/modules/simplescalablockchain.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions src/main/scala/com/zambito/blockchain/Main.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.zambito.blockchain

import org.bouncycastle.jce.provider.BouncyCastleProvider
import scala.xml.PrettyPrinter
import java.security.Security


object Main extends App {
Security.addProvider(new BouncyCastleProvider)

val bob = new Wallet
val alice = new Wallet

lazy val blockchain: Blockchain =
Block.mineBlock(Block("First block data", "0")) #::
Expand All @@ -22,9 +28,6 @@ object Main extends App {
println("</blockchain>")
}


lazyBlockchainPrint(blockchain)

def isChainValid: Blockchain => Boolean = {
case fst +: snd +: tail
if snd.hash == Block.calculateHash(snd) &&
Expand All @@ -39,5 +42,25 @@ object Main extends App {
case _ => true
}


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


val unsignedTransaction = Transaction(bob.publicKey, aklice.publicKey, 5, Seq[TransactionInput]())

val transaction = Transaction.signTransaction(
unsignedTransaction,
bob.privateKey
)


println(s"Valid signature on unsigned: ${unsignedTransaction.hasValidSignature}")
println(s"Valid signature: ${transaction.hasValidSignature}")
println(s"Transaction: $transaction")


lazyBlockchainPrint(blockchain)

println(s"Is valid: ${isChainValid(blockchain)}")
}
33 changes: 30 additions & 3 deletions src/main/scala/com/zambito/blockchain/package.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.zambito

import java.security._
import java.util.Base64

package object blockchain {
val DIFFICULTY = 5
val DIFFICULTY = 3

type Blockchain = Seq[Block]

implicit class StringUtil(str: String) {
import java.security.MessageDigest

def encrypted(algorithm: String = "SHA-256"): String = {
val digest = MessageDigest.getInstance(algorithm)
Expand All @@ -14,8 +18,31 @@ package object blockchain {
.map(s => if(s.length == 1) s + "0" else s)
.mkString
}

def signData(privateKey: PrivateKey): Array[Byte] = {
val dsa = Signature.getInstance("ECDSA", "BC")
dsa.initSign(privateKey)
dsa.update(str.getBytes)
dsa.sign()
}
}

type Blockchain = Seq[Block]
implicit class KeyUtil(key: Key) {
def getStringFromKey: String =
Base64.getEncoder.encodeToString(key.getEncoded)
}

implicit class PublicKeyUtil(pk: PublicKey) {
def verifySig(data: String, signature: Array[Byte]): Boolean = {
try {
val ecdsaVerify = Signature.getInstance("ECDSA", "BC")
ecdsaVerify.initVerify(pk)
ecdsaVerify.update(data.getBytes)
ecdsaVerify.verify(signature)
} catch {
case _: Exception => false
}
}
}

}

0 comments on commit d457d24

Please sign in to comment.