-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
82 lines (70 loc) · 1.83 KB
/
api.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
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
package main
import (
"fmt"
)
type API struct {
}
var BlockChainObj *BlockChain
func (a *API) createBlockChain(address string) {
var err error
if BlockChainObj == nil {
BlockChainObj, err = NewBlockChain(address)
if err != nil {
fmt.Println(err)
}
}
}
func (a *API) GenKeyPair() {
err := GenKeyPair()
if err != nil {
fmt.Println(err)
}
}
func (a *API) printAllBlocks() {
err := BlockChainObj.TraverseAllBlocks(func(block *Block) (bool, error) {
fmt.Printf("\n------------------------------\n")
fmt.Printf("Version : %d\n", block.Version)
fmt.Printf("Hash : %x\n", block.GetBlockHash())
fmt.Printf("PrevHash : %x\n", block.PrevBlockHash)
fmt.Printf("MerkleRoot : %x\n", block.MerkleRoot)
fmt.Printf("TimeStamp : %d\n", block.TimeStamp)
fmt.Printf("Difficulty : %d\n", block.Difficulty)
fmt.Printf("Nonce : %d\n", block.Nonce)
fmt.Printf("Data : %s\n", string(block.Transactions[0].TxInputs[0].UnLockScript.PubKey)) //矿工写入的数据
return false, nil
})
if err != nil {
fmt.Println(err)
}
}
func (a *API) getBalance(address string) {
_, balance, err := BlockChainObj.GetAllUTXOs(address)
if err != nil {
fmt.Println(err)
}
fmt.Println("balance:", balance, address)
}
func (a *API) send(from, to string, amount uint64, miner string) {
err := WalletObj.Send(from, to, amount, BlockChainObj, miner)
if err != nil {
fmt.Println("转账失败:", err)
return
}
fmt.Println("转账成功:", amount, from, to)
}
func (a *API) listAddress() []string {
return WalletObj.ListAllAddr()
}
func (a *API) printTx() {
err := BlockChainObj.TraverseAllBlocks(func(block *Block) (bool, error) {
fmt.Println("\n+++++++++++++++++ 区块分割 +++++++++++++++")
for _, tx := range block.Transactions {
//直接打印交易
fmt.Println(tx)
}
return false, nil
})
if err != nil {
fmt.Println(err)
}
}