Skip to content

Commit fb6d06f

Browse files
committed
change fabric java
1 parent 10cf7e8 commit fb6d06f

File tree

7 files changed

+306
-82
lines changed

7 files changed

+306
-82
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ijarvis.intelliq.ChaincodeOperating;
2+
3+
/*
4+
*
5+
* 借助CA模块实现对链码的基本运维管理操作
6+
*
7+
* */
8+
9+
public class ChaincodeOp {
10+
11+
12+
13+
14+
15+
16+
}

src/main/java/ijarvis/intelliq/Fabric/FabricApp.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
import org.hyperledger.fabric.sdk.exception.CryptoException;
88
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
99
import org.hyperledger.fabric.sdk.security.CryptoSuite;
10+
11+
import java.net.MalformedURLException;
1012
import java.util.Collection;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
16+
import static java.nio.charset.StandardCharsets.UTF_8;
1117

1218
/**
1319
* 实现封装一些超级账本的操作方法,注意此版本为未结合使用Fabric-CA模块的代码示例
@@ -17,59 +23,56 @@ public class FabricApp{
1723
private static Logger logger=Logger.getLogger(FabricApp.class);
1824
public static HFClient client=null;
1925
public static CryptoSuite cs = CryptoSuite.Factory.getCryptoSuite();
26+
public static HashMap<String,SampleOrg> orgHashMap=null;
27+
public static ChaincodeID cid = ChaincodeID.newBuilder().setName(ijarvis.intelliq.FabricCA.TestConfigure.CHAINCODENAME).setVersion(ijarvis.intelliq.FabricCA.TestConfigure.CHAINCODEVERSION).build();
2028
public static User peer0org1=null;
21-
public static String keypath="/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp";
22-
public static String CHAINCODENAME="epointchaincodecommon";
23-
public static String CHAINCODEVERSION="0.2";
2429
/**
2530
* 初始化超级账本的客户端等相关属性
2631
*/
27-
public static void init() throws CryptoException, InvalidArgumentException {
32+
public static void init() throws CryptoException, InvalidArgumentException,MalformedURLException,org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException
33+
{
2834
client = HFClient.createNewInstance();
2935
client.setCryptoSuite(cs);
30-
String keystorepath = FabricApp.class.getResource(keypath).getPath();
31-
peer0org1 = new SampleUser(keystorepath, "Admin");
32-
client.setUserContext(peer0org1);
33-
34-
}
35-
public static void initCA() throws CryptoException, InvalidArgumentException {
36-
client = HFClient.createNewInstance();
37-
client.setCryptoSuite(cs);
38-
String keystorepath = FabricApp.class.getResource(keypath).getPath();
39-
peer0org1 = new SampleUserCA(keystorepath, "Admin");
36+
orgHashMap=TestConfigure.getConfigure();
37+
peer0org1 = orgHashMap.get("org1").getAdmin();
4038
client.setUserContext(peer0org1);
4139

4240
}
4341
/*
4442
* 实现根绝给定的数据调用链码写入账本中
4543
* */
4644
public static void instertFabcar(Channel channel, LedgerRecord record) throws Exception {
47-
QueryByChaincodeRequest req = client.newQueryProposalRequest();
48-
ChaincodeID cid = ChaincodeID.newBuilder().setName(CHAINCODENAME).setVersion(CHAINCODEVERSION).build();
45+
TransactionProposalRequest req = client.newTransactionProposalRequest();
4946
req.setChaincodeID(cid);
5047
req.setFcn("addkv");
5148
req.setArgs(record.toStringArray());
52-
logger.debug("addcard data"+record.toStringArray());
53-
Collection<ProposalResponse> resps = channel.queryByChaincode(req);
49+
//TODO 该段代码必须调用,但是未在官方的代码中找到相关的代码说明
50+
Map<String, byte[]> tm2 = new HashMap<>();
51+
tm2.put("HyperLedgerFabric", "TransactionProposalRequest:JavaSDK".getBytes(UTF_8));
52+
tm2.put("method", "TransactionProposalRequest".getBytes(UTF_8));
53+
tm2.put("result", ":)".getBytes(UTF_8));
54+
req.setTransientMap(tm2);
55+
Collection<ProposalResponse> resps = channel.sendTransactionProposal(req);
5456
for (ProposalResponse resp : resps) {
5557
String payload = new String(resp.getChaincodeActionResponsePayload());
56-
System.out.println("response: " + payload);
58+
logger.debug("response: " + payload);
5759
}
60+
channel.sendTransaction(resps);
5861
}
5962
/*
6063
* 实现根绝给定的Key查询数据
6164
* */
6265
public static void queryFabcar(Channel channel, String key) throws Exception {
6366
QueryByChaincodeRequest req = client.newQueryProposalRequest();
64-
ChaincodeID cid = ChaincodeID.newBuilder().setName(CHAINCODENAME).setVersion(CHAINCODEVERSION).build();
67+
ChaincodeID cid = ChaincodeID.newBuilder().setName(TestConfigure.CHAINCODENAME).setVersion(TestConfigure.CHAINCODEVERSION).build();
6568
req.setChaincodeID(cid);
6669
req.setFcn("query");
6770
req.setArgs(new String[] { key });
6871
System.out.println("Querying for " + key);
6972
Collection<ProposalResponse> resps = channel.queryByChaincode(req);
7073
for (ProposalResponse resp : resps) {
7174
String payload = new String(resp.getChaincodeActionResponsePayload());
72-
System.out.println("response: " + payload);
75+
logger.debug("response: " + payload);
7376
}
7477
}
7578

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package ijarvis.intelliq.Fabric;
2+
3+
import ijarvis.intelliq.Fabric.SampleUser;
4+
import org.hyperledger.fabric.sdk.Peer;
5+
import org.hyperledger.fabric.sdk.User;
6+
import org.hyperledger.fabric_ca.sdk.HFCAClient;
7+
8+
import java.util.*;
9+
10+
/*
11+
* 从官方的Fabric-SDK的test代码中抽象出来的描述fabric组织机构的Bean对象
12+
*/
13+
14+
/**
15+
* Sample Organization Representation
16+
*
17+
* Keeps track which resources are defined for the Organization it represents.
18+
*
19+
*/
20+
public class SampleOrg {
21+
final String name;
22+
final String mspid;
23+
HFCAClient caClient;
24+
25+
Map<String, User> userMap = new HashMap<>();
26+
Map<String, String> peerLocations = new HashMap<>();
27+
Map<String, String> ordererLocations = new HashMap<>();
28+
Map<String, String> eventHubLocations = new HashMap<>();
29+
Set<Peer> peers = new HashSet<>();
30+
private SampleUser admin;
31+
private String caLocation;
32+
private Properties caProperties = null;
33+
34+
private SampleUser peerAdmin;
35+
36+
37+
private String domainName;
38+
39+
public SampleOrg(String name, String mspid) {
40+
this.name = name;
41+
this.mspid = mspid;
42+
}
43+
44+
public SampleUser getAdmin() {
45+
return admin;
46+
}
47+
48+
public void setAdmin(SampleUser admin) {
49+
this.admin = admin;
50+
}
51+
52+
public String getMSPID() {
53+
return mspid;
54+
}
55+
56+
public String getCALocation() {
57+
return this.caLocation;
58+
}
59+
60+
public void setCALocation(String caLocation) {
61+
this.caLocation = caLocation;
62+
}
63+
64+
public void addPeerLocation(String name, String location) {
65+
66+
peerLocations.put(name, location);
67+
}
68+
69+
public void addOrdererLocation(String name, String location) {
70+
71+
ordererLocations.put(name, location);
72+
}
73+
74+
public void addEventHubLocation(String name, String location) {
75+
76+
eventHubLocations.put(name, location);
77+
}
78+
79+
public String getPeerLocation(String name) {
80+
return peerLocations.get(name);
81+
82+
}
83+
84+
public String getOrdererLocation(String name) {
85+
return ordererLocations.get(name);
86+
87+
}
88+
89+
public String getEventHubLocation(String name) {
90+
return eventHubLocations.get(name);
91+
92+
}
93+
94+
public Set<String> getPeerNames() {
95+
96+
return Collections.unmodifiableSet(peerLocations.keySet());
97+
}
98+
99+
100+
public Set<String> getOrdererNames() {
101+
102+
return Collections.unmodifiableSet(ordererLocations.keySet());
103+
}
104+
105+
public Set<String> getEventHubNames() {
106+
107+
return Collections.unmodifiableSet(eventHubLocations.keySet());
108+
}
109+
110+
public HFCAClient getCAClient() {
111+
112+
return caClient;
113+
}
114+
115+
public void setCAClient(HFCAClient caClient) {
116+
117+
this.caClient = caClient;
118+
}
119+
120+
public String getName() {
121+
return name;
122+
}
123+
124+
public void addUser(SampleUser user) {
125+
userMap.put(user.getName(), user);
126+
}
127+
128+
public User getUser(String name) {
129+
return userMap.get(name);
130+
}
131+
132+
public Collection<String> getOrdererLocations() {
133+
return Collections.unmodifiableCollection(ordererLocations.values());
134+
}
135+
136+
public Collection<String> getEventHubLocations() {
137+
return Collections.unmodifiableCollection(eventHubLocations.values());
138+
}
139+
140+
public Set<Peer> getPeers() {
141+
return Collections.unmodifiableSet(peers);
142+
}
143+
144+
public void addPeer(Peer peer) {
145+
peers.add(peer);
146+
}
147+
148+
public void setCAProperties(Properties caProperties) {
149+
this.caProperties = caProperties;
150+
}
151+
152+
public Properties getCAProperties() {
153+
return caProperties;
154+
}
155+
156+
157+
public SampleUser getPeerAdmin() {
158+
return peerAdmin;
159+
}
160+
161+
public void setPeerAdmin(SampleUser peerAdmin) {
162+
this.peerAdmin = peerAdmin;
163+
}
164+
165+
public void setDomainName(String domainName) {
166+
this.domainName = domainName;
167+
}
168+
169+
public String getDomainName() {
170+
return domainName;
171+
}
172+
}

src/main/java/ijarvis/intelliq/Fabric/SampleUser.java

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ijarvis.intelliq.Fabric;
22

3+
import org.apache.log4j.Logger;
34
import org.hyperledger.fabric.sdk.Enrollment;
45
import org.hyperledger.fabric.sdk.User;
56

@@ -16,14 +17,40 @@
1617
import java.util.Set;
1718

1819
public class SampleUser implements User {
19-
20-
private final String certFolder;
20+
private static Logger logger=Logger.getLogger(SampleUser.class);
21+
public String CERTDIR=FabricApp.class.getResource("/").getPath();
22+
private String certfilepath;
23+
private String keyfilepath;
2124
private final String userName;
22-
public SampleUser(String certFolder, String userName) {
23-
this.certFolder = certFolder;
25+
private String mspid;
26+
public SampleUser(String USERTYPE, String userName,String mspid) {
27+
String certfilepath1;
28+
if("peer".equals(USERTYPE)){
29+
CERTDIR=CERTDIR+"/crypto-config/peerOrganizations";
30+
}else if ("orderer".equals(USERTYPE)){
31+
CERTDIR=CERTDIR+"/crypto-config/ordererOrganizations";
32+
}
33+
if (mspid.equals("Org1MSP")){
34+
certfilepath =CERTDIR+"/org1.example.com/users/"+userName+"@org1.example.com/msp/signcerts/";
35+
File skfile=new File(certfilepath);
36+
certfilepath =certfilepath+skfile.listFiles()[0].getName();
37+
38+
keyfilepath =CERTDIR+"/org1.example.com/users/"+userName+"@org1.example.com/msp/keystore/";
39+
File keyfile=new File(keyfilepath);
40+
keyfilepath =keyfilepath+keyfile.listFiles()[0].getName();
41+
}else {
42+
certfilepath =CERTDIR+"/org2.example.com/users/"+userName+"@org2.example.com/msp/signcerts/";
43+
File skfile=new File(certfilepath);
44+
certfilepath =certfilepath+skfile.listFiles()[0].getName();
45+
46+
keyfilepath =CERTDIR+"/org2.example.com/users/"+userName+"@org2.example.com/msp/keystore/";
47+
File keyfile=new File(keyfilepath);
48+
keyfilepath =keyfilepath+keyfile.listFiles()[0].getName();
49+
}
50+
2451
this.userName = userName;
52+
this.mspid=mspid;
2553
}
26-
2754
@Override
2855
public String getName() {
2956
return userName;
@@ -51,15 +78,15 @@ public Enrollment getEnrollment() {
5178
@Override
5279
public PrivateKey getKey() {
5380
try {
54-
return loadPrivateKey(Paths.get(certFolder, "/keystore/ea2db84973c9c54436c47d7e10b9b63420f654ecd7c541fab14646e976294393_sk"));
81+
return loadPrivateKey(Paths.get(keyfilepath));
5582
} catch (Exception e) {
5683
return null;
5784
}
5885
}
5986
@Override
6087
public String getCert() {
6188
try {
62-
return new String(Files.readAllBytes(Paths.get(certFolder, "/signcerts/Admin@org1.example.com-cert.pem")));
89+
return new String(Files.readAllBytes(Paths.get(certfilepath)));
6390
} catch (Exception e) {
6491
return "";
6592
}
@@ -69,16 +96,12 @@ public String getCert() {
6996

7097
@Override
7198
public String getMspId() {
72-
return "Org1MSP";
99+
return this.mspid;
73100
}
101+
102+
74103
/***
75-
* loading private key from .pem-formatted file, ECDSA algorithm
76-
* (from some example on StackOverflow, slightly changed)
77-
* @param fileName - file with the key
78-
* @return Private Key usable
79-
* @throws IOException
80-
* @throws GeneralSecurityException
81-
*/
104+
* */
82105
public static PrivateKey loadPrivateKey(Path fileName) throws IOException, GeneralSecurityException {
83106
PrivateKey key = null;
84107
InputStream is = null;

0 commit comments

Comments
 (0)