Skip to content

Commit

Permalink
update private public key to he
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 27, 2018
1 parent e461883 commit 6e5508f
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/main/resources/genesis-block.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"header":{"index":0,"previousHash":"0000000000000000000000000000000000000000000000000000000000000000","data":"Welcome to Blockchain in Scala!","merkleHash":"e580ae290899b8acc333fdb4e5ce52b6161c0df8f433a96cfcacbc4c211f1dc6","timestamp":1523472721,"difficulty":4,"nonce":289612},"transactions":[{"txIns":[{"previousOut":{"id":"","index":0},"signature":""}],"txOuts":[{"address":"3056301006072a8648ce3d020106052b8104000a034200049671ad288b396bdadf9d2d85640c6c61e14fa4a837b7b335bba21f226ba1525974c1a3f70fa1bc5a55c48ceced51468fe29bbbf67b22afa40383f99b98b841f9","amount":50}],"timestamp":1523472721,"id":"e580ae290899b8acc333fdb4e5ce52b6161c0df8f433a96cfcacbc4c211f1dc6"}],"hash":"0000d691591d3f999dce60c54719bc38b5bc2fb3ac39f76d9343b5ad4c01548b"}
{"header":{"index":0,"previousHash":"0000000000000000000000000000000000000000000000000000000000000000","data":"Welcome to Blockchain in Scala!","merkleHash":"47f123f38ab09c9abd1aa182e27ebf6a1a915a01ce7519fb68ac354978ea0350","timestamp":1523472721,"difficulty":4,"nonce":50466},"transactions":[{"txIns":[{"previousOut":{"id":"","index":0},"signature":""}],"txOuts":[{"address":"04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a2541129368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d","amount":50}],"timestamp":1523472721,"id":"47f123f38ab09c9abd1aa182e27ebf6a1a915a01ce7519fb68ac354978ea0350"}],"hash":"000005827dccd635761469d30df8b6e61dc4223630a4e2d2010e89ab4763e9de"}
2 changes: 1 addition & 1 deletion src/main/resources/private-key
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30818d020100301006072a8648ce3d020106052b8104000a04763074020101042005ab8a244673c5f9d86c64c28a0440368896a35daac9ec08a909de920c3dbc3aa00706052b8104000aa144034200049671ad288b396bdadf9d2d85640c6c61e14fa4a837b7b335bba21f226ba1525974c1a3f70fa1bc5a55c48ceced51468fe29bbbf67b22afa40383f99b98b841f9
36466d36ee0642a530b774de73c778a807a3b6b586201a1fabb0597c640836af
2 changes: 1 addition & 1 deletion src/main/resources/public-key
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3056301006072a8648ce3d020106052b8104000a034200049671ad288b396bdadf9d2d85640c6c61e14fa4a837b7b335bba21f226ba1525974c1a3f70fa1bc5a55c48ceced51468fe29bbbf67b22afa40383f99b98b841f9
04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a2541129368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d
1 change: 0 additions & 1 deletion src/main/resources/signature

This file was deleted.

21 changes: 20 additions & 1 deletion src/main/scala/com/fluency03/blockchain/Crypto.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.fluency03.blockchain

import java.math.BigInteger
import java.security._
import java.security.spec.{PKCS8EncodedKeySpec, X509EncodedKeySpec}

import org.bouncycastle.jce.ECNamedCurveTable
import org.bouncycastle.jce.interfaces.{ECPrivateKey, ECPublicKey}
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.jce.spec.ECParameterSpec
import org.bouncycastle.jce.spec.{ECParameterSpec, ECPrivateKeySpec, ECPublicKeySpec}

object Crypto {

Expand Down Expand Up @@ -45,4 +47,21 @@ object Crypto {
gen.generateKeyPair()
}

def recoverPublicKey(hex: String): PublicKey =
KeyFactory.getInstance(KEY_ALGORITHM)
.generatePublic(new ECPublicKeySpec(ecSpec.getCurve.decodePoint(hex.hex2Bytes), ecSpec))

def recoverPrivateKey(hex: String): PrivateKey =
KeyFactory.getInstance(KEY_ALGORITHM)
.generatePrivate(new ECPrivateKeySpec(new BigInteger(hex, 16), ecSpec))

def publicKeyToHex(publicKey: PublicKey): String =
publicKey.asInstanceOf[ECPublicKey].getQ.getEncoded(false).toHex

def privateKeyToHex(privateKey: PrivateKey): String =
privateKey.asInstanceOf[ECPrivateKey].getD.toString(16)




}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import scala.collection.mutable

