-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathwallet.go
More file actions
327 lines (263 loc) · 7.85 KB
/
wallet.go
File metadata and controls
327 lines (263 loc) · 7.85 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package config
import (
"fmt"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
"github.com/oasisprotocol/cli/wallet"
)
// Wallet contains the configuration of the wallet.
type Wallet struct {
// Default is the name of the default account.
Default string `mapstructure:"default"`
// All is a map of all configured accounts in the wallet.
All map[string]*Account `mapstructure:",remain"`
}
// Migrate migrates configs of all accounts to the latest version and returns true, if any changes were needed.
func (w *Wallet) Migrate() (bool, error) {
var changes bool
for _, acc := range w.All {
af, err := acc.LoadFactory()
if err != nil {
return false, err
}
changes = af.Migrate(acc.Config) || changes
}
return changes, nil
}
// Validate performs config validation.
func (w *Wallet) Validate() error {
// Make sure the default account actually exists.
if _, exists := w.All[w.Default]; w.Default != "" && !exists {
return fmt.Errorf("default account '%s' does not exist in the wallet", w.Default)
}
// Make sure all accounts are valid.
for name, acc := range w.All {
if err := config.ValidateIdentifier(name); err != nil {
return fmt.Errorf("malformed account name '%s': %w", name, err)
}
if err := acc.Validate(); err != nil {
return fmt.Errorf("account '%s': %w", name, err)
}
}
return nil
}
// Create creates a new account.
func (w *Wallet) Create(name string, passphrase string, nw *Account) error {
if _, exists := w.All[name]; exists {
return fmt.Errorf("account '%s' already exists", name)
}
if err := config.ValidateIdentifier(name); err != nil {
return fmt.Errorf("malformed account name '%s': %w", name, err)
}
af, err := wallet.Load(nw.Kind)
if err != nil {
return err
}
acc, err := af.Create(name, passphrase, nw.Config)
if err != nil {
return err
}
// Store address so we don't need to load the account to see the address.
address, err := acc.Address().MarshalText()
if err != nil {
return fmt.Errorf("failed to marshal account address: %w", err)
}
nw.Address = string(address)
if w.All == nil {
w.All = make(map[string]*Account)
}
w.All[name] = nw
// Set default if not set.
if w.Default == "" {
w.Default = name
}
return nil
}
// Load loads the given account.
func (w *Wallet) Load(name string, passphrase string) (wallet.Account, error) {
cfg, exists := w.All[name]
if !exists {
return nil, fmt.Errorf("account '%s' does not exist in the wallet", name)
}
if err := config.ValidateIdentifier(name); err != nil {
return nil, fmt.Errorf("malformed account name '%s': %w", name, err)
}
af, err := wallet.Load(cfg.Kind)
if err != nil {
return nil, err
}
acc, err := af.Load(name, passphrase, cfg.Config)
if err != nil {
return nil, err
}
// Make sure the address matches what we have in the config.
if expected, actual := cfg.GetAddress(), acc.Address(); !actual.Equal(expected) {
return nil, fmt.Errorf("address mismatch after loading account (expected: %s got: %s)",
expected,
actual,
)
}
return acc, nil
}
// Remove removes the given account.
func (w *Wallet) Remove(name string) error {
cfg, exists := w.All[name]
if !exists {
return fmt.Errorf("account '%s' does not exist in the wallet", name)
}
if err := config.ValidateIdentifier(name); err != nil {
return fmt.Errorf("malformed account name '%s': %w", name, err)
}
af, err := wallet.Load(cfg.Kind)
if err != nil {
return err
}
if err := af.Remove(name, cfg.Config); err != nil {
return err
}
delete(w.All, name)
// Clear default if set to this wallet.
if w.Default == name {
w.Default = ""
}
return nil
}
// Rename renames an existing account.
func (w *Wallet) Rename(oldName, newName string) error {
cfg, exists := w.All[oldName]
if !exists {
return fmt.Errorf("account '%s' does not exist in the wallet", oldName)
}
if _, exists = w.All[newName]; exists {
return fmt.Errorf("account '%s' already exists in the wallet", newName)
}
if err := config.ValidateIdentifier(oldName); err != nil {
return fmt.Errorf("malformed old account name '%s': %w", oldName, err)
}
if err := config.ValidateIdentifier(newName); err != nil {
return fmt.Errorf("malformed new account name '%s': %w", newName, err)
}
af, err := wallet.Load(cfg.Kind)
if err != nil {
return err
}
if err := af.Rename(oldName, newName, cfg.Config); err != nil {
return err
}
w.All[newName] = cfg
delete(w.All, oldName)
// Update default if set to this wallet.
if w.Default == oldName {
w.Default = newName
}
return nil
}
// Import imports an existing account.
func (w *Wallet) Import(name string, passphrase string, nw *Account, src *wallet.ImportSource) error {
if _, exists := w.All[name]; exists {
return fmt.Errorf("account '%s' already exists in the wallet", name)
}
if err := config.ValidateIdentifier(name); err != nil {
return fmt.Errorf("malformed account name '%s': %w", name, err)
}
af, err := wallet.Load(nw.Kind)
if err != nil {
return err
}
acc, err := af.Import(name, passphrase, nw.Config, src)
if err != nil {
return err
}
// Store address so we don't need to load the wallet to see the address.
address, err := acc.Address().MarshalText()
if err != nil {
return fmt.Errorf("failed to marshal account address: %w", err)
}
nw.Address = string(address)
if w.All == nil {
w.All = make(map[string]*Account)
}
w.All[name] = nw
// Set default if not set.
if w.Default == "" {
w.Default = name
}
return nil
}
// SetDefault marks the given account as default.
func (w *Wallet) SetDefault(name string) error {
if _, exists := w.All[name]; !exists {
return fmt.Errorf("account '%s' does not exist in the wallet", name)
}
if err := config.ValidateIdentifier(name); err != nil {
return fmt.Errorf("malformed account name '%s': %w", name, err)
}
w.Default = name
return nil
}
// Account is an account configuration object.
type Account struct {
Description string `mapstructure:"description"`
Kind string `mapstructure:"kind"`
Address string `mapstructure:"address"`
// Config contains kind-specific configuration for this wallet.
Config map[string]interface{} `mapstructure:",remain"`
}
// Validate performs config validation.
func (a *Account) Validate() error {
// Check if given account kind is supported.
if _, err := wallet.Load(a.Kind); err != nil {
return fmt.Errorf("kind '%s' is not supported", a.Kind)
}
// Check that address is valid.
var address types.Address
if err := address.UnmarshalText([]byte(a.Address)); err != nil {
return fmt.Errorf("malformed address '%s': %w", a.Address, err)
}
// Check the algorithm is not empty.
if _, ok := a.Config["algorithm"]; !ok {
return fmt.Errorf("algorithm field not defined")
}
return nil
}
// GetAddress returns the parsed account address.
func (a *Account) GetAddress() types.Address {
var address types.Address
if err := address.UnmarshalText([]byte(a.Address)); err != nil {
panic(err)
}
return address
}
// SetConfigFromFlags populates the kind-specific configuration from CLI flags.
func (a *Account) SetConfigFromFlags() error {
af, err := wallet.Load(a.Kind)
if err != nil {
return fmt.Errorf("kind '%s' is not supported", a.Kind)
}
cfg, err := af.GetConfigFromFlags()
if err != nil {
return err
}
a.Config = cfg
return nil
}
// LoadFactory loads the account factory corresponding to this account's kind.
func (a *Account) LoadFactory() (wallet.Factory, error) {
return wallet.Load(a.Kind)
}
// PrettyKind returns a human-friendly account kind.
func (a *Account) PrettyKind() string {
af, err := wallet.Load(a.Kind)
if err != nil {
return ""
}
return af.PrettyKind(a.Config)
}
// HasConsensusSigner returns true, iff there is a consensus layer signer associated with this account.
func (a *Account) HasConsensusSigner() bool {
af, err := wallet.Load(a.Kind)
if err != nil {
return false
}
return af.HasConsensusSigner(a.Config)
}