-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
94 lines (80 loc) · 2.93 KB
/
util.js
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
/*
Copyright 2017 BlocLedger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* jshint node: true */
'use strict';
var debug = require('debug')('poe');
var util = require('util');
var config = require('./configuration.js');
var User = require('fabric-client/lib/User.js');
var HFC = require('fabric-client');
var init = require('./initialize.js');
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
// Set up a new user
function newUser(appUser) {
var retVal = true;
var client = init.client;
var ca = init.ca;
var registrar = init.admin;
var mspid = config.cred.cas[0].msp_id;
var store = client.getStateStore();
// register and enroll a new user
var user;
return client.loadUserFromStateStore(appUser.userName)
.then(function(savedUser) {
if (savedUser && savedUser.isEnrolled()) {
console.log('user is enrolled for ' + appUser.userName);
console.log('saved user name', savedUser.getName());
appUser.hfcUser = savedUser;
// throw an error here just to skip the rest of the promise chain
throw new Error('User already enrolled');
}
// User is not enrolled yet, so first perform the registration
var registrationRequest = {
enrollmentID: appUser.userName,
role: 'client',
affiliation: 'org1' // Hard code this to org1 for now...might be a bug
};
debug('registration request', registrationRequest);
return ca.register(registrationRequest, registrar);
})
.then(function(secret) {
// now enroll the user with the secret
let request = {
enrollmentID: appUser.userName,
enrollmentSecret: secret
};
return ca.enroll(request);
})
.then(function(enrollment) {
debug('user enrollment ', enrollment);
user = new User(appUser.userName, client);
var cryptoSuite = HFC.newCryptoSuite();
cryptoSuite.setCryptoKeyStore(HFC.newCryptoKeyStore({path: config.keyPath}));
user.setCryptoSuite(cryptoSuite);
return user.setEnrollment(enrollment.key, enrollment.certificate, mspid);
})
.then(function() {
user.setAffiliation('org1'); // Hard code this to org1 for now.
debug('hfcUser set for ' + appUser.userName);
appUser.hfcUser = user;
return client.setUserContext(user); // set the context to this user so that the SDK will save the user
})
.then(function(result) {
console.log('User context set to ', result);
})
.catch(function(err) {
retVal = false;
return console.log('getUser error: ' + err);
});
}
exports.newUser = newUser;