-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallets.go
123 lines (93 loc) · 2.36 KB
/
wallets.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bytes"
"crypto/elliptic"
"encoding/gob"
"fmt"
"io/ioutil"
)
type Wallets struct {
WalletsMap map[string]*WalletKeyPair
}
//创建Wallets,返回Wallets实例
func NewWallets() *Wallets {
var ws Wallets
ws.WalletsMap = make(map[string]*WalletKeyPair)
//1、把所有的钱包从本地加载出来
ws.LoadFromFile()
//2、把实例返回
return &ws
}
const WalletName = "wallet.dat"
//这个Wallets是对外的,WalletKeyPair是对内的
//wallets调用WalletKeyPair
func (ws *Wallets) CreateWallet() string {
//调用NewWalletKeyPair
wallet := NewWalletKeyPair()
//将返回的WalletKeyPair添加到WalletMap种
address := wallet.GetAddress()
ws.WalletsMap[address] = wallet
//保存到本地文件
res := ws.SaveToFile()
if !res{
fmt.Printf("创建钱包失败!\n")
return ""
}
return address
}
//保存钱包到文件
func (ws *Wallets)SaveToFile() bool {
//gob编码
var buffer bytes.Buffer
//将接口类型明确注册一下,否则gob编码失败 因为是interface类型
gob.Register((elliptic.P256()))
encoder := gob.NewEncoder(&buffer)
err := encoder.Encode(ws)
if err != nil{
fmt.Printf("钱包序列化失败!,err:%v\n",err)
return false
}
content := buffer.Bytes()
//func WriteFile(filename string, data []byte, perm os.FileMode) error {
err = ioutil.WriteFile(WalletName,content,0600)
if err != nil{
fmt.Printf("钱包创建失败!\n")
return false
}
return true
}
func (ws *Wallets) LoadFromFile() bool {
//判断文件是否存在
if !IsFileExist(WalletName){
fmt.Printf("钱包文件不存在,准备创建!\n")
return true
}
//读取文件
//func ReadFile(filename string) ([]byte, error) {
content,err := ioutil.ReadFile(WalletName)
if err != nil{
return false
}
//解码时也要注册interface与编码时一样
gob.Register(elliptic.P256())
//gob解码
decoder := gob.NewDecoder(bytes.NewReader(content))
var wallets Wallets //也要创建一样的类
err = decoder.Decode(&wallets)
if err != nil{
fmt.Printf("err : %v\n",err)
return false
}
//赋值给ws
ws.WalletsMap = wallets.WalletsMap
return true
}
//遍历钱包,打印所有地址
func (ws *Wallets) ListAddress() []string {
//遍历ws.WalletsMap结构返回key即可
var addresses []string
for address,_ := range ws.WalletsMap{
addresses = append(addresses,address)
}
return addresses
}