Skip to content

Commit 0f84a27

Browse files
committed
add CA Server Support
1 parent d53d8c7 commit 0f84a27

File tree

7 files changed

+395
-2
lines changed

7 files changed

+395
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>org.hyperledger.fabric-sdk-java</groupId>
3131
<artifactId>fabric-sdk-java</artifactId>
32-
<version>1.0.1</version>
32+
<version>1.0.0</version>
3333
</dependency>
3434
</dependencies>
3535
<build>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ijarvis.intelliq.FabricCA;
2+
3+
import ijarvis.intelliq.Fabric.FabricApp;
4+
import ijarvis.intelliq.Fabric.SampleUser;
5+
import org.apache.log4j.Logger;
6+
import org.hyperledger.fabric.sdk.*;
7+
import org.hyperledger.fabric.sdk.exception.CryptoException;
8+
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
9+
import org.hyperledger.fabric.sdk.security.CryptoSuite;
10+
11+
import java.util.Collection;
12+
13+
public class FabricCAApp {
14+
private static Logger logger=Logger.getLogger(FabricCAApp.class);
15+
public static HFClient client=null;
16+
public static CryptoSuite cs = CryptoSuite.Factory.getCryptoSuite();
17+
public static void init(User CAUSER) throws CryptoException, InvalidArgumentException {
18+
client = HFClient.createNewInstance();
19+
client.setCryptoSuite(cs);
20+
client.setUserContext(CAUSER);
21+
}
22+
/*
23+
* 实现根绝给定的Key查询数据
24+
* */
25+
public static void queryFabcar(Channel channel, String key) throws Exception {
26+
QueryByChaincodeRequest req = client.newQueryProposalRequest();
27+
ChaincodeID cid = ChaincodeID.newBuilder().setName("epointchaincodezzk").setVersion("0.1").build();
28+
req.setChaincodeID(cid);
29+
req.setFcn("query");
30+
req.setArgs(new String[] { key });
31+
System.out.println("Querying for " + key);
32+
Collection<ProposalResponse> resps = channel.queryByChaincode(req);
33+
for (ProposalResponse resp : resps) {
34+
String payload = new String(resp.getChaincodeActionResponsePayload());
35+
System.out.println("response: " + payload);
36+
}
37+
}
38+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package ijarvis.intelliq.FabricCA;
2+
3+
import org.hyperledger.fabric.sdk.Peer;
4+
import org.hyperledger.fabric.sdk.User;
5+
import org.hyperledger.fabric_ca.sdk.HFCAClient;
6+
7+
import java.util.*;
8+
9+
/*
10+
* 从官方的Fabric-SDK的test代码中抽象出来的描述fabric组织机构的Bean对象
11+
*/
12+
13+
/**
14+
* Sample Organization Representation
15+
*
16+
* Keeps track which resources are defined for the Organization it represents.
17+
*
18+
*/
19+
public class SampleOrg {
20+
final String name;
21+
final String mspid;
22+
HFCAClient caClient;
23+
24+
Map<String, User> userMap = new HashMap<>();
25+
Map<String, String> peerLocations = new HashMap<>();
26+
Map<String, String> ordererLocations = new HashMap<>();
27+
Map<String, String> eventHubLocations = new HashMap<>();
28+
Set<Peer> peers = new HashSet<>();
29+
private SampleUser admin;
30+
private String caLocation;
31+
private Properties caProperties = null;
32+
33+
private SampleUser peerAdmin;
34+
35+
36+
private String domainName;
37+
38+
public SampleOrg(String name, String mspid) {
39+
this.name = name;
40+
this.mspid = mspid;
41+
}
42+
43+
public SampleUser getAdmin() {
44+
return admin;
45+
}
46+
47+
public void setAdmin(SampleUser admin) {
48+
this.admin = admin;
49+
}
50+
51+
public String getMSPID() {
52+
return mspid;
53+
}
54+
55+
public String getCALocation() {
56+
return this.caLocation;
57+
}
58+
59+
public void setCALocation(String caLocation) {
60+
this.caLocation = caLocation;
61+
}
62+
63+
public void addPeerLocation(String name, String location) {
64+
65+
peerLocations.put(name, location);
66+
}
67+
68+
public void addOrdererLocation(String name, String location) {
69+
70+
ordererLocations.put(name, location);
71+
}
72+
73+
public void addEventHubLocation(String name, String location) {
74+
75+
eventHubLocations.put(name, location);
76+
}
77+
78+
public String getPeerLocation(String name) {
79+
return peerLocations.get(name);
80+
81+
}
82+
83+
public String getOrdererLocation(String name) {
84+
return ordererLocations.get(name);
85+
86+
}
87+
88+
public String getEventHubLocation(String name) {
89+
return eventHubLocations.get(name);
90+
91+
}
92+
93+
public Set<String> getPeerNames() {
94+
95+
return Collections.unmodifiableSet(peerLocations.keySet());
96+
}
97+
98+
99+
public Set<String> getOrdererNames() {
100+
101+
return Collections.unmodifiableSet(ordererLocations.keySet());
102+
}
103+
104+
public Set<String> getEventHubNames() {
105+
106+
return Collections.unmodifiableSet(eventHubLocations.keySet());
107+
}
108+
109+
public HFCAClient getCAClient() {
110+
111+
return caClient;
112+
}
113+
114+
public void setCAClient(HFCAClient caClient) {
115+
116+
this.caClient = caClient;
117+
}
118+
119+
public String getName() {
120+
return name;
121+
}
122+
123+
public void addUser(SampleUser user) {
124+
userMap.put(user.getName(), user);
125+
}
126+
127+
public User getUser(String name) {
128+
return userMap.get(name);
129+
}
130+
131+
public Collection<String> getOrdererLocations() {
132+
return Collections.unmodifiableCollection(ordererLocations.values());
133+
}
134+
135+
public Collection<String> getEventHubLocations() {
136+
return Collections.unmodifiableCollection(eventHubLocations.values());
137+
}
138+
139+
public Set<Peer> getPeers() {
140+
return Collections.unmodifiableSet(peers);
141+
}
142+
143+
public void addPeer(Peer peer) {
144+
peers.add(peer);
145+
}
146+
147+
public void setCAProperties(Properties caProperties) {
148+
this.caProperties = caProperties;
149+
}
150+
151+
public Properties getCAProperties() {
152+
return caProperties;
153+
}
154+
155+
156+
public SampleUser getPeerAdmin() {
157+
return peerAdmin;
158+
}
159+
160+
public void setPeerAdmin(SampleUser peerAdmin) {
161+
this.peerAdmin = peerAdmin;
162+
}
163+
164+
public void setDomainName(String domainName) {
165+
this.domainName = domainName;
166+
}
167+
168+
public String getDomainName() {
169+
return domainName;
170+
}
171+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package ijarvis.intelliq.FabricCA;
2+
3+
import io.netty.util.internal.StringUtil;
4+
import org.bouncycastle.util.encoders.Hex;
5+
import org.hyperledger.fabric.sdk.Enrollment;
6+
import org.hyperledger.fabric.sdk.User;
7+
8+
import java.io.*;
9+
import java.util.Set;
10+
11+
/*
12+
* 从官方的Fabric-SDK的test代码中抽象出来的描述fabric组织机构的Bean对象
13+
*/
14+
15+
16+
public class SampleUser implements User, Serializable {
17+
private static final long serialVersionUID = 8077132186383604355L;
18+
private String name;
19+
private Set<String> roles;
20+
private String account;
21+
private String affiliation;
22+
private String organization;
23+
private String enrollmentSecret;
24+
Enrollment enrollment = null; //need access in test env.
25+
private String keyValStoreName;
26+
27+
SampleUser(String name, String org) {
28+
this.name = name;
29+
this.organization = org;
30+
}
31+
@Override
32+
public String getName() {
33+
return this.name;
34+
}
35+
36+
@Override
37+
public Set<String> getRoles() {
38+
return this.roles;
39+
}
40+
41+
public void setRoles(Set<String> roles) {
42+
this.roles = roles;
43+
}
44+
45+
@Override
46+
public String getAccount() {
47+
return this.account;
48+
}
49+
50+
public void setAccount(String account) {
51+
this.account = account;
52+
}
53+
54+
@Override
55+
public String getAffiliation() {
56+
return this.affiliation;
57+
}
58+
59+
public void setAffiliation(String affiliation) {
60+
this.affiliation = affiliation;
61+
}
62+
63+
@Override
64+
public Enrollment getEnrollment() {
65+
return this.enrollment;
66+
}
67+
68+
69+
public boolean isRegistered() {
70+
return !StringUtil.isNullOrEmpty(enrollmentSecret);
71+
}
72+
73+
74+
public boolean isEnrolled() {
75+
return this.enrollment != null;
76+
}
77+
78+
public String getEnrollmentSecret() {
79+
return enrollmentSecret;
80+
}
81+
82+
public void setEnrollmentSecret(String enrollmentSecret) {
83+
this.enrollmentSecret = enrollmentSecret;
84+
}
85+
86+
public void setEnrollment(Enrollment enrollment) {
87+
this.enrollment = enrollment;
88+
}
89+
90+
@Override
91+
public String getMspId() {
92+
return mspId;
93+
}
94+
95+
String mspId;
96+
97+
public void setMspId(String mspID) {
98+
this.mspId = mspID;
99+
}
100+
101+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ijarvis.intelliq.FabricCA;
2+
3+
//实现初始化调用Fabric-CA模块以及Fabric
4+
5+
import org.hyperledger.fabric.sdk.security.CryptoSuite;
6+
import org.hyperledger.fabric_ca.sdk.HFCAClient;
7+
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;
8+
9+
import java.net.MalformedURLException;
10+
import java.util.HashMap;
11+
12+
public class TestConfigure {
13+
public static HashMap<String,SampleOrg> getConfigure() throws MalformedURLException, InvalidArgumentException {
14+
HashMap<String,SampleOrg> orgHashMap=new HashMap<>();
15+
SampleOrg org1=new SampleOrg("org1","Org1MSP");
16+
org1.addPeerLocation("peer0org1","grpc://192.168.188.112:7051");
17+
org1.addPeerLocation("peer1org1","grpc://192.168.188.113:7051");
18+
org1.addOrdererLocation("orderer","grpc://192.168.188.111:7050");
19+
org1.setCALocation("http://192.168.188.110:7054");
20+
org1.setCAClient(HFCAClient.createNewInstance(org1.getCALocation(),null));
21+
org1.getCAClient().setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
22+
23+
24+
SampleOrg org2=new SampleOrg("org2","Org2MSP");
25+
org2.addPeerLocation("peer0org2","grpc://192.168.188.114:7051");
26+
org2.addPeerLocation("peer1org2","grpc://192.168.188.115:7051");
27+
org2.addOrdererLocation("orderer","grpc://192.168.188.111:7050");
28+
//org2.setCALocation("http://192.168.188.110:8054");//Org2的CA模块未启用所以暂时不提供
29+
30+
31+
orgHashMap.put("org1",org1);
32+
orgHashMap.put("org2",org2);
33+
return orgHashMap;
34+
}
35+
}

0 commit comments

Comments
 (0)