-
Notifications
You must be signed in to change notification settings - Fork 519
/
util.js
233 lines (199 loc) · 7.28 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Copyright 2016 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
const path = require('path');
const fs = require('fs-extra');
const util = require('util');
const copService = require('fabric-ca-client/lib/FabricCAServices.js');
const {Utils: utils, User} = require('fabric-common');
const logger = utils.getLogger('TestUtil');
module.exports.CHAINCODE_PATH = 'github.com/example_cc';
module.exports.END2END = {
channel: 'mychannel',
chaincodeId: 'end2endnodesdk',
chaincodeIdPrivateData: 'end2endnodesdk_privatedata',
chaincodeVersion: 'v0'
};
module.exports.NODE_CHAINCODE_PATH = path.resolve(__dirname, '../fixtures/chaincode/node_cc/example_cc');
module.exports.METADATA_PATH = path.resolve(__dirname, '../fixtures/chaincode/metadata');
module.exports.COLLECTIONS_CONFIG_PATH = path.resolve(__dirname, '../fixtures/chaincode/goLang/src/github.com/example_cc_private/collections_config.json');
module.exports.NODE_END2END = {
channel: 'mychannel',
chaincodeId: 'e2enodecc',
chaincodeLanguage: 'node',
chaincodeVersion: 'v0'
};
module.exports.NETWORK_END2END = {
channel: 'mychannel',
chaincodeId: 'network-e2enodecc',
chaincodeLanguage: 'node',
chaincodeVersion: 'v0'
};
// all temporary files and directories are created under here
const tempdir = path.join(__dirname, '../temp');
logger.info(util.format(
'\n\n*******************************************************************************' +
'\n*******************************************************************************' +
'\n* ' +
'\n* Using temp dir: %s' +
'\n* ' +
'\n*******************************************************************************' +
'\n*******************************************************************************\n', tempdir));
// directory for file based KeyValueStore
module.exports.KVS = path.join(tempdir, 'hfc-test-kvs');
module.exports.storePathForOrg = function (org) {
return module.exports.KVS + '_' + org;
};
// temporarily set $GOPATH to the test fixture folder
module.exports.setupChaincodeDeploy = function () {
process.env.GOPATH = path.join(__dirname, '../fixtures/chaincode/goLang');
};
// specifically set the values to defaults because they may have been overridden when
// running in the overall test bucket ('npm test')
module.exports.resetDefaults = function () {
global.hfc.config = undefined;
require('nconf').reset();
};
utils.addConfigFile(path.join(__dirname, './e2e/config.json'));
const ORGS = utils.getConfigSetting('test-network');
const tlsOptions = {
trustedRoots: [],
verify: false
};
function getMember(username, password, client, t, userOrg) {
const caUrl = ORGS[userOrg].ca.url;
return client.getUserContext(username, true)
.then((user) => {
// eslint-disable-next-line no-unused-vars
return new Promise((resolve, reject) => {
if (user && user.isEnrolled()) {
t.pass('Successfully loaded member from persistence');
return resolve(user);
}
const member = new User(username);
let cryptoSuite = client.getCryptoSuite();
if (!cryptoSuite) {
cryptoSuite = utils.newCryptoSuite();
if (userOrg) {
cryptoSuite.setCryptoKeyStore(utils.newCryptoKeyStore({path: module.exports.storePathForOrg(ORGS[userOrg].name)}));
client.setCryptoSuite(cryptoSuite);
}
}
member.setCryptoSuite(cryptoSuite);
// need to enroll it with CA server
const cop = new copService(caUrl, tlsOptions, ORGS[userOrg].ca.name, cryptoSuite);
return cop.enroll({
enrollmentID: username,
enrollmentSecret: password
}).then((enrollment) => {
t.pass('Successfully enrolled user \'' + username + '\'');
return member.setEnrollment(enrollment.key, enrollment.certificate, ORGS[userOrg].mspid);
}).then(() => {
let skipPersistence = false;
if (!client.getStateStore()) {
skipPersistence = true;
}
return client.setUserContext(member, skipPersistence);
}).then(() => {
return resolve(member);
}).catch((err) => {
t.fail('Failed to enroll and persist user. Enjoy the Error: ' + err.stack ? err.stack : err);
t.end();
});
});
});
}
module.exports.setAdmin = function (client, userOrg) {
return getAdmin(client, null, userOrg);
};
function getAdmin(client, t, userOrg) {
const keyPath = path.join(__dirname, util.format('../fixtures/crypto-material/crypto-config/peerOrganizations/%s.example.com/users/Admin@%s.example.com/msp/keystore', userOrg, userOrg));
const keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString();
const certPath = path.join(__dirname, util.format('../fixtures/crypto-material/crypto-config/peerOrganizations/%s.example.com/users/Admin@%s.example.com/msp/signcerts', userOrg, userOrg));
const certPEM = readAllFiles(certPath)[0];
const cryptoSuite = utils.newCryptoSuite();
if (userOrg) {
cryptoSuite.setCryptoKeyStore(utils.newCryptoKeyStore({path: module.exports.storePathForOrg(ORGS[userOrg].name)}));
client.setCryptoSuite(cryptoSuite);
}
return Promise.resolve(client.createUser({
username: 'peer' + userOrg + 'Admin',
mspid: ORGS[userOrg].mspid,
cryptoContent: {
privateKeyPEM: keyPEM.toString(),
signedCertPEM: certPEM.toString()
}
}));
}
function getOrdererAdmin(client, t) {
const keyPath = path.join(__dirname, '../fixtures/crypto-material/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/keystore');
const keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString();
const certPath = path.join(__dirname, '../fixtures/crypto-material/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts');
const certPEM = readAllFiles(certPath)[0];
t.comment('getOrdererAdmin');
return Promise.resolve(client.createUser({
username: 'ordererAdmin',
mspid: 'OrdererMSP',
cryptoContent: {
privateKeyPEM: keyPEM.toString(),
signedCertPEM: certPEM.toString()
}
}));
}
function readFile(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) {
reject(new Error('Failed to read file ' + filePath + ' due to error: ' + err));
} else {
resolve(data);
}
});
});
}
module.exports.readFile = readFile;
function readAllFiles(dir) {
const files = fs.readdirSync(dir);
const certs = [];
files.forEach((file_name) => {
const file_path = path.join(dir, file_name);
logger.debug(' looking at file ::' + file_path);
const data = fs.readFileSync(file_path);
certs.push(data);
});
return certs;
}
module.exports.getOrderAdminSubmitter = function (client, test) {
return getOrdererAdmin(client, test);
};
module.exports.getSubmitter = function (client, test, peerOrgAdmin, org) {
if (arguments.length < 2) {
throw new Error('"client" and "test" are both required parameters');
}
let peerAdmin, userOrg;
if (typeof peerOrgAdmin === 'boolean') {
peerAdmin = peerOrgAdmin;
} else {
peerAdmin = false;
}
// if the 3rd argument was skipped
if (typeof peerOrgAdmin === 'string') {
userOrg = peerOrgAdmin;
} else {
if (typeof org === 'string') {
userOrg = org;
} else {
userOrg = 'org1';
}
}
if (peerAdmin) {
return getAdmin(client, test, userOrg);
} else {
return getMember('admin', 'adminpw', client, test, userOrg);
}
};
module.exports.sleep = function (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};