Skip to content

Commit

Permalink
add Wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 9, 2018
1 parent 838d064 commit a53f25c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
37 changes: 21 additions & 16 deletions src/main/scala/com/github/fluency03/blockchain/core/Wallet.scala
Original file line number Diff line number Diff line change
@@ -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 = ???

}



0 comments on commit a53f25c

Please sign in to comment.