-
Notifications
You must be signed in to change notification settings - Fork 29
/
types.go
108 lines (86 loc) · 2.76 KB
/
types.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
package types
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ixofoundation/ixo-blockchain/x/did/exported"
)
var (
ValidDid = regexp.MustCompile(`^did:(ixo:|sov:)([a-zA-Z0-9]){21,22}([/][a-zA-Z0-9:]+|)$`)
ValidPubKey = regexp.MustCompile(`^[123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ]{43,44}$`)
IsValidDid = ValidDid.MatchString
IsValidPubKey = ValidPubKey.MatchString
// https://sovrin-foundation.github.io/sovrin/spec/did-method-spec-template.html
// IsValidDid adapted from the above link but assumes no sub-namespaces
// TODO: ValidDid needs to be updated once we no longer want to be able
// to consider project accounts as DIDs (especially in treasury module),
// possibly should just be `^did:(ixo:|sov:)([a-zA-Z0-9]){21,22}$`.
)
var _ exported.DidDoc = &BaseDidDoc{}
func (dd BaseDidDoc) MarshalYAML() (interface{}, error) {
bz, err := codec.MarshalYAML(codec.NewProtoCodec(codectypes.NewInterfaceRegistry()), &dd)
if err != nil {
return nil, err
}
return string(bz), err
}
func (dd BaseDidDoc) String() string {
out, _ := dd.MarshalYAML()
return out.(string)
}
func NewBaseDidDoc(did exported.Did, pubKey string) BaseDidDoc {
return BaseDidDoc{
Did: did,
PubKey: pubKey,
Credentials: []*DidCredential{},
}
}
func (dd BaseDidDoc) GetDid() exported.Did { return dd.Did }
func (dd BaseDidDoc) GetPubKey() string { return dd.PubKey }
func (dd BaseDidDoc) GetCredentials() []DidCredential {
lstToRet := make([]DidCredential, 0)
credentials := dd.Credentials
for _, cred := range credentials {
lstToRet = append(lstToRet, *cred)
}
return lstToRet
}
func (dd *BaseDidDoc) SetDid(did exported.Did) error {
if len(dd.Did) != 0 {
return errors.New("cannot override BaseDidDoc did")
}
dd.Did = did
return nil
}
func (dd *BaseDidDoc) SetPubKey(pubKey string) error {
if len(dd.PubKey) != 0 {
return errors.New("cannot override BaseDidDoc pubKey")
}
dd.PubKey = pubKey
return nil
}
func (dd BaseDidDoc) Address() sdk.AccAddress {
return exported.VerifyKeyToAddr(dd.GetPubKey())
}
func (dd *BaseDidDoc) AddCredential(cred *DidCredential) {
if dd.Credentials == nil {
dd.Credentials = make([]*DidCredential, 0)
}
dd.Credentials = append(dd.Credentials, cred)
}
func fromJsonString(jsonIxoDid string) (exported.IxoDid, error) {
var did exported.IxoDid
err := json.Unmarshal([]byte(jsonIxoDid), &did)
if err != nil {
err := fmt.Errorf("could not unmarshal did into struct due to error: %s", err.Error())
return exported.IxoDid{}, err
}
return did, nil
}
func UnmarshalIxoDid(jsonIxoDid string) (exported.IxoDid, error) {
return fromJsonString(jsonIxoDid)
}