object PeerActor {
final case object GetPublicKeys
final case object CreateKeyPair
def props: Props = Props[PeerActor]
}

Expand All @@ -20,16 +21,20 @@ class PeerActor extends ActorSupport {

// TODO (Chang): need persistence
val wallets = mutable.Map.empty[String, KeyPair]
wallets += {
val pair: KeyPair = Crypto.generateKeyPair()
(pair.getPublic.getEncoded.toHex, pair)
}
addNewKeyPair()

val others = mutable.Map.empty[String, Peer]

def receive: Receive = {
case GetPublicKeys => sender() ! wallets.values.map(_.getPublic.getEncoded.toHex).toSet
case CreateKeyPair => sender() ! addNewKeyPair().getPublic.getEncoded.toHex
case _ => unhandled _
}

private def addNewKeyPair(): KeyPair = {
val pair: KeyPair = Crypto.generateKeyPair()
wallets += (pair.getPublic.getEncoded.toHex -> pair)
pair
}

}
18 changes: 9 additions & 9 deletions src/main/scala/com/fluency03/blockchain/core/Block.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core

import com.fluency03.blockchain.core.BlockHeader.hashOfHeaderFields
import com.fluency03.blockchain.core.Transaction.createCoinbaseTx
import org.json4s.JsonDSL._
import org.json4s.native.JsonMethods.{compact, render}
import org.json4s.{Extraction, JValue}

