-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandLine.go
More file actions
92 lines (81 loc) · 2.16 KB
/
Copy pathcommandLine.go
File metadata and controls
92 lines (81 loc) · 2.16 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"log"
"time"
)
func (cli *CLI) PrintBlockChain() {
bc := cli.bc
iterator := bc.NewIterator()
for {
// 返回区块,游标左移
block := iterator.Next()
date := time.Unix(int64(block.TimeStamp), 0).Format("2006-01-02 15:04:05")
fmt.Printf("===== 当前区块高度 %d =====\n", 0)
fmt.Printf("终端版本: %d\n", block.Version)
fmt.Printf("前区块hash值: %x\n", block.PrevHash)
fmt.Printf("梅克尔根hash值: %x\n", block.MerkelRoot)
fmt.Printf("块产生时间: %s\n", date)
fmt.Printf("块难度: %d\n", block.Difficulty)
fmt.Printf("随机数: %d\n", block.Nonce)
fmt.Printf("当前区块hash值: %x\n", block.Hash)
fmt.Printf("当前区块数据: %s\n", block.Transactions[0].TxInputs[0].PubKey)
if len(block.PrevHash) == 0 {
break
}
}
}
func (cli *CLI) PrintTransactions() {
bc := cli.bc
iterator := bc.NewIterator()
for {
// 返回区块,游标左移
block := iterator.Next()
for _, tx := range block.Transactions {
txStr := tx.String()
fmt.Println(txStr)
}
if len(block.PrevHash) == 0 {
break
}
}
}
func (cli *CLI) GetBalance(addr string) {
// 1. 校验地址
if !IsValidAddress(addr) {
log.Printf("address %s is invalid\n", addr)
return
}
// 2. 生成公钥hash
pubKeyHash := GetPubKeyFromAddress(addr)
utxos := cli.bc.FindUTXOs(pubKeyHash)
amount := 0.0
for _, utxo := range utxos {
amount += utxo.Amount
}
log.Printf("%s balance: %f\n", addr, amount)
}
func (cli *CLI) Send(from, to string, amount float64, miner, data string) {
// 1. 创建挖矿交易
coinbase := NewCoinBaseTx(miner, data)
// 2. 创建一个普通交易
tx := NewTransaction(from, to, amount, cli.bc)
if coinbase == nil || tx == nil {
return
}
// 3. 添加到区块
cli.bc.AddBlock([]*Transaction{coinbase, tx})
}
func (cli *CLI) NewWallet() {
wallets := NewWallets()
address := wallets.CreateWallet()
fmt.Printf("your new address: %s\n", address)
}
func (cli *CLI) ListAddress() {
wallets := NewWallets()
addresses := wallets.GetAllAddress()
fmt.Println("Tips: the order of all list addresses is random!")
for i, addr := range addresses {
fmt.Printf("wallet[%d]: %s\n", i, addr)
}
}