2017-09-10 00:53:06 -05:00
|
|
|
package main
|
|
|
|
|
2017-09-10 01:53:14 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
)
|
2017-09-10 00:53:06 -05:00
|
|
|
|
2017-10-03 03:54:31 -05:00
|
|
|
func (cli *CLI) send(from, to string, amount int, nodeID string, mineNow bool) {
|
2017-09-10 01:53:14 -05:00
|
|
|
if !ValidateAddress(from) {
|
|
|
|
log.Panic("ERROR: Sender address is not valid")
|
|
|
|
}
|
|
|
|
if !ValidateAddress(to) {
|
|
|
|
log.Panic("ERROR: Recipient address is not valid")
|
|
|
|
}
|
|
|
|
|
2017-09-30 22:42:34 -05:00
|
|
|
bc := NewBlockchain(nodeID)
|
2017-09-16 23:49:59 -05:00
|
|
|
UTXOSet := UTXOSet{bc}
|
2017-09-10 00:53:06 -05:00
|
|
|
defer bc.db.Close()
|
|
|
|
|
2017-09-30 22:48:51 -05:00
|
|
|
wallets, err := NewWallets(nodeID)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
wallet := wallets.GetWallet(from)
|
|
|
|
|
|
|
|
tx := NewUTXOTransaction(&wallet, to, amount, &UTXOSet)
|
2017-09-16 21:16:50 -05:00
|
|
|
|
2017-10-03 03:54:31 -05:00
|
|
|
if mineNow {
|
2017-10-03 04:21:15 -05:00
|
|
|
cbTx := NewCoinbaseTX(from, "")
|
2017-10-03 03:54:31 -05:00
|
|
|
txs := []*Transaction{cbTx, tx}
|
2017-10-03 03:47:27 -05:00
|
|
|
|
2017-10-03 03:54:31 -05:00
|
|
|
newBlock := bc.MineBlock(txs)
|
|
|
|
UTXOSet.Update(newBlock)
|
|
|
|
} else {
|
|
|
|
sendTx(knownNodes[0], tx)
|
|
|
|
}
|
2017-10-03 03:47:27 -05:00
|
|
|
|
2017-09-10 00:53:06 -05:00
|
|
|
fmt.Println("Success!")
|
|
|
|
}
|