Repository navigation
Expand file tree
/
Copy pathCAUtil.js
More file actions
executable file
·194 lines (166 loc) · 6.68 KB
/
Copy pathCAUtil.js
File metadata and controls
executable file
·194 lines (166 loc) · 6.68 KB
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
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const forge = require('node-forge');
const adminUserId = 'admin';
const adminUserPasswd = 'adminpw';
/**
*
* @param {*} FabricCAServices
* @param {*} ccp
*/
exports.buildCAClient = (FabricCAServices, ccp, caHostName) => {
// Create a new CA client for interacting with the CA.
const caInfo = ccp.certificateAuthorities[caHostName]; //lookup CA details from config
const caTLSCACerts = caInfo.tlsCACerts.pem;
const caClient = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);
console.log(`Built a CA Client named ${caInfo.caName}`);
return caClient;
};
exports.enrollAdmin = async (caClient, wallet, orgMspId) => {
try {
// Check to see if we've already enrolled the admin user.
const identity = await wallet.get(adminUserId);
if (identity) {
console.log('An identity for the admin user already exists in the wallet');
return;
}
// Enroll the admin user, and import the new identity into the wallet.
const enrollment = await caClient.enroll({ enrollmentID: adminUserId, enrollmentSecret: adminUserPasswd });
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: orgMspId,
type: 'X.509',
};
await wallet.put(adminUserId, x509Identity);
console.log('Successfully enrolled admin user and imported it into the wallet');
} catch (error) {
console.error(`Failed to enroll admin user : ${error}`);
}
};
exports.registerAndEnrollUser = async (caClient, wallet, orgMspId, userId, affiliation, attributeValue) => {
try {
// Check to see if we've already enrolled the user
let userIdentity = await wallet.get(userId);
if (userIdentity) {
console.log(`An identity for the user ${userId} already exists in the wallet`);
return;
}
//to allow multiple user of the same type
/*let count = 1;
let tempUser = userId;
while(userIdentity){
const { dept, org } = extractDeptOrgFromIdentity(userIdentity);
console.log("*******************")
console.log(dept)
console.log(org)
if(dept === attributeValue.dept && org === attributeValue.org){
console.log(`An identity for the user ${userId} already exists in the wallet`);
return;
}
userId = tempUser + (count).toString();
userIdentity = await wallet.get(userId);
count++;
}*/
// Must use an admin to register a new user
const adminIdentity = await wallet.get(adminUserId);
if (!adminIdentity) {
console.log('An identity for the admin user does not exist in the wallet');
console.log('Enroll the admin user before retrying');
return;
}
// build a user object for authenticating with the CA
const provider = wallet.getProviderRegistry().getProvider(adminIdentity.type);
const adminUser = await provider.getUserContext(adminIdentity, adminUserId);
// Register the user, enroll the user, and import the new identity into the wallet.
// if affiliation is specified by client, the affiliation value must be configured in CA
console.log('attribute Value: ', `${attributeValue.dept}`)//`party1_${attributeValue}`
const attributes = [{name: 'HF.name', value: `${attributeValue.name}`, ecert:true},
{ name: 'HF.role', value: `${attributeValue.type}`, ecert: true },
{name: 'department', value: `${attributeValue.dept}`, ecert:true},
{name: 'organization', value: `${attributeValue.org}`, ecert:true}];
const secret = await caClient.register({
affiliation: affiliation,
enrollmentID: userId,
attrs: attributes, //used of access controll, it can have role(like admin, member) or specfifc charactricstic(like org,deprt, phone, emial)
role: 'client', // optional: type of identity liek user, client, app, peer, orderer, etc
caname: 'ca-org1'
}, adminUser);
const enrollment = await caClient.enroll({
enrollmentID: userId,
enrollmentSecret: secret
});
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: orgMspId,
type: 'X.509',
};
await wallet.put(userId, x509Identity);
console.log('\nRegistered user attributes:', attributes);
console.log(`Successfully registered and enrolled user ${userId} and imported it into the wallet`);
} catch (error) {
console.error(`Failed to register user : ${error}`);
}
};
// register and enroll sensor
exports.registerAndEnrollSensor = async (caClient, wallet, orgMspId, sensorId, affiliation, sensorMeta) => {
try {
// Check if already enrolled
const sensorIdentity = await wallet.get(sensorId);
if (sensorIdentity) {
console.log(`An identity for the sensor ${sensorId} already exists in the wallet`);
return;
}
// Use admin for registering
const adminIdentity = await wallet.get(adminUserId);
if (!adminIdentity) throw new Error("Enroll admin first");
const provider = wallet.getProviderRegistry().getProvider(adminIdentity.type);
const adminUser = await provider.getUserContext(adminIdentity, adminUserId);
// Register with CA (contractId removed, sensorType added)
const attributes = [
{ name: 'HF.sensorId', value: `${sensorMeta.sensorId}`, ecert: true },
{ name: 'HF.role', value: 'sensor', ecert: true },
{ name: 'sensorType', value: `${sensorMeta.sensorType}`, ecert: true },
{ name: 'organization', value: sensorMeta.org || 'IoT Inc', ecert: true }
];
const secret = await caClient.register({
affiliation,
enrollmentID: sensorId,
attrs: attributes,
role: 'client',
caname: 'ca-org1'
}, adminUser);
const enrollment = await caClient.enroll({
enrollmentID: sensorId,
enrollmentSecret: secret
});
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: orgMspId,
type: 'X.509',
};
await wallet.put(sensorId, x509Identity);
console.log(`✅ Registered and enrolled sensor ${sensorId} (${sensorMeta.sensorType})`);
} catch (err) {
console.error(`❌ Failed to register sensor ${sensorId}: ${err}`);
}
};
// Function to build a Fabric CA client using static CA certificates
exports.buildStaticCAClient = (caURL, caTLSCert, caName) =>{
// Create a new instance of FabricCAServices
const caService = new FabricCAServices(caURL, { trustedRoots: caTLSCert, verify: false }, caName);
// Return the Fabric CA client
return caService;
};