blockchain_go/cli_getbalance.go

27 lines
492 B
Go
Raw Normal View History

package main
2017-09-10 01:53:14 -05:00
import (
"fmt"
"log"
)
func (cli *CLI) getBalance(address string) {
2017-09-10 01:53:14 -05:00
if !ValidateAddress(address) {
log.Panic("ERROR: Address is not valid")
}
bc := NewBlockchain()
2017-09-16 23:01:29 -05:00
UTXOSet := UTXOSet{bc}
defer bc.db.Close()
balance := 0
pubKeyHash := Base58Decode([]byte(address))
pubKeyHash = pubKeyHash[1 : len(pubKeyHash)-4]
2017-09-16 23:01:29 -05:00
UTXOs := UTXOSet.FindUTXO(pubKeyHash)
for _, out := range UTXOs {
balance += out.Value
}
fmt.Printf("Balance of '%s': %d\n", address, balance)
}