-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathidentity.go
69 lines (58 loc) · 1.72 KB
/
identity.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
package testing
import (
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger-labs/cckit/identity"
)
type (
Identities map[string]identity.Identity
ReadFile func(string) ([]byte, error)
IdentitySample interface {
SigningIdentity() msp.SigningIdentity
}
)
func MustIdentityFromPem(mspID string, certPEM []byte) *identity.CertIdentity {
if id, err := identity.New(mspID, certPEM); err != nil {
panic(err)
} else {
return id
}
}
// IdentitiesFromPem returns CertIdentity (MSP ID and X.509 cert) converted PEM content
func IdentitiesFromPem(mspID string, certPEMs map[string][]byte) (ids Identities, err error) {
identities := make(Identities)
for role, certPEM := range certPEMs {
if identities[role], err = identity.New(mspID, certPEM); err != nil {
return
}
}
return identities, nil
}
// IdentitiesFromFiles returns map of CertIdentity, loaded from PEM files
func IdentitiesFromFiles(mspID string, files map[string]string, readFile ReadFile) (Identities, error) {
contents := make(map[string][]byte)
for key, filename := range files {
content, err := readFile(filename)
if err != nil {
return nil, err
}
contents[key] = content
}
return IdentitiesFromPem(mspID, contents)
}
// IdentityFromFile returns Identity struct containing mspId and certificate
func IdentityFromFile(mspID string, file string, readFile ReadFile) (*identity.CertIdentity, error) {
content, err := readFile(file)
if err != nil {
return nil, err
}
return identity.New(mspID, content)
}
// MustIdentitiesFromFiles
func MustIdentitiesFromFiles(mspID string, files map[string]string, readFile ReadFile) Identities {
ids, err := IdentitiesFromFiles(mspID, files, readFile)
if err != nil {
panic(err)
} else {
return ids
}
}