-
Notifications
You must be signed in to change notification settings - Fork 378
/
verifiable_public_key_fetcher.go
58 lines (47 loc) · 1.38 KB
/
verifiable_public_key_fetcher.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
package bertyvcissuer
import (
"crypto/ed25519"
"fmt"
"strings"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/verifier"
"github.com/hyperledger/aries-framework-go/pkg/kms"
"github.com/multiformats/go-multibase"
"berty.tech/berty/v2/go/pkg/errcode"
)
func embeddedPublicKeyFetcher(issuerID string, allowList []string) (*verifier.PublicKey, error) {
if !strings.HasPrefix(issuerID, "did:key:z6Mk") {
return nil, fmt.Errorf("unexpected key format")
}
if len(allowList) > 0 {
found := false
for _, allowed := range allowList {
if allowed == issuerID {
found = true
break
}
}
if !found {
return nil, errcode.ErrServicesDirectoryInvalidVerifiedCredentialID.Wrap(fmt.Errorf("issuer is not allowed"))
}
}
_, rawData, err := multibase.Decode(issuerID[8:])
if err != nil {
return nil, err
}
if len(rawData) != ed25519.PublicKeySize+2 {
return nil, errcode.ErrInvalidInput
}
return &verifier.PublicKey{
Type: kms.ED25519,
Value: rawData[2:],
JWK: nil,
}, nil
}
func EmbeddedPublicKeyFetcher(issuerID, keyID string) (*verifier.PublicKey, error) {
return embeddedPublicKeyFetcher(issuerID, nil)
}
func EmbeddedPublicKeyFetcherAllowList(allowList []string) func(issuerID, keyID string) (*verifier.PublicKey, error) {
return func(issuerID, keyID string) (*verifier.PublicKey, error) {
return embeddedPublicKeyFetcher(issuerID, allowList)
}
}