This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
forked from s7techlab/hlf-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
identity.go
173 lines (140 loc) · 3.89 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package identity
import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"time"
"github.com/golang/protobuf/proto"
mspPb "github.com/hyperledger/fabric-protos-go/msp"
"github.com/hyperledger/fabric/msp"
_ "github.com/hyperledger/fabric/msp"
"github.com/pkg/errors"
"github.com/atomyze-ru/hlf-sdk-go/api"
)
type (
identity struct {
privateKey interface{}
publicKey interface{}
certificate *x509.Certificate
mspId string
}
signingIdentity struct {
identity *identity
cryptoSuite api.CryptoSuite
}
)
func New(mspId string, cert *x509.Certificate, privateKey interface{}) *identity {
return &identity{
mspId: mspId,
privateKey: privateKey,
certificate: cert,
publicKey: cert.PublicKey,
}
}
func FromBytesWithoutSigning(mspId string, certRaw []byte) (*identity, error) {
cert, err := Certificate(certRaw)
if err != nil {
return nil, fmt.Errorf(`certificate: %w`, err)
}
return New(mspId, cert, nil), nil
}
func FromBytes(mspId string, certRaw []byte, keyRaw []byte) (*identity, error) {
cert, err := Certificate(certRaw)
if err != nil {
return nil, fmt.Errorf(`certificate: %w`, err)
}
key, err := Key(keyRaw)
if err != nil {
return nil, fmt.Errorf(`key: %w`, err)
}
return New(mspId, cert, key), nil
}
func FromCertKeyPath(mspId string, certPath string, keyPath string) (api.Identity, error) {
certPEM, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, fmt.Errorf(`read certificate from file=%s: %w`, certPath, err)
}
keyPEM, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, fmt.Errorf(`read key from file=%s: %w`, keyPath, err)
}
return FromBytes(mspId, certPEM, keyPEM)
}
func SignerFromMSPPath(mspId string, mspPath string) (*identity, error) {
return FirstFromPath(mspId, SignCertsPath(mspPath), KeystorePath(mspPath))
}
func (i *identity) GetSigningIdentity(cs api.CryptoSuite) msp.SigningIdentity {
return &signingIdentity{
identity: i,
cryptoSuite: cs,
}
}
func (i *identity) GetMSPIdentifier() string {
return i.mspId
}
func PEMEncode(certRaw []byte) []byte {
return pem.EncodeToMemory(&pem.Block{
Type: `CERTIFICATE`,
Bytes: certRaw,
})
}
func (i *identity) GetPEM() []byte {
return PEMEncode(i.certificate.Raw)
}
func (i *identity) GetCert() *x509.Certificate {
return i.certificate
}
func (i *identity) GetIdentifier() *msp.IdentityIdentifier {
return &msp.IdentityIdentifier{
Mspid: i.mspId,
Id: i.certificate.Subject.CommonName,
}
}
func (s *signingIdentity) Anonymous() bool {
return false
}
// ExpiresAt returns date of certificate expiration
func (s *signingIdentity) ExpiresAt() time.Time {
return s.identity.certificate.NotAfter
}
func (s *signingIdentity) GetIdentifier() *msp.IdentityIdentifier {
return s.identity.GetIdentifier()
}
// GetMSPIdentifier returns current MspID of identity
func (s *signingIdentity) GetMSPIdentifier() string {
return s.identity.GetMSPIdentifier()
}
func (s *signingIdentity) Validate() error {
// TODO
return nil
}
func (s *signingIdentity) GetOrganizationalUnits() []*msp.OUIdentifier {
// TODO
return nil
}
func (s *signingIdentity) Verify(msg []byte, sig []byte) error {
return s.cryptoSuite.Verify(s.identity.publicKey, msg, sig)
}
func (s *signingIdentity) Serialize() ([]byte, error) {
pb := &pem.Block{Bytes: s.identity.certificate.Raw, Type: "CERTIFICATE"}
pemBytes := pem.EncodeToMemory(pb)
if pemBytes == nil {
return nil, errors.New("encoding of identity failed")
}
sId := &mspPb.SerializedIdentity{Mspid: s.identity.mspId, IdBytes: pemBytes}
idBytes, err := proto.Marshal(sId)
if err != nil {
return nil, err
}
return idBytes, nil
}
func (s *signingIdentity) SatisfiesPrincipal(principal *mspPb.MSPPrincipal) error {
panic("implement me")
}
func (s *signingIdentity) Sign(msg []byte) ([]byte, error) {
return s.cryptoSuite.Sign(msg, s.identity.privateKey)
}
func (s *signingIdentity) GetPublicVersion() msp.Identity {
return nil
}