Skip to content

Commit

Permalink
add sign to Wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 27, 2018
1 parent 0280376 commit 5b8474b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/scala/com/fluency03/blockchain/core/Wallet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package core
import java.security.KeyPair

import com.fluency03.blockchain.core.Wallet.balanceOfWallet
import com.fluency03.blockchain.core.Transaction.signTxIn

import scala.collection.mutable

Expand All @@ -15,6 +16,9 @@ case class Wallet() {

def balance(uTxOs: mutable.Map[Outpoint, TxOut]): Long = balanceOfWallet(this, uTxOs)

def sign(txId: String, txIn: TxIn, uTxOs: mutable.Map[Outpoint, TxOut]): Option[TxIn] =
signTxIn(txId, txIn, keyPair, uTxOs)

}

object Wallet {
Expand Down
19 changes: 18 additions & 1 deletion src/test/scala/com/fluency03/blockchain/core/WalletTest.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.fluency03.blockchain.core
package com.fluency03.blockchain
package core

import com.fluency03.blockchain.core.Wallet._

Expand All @@ -22,5 +23,21 @@ class WalletTest extends FlatSpec with Matchers {
wallet.balance(uTxOs) shouldEqual 40
}

"Wallet" should "be able to sign a TxIn." in {
val wallet = Wallet()
val id = "".toSha256
val txIn = TxIn(Outpoint("def0", 0), "abc")
val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut]

val signedTxIn0 = wallet.sign(id, txIn, uTxOs)
signedTxIn0 shouldEqual None

uTxOs += (Outpoint("def0", 0) -> TxOut(wallet.address, 40))
uTxOs += (Outpoint("def0", 1) -> TxOut("abc4", 40))

val signedTxIn = wallet.sign(id, txIn, uTxOs)
signedTxIn shouldEqual Some(TxIn(Outpoint("def0", 0), signedTxIn.get.signature))
}


}

0 comments on commit 5b8474b

Please sign in to comment.