-
Notifications
You must be signed in to change notification settings - Fork 23
/
utils.go
71 lines (60 loc) · 1.52 KB
/
utils.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
package utils
import (
"crypto/ed25519"
"strings"
"os/exec"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
)
func FindPublicKey(signer types.Signer, id string) (ed25519.PublicKey, error) {
if signer.Authentication != nil {
for _, authentication := range signer.Authentication {
if authentication == id {
vm := FindVerificationMethod(signer.VerificationMethod, id)
if vm == nil {
return nil, types.ErrVerificationMethodNotFound.Wrap(id)
}
return vm.GetPublicKey()
}
}
}
if signer.AssertionMethod != nil {
for _, assertionMethod := range signer.AssertionMethod {
if assertionMethod == id {
vm := FindVerificationMethod(signer.VerificationMethod, id)
if vm == nil {
return nil, types.ErrVerificationMethodNotFound.Wrap(id)
}
return vm.GetPublicKey()
}
}
}
return nil, types.ErrVerificationMethodNotFound.Wrap(id)
}
func FindVerificationMethod(vms []*types.VerificationMethod, id string) *types.VerificationMethod {
for _, vm := range vms {
if vm.Id == id {
return vm
}
}
return nil
}
func MergeUrlWithResource(url string, resource string) string {
if url[len(url)-1] == '/' {
url = url[:len(url)-1]
}
if resource[0] == '/' {
resource = resource[1:]
}
return url + "/" + resource
}
func SplitDidUrlIntoDid(didUrl string) (string, string) {
segments := strings.Split(didUrl, "#")
return segments[0], segments[1]
}
func UUID() string {
out, err := exec.Command("uuidgen").Output()
if err != nil {
panic(err)
}
return string(out)
}