add GetBalance to chain

This commit is contained in:
Wei Yang 2017-10-22 10:53:22 +08:00
parent 9b2f73b11b
commit 56c9e3faa6
1 changed files with 13 additions and 0 deletions

View File

@ -376,3 +376,16 @@ func (bc *Blockchain) PrintHTML() string {
return strings.Join(lines, "\n")
}
func (bc *Blockchain) GetBalance(address string) int {
UTXOSet := UTXOSet{bc}
balance := 0
pubKeyHash := Base58Decode([]byte(address))
pubKeyHash = pubKeyHash[1 : len(pubKeyHash)-4]
UTXOs := UTXOSet.FindUTXO(pubKeyHash)
for _, out := range UTXOs {
balance += out.Value
}
return balance
}