Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5feef0a

Browse files
committed
add CATest
1 parent9f55f3c commit5feef0a

File tree

7 files changed

+197
-9
lines changed

7 files changed

+197
-9
lines changed

‎src/main/java/ijarvis/intelliq/FabricApp.javarenamed 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-
packageijarvis.intelliq;
1+
packageijarvis.intelliq.Fabric;
22

3+
importijarvis.intelliq.FabricCA.SampleUserCA;
4+
importijarvis.intelliq.LedgerRecord;
5+
importijarvis.intelliq.SampleUser;
36
importorg.apache.log4j.Logger;
47
importorg.hyperledger.fabric.sdk.*;
58
importorg.hyperledger.fabric.sdk.exception.CryptoException;
@@ -16,17 +19,26 @@ public class FabricApp{
1619
publicstaticHFClientclient=null;
1720
publicstaticCryptoSuitecs =CryptoSuite.Factory.getCryptoSuite();
1821
publicstaticUserpeer0org1=null;
22+
publicstaticStringkeypath="/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp";
1923

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

34+
}
35+
publicstaticvoidinitCA()throwsCryptoException,InvalidArgumentException {
36+
client =HFClient.createNewInstance();
37+
client.setCryptoSuite(cs);
38+
Stringkeystorepath =FabricApp.class.getResource(keypath).getPath();
39+
peer0org1 =newSampleUserCA(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+
packageijarvis.intelliq.FabricCA;
2+
3+
importorg.hyperledger.fabric.sdk.Enrollment;
4+
importorg.hyperledger.fabric.sdk.User;
5+
6+
importjavax.xml.bind.DatatypeConverter;
7+
importjava.io.*;
8+
importjava.nio.file.Files;
9+
importjava.nio.file.Path;
10+
importjava.nio.file.Paths;
11+
importjava.security.GeneralSecurityException;
12+
importjava.security.KeyFactory;
13+
importjava.security.PrivateKey;
14+
importjava.security.spec.PKCS8EncodedKeySpec;
15+
importjava.util.HashSet;
16+
importjava.util.Set;
17+
18+
publicclassSampleUserCAimplementsUser {
19+
20+
privatefinalStringcertFolder;
21+
privatefinalStringuserName;
22+
privateStringkeyname;
23+
privateStringcert;
24+
25+
publicSampleUserCA(StringcertFolder,StringuserName) {
26+
this.certFolder =certFolder;
27+
this.userName =userName;
28+
}
29+
30+
@Override
31+
publicStringgetName() {
32+
returnuserName;
33+
}
34+
35+
@Override
36+
publicSet<String>getRoles() {
37+
returnnewHashSet<String>();
38+
}
39+
40+
@Override
41+
publicStringgetAccount() {
42+
return"";
43+
}
44+
45+
@Override
46+
publicStringgetAffiliation() {
47+
return"";
48+
}
49+
50+
@Override
51+
publicEnrollmentgetEnrollment() {
52+
returnnewEnrollment() {
53+
54+
@Override
55+
publicPrivateKeygetKey() {
56+
try {
57+
returnloadPrivateKey(Paths.get(certFolder,"/caen.key"));
58+
}catch (Exceptione) {
59+
returnnull;
60+
}
61+
}
62+
@Override
63+
publicStringgetCert() {
64+
try {
65+
returnnewString(Files.readAllBytes(Paths.get(certFolder,"/cert.pem")));
66+
}catch (Exceptione) {
67+
return"";
68+
}
69+
}
70+
};
71+
}
72+
73+
@Override
74+
publicStringgetMspId() {
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+
publicstaticPrivateKeyloadPrivateKey(PathfileName)throwsIOException,GeneralSecurityException {
86+
PrivateKeykey =null;
87+
InputStreamis =null;
88+
try {
89+
is =newFileInputStream(fileName.toString());
90+
BufferedReaderbr =newBufferedReader(newInputStreamReader(is));
91+
StringBuilderbuilder =newStringBuilder();
92+
booleaninKey =false;
93+
for (Stringline =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+
PKCS8EncodedKeySpeckeySpec =newPKCS8EncodedKeySpec(encoded);
110+
KeyFactorykf =KeyFactory.getInstance("ECDSA");
111+
key =kf.generatePrivate(keySpec);
112+
}finally {
113+
is.close();
114+
}
115+
returnkey;
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
privatefinalStringcertFolder;
2121
privatefinalStringuserName;
22-
2322
publicSampleUser(StringcertFolder,StringuserName) {
2423
this.certFolder =certFolder;
2524
this.userName =userName;

‎src/test/java/ijarvis/intelliq/AppTest.javarenamed 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-
packageijarvis.intelliq;
1+
packageijarvis.intelliq.Fabric;
22

33

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

@@ -15,22 +14,21 @@
1514
*/
1615
publicclassAppTest {
1716
privatestaticLoggerlogger=Logger.getLogger(AppTest.class);
18-
1917
privatestaticStringCONNFIG_Orderer="grpc://192.168.188.111:7050";
2018
privatestaticStringCONNFIG_Peer0Org1="grpc://192.168.188.112:7051";
2119
privatestaticStringCONNFIG_Peer1Org1="grpc://192.168.188.113:7051";
2220
privatestaticStringCONNFIG_Peer0Org2="grpc://192.168.188.114:7051";
2321
privatestaticStringCONNFIG_Peer1Org2="grpc://192.168.188.115:7051";
2422
privatestaticStringCHANNELID="epointchannel";
25-
privatestaticLedgerRecordPERSONINFO=newLedgerRecord("liuwenru","liuwenru1","2017-12-12","江苏省张家港市","10000","江苏省苏州市张家港市国泰新点");
23+
privatestaticLedgerRecordPERSONINFO=newLedgerRecord("liudong","刘东","2017-12-12","江苏省张家港市","10000","江苏省苏州市张家港市国泰新点");
2624
@Before
2725
publicvoidSetup()throwsCryptoException,InvalidArgumentException {
2826
logger.debug("Fabric Test Init........");
2927
FabricAppfabricApp=newFabricApp();
3028
FabricApp.init();
3129
}
3230
@Test
33-
publicvoidTestEpointChainCodeInster()throwsException {
31+
publicvoidTestEpointChainCodeInstert()throwsException {
3432
logger.debug("测试Fabric 插入功能");
3533
Channelchannel =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+
packageijarvis.intelliq.FabricCA;
2+
3+
importijarvis.intelliq.Fabric.AppTest;
4+
importijarvis.intelliq.Fabric.FabricApp;
5+
importijarvis.intelliq.LedgerRecord;
6+
importorg.apache.log4j.Logger;
7+
importorg.hyperledger.fabric.sdk.Channel;
8+
importorg.hyperledger.fabric.sdk.exception.CryptoException;
9+
importorg.hyperledger.fabric.sdk.exception.InvalidArgumentException;
10+
importorg.junit.Before;
11+
importorg.junit.Test;
12+
13+
publicclassFabricCATest {
14+
privatestaticLoggerlogger=Logger.getLogger(AppTest.class);
15+
privatestaticStringCONNFIG_Orderer="grpc://192.168.188.111:7050";
16+
privatestaticStringCONNFIG_Peer0Org1="grpc://192.168.188.112:7051";
17+
privatestaticStringCONNFIG_Peer1Org1="grpc://192.168.188.113:7051";
18+
privatestaticStringCONNFIG_Peer0Org2="grpc://192.168.188.114:7051";
19+
privatestaticStringCONNFIG_Peer1Org2="grpc://192.168.188.115:7051";
20+
privatestaticStringCHANNELID="epointchannel";
21+
privatestaticStringkeypath="";
22+
privatestaticLedgerRecordPERSONINFO=newLedgerRecord("liuwenru","刘东","2017-12-12","江苏省张家港市","10000","江苏省苏州市张家港市国泰新点");
23+
@Before
24+
publicvoidSetup()throwsCryptoException,InvalidArgumentException {
25+
logger.debug("Fabric Test Init........");
26+
FabricAppfabricApp=newFabricApp();
27+
FabricApp.keypath="/FabricCAcert";
28+
FabricApp.initCA();
29+
}
30+
@Test
31+
publicvoidTestEpointChainCodeQuery()throwsException {
32+
logger.debug("测试Fabric 查询功能");
33+
Channelchannel =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)

[8]ページ先頭

©2009-2025 Movatter.jp