Expand All @@ -13,13 +12,13 @@ import org.json4s.{Extraction, JValue}
* @param transactions Seq of Transactions included in current Block
*/
case class Block(header: BlockHeader, transactions: Seq[Transaction], hash: String) {
lazy val index: Int = header.index
lazy val previousHash: String = header.previousHash
lazy val data: String = header.data
lazy val merkleHash: String = header.merkleHash
lazy val timestamp: Long = header.timestamp
lazy val difficulty: Int = header.difficulty
lazy val nonce: Int = header.nonce
def index: Int = header.index
def previousHash: String = header.previousHash
def data: String = header.data
def merkleHash: String = header.merkleHash
def timestamp: Long = header.timestamp
def difficulty: Int = header.difficulty
def nonce: Int = header.nonce

def nextTrial(): Block = Block(header.nextTrial(), transactions)

Expand Down Expand Up @@ -113,8 +112,9 @@ object Block {
transactions: Seq[Transaction]): Block =
mineNextBlock(currentBlock.index + 1, currentBlock.hash, newBlockData, timestamp, difficulty, transactions)

// TODO (Chang): Check transactions in the new block?
def canBeChained(newBlock: Block, previousBlock: Block): Boolean =
previousBlock.index + 1 == newBlock.index && previousBlock.hash == newBlock.previousHash
previousBlock.index + 1 == newBlock.index && previousBlock.hash == newBlock.previousHash && newBlock.hasValidHash



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.json4s.{Extraction, JValue}
*/
case class Blockchain(difficulty: Int = 4, chain: Seq[Block] = Seq(Block.genesisBlock)) {


def addBlock(newBlockData: String, transactions: Seq[Transaction]): Blockchain =
Blockchain(difficulty, mineNextBlock(newBlockData, transactions) +: chain)

Expand Down Expand Up @@ -50,13 +49,8 @@ object Blockchain {
def isValidChain(chain: Seq[Block]): Boolean = chain match {
case Nil => true
case g +: Nil => g.previousHash == ZERO64 && g.index == 0 && g.hasValidHash
case a +: b +: tail => canBeChained(a, b) && a.hasValidHash && isValidChain(b +: tail)
case a +: b +: tail => canBeChained(a, b) && isValidChain(b +: tail)
}






}

11 changes: 10 additions & 1 deletion src/main/scala/com/fluency03/blockchain/package.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.fluency03

import java.nio.charset.Charset
import java.security.MessageDigest
import java.security.{MessageDigest, PrivateKey, PublicKey}
import java.time.Instant

import com.fluency03.blockchain.Crypto.{privateKeyToHex, publicKeyToHex}
import org.bouncycastle.util.encoders.{Base64, Hex}
import org.json4s.{Formats, NoTypeHints}
import org.json4s.native.Serialization
Expand Down Expand Up @@ -41,6 +42,14 @@ package object blockchain {
def toBase64: String = base64Of(bytes)
}

implicit class PublicKeyImplicit(val publicKey: PublicKey) {
def toHex: String = publicKeyToHex(publicKey)
}

implicit class PrivateKeyImplicit(val privateKey: PrivateKey) {
def toHex: String = privateKeyToHex(privateKey)
}


/**
* Generate SHA256 Hash from a input String.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/genesis-block.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"header":{"index":0,"previousHash":"0000000000000000000000000000000000000000000000000000000000000000","data":"Welcome to Blockchain in Scala!","merkleHash":"e580ae290899b8acc333fdb4e5ce52b6161c0df8f433a96cfcacbc4c211f1dc6","timestamp":1523472721,"difficulty":4,"nonce":289612},"transactions":[{"txIns":[{"previousOut":{"id":"","index":0},"signature":""}],"txOuts":[{"address":"3056301006072a8648ce3d020106052b8104000a034200049671ad288b396bdadf9d2d85640c6c61e14fa4a837b7b335bba21f226ba1525974c1a3f70fa1bc5a55c48ceced51468fe29bbbf67b22afa40383f99b98b841f9","amount":50}],"timestamp":1523472721,"id":"e580ae290899b8acc333fdb4e5ce52b6161c0df8f433a96cfcacbc4c211f1dc6"}],"hash":"0000d691591d3f999dce60c54719bc38b5bc2fb3ac39f76d9343b5ad4c01548b"}
{"header":{"index":0,"previousHash":"0000000000000000000000000000000000000000000000000000000000000000","data":"Welcome to Blockchain in Scala!","merkleHash":"47f123f38ab09c9abd1aa182e27ebf6a1a915a01ce7519fb68ac354978ea0350","timestamp":1523472721,"difficulty":4,"nonce":50466},"transactions":[{"txIns":[{"previousOut":{"id":"","index":0},"signature":""}],"txOuts":[{"address":"04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a2541129368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d","amount":50}],"timestamp":1523472721,"id":"47f123f38ab09c9abd1aa182e27ebf6a1a915a01ce7519fb68ac354978ea0350"}],"hash":"000005827dccd635761469d30df8b6e61dc4223630a4e2d2010e89ab4763e9de"}
2 changes: 1 addition & 1 deletion src/test/resources/private-key
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30818d020100301006072a8648ce3d020106052b8104000a04763074020101042005ab8a244673c5f9d86c64c28a0440368896a35daac9ec08a909de920c3dbc3aa00706052b8104000aa144034200049671ad288b396bdadf9d2d85640c6c61e14fa4a837b7b335bba21f226ba1525974c1a3f70fa1bc5a55c48ceced51468fe29bbbf67b22afa40383f99b98b841f9
36466d36ee0642a530b774de73c778a807a3b6b586201a1fabb0597c640836af
2 changes: 1 addition & 1 deletion src/test/resources/public-key
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3056301006072a8648ce3d020106052b8104000a034200049671ad288b396bdadf9d2d85640c6c61e14fa4a837b7b335bba21f226ba1525974c1a3f70fa1bc5a55c48ceced51468fe29bbbf67b22afa40383f99b98b841f9
04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a2541129368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d
1 change: 0 additions & 1 deletion src/test/resources/signature

This file was deleted.

12 changes: 9 additions & 3 deletions src/test/scala/com/fluency03/blockchain/CryptoTest.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.fluency03.blockchain

import com.fluency03.blockchain.Crypto._

import java.security.KeyPair

import org.bouncycastle.jce.interfaces.{ECPrivateKey, ECPublicKey}
import org.scalatest.{FlatSpec, Matchers}

class CryptoTest extends FlatSpec with Matchers {

"Crypto" should "be able to sign a data and verify the signature. " in {
val pair: KeyPair = Crypto.generateKeyPair()
val pair: KeyPair = generateKeyPair()
val data = "Welcome to Blockchain in Scala!".toCharArray.map(_.toByte)
val signature = Crypto.sign(data, pair.getPrivate.getEncoded)
Crypto.verify(data, pair.getPublic.getEncoded, signature) shouldEqual true
val signature = sign(data, pair.getPrivate.getEncoded)
verify(data, pair.getPublic.getEncoded, signature) shouldEqual true

recoverPublicKey(publicKeyToHex(pair.getPublic)) shouldEqual pair.getPublic
recoverPrivateKey(privateKeyToHex(pair.getPrivate)) shouldEqual pair.getPrivate
}


Expand Down

0 comments on commit 6e5508f

Please sign in to comment.