Skip to content

Commit 17ff75e

Browse files
committed
fix bug
1 parent d4b5bcd commit 17ff75e

File tree

8 files changed

+116
-102
lines changed

8 files changed

+116
-102
lines changed

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

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

3-
import ijarvis.intelliq.FabricCA.SampleUserCA;
43
import ijarvis.intelliq.LedgerRecord;
54
import org.apache.log4j.Logger;
65
import org.hyperledger.fabric.sdk.*;
@@ -24,7 +23,7 @@ public class FabricApp{
2423
public static HFClient client=null;
2524
public static CryptoSuite cs = CryptoSuite.Factory.getCryptoSuite();
2625
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();
26+
public static ChaincodeID cid = ChaincodeID.newBuilder().setName(ijarvis.intelliq.TestConfigure.CHAINCODENAME).setVersion(ijarvis.intelliq.TestConfigure.CHAINCODEVERSION).build();
2827
public static User peer0org1=null;
2928
/**
3029
* 初始化超级账本的客户端等相关属性

src/main/java/ijarvis/intelliq/FabricCA/FabricCAApp.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package ijarvis.intelliq.FabricCA;
2+
/**
3+
* 负责实际测试时干活的类,负责调用链上的链码
4+
*/
5+
26

3-
import ijarvis.intelliq.Fabric.FabricApp;
4-
import ijarvis.intelliq.Fabric.SampleUser;
57
import ijarvis.intelliq.LedgerRecord;
8+
import ijarvis.intelliq.TestConfigure;
69
import org.apache.log4j.Logger;
710
import org.hyperledger.fabric.sdk.*;
811
import org.hyperledger.fabric.sdk.exception.CryptoException;
@@ -50,6 +53,7 @@ public static void addKV(Channel channel, LedgerRecord record) throws ProposalEx
5053
logger.debug("response: " + payload);
5154
}
5255
channel.sendTransaction(resps);
56+
5357
}
5458
/**
5559
* 调用链码更新

src/main/java/ijarvis/intelliq/FabricCA/SampleOrg.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ public class SampleOrg {
2929
private SampleUser admin;
3030
private String caLocation;
3131
private Properties caProperties = null;
32-
3332
private SampleUser peerAdmin;
34-
35-
3633
private String domainName;
3734

3835
public SampleOrg(String name, String mspid) {
Lines changed: 48 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,84 @@
11
package ijarvis.intelliq.FabricCA;
22

3+
import ijarvis.intelliq.TestConfigure;
34
import org.hyperledger.fabric.sdk.Enrollment;
45
import org.hyperledger.fabric.sdk.User;
6+
import org.hyperledger.fabric.sdk.security.CryptoSuite;
7+
import org.hyperledger.fabric_ca.sdk.HFCAClient;
8+
import org.hyperledger.fabric_ca.sdk.HFCAEnrollment;
9+
import org.hyperledger.fabric_ca.sdk.exception.EnrollmentException;
10+
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;
511

6-
import javax.xml.bind.DatatypeConverter;
7-
import java.io.*;
8-
import java.nio.file.Files;
9-
import java.nio.file.Path;
10-
import java.nio.file.Paths;
11-
import java.security.GeneralSecurityException;
12-
import java.security.KeyFactory;
13-
import java.security.PrivateKey;
14-
import java.security.spec.PKCS8EncodedKeySpec;
15-
import java.util.HashSet;
12+
import java.net.MalformedURLException;
1613
import java.util.Set;
1714

1815
public class SampleUserCA implements User {
19-
20-
private final String certFolder;
21-
private final String userName;
22-
private String keyname;
23-
private String cert;
24-
25-
public SampleUserCA(String certFolder, String userName) {
26-
this.certFolder = certFolder;
16+
private String userName="";
17+
private String userPass="";
18+
private String MSPID="";
19+
private Enrollment enrollment;
20+
public SampleUserCA(String userName, String userPass, String MSPID) throws MalformedURLException, InvalidArgumentException, EnrollmentException {
2721
this.userName = userName;
22+
this.userPass = userPass;
23+
this.MSPID = MSPID;
24+
//调用Fabric的接口获取到注册用户的证书信息
25+
HFCAClient client=HFCAClient.createNewInstance("epointca",TestConfigure.Fabric_CA_Server,null);
26+
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
27+
this.enrollment=client.enroll(this.userName,this.userPass);
28+
2829
}
2930

3031
@Override
3132
public String getName() {
32-
return userName;
33+
return this.userName;
3334
}
3435

3536
@Override
3637
public Set<String> getRoles() {
37-
return new HashSet<String>();
38+
return null;
3839
}
3940

4041
@Override
4142
public String getAccount() {
42-
return "";
43+
return null;
4344
}
4445

4546
@Override
4647
public String getAffiliation() {
47-
return "";
48+
return null;
4849
}
4950

5051
@Override
5152
public Enrollment getEnrollment() {
52-
return new Enrollment() {
53-
54-
@Override
55-
public PrivateKey getKey() {
56-
try {
57-
return loadPrivateKey(Paths.get(certFolder, "/caen.key"));
58-
} catch (Exception e) {
59-
return null;
60-
}
61-
}
62-
@Override
63-
public String getCert() {
64-
try {
65-
return new String(Files.readAllBytes(Paths.get(certFolder, "/cert.pem")));
66-
} catch (Exception e) {
67-
return "";
68-
}
69-
}
70-
};
53+
return this.enrollment;
7154
}
7255

7356
@Override
7457
public String getMspId() {
75-
return "Org1MSP";
58+
return this.MSPID;
7659
}
77-
/***
78-
* loading private key from .pem-formatted file, ECDSA algorithm
79-
* (from some example on StackOverflow, slightly changed)
80-
* @param fileName - file with the key
81-
* @return Private Key usable
82-
* @throws IOException
83-
* @throws GeneralSecurityException
84-
*/
85-
public static PrivateKey loadPrivateKey(Path fileName) throws IOException, GeneralSecurityException {
86-
PrivateKey key = null;
87-
InputStream is = null;
88-
try {
89-
is = new FileInputStream(fileName.toString());
90-
BufferedReader br = new BufferedReader(new InputStreamReader(is));
91-
StringBuilder builder = new StringBuilder();
92-
boolean inKey = false;
93-
for (String line = br.readLine(); line != null; line = br.readLine()) {
94-
if (!inKey) {
95-
if (line.startsWith("-----BEGIN ") && line.endsWith(" PRIVATE KEY-----")) {
96-
inKey = true;
97-
}
98-
continue;
99-
} else {
100-
if (line.startsWith("-----END ") && line.endsWith(" PRIVATE KEY-----")) {
101-
inKey = false;
102-
break;
103-
}
104-
builder.append(line);
105-
}
106-
}
107-
//
108-
byte[] encoded = DatatypeConverter.parseBase64Binary(builder.toString());
109-
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
110-
KeyFactory kf = KeyFactory.getInstance("ECDSA");
111-
key = kf.generatePrivate(keySpec);
112-
} finally {
113-
is.close();
114-
}
115-
return key;
60+
61+
public String getUserName() {
62+
return userName;
63+
}
64+
65+
public void setUserName(String userName) {
66+
this.userName = userName;
67+
}
68+
69+
public String getUserPass() {
70+
return userPass;
71+
}
72+
73+
public void setUserPass(String userPass) {
74+
this.userPass = userPass;
75+
}
76+
77+
public String getMSPID() {
78+
return MSPID;
79+
}
80+
81+
public void setMSPID(String MSPID) {
82+
this.MSPID = MSPID;
11683
}
11784
}

