diff --git a/blockchain.go b/blockchain.go index ccb5979..d825955 100644 --- a/blockchain.go +++ b/blockchain.go @@ -106,8 +106,24 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction { return unspentTXs } -// FindUTXOs finds and returns unspend transaction outputs for the address -func (bc *Blockchain) FindUTXOs(address string, amount int) (int, map[string][]int) { +// FindUTXO finds and returns all unspent transaction outputs +func (bc *Blockchain) FindUTXO(address string) []TXOutput { + var UTXOs []TXOutput + unspentTransactions := bc.FindUnspentTransactions(address) + + for _, tx := range unspentTransactions { + for _, out := range tx.Vout { + if out.CanBeUnlockedWith(address) { + UTXOs = append(UTXOs, out) + } + } + } + + return UTXOs +} + +// FindSpendableOutputs finds and returns unspent outputs to reference in inputs +func (bc *Blockchain) FindSpendableOutputs(address string, amount int) (int, map[string][]int) { unspentOutputs := make(map[string][]int) unspentTXs := bc.FindUnspentTransactions(address) accumulated := 0 diff --git a/cli.go b/cli.go index fb407a0..1270c6e 100644 --- a/cli.go +++ b/cli.go @@ -22,14 +22,10 @@ func (cli *CLI) getBalance(address string) { defer bc.db.Close() balance := 0 - utxs := bc.FindUnspentTransactions(address) + UTXOs := bc.FindUTXO(address) - for _, tx := range utxs { - for _, out := range tx.Vout { - if out.CanBeUnlockedWith(address) { - balance += out.Value - } - } + for _, out := range UTXOs { + balance += out.Value } fmt.Printf("Balance of '%s': %d\n", address, balance) diff --git a/transaction.go b/transaction.go index b7afd5a..d67a505 100644 --- a/transaction.go +++ b/transaction.go @@ -79,7 +79,7 @@ func NewUTXOTransaction(from, to string, value int, bc *Blockchain) *Transaction var inputs []TXInput var outputs []TXOutput - acc, validOutputs := bc.FindUTXOs(from, value) + acc, validOutputs := bc.FindSpendableOutputs(from, value) if acc < value { log.Panic("ERROR: Not enough funds")