-
Notifications
You must be signed in to change notification settings - Fork 41
/
vsp.go
192 lines (167 loc) · 5.04 KB
/
vsp.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
package dcrlibwallet
import (
"context"
"crypto/ed25519"
"encoding/base64"
"fmt"
"strings"
"decred.org/dcrwallet/v2/errors"
"github.com/planetdecred/dcrlibwallet/internal/vsp"
)
// VSPClient loads or creates a VSP client instance for the specified host.
func (wallet *Wallet) VSPClient(host string, pubKey []byte) (*vsp.Client, error) {
wallet.vspClientsMu.Lock()
defer wallet.vspClientsMu.Unlock()
client, ok := wallet.vspClients[host]
if ok {
return client, nil
}
cfg := vsp.Config{
URL: host,
PubKey: base64.StdEncoding.EncodeToString(pubKey),
Dialer: nil, // optional, but consider providing a value
Wallet: wallet.Internal(),
}
client, err := vsp.New(cfg)
if err != nil {
return nil, err
}
wallet.vspClients[host] = client
return client, nil
}
// KnownVSPs returns a list of known VSPs. This list may be updated by calling
// ReloadVSPList. This method is safe for concurrent access.
func (mw *MultiWallet) KnownVSPs() []*VSP {
mw.vspMu.RLock()
defer mw.vspMu.RUnlock()
return mw.vsps // TODO: Return a copy.
}
// SaveVSP marks a VSP as known and will be susbequently included as part of
// known VSPs.
func (mw *MultiWallet) SaveVSP(host string) (err error) {
// check if host already exists
vspDbData := mw.getVSPDBData()
for _, savedHost := range vspDbData.SavedHosts {
if savedHost == host {
return fmt.Errorf("duplicate host %s", host)
}
}
// validate host network
info, err := vspInfo(host)
if err != nil {
return err
}
// TODO: defaultVSPs() uses strings.Contains(network, vspInfo.Network).
if info.Network != mw.NetType() {
return fmt.Errorf("invalid net %s", info.Network)
}
vspDbData.SavedHosts = append(vspDbData.SavedHosts, host)
mw.updateVSPDBData(vspDbData)
mw.vspMu.Lock()
mw.vsps = append(mw.vsps, &VSP{Host: host, VspInfoResponse: info})
mw.vspMu.Unlock()
return
}
// LastUsedVSP returns the host of the last used VSP, as saved by the
// SaveLastUsedVSP() method.
func (mw *MultiWallet) LastUsedVSP() string {
return mw.getVSPDBData().LastUsedVSP
}
// SaveLastUsedVSP saves the host of the last used VSP.
func (mw *MultiWallet) SaveLastUsedVSP(host string) {
vspDbData := mw.getVSPDBData()
vspDbData.LastUsedVSP = host
mw.updateVSPDBData(vspDbData)
}
type vspDbData struct {
SavedHosts []string
LastUsedVSP string
}
func (mw *MultiWallet) getVSPDBData() *vspDbData {
vspDbData := new(vspDbData)
mw.ReadUserConfigValue(KnownVSPsConfigKey, vspDbData)
return vspDbData
}
func (mw *MultiWallet) updateVSPDBData(data *vspDbData) {
mw.SaveUserConfigValue(KnownVSPsConfigKey, data)
}
// ReloadVSPList reloads the list of known VSPs.
// This method makes multiple network calls; should be called in a goroutine
// to prevent blocking the UI thread.
func (mw *MultiWallet) ReloadVSPList(ctx context.Context) {
log.Debugf("Reloading list of known VSPs")
defer log.Debugf("Reloaded list of known VSPs")
vspDbData := mw.getVSPDBData()
vspList := make(map[string]*VspInfoResponse)
for _, host := range vspDbData.SavedHosts {
vspInfo, err := vspInfo(host)
if err != nil {
// User saved this VSP. Log an error message.
log.Errorf("get vsp info error for %s: %v", host, err)
} else {
vspList[host] = vspInfo
}
if ctx.Err() != nil {
return // context canceled, abort
}
}
otherVSPHosts, err := defaultVSPs(mw.NetType())
if err != nil {
log.Debugf("get default vsp list error: %v", err)
}
for _, host := range otherVSPHosts {
if _, wasAdded := vspList[host]; wasAdded {
continue
}
vspInfo, err := vspInfo(host)
if err != nil {
log.Debugf("vsp info error for %s: %v\n", host, err) // debug only, user didn't request this VSP
} else {
vspList[host] = vspInfo
}
if ctx.Err() != nil {
return // context canceled, abort
}
}
mw.vspMu.Lock()
mw.vsps = make([]*VSP, 0, len(vspList))
for host, info := range vspList {
mw.vsps = append(mw.vsps, &VSP{Host: host, VspInfoResponse: info})
}
mw.vspMu.Unlock()
}
func vspInfo(vspHost string) (*VspInfoResponse, error) {
vspInfoResponse := new(VspInfoResponse)
resp, respBytes, err := HttpGet(vspHost+"/api/v3/vspinfo", vspInfoResponse)
if err != nil {
return nil, err
}
// Validate server response.
sigStr := resp.Header.Get("VSP-Server-Signature")
sig, err := base64.StdEncoding.DecodeString(sigStr)
if err != nil {
return nil, fmt.Errorf("error validating VSP signature: %v", err)
}
if !ed25519.Verify(vspInfoResponse.PubKey, respBytes, sig) {
return nil, errors.New("bad signature from VSP")
}
return vspInfoResponse, nil
}
// defaultVSPs returns a list of known VSPs.
func defaultVSPs(network string) ([]string, error) {
var vspInfoResponse map[string]*VspInfoResponse
_, _, err := HttpGet("https://api.decred.org/?c=vsp", &vspInfoResponse)
if err != nil {
return nil, err
}
// The above API does not return the pubKeys for the
// VSPs. Only return the host since we'll still need
// to make another API call to get the VSP pubKeys.
vsps := make([]string, 0)
for url, vspInfo := range vspInfoResponse {
if strings.Contains(network, vspInfo.Network) {
vsps = append(vsps, "https://"+url)
}
}
return vsps, nil
}