18
18
19
19
var util = require ( 'util' ) ;
20
20
var sdkUtils = require ( './utils.js' ) ;
21
+ var api = require ( './api.js' ) ;
21
22
var logger = sdkUtils . getLogger ( 'Client.js' ) ;
23
+ var Identity = require ( './msp/identity.js' ) ;
24
+ var MSP = require ( './msp/msp.js' ) ;
22
25
23
26
/**
24
27
* The User class represents users that have been enrolled and represented by
@@ -59,10 +62,20 @@ var User = class {
59
62
}
60
63
61
64
this . _enrollmentSecret = '' ;
62
- this . _enrollment = null ;
65
+ this . _identity = null ;
66
+ this . _signingIdentity = null ;
63
67
64
68
this . _client = client ;
65
69
this . cryptoPrimitives = sdkUtils . getCryptoSuite ( ) ;
70
+
71
+ // TODO: this should be using config properties obtained from the environment
72
+ this . mspImpl = new MSP ( {
73
+ trustedCerts : [ ] ,
74
+ signer : 'blah' ,
75
+ admins : [ ] ,
76
+ id : 'DEFAULT' ,
77
+ cryptoSuite : this . cryptoPrimitives
78
+ } ) ;
66
79
}
67
80
68
81
/**
@@ -106,27 +119,43 @@ var User = class {
106
119
}
107
120
108
121
/**
109
- * Get the enrollment object for this User instance
110
- * @returns {Enrollment } the enrollment object
122
+ * Get the {@link Identity} object for this User instance, used to verify signatures
123
+ * @returns {Identity } the identity object that encapsulates the user's enrollment certificate
124
+ */
125
+ getIdentity ( ) {
126
+ return this . _identity ;
127
+ }
128
+
129
+ /**
130
+ * Get the {@link SigningIdentity} object for this User instance, used to generate signatures
131
+ * @returns {SigningIdentity } the identity object that encapsulates the user's private key for signing
111
132
*/
112
- getEnrollment ( ) {
113
- return this . _enrollment ;
133
+ getSigningIdentity ( ) {
134
+ return this . _signingIdentity ;
114
135
}
115
136
116
137
/**
117
138
* Set the enrollment object for this User instance
118
- * @param {Enrollment } the enrollment object
139
+ * @param {Key } privateKey the private key object
140
+ * @param {string } certificate the PEM-encoded string of certificate
119
141
*/
120
- setEnrollment ( enrollment ) {
121
- if ( typeof enrollment . privateKey === 'undefined' || enrollment . privateKey === null || enrollment . privateKey === '' ) {
122
- throw new Error ( 'Invalid enrollment object . Must have a valid private key.' ) ;
142
+ setEnrollment ( privateKey , certificate ) {
143
+ if ( typeof privateKey === 'undefined' || privateKey === null || privateKey === '' ) {
144
+ throw new Error ( 'Invalid parameter . Must have a valid private key.' ) ;
123
145
}
124
146
125
- if ( typeof enrollment . certificate === 'undefined' || enrollment . certificate === null || enrollment . certificate === '' ) {
126
- throw new Error ( 'Invalid enrollment object . Must have a valid certificate.' ) ;
147
+ if ( typeof certificate === 'undefined' || certificate === null || certificate === '' ) {
148
+ throw new Error ( 'Invalid parameter . Must have a valid certificate.' ) ;
127
149
}
128
150
129
- this . _enrollment = enrollment ;
151
+ var pubKey = this . cryptoPrimitives . importKey ( certificate , { algorithm : api . CryptoAlgorithms . X509Certificate } ) ;
152
+ var identity = new Identity ( 'testIdentity' , certificate , pubKey , this . mspImpl ) ;
153
+ this . _identity = identity ;
154
+
155
+ // TODO: to be encapsulated by a new class SigningIdentity
156
+ this . _signingIdentity = {
157
+ key : privateKey
158
+ } ;
130
159
}
131
160
132
161
/**
@@ -155,7 +184,7 @@ var User = class {
155
184
* @returns {boolean } True if enrolled; otherwise, false.
156
185
*/
157
186
isEnrolled ( ) {
158
- return this . _enrollment !== null ;
187
+ return this . _identity !== null && this . _signingIdentity ! = null ;
159
188
}
160
189
161
190
/**
@@ -174,15 +203,21 @@ var User = class {
174
203
this . _roles = state . roles ;
175
204
this . _affiliation = state . affiliation ;
176
205
this . _enrollmentSecret = state . enrollmentSecret ;
177
- this . _enrollment = state . enrollment ;
178
206
179
207
var self = this ;
180
208
209
+ var pubKey = this . cryptoPrimitives . importKey ( state . enrollment . identity . certificate , { algorithm : api . CryptoAlgorithms . X509Certificate } ) ;
210
+ var identity = new Identity ( state . enrollment . identity . id , state . enrollment . identity . certificate , pubKey , this . mspImpl ) ;
211
+ this . _identity = identity ;
212
+
181
213
// during serialization (see toString() below) only the key's SKI are saved
182
214
// swap out that for the real key from the crypto provider
183
- var promise = this . cryptoPrimitives . getKey ( this . _enrollment . privateKey )
184
- . then ( function ( key ) {
185
- self . _enrollment . privateKey = key ;
215
+ var promise = this . cryptoPrimitives . getKey ( state . enrollment . signingIdentity )
216
+ . then ( function ( privateKey ) {
217
+ self . _signingIdentity = {
218
+ key : privateKey
219
+ } ;
220
+
186
221
return self ;
187
222
} ) ;
188
223
@@ -194,9 +229,16 @@ var User = class {
194
229
* @return {string } The state of this member as a string
195
230
*/
196
231
toString ( ) {
197
- var serializedEnrollment = ( this . _enrollment ) ? Object . assign ( { } , this . _enrollment ) : null ;
198
- if ( this . _enrollment && this . _enrollment . privateKey ) {
199
- serializedEnrollment . privateKey = this . _enrollment . privateKey . getSKI ( ) ;
232
+ var serializedEnrollment = { } ;
233
+ if ( this . _signingIdentity ) {
234
+ serializedEnrollment . signingIdentity = this . _signingIdentity . key . getSKI ( ) ;
235
+ }
236
+
237
+ if ( this . _identity ) {
238
+ serializedEnrollment . identity = {
239
+ id : this . _identity . getId ( ) ,
240
+ certificate : this . _identity . _certificate
241
+ } ;
200
242
}
201
243
202
244
var state = {
0 commit comments