-
Notifications
You must be signed in to change notification settings - Fork 42
/
diddoc_verification_method.go
176 lines (143 loc) · 5.07 KB
/
diddoc_verification_method.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
package types
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"errors"
"fmt"
"github.com/cheqd/cheqd-node/x/did/utils"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/lestrrat-go/jwx/jwk"
"github.com/mr-tron/base58"
"github.com/multiformats/go-multibase"
)
const (
JSONWebKey2020Type = "JsonWebKey2020"
Ed25519VerificationKey2020Type = "Ed25519VerificationKey2020"
Ed25519VerificationKey2018Type = "Ed25519VerificationKey2018"
)
var SupportedMethodTypes = []string{
JSONWebKey2020Type,
Ed25519VerificationKey2020Type,
Ed25519VerificationKey2018Type,
}
func NewVerificationMethod(id string, vmType string, controller string, verificationMaterial string) *VerificationMethod {
return &VerificationMethod{
Id: id,
VerificationMethodType: vmType,
Controller: controller,
VerificationMaterial: verificationMaterial,
}
}
// Helpers
func FindVerificationMethod(vms []VerificationMethod, id string) (VerificationMethod, bool) {
for _, vm := range vms {
if vm.Id == id {
return vm, true
}
}
return VerificationMethod{}, false
}
func GetVerificationMethodIds(vms []*VerificationMethod) []string {
res := make([]string, len(vms))
for i := range vms {
res[i] = vms[i].Id
}
return res
}
func VerifySignature(vm VerificationMethod, message []byte, signature []byte) error {
var verificationError error
switch vm.VerificationMethodType {
case Ed25519VerificationKey2020Type:
_, multibaseBytes, err := multibase.Decode(vm.VerificationMaterial)
if err != nil {
return err
}
keyBytes := utils.GetEd25519VerificationKey2020(multibaseBytes)
verificationError = utils.VerifyED25519Signature(keyBytes, message, signature)
case JSONWebKey2020Type:
var raw interface{}
err := jwk.ParseRawKey([]byte(vm.VerificationMaterial), &raw)
if err != nil {
return fmt.Errorf("can't parse jwk: %s", err.Error())
}
switch pubKey := raw.(type) {
case *rsa.PublicKey:
verificationError = utils.VerifyRSASignature(*pubKey, message, signature)
case *ecdsa.PublicKey:
verificationError = utils.VerifyECDSASignature(*pubKey, message, signature)
case ed25519.PublicKey:
verificationError = utils.VerifyED25519Signature(pubKey, message, signature)
default:
panic("unsupported jwk key") // This should have been checked during basic validation
}
case Ed25519VerificationKey2018Type:
publicKeyBytes, err := base58.Decode(vm.VerificationMaterial)
if err != nil {
return err
}
verificationError = utils.VerifyED25519Signature(publicKeyBytes, message, signature)
default:
panic("unsupported verification method type") // This should have also been checked during basic validation
}
if verificationError != nil {
return ErrInvalidSignature.Wrapf("verification method: %s, err: %s", vm.Id, verificationError.Error())
}
return nil
}
func VerificationMethodListToMapByFragment(vms []*VerificationMethod) map[string]VerificationMethod {
result := map[string]VerificationMethod{}
for _, vm := range vms {
_, _, _, fragment := utils.MustSplitDIDUrl(vm.Id)
result[fragment] = *vm
}
return result
}
// ReplaceDids replaces ids in all fields
func (vm *VerificationMethod) ReplaceDids(old, new string) {
// Controller
if vm.Controller == old {
vm.Controller = new
}
// Id
vm.Id = utils.ReplaceDidInDidURL(vm.Id, old, new)
}
// Validation
func (vm VerificationMethod) Validate(baseDid string, allowedNamespaces []string) error {
return validation.ValidateStruct(&vm,
validation.Field(&vm.Id, validation.Required, IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(baseDid)),
validation.Field(&vm.Controller, validation.Required, IsDID(allowedNamespaces)),
validation.Field(&vm.VerificationMethodType, validation.Required, validation.In(utils.ToInterfaces(SupportedMethodTypes)...)),
validation.Field(&vm.VerificationMaterial,
validation.When(vm.VerificationMethodType == Ed25519VerificationKey2020Type, validation.Required, IsMultibaseEd25519VerificationKey2020()),
),
validation.Field(&vm.VerificationMaterial,
validation.When(vm.VerificationMethodType == Ed25519VerificationKey2018Type, validation.Required, IsBase58Ed25519VerificationKey2018()),
),
validation.Field(&vm.VerificationMaterial,
validation.When(vm.VerificationMethodType == JSONWebKey2020Type, validation.Required, IsJWK()),
),
)
}
func ValidVerificationMethodRule(baseDid string, allowedNamespaces []string) *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(VerificationMethod)
if !ok {
panic("ValidVerificationMethodRule must be only applied on verification methods")
}
return casted.Validate(baseDid, allowedNamespaces)
})
}
func IsUniqueVerificationMethodListByIDRule() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.([]*VerificationMethod)
if !ok {
panic("IsUniqueVerificationMethodListByIdRule must be only applied on VM lists")
}
ids := GetVerificationMethodIds(casted)
if !utils.IsUnique(ids) {
return errors.New("there are verification method duplicates")
}
return nil
})
}