-
Notifications
You must be signed in to change notification settings - Fork 255
/
addrbook.go
282 lines (234 loc) · 5.97 KB
/
addrbook.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package dht
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"sync"
"github.com/33cn/chain33/common/db"
p2pty "github.com/33cn/chain33/system/p2p/dht/types"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/wallet/bipwallet"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
)
const (
addrkeyTag = "mutiaddrs"
privKeyTag = "privkey"
privKeyCompressBytesLen = 32
)
// AddrBook peer address manager
type AddrBook struct {
mtx sync.Mutex
cfg *types.P2P
privkey string
pubkey string
bookDb db.DB
}
// NewAddrBook new addr book
func NewAddrBook(cfg *types.P2P) *AddrBook {
a := &AddrBook{
cfg: cfg,
}
dbPath := cfg.DbPath + "/" + p2pty.DHTTypeName
a.bookDb = db.NewDB("addrBook", a.cfg.Driver, dbPath, a.cfg.DbCache)
a.loadDb()
return a
}
// AddrsInfo get addr infos
func (a *AddrBook) AddrsInfo() []peer.AddrInfo {
var addrsInfo []peer.AddrInfo
if a.bookDb == nil {
return addrsInfo
}
addrsInfoBs, err := a.bookDb.Get([]byte(addrkeyTag))
if err != nil {
log.Error("LoadAddrsInfo", " Get addrkeyTag", err.Error())
return addrsInfo
}
err = json.Unmarshal(addrsInfoBs, &addrsInfo)
if err != nil {
log.Error("LoadAddrsInfo", "Unmarshal err", err.Error())
return nil
}
return addrsInfo
}
func (a *AddrBook) loadDb() bool {
privkey, err := a.bookDb.Get([]byte(privKeyTag))
if len(privkey) != 0 && err == nil {
pubkey, err := GenPubkey(string(privkey))
if err != nil {
log.Error("genPubkey", "err", err.Error())
panic(err)
}
a.setKey(string(privkey), pubkey)
return true
}
return false
}
//Randkey Rand keypair
func (a *AddrBook) Randkey() crypto.PrivKey {
a.initKey()
return a.GetPrivkey()
}
func (a *AddrBook) initKey() {
priv, pub, err := GenPrivPubkey()
if err != nil {
var maxRetry = 10
for i := 0; i < maxRetry; i++ {
priv, pub, err = GenPrivPubkey()
if err == nil {
break
}
if i == maxRetry-1 && err != nil {
panic(err.Error())
}
}
}
a.saveKey(hex.EncodeToString(priv), hex.EncodeToString(pub))
}
func (a *AddrBook) saveKey(priv, pub string) {
a.setKey(priv, pub)
err := a.bookDb.Set([]byte(privKeyTag), []byte(priv))
if err != nil {
panic(err)
}
}
// GetPrivkey get private key
func (a *AddrBook) GetPrivkey() crypto.PrivKey {
a.mtx.Lock()
defer a.mtx.Unlock()
if a.privkey == "" {
return nil
}
keybytes, err := hex.DecodeString(a.privkey)
if err != nil {
log.Error("GetPrivkey", "DecodeString Error", err.Error())
return nil
}
log.Debug("GetPrivkey", "privsize", len(keybytes))
privkey, err := crypto.UnmarshalSecp256k1PrivateKey(keybytes)
if err != nil {
privkey, err = crypto.UnmarshalPrivateKey(keybytes)
if err != nil {
log.Error("GetPrivkey", "UnmarshalPrivateKey", err.Error())
return nil
}
}
return privkey
}
// GetPrivPubKey return privkey and pubkey
func (a *AddrBook) GetPrivPubKey() (string, string) {
a.mtx.Lock()
defer a.mtx.Unlock()
return a.privkey, a.pubkey
}
func (a *AddrBook) setKey(privkey, pubkey string) {
a.mtx.Lock()
defer a.mtx.Unlock()
a.privkey = privkey
a.pubkey = pubkey
}
// SaveAddr save addr
func (a *AddrBook) SaveAddr(addrinfos []peer.AddrInfo) error {
jsonBytes, err := json.Marshal(addrinfos)
if err != nil {
log.Error("Failed to save AddrBook to file", "err", err)
return err
}
log.Debug("saveToDb", "addrs", string(jsonBytes))
return a.bookDb.Set([]byte(addrkeyTag), jsonBytes)
}
// StoreHostID store host id into file
func (a *AddrBook) StoreHostID(id peer.ID, path string) {
dbPath := path + "/" + p2pty.DHTTypeName
if os.MkdirAll(dbPath, 0755) != nil {
return
}
pf, err := os.Create(fmt.Sprintf("%v/%v", dbPath, id.Pretty()))
if err != nil {
log.Error("StoreHostId", "Create file", err.Error())
return
}
defer pf.Close()
var peerInfo = make(map[string]interface{})
var info = make(map[string]interface{})
pubkey, err := PeerIDToPubkey(id.Pretty())
if err == nil {
info["pubkey"] = pubkey
pbytes, _ := hex.DecodeString(pubkey)
addr, _ := bipwallet.PubToAddress(pbytes)
info["address"] = addr
}
peerInfo[id.Pretty()] = info
bs, err := json.MarshalIndent(peerInfo, "", "\t")
if err != nil {
log.Error("StoreHostId", "MarshalIndent", err.Error())
return
}
_, err = pf.Write(bs)
if err != nil {
return
}
}
// GenPrivPubkey return key and pubkey in bytes
func GenPrivPubkey() ([]byte, []byte, error) {
priv, pub, err := crypto.GenerateSecp256k1Key(rand.Reader)
//priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.Secp256k1, 2048, rand.Reader)
if err != nil {
return nil, nil, err
}
privkey, err := priv.Raw()
if err != nil {
return nil, nil, err
}
pubkey, err := pub.Raw()
if err != nil {
return nil, nil, err
}
return privkey, pubkey, nil
}
// GenPubkey generate public key
func GenPubkey(key string) (string, error) {
keybytes, err := hex.DecodeString(key)
if err != nil {
log.Error("DecodeString Error", "Error", err.Error())
return "", err
}
log.Info("GenPubkey", "key size", len(keybytes))
privkey, err := crypto.UnmarshalSecp256k1PrivateKey(keybytes)
if err != nil {
privkey, err = crypto.UnmarshalPrivateKey(keybytes)
if err != nil {
//切换
log.Error("genPubkey", "UnmarshalPrivateKey", err.Error())
return "", err
}
}
pubkey, err := privkey.GetPublic().Raw()
if err != nil {
log.Error("genPubkey", "GetPubkey", err.Error())
return "", err
}
return hex.EncodeToString(pubkey), nil
}
//PeerIDToPubkey 提供节点ID转换为pubkey,进而通过pubkey创建chain33 地址的功能
func PeerIDToPubkey(id string) (string, error) {
//encodeIdStr := "16Uiu2HAm7vDB7XDuEv8XNPcoPqumVngsjWoogGXENNDXVYMiCJHM"
//hexpubStr:="02b99bc73bfb522110634d5644d476b21b3171eefab517da0646ef2aba39dbf4a0"
//chain33 address:13ohj5JH6NE15ENfuQRneqGdg29nT27K3k
pID, err := peer.Decode(id)
if err != nil {
return "", err
}
pub, err := pID.ExtractPublicKey()
if err != nil {
return "", err
}
pbs, err := pub.Raw()
if err != nil {
return "", err
}
return hex.EncodeToString(pbs), nil
}