-
Notifications
You must be signed in to change notification settings - Fork 3
/
wallets.go
87 lines (78 loc) · 1.9 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
package main
import (
"bytes"
"crypto/elliptic"
"encoding/gob"
"github.com/btcsuite/btcutil/base58"
"io/ioutil"
"log"
"os"
)
const walletFile = "wallet.dat"
//定义一个Wallets结构,保存所有的wallet以及它的地址
type Wallets struct {
WalletsMap map[string]*Wallet
}
//创建方法
func NewWallets() *Wallets {
var ws Wallets
ws.WalletsMap = make(map[string]*Wallet)
ws.LoadFile()
return &ws
}
func (ws *Wallets) CreateWallet() string {
wallet := NewWallet()
address := wallet.NewAddress()
//wallets.WalletsMap = make(map[string]*Wallet)
ws.WalletsMap[address] = wallet
ws.SaveToFile()
return address
}
//保存方法,把新建的wallet添加进去
func (ws *Wallets) SaveToFile() {
var buffer bytes.Buffer
gob.Register(elliptic.P256())
encode := gob.NewEncoder(&buffer)
err := encode.Encode(ws)
if err != nil {
log.Panic(err)
}
ioutil.WriteFile(walletFile, buffer.Bytes(), 0600)
}
//读取文件方法,把所有的wallet读出来
func (ws *Wallets) LoadFile() {
//读取之前确认文件是否存在
_, err := os.Stat(walletFile)
if os.IsNotExist(err) {
//ws.WalletsMap = make(map[string]*Wallet)
return
}
content, err := ioutil.ReadFile(walletFile)
if err != nil {
log.Panic(err)
}
gob.Register(elliptic.P256())
//解码
decoder := gob.NewDecoder(bytes.NewReader(content))
var wsLocal Wallets
err = decoder.Decode(&wsLocal)
if err != nil {
log.Panic(err)
}
ws.WalletsMap = wsLocal.WalletsMap
}
func (ws *Wallets) ListAllAddresses() []string {
var addresses []string
for address := range ws.WalletsMap {
addresses = append(addresses, address)
}
return addresses
}
//通过地址返回公钥哈希
func GetPubKeyHashFromAddress(address string) []byte {
//1.解码
addressByte := base58.Decode(address) //25字节
//2.截取出公钥hash,去除version--1字节,去除校验码--4字节
pubKeyHash := addressByte[1 : len(addressByte)-4]
return pubKeyHash
}