diff --git a/src/main/scala/com/github/fluency03/blockchain/core/KeyContainer.scala b/src/main/scala/com/github/fluency03/blockchain/core/KeyContainer.scala index 3fd74c5..accbfb0 100644 --- a/src/main/scala/com/github/fluency03/blockchain/core/KeyContainer.scala +++ b/src/main/scala/com/github/fluency03/blockchain/core/KeyContainer.scala @@ -13,6 +13,7 @@ case class KeyContainer() { private[this] val keyPair: KeyPair = Secp256k1.generateKeyPair() + // TODO (Chang): change it to actual address of a PublicKey lazy val address: String = keyPair.getPublic.toHex lazy val publicKeyHex: String = keyPair.getPublic.toHex diff --git a/src/main/scala/com/github/fluency03/blockchain/core/Transaction.scala b/src/main/scala/com/github/fluency03/blockchain/core/Transaction.scala index 9b46674..920430c 100644 --- a/src/main/scala/com/github/fluency03/blockchain/core/Transaction.scala +++ b/src/main/scala/com/github/fluency03/blockchain/core/Transaction.scala @@ -49,6 +49,7 @@ object Transaction { def createCoinbase(blockIndex: Int): TxIn = TxIn(Outpoint("", blockIndex), "") + // TODO (Chang): correct coinbase format def createCoinbaseTx(blockIndex: Int, miner: String, timestamp: Long): Transaction = Transaction(Seq(createCoinbase(blockIndex)), Seq(TxOut(miner, COINBASE_AMOUNT)), timestamp) diff --git a/src/main/scala/com/github/fluency03/blockchain/core/Wallet.scala b/src/main/scala/com/github/fluency03/blockchain/core/Wallet.scala index 4546f5e..fbe7036 100644 --- a/src/main/scala/com/github/fluency03/blockchain/core/Wallet.scala +++ b/src/main/scala/com/github/fluency03/blockchain/core/Wallet.scala @@ -1,41 +1,46 @@ package com.github.fluency03.blockchain.core -trait Wallet { - -} +import com.github.fluency03.blockchain.core.KeyContainer.balanceOfKey +import scala.collection.mutable +trait Wallet { -case class RandomWallet() extends Wallet { + def getKey(str: String): Option[KeyContainer] + def newKey(): KeyContainer + def balance(uTxOs: mutable.Map[Outpoint, TxOut]): Long } +case class RandomWallet() extends Wallet { -object RandomWallet { - - - -} - - + private[this] val keys = mutable.Map.empty[String, KeyContainer] + override def getKey(str: String): Option[KeyContainer] = keys.get(str) -case class SeededWallet() extends Wallet { + override def newKey(): KeyContainer = { + val aNewKey = KeyContainer() + keys += (aNewKey.publicKeyHex -> aNewKey) + aNewKey + } + override def balance(uTxOs: mutable.Map[Outpoint, TxOut]): Long = + keys.values.map(k => balanceOfKey(k, uTxOs)).sum } +// TODO (Chang): To be implemented +case class SeededWallet() extends Wallet { -object SeededWallet { + override def getKey(str: String): Option[KeyContainer] = ??? + override def newKey(): KeyContainer = ??? + override def balance(uTxOs: mutable.Map[Outpoint, TxOut]): Long = ??? } - - -