-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhitelist.go
79 lines (63 loc) · 2.14 KB
/
whitelist.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
package vtxnbuild
import (
"github.com/pkg/errors"
"github.com/stellar/go/strkey"
"github.com/velo-protocol/DRSv1/libs/xdr"
)
// Whitelist represents the Velo whitelist Operation.
type Whitelist struct {
Address string
Role string
Currency string
}
// BuildXDR for Whitelist returns a fully configured XDR Operation.
func (whitelist *Whitelist) BuildXDR() (vxdr.VeloOp, error) {
if err := whitelist.Validate(); err != nil {
return vxdr.VeloOp{}, err
}
var vXdrOp vxdr.WhitelistOp
err := vXdrOp.Address.SetAddress(whitelist.Address)
if err != nil {
return vxdr.VeloOp{}, errors.Wrap(err, "failed to set whitelist address")
}
vXdrOp.Role = vxdr.Role(whitelist.Role)
if whitelist.Currency != "" {
vXdrOp.Currency = vxdr.Currency(whitelist.Currency)
}
body, err := vxdr.NewOperationBody(vxdr.OperationTypeWhitelist, vXdrOp)
if err != nil {
return vxdr.VeloOp{}, errors.Wrap(err, "failed to build XDR operation body")
}
return vxdr.VeloOp{Body: body}, nil
}
// FromXDR for Whitelist initialises the vtxnbuild struct from the corresponding XDR Operation.
func (whitelist *Whitelist) FromXDR(vXdrOp vxdr.VeloOp) error {
whitelistOp := vXdrOp.Body.WhitelistOp
if whitelistOp == nil {
return errors.New("error parsing whitelist operation from xdr")
}
whitelist.Role = string(whitelistOp.Role)
whitelist.Address = whitelistOp.Address.Address()
whitelist.Currency = string(whitelistOp.Currency)
return nil
}
// Validation function for Whitelist. Validates the required struct fields. It returns an error if any of the fields are
// invalid. Otherwise, it returns nil.
func (whitelist *Whitelist) Validate() error {
if whitelist.Address == "" {
return errors.New("address must not be blank")
}
if whitelist.Role == "" {
return errors.New("role must not be blank")
}
if !strkey.IsValidEd25519PublicKey(whitelist.Address) {
return errors.New("invalid address format")
}
if !vxdr.Role(whitelist.Role).IsValid() {
return errors.New("role specified does not exist")
}
if whitelist.Currency != "" && !vxdr.Currency(whitelist.Currency).IsValid() {
return errors.Errorf("currency %s does not exist", whitelist.Currency)
}
return nil
}