-
Notifications
You must be signed in to change notification settings - Fork 9
/
vdrkeyresolver.go
56 lines (46 loc) · 1.66 KB
/
vdrkeyresolver.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
/*
Copyright Gen Digital Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package vermethod
import (
"fmt"
"github.com/trustbloc/did-go/doc/did"
vdrapi "github.com/trustbloc/did-go/vdr/api"
)
type didResolver interface {
Resolve(did string, opts ...vdrapi.DIDMethodOption) (*did.DocResolution, error)
}
// VDRResolver resolves DID in order to find public keys for VC verification using vdr.Registry.
// A source of DID could be issuer of VC or holder of VP. It can be also obtained from
// JWS "issuer" claim or "verificationMethod" of Linked Data Proof.
type VDRResolver struct {
vdr didResolver
}
// NewVDRResolver creates VDRResolver.
func NewVDRResolver(vdr didResolver) *VDRResolver {
return &VDRResolver{vdr: vdr}
}
// ResolveVerificationMethod resolves verification method by key id.
func (r *VDRResolver) ResolveVerificationMethod(
verificationMethod string,
expectedKeyController string,
) (*VerificationMethod, error) {
docResolution, err := r.vdr.Resolve(expectedKeyController)
if err != nil {
return nil, fmt.Errorf("resolve DID %s: %w", expectedKeyController, err)
}
for _, verifications := range docResolution.DIDDocument.VerificationMethods() {
for _, verification := range verifications {
if verification.VerificationMethod.ID == verificationMethod &&
verification.Relationship != did.KeyAgreement {
return &VerificationMethod{
Type: verification.VerificationMethod.Type,
Value: verification.VerificationMethod.Value,
JWK: verification.VerificationMethod.JSONWebKey(),
}, nil
}
}
}
return nil, fmt.Errorf("public key with KID %s is not found for DID %s", verificationMethod, expectedKeyController)
}