diff --git a/src/main/scala/com/fluency03/blockchain/core/Wallet.scala b/src/main/scala/com/fluency03/blockchain/core/Wallet.scala index fcaf404..65173a8 100644 --- a/src/main/scala/com/fluency03/blockchain/core/Wallet.scala +++ b/src/main/scala/com/fluency03/blockchain/core/Wallet.scala @@ -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 @@ -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 { diff --git a/src/test/scala/com/fluency03/blockchain/core/WalletTest.scala b/src/test/scala/com/fluency03/blockchain/core/WalletTest.scala index 856fe73..ba25c80 100644 --- a/src/test/scala/com/fluency03/blockchain/core/WalletTest.scala +++ b/src/test/scala/com/fluency03/blockchain/core/WalletTest.scala @@ -1,4 +1,5 @@ -package com.fluency03.blockchain.core +package com.fluency03.blockchain +package core import com.fluency03.blockchain.core.Wallet._ @@ -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)) + } + }