-
Notifications
You must be signed in to change notification settings - Fork 42
/
diddoc_verification_method.go
170 lines (138 loc) · 4.88 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
package types
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"github.com/cheqd/cheqd-node/x/did/utils"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/lestrrat-go/jwx/jwk"
"github.com/multiformats/go-multibase"
)
var SupportedMethodTypes = []string{
JsonWebKey2020{}.Type(),
Ed25519VerificationKey2020{}.Type(),
}
func NewVerificationMethod(id string, type_ string, controller string, verificationMaterial string) *VerificationMethod {
return &VerificationMethod{
Id: id,
Type: type_,
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.Type {
case Ed25519VerificationKey2020{}.Type():
var ed25519VerificationKey2020 Ed25519VerificationKey2020
err := json.Unmarshal([]byte(vm.VerificationMaterial), &ed25519VerificationKey2020)
if err != nil {
return sdkerrors.Wrapf(err, "failed to unmarshal verification material for %s", vm.Id)
}
_, keyBytes, err := multibase.Decode(ed25519VerificationKey2020.PublicKeyMultibase)
if err != nil {
return err
}
verificationError = utils.VerifyED25519Signature(keyBytes, message, signature)
case JsonWebKey2020{}.Type():
var jsonWebKey2020 JsonWebKey2020
err := json.Unmarshal([]byte(vm.VerificationMaterial), &jsonWebKey2020)
if err != nil {
return sdkerrors.Wrapf(err, "failed to unmarshal verification material for %s", vm.Id)
}
var raw interface{}
err = jwk.ParseRawKey([]byte(jsonWebKey2020.PublicKeyJwk), &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
}
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.Type, validation.Required, validation.In(utils.ToInterfaces(SupportedMethodTypes)...)),
validation.Field(&vm.VerificationMaterial,
validation.When(vm.Type == Ed25519VerificationKey2020{}.Type(), validation.Required, ValidEd25519VerificationKey2020Rule()),
),
validation.Field(&vm.VerificationMaterial,
validation.When(vm.Type == JsonWebKey2020{}.Type(), validation.Required, ValidJsonWebKey2020Rule()),
),
)
}
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
})
}