forked from hyperledger-labs/hlf-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegisterUser.java
128 lines (107 loc) · 4.09 KB
/
RegisterUser.java
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
package hlf.java.rest.client.util;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.util.Properties;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.fabric.gateway.Identities;
import org.hyperledger.fabric.gateway.Identity;
import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallets;
import org.hyperledger.fabric.gateway.X509Identity;
import org.hyperledger.fabric.sdk.Enrollment;
import org.hyperledger.fabric.sdk.User;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric.sdk.security.CryptoSuiteFactory;
import org.hyperledger.fabric_ca.sdk.HFCAClient;
import org.hyperledger.fabric_ca.sdk.RegistrationRequest;
import org.hyperledger.fabric_ca.sdk.exception.RegistrationException;
@Slf4j
public class RegisterUser {
static {
System.setProperty("org.hyperledger.fabric.sdk.service_discovery.as_localhost", "true");
}
public static void main(String[] args) {
RegisterUser registerUser = new RegisterUser();
try {
String pemFilePath =
"./fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem";
registerUser.generateWallet(pemFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
public void generateWallet(String pemFilePath) throws Exception {
// Create a CA client for interacting with the CA.
Properties props = new Properties();
props.put("pemFile", pemFilePath);
props.put("allowAllHostNames", "true");
HFCAClient caClient = HFCAClient.createNewInstance("https://localhost:7054", props);
CryptoSuite cryptoSuite = CryptoSuiteFactory.getDefault().getCryptoSuite();
caClient.setCryptoSuite(cryptoSuite);
// Create a wallet for managing identities
Wallet wallet = Wallets.newFileSystemWallet(Paths.get("testwallet"));
// Check to see if we've already enrolled the user.
if (wallet.get("clientUser") != null) {
log.info("An identity for the user \"clientUser\" already exists in the wallet");
return;
}
X509Identity adminIdentity = (X509Identity) wallet.get("admin");
if (adminIdentity == null) {
log.info("\"admin\" needs to be enrolled and added to the wallet first");
return;
}
User admin =
new User() {
@Override
public String getName() {
return "admin";
}
@Override
public Set<String> getRoles() {
return null;
}
@Override
public String getAccount() {
return null;
}
@Override
public String getAffiliation() {
return "org1.supplychain";
}
@Override
public Enrollment getEnrollment() {
return new Enrollment() {
@Override
public PrivateKey getKey() {
return adminIdentity.getPrivateKey();
}
@Override
public String getCert() {
return Identities.toPemString(adminIdentity.getCertificate());
}
};
}
@Override
public String getMspId() {
return "Org1MSP";
}
};
// Register the user, enroll the user, and import the new identity into the wallet.
RegistrationRequest registrationRequest = new RegistrationRequest("clientUser");
registrationRequest.setAffiliation("org1.supplychain");
registrationRequest.setEnrollmentID("clientUser");
try {
String enrollmentSecret = caClient.register(registrationRequest, admin);
Enrollment enrollment = caClient.enroll("clientUser", enrollmentSecret);
log.debug("Certificate: " + enrollment.getCert());
} catch (RegistrationException rex) {
log.error("Error: " + rex.getMessage());
}
Identity user =
Identities.newX509Identity(
"Org1MSP", adminIdentity.getCertificate(), adminIdentity.getPrivateKey());
wallet.put("clientUser", user);
log.info("Successfully enrolled user \"clientUser\" and imported it into the wallet");
}
}