Skip to content

Commit 5feef0a

Browse files
committed
add CATest
1 parent 9f55f3c commit 5feef0a

File tree

7 files changed

+197
-9
lines changed

7 files changed

+197
-9
lines changed

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

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

3+
import ijarvis.intelliq.FabricCA.SampleUserCA;
4+
import ijarvis.intelliq.LedgerRecord;
5+
import ijarvis.intelliq.SampleUser;
36
import org.apache.log4j.Logger;
47
import org.hyperledger.fabric.sdk.*;
58
import org.hyperledger.fabric.sdk.exception.CryptoException;
@@ -16,17 +19,26 @@ public class FabricApp{
1619
public static HFClient client=null;
1720
public static CryptoSuite cs = CryptoSuite.Factory.getCryptoSuite();
1821
public static User peer0org1=null;
22+
public static String keypath="/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp";
1923

2024
/**
2125
* 初始化超级账本的客户端等相关属性
2226
*/
2327
public static void init() throws CryptoException, InvalidArgumentException {
2428
client = HFClient.createNewInstance();
2529
client.setCryptoSuite(cs);
26-
String keystorepath= FabricApp.class.getResource("/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp").getPath();
30+
String keystorepath = FabricApp.class.getResource(keypath).getPath();
2731
peer0org1 = new SampleUser(keystorepath, "Admin");
2832
client.setUserContext(peer0org1);
2933

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");
40+
client.setUserContext(peer0org1);
41+
3042
}
3143
/*
3244
* 实现根绝给定的数据调用链码写入账本中
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package ijarvis.intelliq.FabricCA;
2+
3+
import org.hyperledger.fabric.sdk.Enrollment;
4+
import org.hyperledger.fabric.sdk.User;
5+
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;
16+
import java.util.Set;
17+
18+
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;
27+
this.userName = userName;
28+
}
29+
30+
@Override
31+
public String getName() {
32+
return userName;
33+
}
34+
35+
@Override
36+
public Set<String> getRoles() {
37+
return new HashSet<String>();
38+
}
39+
40+
@Override
41+
public String getAccount() {
42+
return "";
43+
}
44+
45+
@Override
46+
public String getAffiliation() {
47+
return "";
48+
}
49+
50+
@Override
51+
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+
};
71+
}
72+
73+
@Override
74+
public String getMspId() {
75+
return "Org1MSP";
76+
}
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;
116+
}
117+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class SampleUser implements User {
1919

2020
private final String certFolder;
2121
private final String userName;
22-
2322
public SampleUser(String certFolder, String userName) {
2423
this.certFolder = certFolder;
2524
this.userName = userName;

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

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

33

4+
import ijarvis.intelliq.LedgerRecord;
45
import org.apache.log4j.Logger;
56
import org.hyperledger.fabric.sdk.Channel;
6-
import org.hyperledger.fabric.sdk.Peer;
77
import org.hyperledger.fabric.sdk.exception.CryptoException;
88
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
9-
import org.hyperledger.fabric.sdk.exception.TransactionException;
109
import org.junit.Before;
1110
import org.junit.Test;
1211

@@ -15,22 +14,21 @@
1514
*/
1615
public class AppTest {
1716
private static Logger logger=Logger.getLogger(AppTest.class);
18-
1917
private static String CONNFIG_Orderer="grpc://192.168.188.111:7050";
2018
private static String CONNFIG_Peer0Org1="grpc://192.168.188.112:7051";
2119
private static String CONNFIG_Peer1Org1="grpc://192.168.188.113:7051";
2220
private static String CONNFIG_Peer0Org2="grpc://192.168.188.114:7051";
2321
private static String CONNFIG_Peer1Org2="grpc://192.168.188.115:7051";
2422
private static String CHANNELID="epointchannel";
25-
private static LedgerRecord PERSONINFO=new LedgerRecord("liuwenru","liuwenru1","2017-12-12","江苏省张家港市","10000","江苏省苏州市张家港市国泰新点");
23+
private static LedgerRecord PERSONINFO=new LedgerRecord("liudong","刘东","2017-12-12","江苏省张家港市","10000","江苏省苏州市张家港市国泰新点");
2624
@Before
2725
public void Setup() throws CryptoException, InvalidArgumentException {
2826
logger.debug("Fabric Test Init........");
2927
FabricApp fabricApp=new FabricApp();
3028
FabricApp.init();
3129
}
3230
@Test
33-
public void TestEpointChainCodeInster() throws Exception {
31+
public void TestEpointChainCodeInstert() throws Exception {
3432
logger.debug("测试Fabric 插入功能");
3533
Channel channel = FabricApp.client.newChannel(CHANNELID);
3634
channel.addPeer(FabricApp.client.newPeer("peer", CONNFIG_Peer0Org1));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ijarvis.intelliq.FabricCA;
2+
3+
import ijarvis.intelliq.Fabric.AppTest;
4+
import ijarvis.intelliq.Fabric.FabricApp;
5+
import ijarvis.intelliq.LedgerRecord;
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.sdk.exception.InvalidArgumentException;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
public class FabricCATest {
14+
private static Logger logger=Logger.getLogger(AppTest.class);
15+
private static String CONNFIG_Orderer="grpc://192.168.188.111:7050";
16+
private static String CONNFIG_Peer0Org1="grpc://192.168.188.112:7051";
17+
private static String CONNFIG_Peer1Org1="grpc://192.168.188.113:7051";
18+
private static String CONNFIG_Peer0Org2="grpc://192.168.188.114:7051";
19+
private static String CONNFIG_Peer1Org2="grpc://192.168.188.115:7051";
20+
private static String CHANNELID="epointchannel";
21+
private static String keypath="";
22+
private static LedgerRecord PERSONINFO=new LedgerRecord("liuwenru","刘东","2017-12-12","江苏省张家港市","10000","江苏省苏州市张家港市国泰新点");
23+
@Before
24+
public void Setup() throws CryptoException, InvalidArgumentException {
25+
logger.debug("Fabric Test Init........");
26+
FabricApp fabricApp=new FabricApp();
27+
FabricApp.keypath="/FabricCAcert";
28+
FabricApp.initCA();
29+
}
30+
@Test
31+
public void TestEpointChainCodeQuery() throws Exception {
32+
logger.debug("测试Fabric 查询功能");
33+
Channel channel = FabricApp.client.newChannel(CHANNELID);
34+
channel.addPeer(FabricApp.client.newPeer("peer", CONNFIG_Peer0Org1));
35+
channel.addOrderer(FabricApp.client.newOrderer("orderer", CONNFIG_Orderer));
36+
channel.initialize();
37+
FabricApp.queryFabcar(channel, PERSONINFO.getPerid());
38+
}
39+
40+
41+
42+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgjNLnbU1pD6/IhXbk
3+
YBDuww2U/ejsdbe6c0UnMmHrufehRANCAARYnA7HoasgSuCDk/K9lGC+YI1P6Ca1
4+
nP9KoPMup22MqCZ61QchJWF9GzWluxOmXXRBzzcr2U8rQHYH9B+iGfH/
5+
-----END PRIVATE KEY-----
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIICYjCCAgigAwIBAgIUO6rqOJ3iKddkavbf8vxWt2/jdDkwCgYIKoZIzj0EAwIw
3+
czELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh
4+
biBGcmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHDAaBgNVBAMT
5+
E2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTcxMjEyMTUxNjAwWhcNMTgxMjEyMTUx
6+
NjAwWjBdMQswCQYDVQQGEwJVUzEXMBUGA1UECBMOTm9ydGggQ2Fyb2xpbmExFDAS
7+
BgNVBAoTC0h5cGVybGVkZ2VyMQ8wDQYDVQQLEwZGYWJyaWMxDjAMBgNVBAMTBWFk
8+
bWluMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWJwOx6GrIErgg5PyvZRgvmCN
9+
T+gmtZz/SqDzLqdtjKgmetUHISVhfRs1pbsTpl10Qc83K9lPK0B2B/Qfohnx/6OB
10+
jzCBjDAOBgNVHQ8BAf8EBAMCB4AwDAYDVR0TAQH/BAIwADAdBgNVHQ4EFgQUDHZz
11+
RU4kJPoLLZf2TNF3yQ9sPWkwKwYDVR0jBCQwIoAgqdK4yjzwy8h99NKcYnjhSN7c
12+
Z+02KjlltVsIU4IoEA8wIAYDVR0RBBkwF4IVbG9jYWxob3N0LmxvY2FsZG9tYWlu
13+
MAoGCCqGSM49BAMCA0gAMEUCIQDf/A7EnUUZyQYYEWO8jhRnBQPbhHnzWa471mJt
14+
5Bq8UAIgDLz5+gh4jwnouLwKP2aDVgE4c3v4vSB5A+60/s/tUxw=
15+
-----END CERTIFICATE-----

0 commit comments

Comments
 (0)