forked from s7techlab/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
104 lines (83 loc) · 2.11 KB
/
entry.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Package access contains structs for storing chaincode access control information
package identity
import (
"crypto/x509"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
// Entry structure for storing identity information
// string representation certificate Subject and Issuer can be used for reach query searching
type Entry struct {
MSPId string
Subject string
Issuer string
PEM []byte
cert *x509.Certificate `json:"-"` // temporary cert
}
// Id structure defines short id representation
type Id struct {
MSP string
Cert string
}
// IdentityEntry interface
type IdentityEntry interface {
GetIdentityEntry() Entry
}
// ======== Identity interface ===================
// GetID identifier by certificate subject and issuer
func (e Entry) GetID() string {
return ID(e.Subject, e.Issuer)
}
// GetMSPID membership service provider identifier
func (e Entry) GetMSPID() string {
return e.MSPId
}
// GetSubject certificate subject
func (e Entry) GetSubject() string {
return e.Subject
}
// GetIssuer certificate issuer
func (e Entry) GetIssuer() string {
return e.Issuer
}
// GetPK certificate issuer
func (e Entry) GetPEM() []byte {
return e.PEM
}
func (e Entry) GetPublicKey() interface{} {
if e.cert == nil {
cert, err := Certificate(e.PEM)
if err != nil {
return err
}
e.cert = cert
}
return e.cert.PublicKey
}
// Is checks IdentityEntry is equal to an other Identity
func (e Entry) Is(id Identity) bool {
return e.MSPId == id.GetMSPID() && e.Subject == id.GetSubject()
}
//func (e Entry) FromBytes(bb []byte) (interface{}, error) {
// entry := new(Entry)
// err := json.Unmarshal(bb, entry)
// return entry, err
//}
func (e Entry) GetIdentityEntry() Entry {
return e
}
// CreateEntry creates IdentityEntry structure from an identity interface
func CreateEntry(i Identity) (g *Entry, err error) {
return &Entry{
MSPId: i.GetMSPID(),
Subject: i.GetSubject(),
Issuer: i.GetIssuer(),
PEM: i.GetPEM(),
}, nil
}
func EntryFromStub(stub shim.ChaincodeStubInterface) (g *Entry, err error) {
id, err := FromStub(stub)
if err != nil {
return nil, err
}
return CreateEntry(id)
}