-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.go
55 lines (42 loc) · 1.29 KB
/
util.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package diddoc
import (
"fmt"
"strings"
"github.com/trustbloc/did-go/doc/did"
vdrapi "github.com/trustbloc/did-go/vdr/api"
)
const (
creatorParts = 2
invalidFormatErrMsgFmt = "verificationMethod value %s should be in did#keyID format"
)
// GetDIDFromVerificationMethod fetches did from the verification method.
func GetDIDFromVerificationMethod(creator string) (string, error) {
idSplit := strings.Split(creator, "#")
if len(idSplit) != creatorParts {
return "", fmt.Errorf(fmt.Sprintf(invalidFormatErrMsgFmt, creator))
}
return idSplit[0], nil
}
// GetKeyIDFromVerificationMethod fetches keyid from the verification method.
func GetKeyIDFromVerificationMethod(creator string) (string, error) {
idSplit := strings.Split(creator, "#")
if len(idSplit) != creatorParts {
return "", fmt.Errorf(fmt.Sprintf(invalidFormatErrMsgFmt, creator))
}
return idSplit[1], nil
}
func GetDIDDocFromVerificationMethod(verificationMethod string, vdr vdrapi.Registry) (*did.Doc, error) {
didID, err := GetDIDFromVerificationMethod(verificationMethod)
if err != nil {
return nil, err
}
docResolution, err := vdr.Resolve(didID)
if err != nil {
return nil, err
}
return docResolution.DIDDocument, nil
}