src/main/java/ijarvis/intelliq/FabricCA/TestConfigure.java renamed to src/main/java/ijarvis/intelliq/TestConfigure.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ijarvis.intelliq.FabricCA;
1+
package ijarvis.intelliq;
22

33
/**
44
* 实现初始化调用Fabric-CA模块以及Fabric
@@ -8,6 +8,7 @@
88

99

1010

11+
import ijarvis.intelliq.FabricCA.SampleOrg;
1112
import org.hyperledger.fabric.sdk.security.CryptoSuite;
1213
import org.hyperledger.fabric_ca.sdk.HFCAClient;
1314
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;
@@ -16,26 +17,28 @@
1617
import java.util.HashMap;
1718

1819
public class TestConfigure {
19-
public static String CHAINCODENAME="epointchaincodecommon";
20-
public static String CHAINCODEVERSION="0.1";
21-
public static String CHANNLNAME="epointchannel";
20+
public static String CHAINCODENAME="epointchaincodecommon"; //链码名称
21+
public static String CHAINCODEVERSION="0.1"; //链码版本
22+
public static String CHANNLNAME="epointchannel"; //链码通道名称
23+
public static String Fabric_CA_NAME="epoint";
24+
public static String Fabric_CA_PASS="epoint";
25+
public static String Fabric_CA_Server="http://192.168.186.151:7054";
2226
public static HashMap<String,SampleOrg> getConfigure() throws MalformedURLException, InvalidArgumentException {
2327
HashMap<String,SampleOrg> orgHashMap=new HashMap<>();
2428
SampleOrg org1=new SampleOrg("city1","city1MSP");
2529
org1.addPeerLocation("peer0city1","grpc://192.168.186.153:7051");
2630
org1.addPeerLocation("peer1city1","grpc://192.168.186.154:7051");
27-
org1.addOrdererLocation("orderer","grpc://192.168.186.152:7050");
31+
org1.addOrdererLocation("orderer1","grpc://192.168.186.152:7050");
2832
org1.setCALocation("http://192.168.186.151:7054");
2933
org1.setCAClient(HFCAClient.createNewInstance(org1.getCALocation(),null));
3034
org1.getCAClient().setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
3135

3236
SampleOrg org2=new SampleOrg("city2","city2MSP");
3337
org2.addPeerLocation("peer0city2","grpc://192.168.186.155:7051");
3438
org2.addPeerLocation("peer1city2","grpc://192.168.186.156:7051");
35-
org2.addOrdererLocation("orderer","grpc://192.168.186.152:7050");
39+
org2.addOrdererLocation("orderer1","grpc://192.168.186.152:7050");
3640
//org2.setCALocation("http://192.168.188.110:8054");//Org2的CA模块未启用所以暂时不提供
3741

38-
3942
orgHashMap.put("city1",org1);
4043
orgHashMap.put("city2",org2);
4144
return orgHashMap;

src/test/java/ijarvis/intelliq/Fabric/AppTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package ijarvis.intelliq.Fabric;
22

33

4-
import ijarvis.intelliq.FabricCA.FabricCAApp;
5-
import ijarvis.intelliq.FabricCA.TestConfigure;
4+
import ijarvis.intelliq.TestConfigure;
65
import ijarvis.intelliq.LedgerRecord;
76
import org.apache.log4j.Logger;
87
import org.hyperledger.fabric.sdk.Channel;
9-
import org.hyperledger.fabric.sdk.exception.CryptoException;
10-
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
118
import org.junit.Before;
129
import org.junit.Ignore;
1310
import org.junit.Test;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ijarvis.intelliq.FabricCA;
2+
3+
4+
import ijarvis.intelliq.LedgerRecord;
5+
import ijarvis.intelliq.TestConfigure;
6+
import org.apache.log4j.Logger;
7+
import org.hyperledger.fabric.sdk.Channel;
8+
import org.hyperledger.fabric.sdk.exception.CryptoException;
9+
import org.hyperledger.fabric_ca.sdk.HFCAClient;
10+
import org.hyperledger.fabric_ca.sdk.exception.EnrollmentException;
11+
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
import java.net.MalformedURLException;
16+
import java.util.HashMap;
17+
18+
public class FabricCATestCaseWithCAServer {
19+
private static Logger logger=Logger.getLogger(FabricCATestCaseWithCAServer.class);
20+
HashMap<String,SampleOrg> orgHashMap=new HashMap<>();
21+
private static LedgerRecord PERSONINFO=new LedgerRecord("liuwenru","基础设置支持部");
22+
@Before
23+
public void Setup() throws EnrollmentException, InvalidArgumentException, CryptoException, org.hyperledger.fabric.sdk.exception.InvalidArgumentException, MalformedURLException {
24+
logger.debug("Fabric CA Test Start.......");
25+
orgHashMap=TestConfigure.getConfigure();
26+
FabricCAApp fabricCAApp=new FabricCAApp();
27+
SampleUserCA user1=new SampleUserCA("city1user0","epoint","city1MSP");
28+
HFCAClient hfcaClient=orgHashMap.get("city1").getCAClient();
29+
FabricCAApp.init(user1);
30+
}
31+
@Test
32+
public void TestEpointChainCodeAddKV()throws Exception{
33+
logger.debug("链码测试........向链码中添加KV");
34+
Channel channel = FabricCAApp.client.newChannel(TestConfigure.CHANNLNAME);
35+
channel.addPeer(FabricCAApp.client.newPeer("peer", orgHashMap.get("city1").getPeerLocation("peer0city1")));
36+
channel.addOrderer(FabricCAApp.client.newOrderer("orderer1", orgHashMap.get("city1").getOrdererLocation("orderer1")));
37+
channel.initialize();
38+
FabricCAApp.addKV(channel, PERSONINFO);
39+
}
40+
@Test
41+
public void TestEpointChainCodeQuery() throws Exception{
42+
logger.debug("链码测试........向链码查询key值");
43+
Channel channel = FabricCAApp.client.newChannel(TestConfigure.CHANNLNAME);
44+
channel.addPeer(FabricCAApp.client.newPeer("peer", orgHashMap.get("city1").getPeerLocation("peer0city1")));
45+
channel.addOrderer(FabricCAApp.client.newOrderer("orderer1", orgHashMap.get("city1").getOrdererLocation("orderer1")));
46+
channel.initialize();
47+
FabricCAApp.querykv(channel, PERSONINFO.getKey());
48+
}
49+
}

src/test/java/ijarvis/intelliq/FabricCA/FabricCATestUseCAServer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package ijarvis.intelliq.FabricCA;
22

3-
import ijarvis.intelliq.Fabric.FabricApp;
43
import ijarvis.intelliq.LedgerRecord;
4+
import ijarvis.intelliq.TestConfigure;
55
import org.apache.log4j.Logger;
66
import org.hyperledger.fabric.sdk.Channel;
77
import org.hyperledger.fabric.sdk.exception.CryptoException;
8-
import org.hyperledger.fabric.sdk.exception.TransactionException;
9-
import org.hyperledger.fabric.sdk.security.CryptoSuite;
108
import org.hyperledger.fabric_ca.sdk.HFCAClient;
119
import org.hyperledger.fabric_ca.sdk.exception.EnrollmentException;
1210
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;

0 commit comments

Comments
 (0)