/
account.go
542 lines (489 loc) · 16.9 KB
/
account.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package account
import (
"bytes"
crud "github.com/iov-one/cosmos-sdk-crud/pkg/crud"
"github.com/iov-one/iovns/pkg/utils"
"regexp"
"time"
"github.com/iov-one/iovns/x/starname/controllers/domain"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/iov-one/iovns/x/configuration"
"github.com/iov-one/iovns/x/starname/keeper"
"github.com/iov-one/iovns/x/starname/types"
)
// accountControllerFunc is the function signature used by account controllers
type accountControllerFunc func(ctrl *Account) error
// Account is an account controller, it caches information
// in order to avoid useless query to state to get the same
// information. Order of execution of controllers matters
// if the correct order is not followed the controller will
// panic because of bad operation flow.
// Errors returned are wrapped sdk.Error types.
type Account struct {
validators []accountControllerFunc
name, domain string
account *types.Account
conf *configuration.Config
ctx sdk.Context
k keeper.Keeper
store crud.Store
domainCtrl *domain.Domain
}
// Validate verifies the account against the order of provided controllers
func (a *Account) Validate() error {
for _, check := range a.validators {
if err := check(a); err != nil {
return err
}
}
return nil
}
// MustExist asserts that the given account exists
func (a *Account) MustExist() *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.mustExist()
})
return a
}
// MustNotExist asserts that the given account does not exist
func (a *Account) MustNotExist() *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.mustNotExist()
})
return a
}
// ValidName asserts the account name is valid
func (a *Account) ValidName() *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return a.validName()
})
return a
}
// NotExpired asserts the account is not expired
func (a *Account) NotExpired() *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.notExpired()
})
return a
}
// Renewable asserts that the account is renewable
func (a *Account) Renewable() *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.renewable()
})
return a
}
// OwnedBy asserts that the account is owned by the provided address
func (a *Account) OwnedBy(addr sdk.AccAddress) *Account {
f := func(ctrl *Account) error {
return ctrl.ownedBy(addr)
}
a.validators = append(a.validators, f)
return a
}
// CertificateSizeNotExceeded asserts that the size of a cert is not beyond the limits
func (a *Account) CertificateSizeNotExceeded(cert []byte) *Account {
f := func(ctrl *Account) error {
return ctrl.certSizeNotExceeded(cert)
}
a.validators = append(a.validators, f)
return a
}
// CertificateLimitNotExceeded asserts that the numbers of certificates in an account was not exceeded
func (a *Account) CertificateLimitNotExceeded() *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.certLimitNotExceeded()
})
return a
}
// DeletableBy checks if the account can be deleted by the provided address
func (a *Account) DeletableBy(addr sdk.AccAddress) *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.deletableBy(addr)
})
return a
}
// CertificateExists asserts that the provided certificate
// exists and if it does the index is saved in the provided pointer
// if certIndex pointer is nil the certificate index will not be saved
func (a *Account) CertificateExists(cert []byte, certIndex *int) *Account {
f := func(ctrl *Account) error {
err := ctrl.certNotExist(cert, certIndex)
if err == nil {
return sdkerrors.Wrapf(types.ErrCertificateDoesNotExist, "%x", cert)
}
return nil
}
a.validators = append(a.validators, f)
return a
}
// ValidResources verifies that the provided resources are valid for the account
func (a *Account) ValidResources(resources []types.Resource) *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.validResources(resources)
})
return a
}
// TransferableBy checks if the account can be transferred by the provided address
func (a *Account) TransferableBy(addr sdk.AccAddress) *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.transferableBy(addr)
})
return a
}
// ResettableBy checks if the account attributes resettable by the provided address
func (a *Account) ResettableBy(addr sdk.AccAddress, reset bool) *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.resettableBy(addr, reset)
})
return a
}
// ResettableBy checks if the account attributes resettable by the provided address
func (a *Account) ResourceLimitNotExceeded(resources []types.Resource) *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.resourceLimitNotExceeded(resources)
})
return a
}
// MetadataSizeNotExceeded asserts that the metadata size of an account was not exceeded
func (a *Account) MetadataSizeNotExceeded(metadata string) *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.metadataSizeNotExceeded(metadata)
})
return a
}
// RegistrableBy asserts that an account can be registered by the provided address
func (a *Account) RegistrableBy(addr sdk.AccAddress) *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.registrableBy(addr)
})
return a
}
// CertificateNotExist asserts the provided certificate
// does not exist in the account already
func (a *Account) CertificateNotExist(cert []byte) *Account {
a.validators = append(a.validators, func(ctrl *Account) error {
return ctrl.certNotExist(cert, nil)
})
return a
}
// NewController is Account constructor
func NewController(ctx sdk.Context, k keeper.Keeper, domain, name string) *Account {
return &Account{
name: name,
domain: domain,
ctx: ctx,
k: k,
store: k.AccountStore(ctx),
}
}
// WithDomainController allows to specify a cached domain controller
func (a *Account) WithDomainController(dom *domain.Domain) *Account {
a.domainCtrl = dom
return a
}
// WithConfiguration allows to specify a cached config
func (a *Account) WithConfiguration(cfg configuration.Config) *Account {
a.conf = &cfg
return a
}
// WithAccount allows to specify a cached account
func (a *Account) WithAccount(acc types.Account) *Account {
a.account = &acc
a.domain = acc.Domain
a.name = *acc.Name
return a
}
// requireDomain builds the domain controller after asserting domain existence
func (a *Account) requireDomain() error {
if a.domainCtrl != nil {
return nil
}
a.domainCtrl = domain.NewController(a.ctx, a.k, a.domain)
return a.domainCtrl.MustExist().Validate()
}
// requireAccount finds the accounts and caches it, so future
// queries will always use the same account first found account
func (a *Account) requireAccount() error {
if a.account != nil {
return nil
}
account := new(types.Account)
ok := a.store.Read((&types.Account{Domain: a.domain, Name: utils.StrPtr(a.name)}).PrimaryKey(), account)
if !ok {
return sdkerrors.Wrapf(types.ErrAccountDoesNotExist, "%s was not found in domain %s", a.name, a.domain)
}
a.account = account
return nil
}
// mustExist makes sure an account exist
func (a *Account) mustExist() error {
return a.requireAccount()
}
// mustNotExist is the unexported function executed by MustNotExist
func (a *Account) mustNotExist() error {
err := a.requireAccount()
if err != nil {
return nil
}
return sdkerrors.Wrapf(types.ErrAccountExists, "account %s already exists in domain %s", a.name, a.domain)
}
// requireConfiguration updates the configuration
// if it is not already set, and caches it after
func (a *Account) requireConfiguration() {
if a.conf != nil {
return
}
conf := a.k.ConfigurationKeeper.GetConfiguration(a.ctx)
a.conf = &conf
}
// validName is the unexported function used by ValidAccountName
func (a *Account) validName() error {
a.requireConfiguration()
if !regexp.MustCompile(a.conf.ValidAccountName).MatchString(a.name) {
return sdkerrors.Wrapf(types.ErrInvalidAccountName, "invalid name: %s", a.name)
}
return nil
}
// notExpired is the unexported function used by NotExpired
func (a *Account) notExpired() error {
if err := a.requireAccount(); err != nil {
panic("validation check is not allowed on a non existing account")
}
if err := a.requireDomain(); err != nil {
panic("validation check is not allowed on a non existing domain")
}
switch a.domainCtrl.Domain().Type {
// if domain is closed type then skip the expiration validation checks
case types.ClosedDomain:
return nil
}
// check if account has expired
expireTime := utils.SecondsToTime(a.account.ValidUntil)
if !expireTime.Before(a.ctx.BlockTime()) {
return nil
}
// if it has expired return error
return sdkerrors.Wrapf(types.ErrAccountExpired, "account %s in domain %s has expired", a.name, a.domain)
}
func (a *Account) renewable() error {
if err := a.requireAccount(); err != nil {
panic("validation check is not allowed on a non existing account")
}
a.requireConfiguration()
// do calculations
newValidUntil := utils.SecondsToTime(a.account.ValidUntil).Add(a.conf.AccountRenewalPeriod)
// renew count bumped because domain is already at count 1 when created
renewCount := a.conf.AccountRenewalCountMax + 1
// set new expected valid until
maximumValidUntil := a.ctx.BlockTime().Add(a.conf.AccountRenewalPeriod * time.Duration(renewCount))
// check if new valid until is after maximum allowed
if newValidUntil.After(maximumValidUntil) {
return sdkerrors.Wrapf(types.ErrUnauthorized, "unable to renew account %s in domain %s, maximum account renewal has exceeded: %s", *a.account.Name, a.domain, maximumValidUntil)
}
// if it has expired return error
return nil
}
// ownedBy is the unexported function used by Owner
func (a *Account) ownedBy(addr sdk.AccAddress) error {
// assert domain exists
if err := a.requireAccount(); err != nil {
panic("validation check is not allowed on a non existing account")
}
// check if admin matches at least one address
if a.account.Owner.Equals(addr) {
return nil
}
return sdkerrors.Wrapf(types.ErrUnauthorized, "%s is not allowed to perform operation in the account owned by %s", addr, a.account.Owner)
}
// certNotExist is the unexported function used by CertificateNotExist
// and CertificateExists, it saves the index of the found certificate
// in indexPointer if it is not nil
func (a *Account) certNotExist(newCert []byte, indexPointer *int) error {
// assert domain exists
if err := a.requireAccount(); err != nil {
panic("validation check is not allowed on a non existing account")
}
// check if certificate is already present in account
for i, cert := range a.account.Certificates {
if bytes.Equal(cert, newCert) {
if indexPointer != nil {
*indexPointer = i
}
return sdkerrors.Wrapf(types.ErrCertificateExists, "certificate is already present")
}
}
return nil
}
func (a *Account) certSizeNotExceeded(newCert []byte) error {
// assert domain exists
if err := a.requireAccount(); err != nil {
panic("validation check is not allowed on a non existing account")
}
a.requireConfiguration()
if uint64(len(newCert)) > a.conf.CertificateSizeMax {
return sdkerrors.Wrapf(types.ErrCertificateSizeExceeded, "max certificate size %d exceeded", a.conf.CertificateSizeMax)
}
return nil
}
func (a *Account) certLimitNotExceeded() error {
// assert domain exists
if err := a.requireAccount(); err != nil {
panic("validation check is not allowed on a non existing account")
}
a.requireConfiguration()
if uint32(len(a.account.Certificates)) >= a.conf.CertificateCountMax {
return sdkerrors.Wrapf(types.ErrCertificateLimitReached, "max certificate limit %d reached, cannot add more", a.conf.CertificateCountMax)
}
return nil
}
func (a *Account) deletableBy(addr sdk.AccAddress) error {
if err := a.requireDomain(); err != nil {
panic("validation check on a non existing domain is not allowed")
}
// get cached domain
d := a.domainCtrl.Domain()
if err := a.requireAccount(); err != nil {
panic("validation check on a non existing account is not allowed")
}
switch d.Type {
case types.ClosedDomain:
if err := a.domainCtrl.
Admin(addr).
NotExpired().
Validate(); err != nil {
return err
}
case types.OpenDomain:
if a.gracePeriodFinished() != nil {
if a.ownedBy(addr) != nil {
return sdkerrors.Wrapf(types.ErrUnauthorized, "only account owner %s is allowed to delete the account before grace period", a.account.Owner)
}
}
}
return nil
}
// validResources validates different resources
func (a *Account) validResources(resources []types.Resource) error {
a.requireConfiguration()
validURI := regexp.MustCompile(a.conf.ValidURI)
validResource := regexp.MustCompile(a.conf.ValidResource)
// create resources set to identify duplicates
sets := make(map[string]struct{}, len(resources))
// iterate over resources to check their validity
for _, resource := range resources {
// check if URI was already specified
if _, ok := sets[resource.URI]; ok {
return sdkerrors.Wrapf(types.ErrInvalidResource, "duplicate URI %s", resource.URI)
}
sets[resource.URI] = struct{}{}
// is uri valid?
if !validURI.MatchString(resource.URI) {
return sdkerrors.Wrapf(types.ErrInvalidResource, "%s is not a valid URI", resource.URI)
}
// is resource valid?
if !validResource.MatchString(resource.Resource) {
return sdkerrors.Wrapf(types.ErrInvalidResource, "%s is not a valid resource", resource.Resource)
}
}
// success
return nil
}
func (a *Account) transferableBy(addr sdk.AccAddress) error {
if err := a.requireDomain(); err != nil {
panic("validation check not allowed on a non existing domain")
}
// check if domain has super user
switch a.domainCtrl.Domain().Type {
// if it has a super user then only domain admin can transfer accounts
case types.ClosedDomain:
if a.domainCtrl.
Admin(addr).
Validate() != nil {
return sdkerrors.Wrapf(types.ErrUnauthorized, "only domain admin %s is allowed to transfer accounts", a.domainCtrl.Domain().Admin)
}
// if it has not a super user then only account owner can transfer the account
case types.OpenDomain:
if a.ownedBy(addr) != nil {
return sdkerrors.Wrapf(types.ErrUnauthorized, "only account owner %s is allowed to transfer the account", a.account.Owner)
}
}
return nil
}
func (a *Account) resettableBy(addr sdk.AccAddress, reset bool) error {
if err := a.requireDomain(); err != nil {
panic("validation check not allowed on a non existing domain")
}
d := a.domainCtrl.Domain()
switch d.Type {
case types.OpenDomain:
if reset {
if d.Admin.Equals(addr) {
return sdkerrors.Wrapf(types.ErrUnauthorized, "domain admin is not authorized to reset account contents on open domains")
}
}
case types.ClosedDomain:
}
return nil
}
// gracePeriodFinished is the condition that checks if given account's grace period has finished
func (a *Account) gracePeriodFinished() error {
// require configuration
a.requireConfiguration()
// assert domain exists
if err := a.requireAccount(); err != nil {
panic("condition check not allowed on non existing account ")
}
// get grace period and expiration time
gracePeriod := a.conf.AccountGracePeriod
expireTime := utils.SecondsToTime(a.account.ValidUntil)
if a.ctx.BlockTime().After(expireTime.Add(gracePeriod)) {
return nil
}
return sdkerrors.Wrapf(types.ErrAccountGracePeriodNotFinished, "account %s grace period has not finished", *a.account.Name)
}
func (a *Account) resourceLimitNotExceeded(resources []types.Resource) error {
if err := a.requireAccount(); err != nil {
panic("validation check is not allowed on a non existing account")
}
a.requireConfiguration()
if uint32(len(resources)) > a.conf.ResourcesMax {
return sdkerrors.Wrapf(types.ErrResourceLimitExceeded, "resource limit: %d", a.conf.ResourcesMax)
}
return nil
}
func (a *Account) metadataSizeNotExceeded(metadata string) error {
// assert domain exists
if err := a.requireAccount(); err != nil {
panic("validation check is not allowed on a non existing account")
}
a.requireConfiguration()
if uint64(len(metadata)) > a.conf.MetadataSizeMax {
return sdkerrors.Wrapf(types.ErrMetadataSizeExceeded, "max metadata size %d exceeded", a.conf.MetadataSizeMax)
}
return nil
}
func (a *Account) registrableBy(addr sdk.AccAddress) error {
if err := a.requireDomain(); err != nil {
panic("validation check is not allowed on a non existing domain")
}
// check domain type
switch a.domainCtrl.Domain().Type {
// if domain is closed then the registerer must be domain owner
case types.ClosedDomain:
return a.domainCtrl.
Admin(addr).
Validate()
default:
return nil
}
}
// Account returns the cached account, if the account existence
// was not asserted before, it panics.
func (a *Account) Account() types.Account {
if err := a.requireAccount(); err != nil {
panic("getting an account is not allowed before existence checks")
}
return *a.account
}