-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
611 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1 @@ | ||
{ | ||
"header": { | ||
"index": 0, | ||
"previousHash": "0000000000000000000000000000000000000000000000000000000000000000", | ||
"data": "Welcome to Blockchain in Scala!", | ||
"merkleHash": "7814a9c43e9015462e5ffec1a3a9a69be024c1aacfa3ec4c879b5cd544761e7e", | ||
"timestamp": 1523472721, | ||
"nonce": 13860, | ||
"hash": "00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f" | ||
}, | ||
"transactions": [ | ||
{ | ||
"sender": "0000000000000000000000000000000000000000000000000000000000000000", | ||
"receiver": "0000000000000000000000000000000000000000000000000000000000000000", | ||
"amount": 50.0 | ||
} | ||
] | ||
} | ||
{"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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
30818d020100301006072a8648ce3d020106052b8104000a04763074020101042005ab8a244673c5f9d86c64c28a0440368896a35daac9ec08a909de920c3dbc3aa00706052b8104000aa144034200049671ad288b396bdadf9d2d85640c6c61e14fa4a837b7b335bba21f226ba1525974c1a3f70fa1bc5a55c48ceced51468fe29bbbf67b22afa40383f99b98b841f9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3056301006072a8648ce3d020106052b8104000a034200049671ad288b396bdadf9d2d85640c6c61e14fa4a837b7b335bba21f226ba1525974c1a3f70fa1bc5a55c48ceced51468fe29bbbf67b22afa40383f99b98b841f9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3045022100a15dff564d1e0fd4e475956d6fe1c8fc00fb86cb3c38729b47386011a8e9b38d02200c575841124500d4a1b250dec4fa6dfa48388cec54649b4f168767aae3062d65 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.fluency03.blockchain | ||
|
||
import java.security._ | ||
import java.security.spec.{PKCS8EncodedKeySpec, X509EncodedKeySpec} | ||
|
||
import org.bouncycastle.jce.ECNamedCurveTable | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider | ||
import org.bouncycastle.jce.spec.ECParameterSpec | ||
|
||
object Crypto { | ||
|
||
Security.addProvider(new BouncyCastleProvider) | ||
|
||
val SPECP256K1 = "secp256k1" | ||
val KEY_ALGORITHM = "ECDSA" | ||
val KEY_PROVIDER = "BC" | ||
|
||
val ecSpec: ECParameterSpec = ECNamedCurveTable.getParameterSpec(SPECP256K1) | ||
|
||
def sign(data: Array[Byte], privateKey: Array[Byte]): Array[Byte] = { | ||
val keySpec: PKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey) | ||
val keyFactory: KeyFactory = KeyFactory.getInstance(KEY_ALGORITHM) | ||
val key: PrivateKey = keyFactory.generatePrivate(keySpec) | ||
|
||
val sig: Signature = Signature.getInstance(KEY_ALGORITHM, KEY_PROVIDER) | ||
sig.initSign(key, new SecureRandom) | ||
sig.update(data) | ||
sig.sign() | ||
} | ||
|
||
def verify(data: Array[Byte], publicKey: Array[Byte], signature: Array[Byte]): Boolean = { | ||
val keySpec: X509EncodedKeySpec = new X509EncodedKeySpec(publicKey) | ||
val keyFactory: KeyFactory = KeyFactory.getInstance(KEY_ALGORITHM) | ||
val key: PublicKey = keyFactory.generatePublic(keySpec) | ||
|
||
val sig: Signature = Signature.getInstance(KEY_ALGORITHM, KEY_PROVIDER) | ||
sig.initVerify(key) | ||
sig.update(data) | ||
sig.verify(signature) | ||
} | ||
|
||
def generateKeyPair(): KeyPair = { | ||
val gen: KeyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM, KEY_PROVIDER) | ||
gen.initialize(ecSpec, new SecureRandom) | ||
gen.generateKeyPair() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.fluency03.blockchain.api.actors | ||
|
||
import akka.actor.{Actor, ActorLogging, ActorRef, ActorSelection, Props} | ||
import com.fluency03.blockchain.api.actors.NetworkActor._ | ||
import com.fluency03.blockchain.api.utils.GenericMessage.Response | ||
import com.fluency03.blockchain.api.{BLOCKCHAIN_ACTOR_NAME, BLOCK_ACTOR_NAME, PARENT_UP} | ||
|
||
import scala.collection.mutable | ||
|
||
object NetworkActor { | ||
final case object GetPeers | ||
final case class CreatePeer(id: String) | ||
final case class GetPeer(id: String) | ||
final case class DeletePeer(id: String) | ||
|
||
def props: Props = Props[NetworkActor] | ||
} | ||
|
||
class NetworkActor extends Actor with ActorLogging { | ||
override def preStart(): Unit = log.info("{} started!", this.getClass.getSimpleName) | ||
override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName) | ||
|
||
def receive: Receive = { | ||
case GetPeers => context.children.map(_.path.name).toList | ||
case CreatePeer(id) => | ||
if (context.child(id).isDefined) sender() ! Response(s"Peer $id has been created.") | ||
else { | ||
val _ = context.actorOf(Props[PeerActor], name = id) | ||
sender() ! Response(s"Peer $id created.") | ||
} | ||
case GetPeer(id) => | ||
sender() ! context.child(id).isDefined | ||
case DeletePeer(id) => | ||
if (context.child(id).isDefined) { | ||
context stop context.child(id).get | ||
sender() ! Response(s"Peer $id deleted.") | ||
} else sender() ! Response(s"Peer $id does not exist.") | ||
case _ => unhandled _ | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.fluency03.blockchain.api.actors | ||
|
||
import java.security.{KeyPair, PrivateKey, PublicKey} | ||
|
||
import akka.actor.{Actor, ActorLogging, Props} | ||
|
||
import scala.collection.mutable | ||
|
||
object PeerActor { | ||
|
||
|
||
def props: Props = Props[PeerActor] | ||
} | ||
|
||
class PeerActor extends Actor with ActorLogging { | ||
override def preStart(): Unit = log.info("{} started!", this.getClass.getSimpleName) | ||
override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName) | ||
|
||
// TODO (Chang): not persistent | ||
val wallets = mutable.Map.empty[String, KeyPair] | ||
val publicKeys = mutable.Map.empty[String, PublicKey] | ||
|
||
def receive: Receive = { | ||
case _ => ??? | ||
// case _ => unhandled _ | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.