-
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
5 changed files
with
77 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.fluency03.blockchain | ||
package core | ||
|
||
import java.security.KeyPair | ||
|
||
import com.fluency03.blockchain.core.Wallet.balanceOfWallet | ||
|
||
import scala.collection.mutable | ||
|
||
case class Wallet() { | ||
|
||
private[this] val keyPair: KeyPair = Crypto.generateKeyPair() | ||
|
||
lazy val address: String = keyPair.getPublic.toHex | ||
|
||
def balance(uTxOs: mutable.Map[Outpoint, TxOut]): Long = balanceOfWallet(this, uTxOs) | ||
|
||
} | ||
|
||
object Wallet { | ||
|
||
def balanceOfWallet(wallet: Wallet, uTxOs: mutable.Map[Outpoint, TxOut]): Long = | ||
balanceOfAddress(wallet.address, uTxOs) | ||
|
||
def balanceOfAddress(address: String, uTxOs: mutable.Map[Outpoint, TxOut]): Long = | ||
uTxOs.values.filter(_.address == address).map(_.amount).sum | ||
|
||
} |
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
26 changes: 26 additions & 0 deletions
26
src/test/scala/com/fluency03/blockchain/core/WalletTest.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,26 @@ | ||
package com.fluency03.blockchain.core | ||
|
||
import com.fluency03.blockchain.core.Wallet._ | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
import scala.collection.mutable | ||
|
||
class WalletTest extends FlatSpec with Matchers { | ||
|
||
"balanceOfWallet" should "obtain the balance of a Wallet based on UTXOs." in { | ||
val wallet = Wallet() | ||
val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut] | ||
|
||
balanceOfWallet(wallet, uTxOs) shouldEqual 0 | ||
wallet.balance(uTxOs) shouldEqual 0 | ||
|
||
uTxOs += (Outpoint("def0", 0) -> TxOut(wallet.address, 40)) | ||
uTxOs += (Outpoint("def0", 1) -> TxOut("abc4", 40)) | ||
|
||
balanceOfWallet(wallet, uTxOs) shouldEqual 40 | ||
wallet.balance(uTxOs) shouldEqual 40 | ||
} | ||
|
||
|
||
} |