Skip to content

Commit

Permalink
fix address as public key
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 27, 2018
1 parent afbaabc commit 1886627
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class PeerActor extends ActorSupport {
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 GetPublicKeys => sender() ! wallets.values.map(_.getPublic.toHex).toSet
case CreateKeyPair => sender() ! addNewKeyPair().getPublic.toHex
case _ => unhandled _
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core

import java.security.KeyPair

import com.fluency03.blockchain.Crypto.recoverPublicKey
import com.fluency03.blockchain.core.Transaction.hashOfTransaction
import org.json4s.native.JsonMethods.{compact, render}
import org.json4s.{Extraction, JValue}
Expand Down Expand Up @@ -63,7 +64,7 @@ object Transaction {
def signTxIn(txId: Bytes, txIn: TxIn, keyPair: KeyPair, unspentTxOuts: mutable.Map[Outpoint, TxOut]): Option[TxIn] =
unspentTxOuts.get(txIn.previousOut) match {
case Some(uTxO) =>
if (keyPair.getPublic.getEncoded.toHex != uTxO.address) None
if (keyPair.getPublic.toHex != uTxO.address) None
else Some(TxIn(txIn.previousOut, Crypto.sign(txId, keyPair.getPrivate.getEncoded).toHex))
case None => None
}
Expand All @@ -73,7 +74,7 @@ object Transaction {

def validateTxIn(txIn: TxIn, txId: Bytes, unspentTxOuts: mutable.Map[Outpoint, TxOut]): Boolean =
unspentTxOuts.get(txIn.previousOut) match {
case Some(txOut) => Crypto.verify(txId, txOut.address.hex2Bytes, txIn.signature.hex2Bytes)
case Some(txOut) => Crypto.verify(txId, recoverPublicKey(txOut.address).getEncoded, txIn.signature.hex2Bytes)
case None => false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class TransactionTest extends FlatSpec with Matchers {
val signedTxIn0 = signTxIn(hash, txIn, pair, unspentTxOuts)
signedTxIn0 shouldEqual None

unspentTxOuts += (Outpoint("def0", 0) -> TxOut(pair.getPublic.getEncoded.toHex, 40))
unspentTxOuts += (Outpoint("def0", 0) -> TxOut(pair.getPublic.toHex, 40))
unspentTxOuts += (Outpoint("def0", 1) -> TxOut("abc4", 40))

val signedTxIn = signTxIn(hash, txIn, pair, unspentTxOuts)
Expand All @@ -121,7 +121,7 @@ class TransactionTest extends FlatSpec with Matchers {

val txIn = TxIn(Outpoint("def0", 0), "abc1")
val unspentTxOuts: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut]
unspentTxOuts += (Outpoint("def0", 0) -> TxOut(pair.getPublic.getEncoded.toHex, 40))
unspentTxOuts += (Outpoint("def0", 0) -> TxOut(pair.getPublic.toHex, 40))
unspentTxOuts += (Outpoint("def0", 1) -> TxOut("abc4", 40))

val signedTxIn = signTxIn(hash, txIn, pair, unspentTxOuts)
Expand Down

0 comments on commit 1886627

Please sign in to comment.