forked from Jeiwan/blockchain_go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli_send.go
43 lines (35 loc) · 822 Bytes
/
cli_send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package bc
import (
"fmt"
"log"
)
func (cli *CLI) send(from, to string, amount int, nodeID string, mineNow bool) {
if !ValidateAddress(from) {
log.Panic("ERROR: Sender address is not valid")
}
if !ValidateAddress(to) {
log.Panic("ERROR: Recipient address is not valid")
}
bc := NewBlockchain(nodeID)
UTXOSet := UTXOSet{bc}
defer bc.DB.Close()
wallets, err := NewWallets(nodeID)
if err != nil {
log.Panic(err)
}
wallet := wallets.GetWallet(from)
if wallet == nil {
fmt.Println("The Address doesn't belongs to you!")
return
}
tx := NewUTXOTransaction(wallet, to, amount, &UTXOSet)
if mineNow {
cbTx := NewCoinbaseTX(from, "")
txs := []*Transaction{cbTx, tx}
newBlock := bc.MineBlock(txs)
UTXOSet.Update(newBlock)
} else {
sendTx(knownNodes[0], tx)
}
fmt.Println("Success!")
}