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

Commitfb6d06f

Browse files
committed
change fabric java
1 parent10cf7e8 commitfb6d06f

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+
packageijarvis.intelliq.ChaincodeOperating;
2+
3+
/*
4+
*
5+
* 借助CA模块实现对链码的基本运维管理操作
6+
*
7+
* */
8+
9+
publicclassChaincodeOp {
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
importorg.hyperledger.fabric.sdk.exception.CryptoException;
88
importorg.hyperledger.fabric.sdk.exception.InvalidArgumentException;
99
importorg.hyperledger.fabric.sdk.security.CryptoSuite;
10+
11+
importjava.net.MalformedURLException;
1012
importjava.util.Collection;
13+
importjava.util.HashMap;
14+
importjava.util.Map;
15+
16+
importstaticjava.nio.charset.StandardCharsets.UTF_8;
1117

1218
/**
1319
* 实现封装一些超级账本的操作方法,注意此版本为未结合使用Fabric-CA模块的代码示例
@@ -17,59 +23,56 @@ public class FabricApp{
1723
privatestaticLoggerlogger=Logger.getLogger(FabricApp.class);
1824
publicstaticHFClientclient=null;
1925
publicstaticCryptoSuitecs =CryptoSuite.Factory.getCryptoSuite();
26+
publicstaticHashMap<String,SampleOrg>orgHashMap=null;
27+
publicstaticChaincodeIDcid =ChaincodeID.newBuilder().setName(ijarvis.intelliq.FabricCA.TestConfigure.CHAINCODENAME).setVersion(ijarvis.intelliq.FabricCA.TestConfigure.CHAINCODEVERSION).build();
2028
publicstaticUserpeer0org1=null;
21-
publicstaticStringkeypath="/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp";
22-
publicstaticStringCHAINCODENAME="epointchaincodecommon";
23-
publicstaticStringCHAINCODEVERSION="0.2";
2429
/**
2530
* 初始化超级账本的客户端等相关属性
2631
*/
27-
publicstaticvoidinit()throwsCryptoException,InvalidArgumentException {
32+
publicstaticvoidinit()throwsCryptoException,InvalidArgumentException,MalformedURLException,org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException
33+
{
2834
client =HFClient.createNewInstance();
2935
client.setCryptoSuite(cs);
30-
Stringkeystorepath =FabricApp.class.getResource(keypath).getPath();
31-
peer0org1 =newSampleUser(keystorepath,"Admin");
32-
client.setUserContext(peer0org1);
33-
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");
36+
orgHashMap=TestConfigure.getConfigure();
37+
peer0org1 =orgHashMap.get("org1").getAdmin();
4038
client.setUserContext(peer0org1);
4139

4240
}
4341
/*
4442
* 实现根绝给定的数据调用链码写入账本中
4543
* */
4644
publicstaticvoidinstertFabcar(Channelchannel,LedgerRecordrecord)throwsException {
47-
QueryByChaincodeRequestreq =client.newQueryProposalRequest();
48-
ChaincodeIDcid =ChaincodeID.newBuilder().setName(CHAINCODENAME).setVersion(CHAINCODEVERSION).build();
45+
TransactionProposalRequestreq =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 =newHashMap<>();
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 (ProposalResponseresp :resps) {
5557
Stringpayload =newString(resp.getChaincodeActionResponsePayload());
56-
System.out.println("response: " +payload);
58+
logger.debug("response: " +payload);
5759
}
60+
channel.sendTransaction(resps);
5861
}
5962
/*
6063
* 实现根绝给定的Key查询数据
6164
* */
6265
publicstaticvoidqueryFabcar(Channelchannel,Stringkey)throwsException {
6366
QueryByChaincodeRequestreq =client.newQueryProposalRequest();
64-
ChaincodeIDcid =ChaincodeID.newBuilder().setName(CHAINCODENAME).setVersion(CHAINCODEVERSION).build();
67+
ChaincodeIDcid =ChaincodeID.newBuilder().setName(TestConfigure.CHAINCODENAME).setVersion(TestConfigure.CHAINCODEVERSION).build();
6568
req.setChaincodeID(cid);
6669
req.setFcn("query");
6770
req.setArgs(newString[] {key });
6871
System.out.println("Querying for " +key);
6972
Collection<ProposalResponse>resps =channel.queryByChaincode(req);
7073
for (ProposalResponseresp :resps) {
7174
Stringpayload =newString(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+
packageijarvis.intelliq.Fabric;
2+
3+
importijarvis.intelliq.Fabric.SampleUser;
4+
importorg.hyperledger.fabric.sdk.Peer;
5+
importorg.hyperledger.fabric.sdk.User;
6+
importorg.hyperledger.fabric_ca.sdk.HFCAClient;
7+
8+
importjava.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+
publicclassSampleOrg {
21+
finalStringname;
22+
finalStringmspid;
23+
HFCAClientcaClient;
24+
25+
Map<String,User>userMap =newHashMap<>();
26+
Map<String,String>peerLocations =newHashMap<>();
27+
Map<String,String>ordererLocations =newHashMap<>();
28+
Map<String,String>eventHubLocations =newHashMap<>();
29+
Set<Peer>peers =newHashSet<>();
30+
privateSampleUseradmin;
31+
privateStringcaLocation;
32+
privatePropertiescaProperties =null;
33+
34+
privateSampleUserpeerAdmin;
35+
36+
37+
privateStringdomainName;
38+
39+
publicSampleOrg(Stringname,Stringmspid) {
40+
this.name =name;
41+
this.mspid =mspid;
42+
}
43+
44+
publicSampleUsergetAdmin() {
45+
returnadmin;
46+
}
47+
48+
publicvoidsetAdmin(SampleUseradmin) {
49+
this.admin =admin;
50+
}
51+
52+
publicStringgetMSPID() {
53+
returnmspid;
54+
}
55+
56+
publicStringgetCALocation() {
57+
returnthis.caLocation;
58+
}
59+
60+
publicvoidsetCALocation(StringcaLocation) {
61+
this.caLocation =caLocation;
62+
}
63+
64+
publicvoidaddPeerLocation(Stringname,Stringlocation) {
65+
66+
peerLocations.put(name,location);
67+
}
68+
69+
publicvoidaddOrdererLocation(Stringname,Stringlocation) {
70+
71+
ordererLocations.put(name,location);
72+
}
73+
74+
publicvoidaddEventHubLocation(Stringname,Stringlocation) {
75+
76+
eventHubLocations.put(name,location);
77+
}
78+
79+
publicStringgetPeerLocation(Stringname) {
80+
returnpeerLocations.get(name);
81+
82+
}
83+
84+
publicStringgetOrdererLocation(Stringname) {
85+
returnordererLocations.get(name);
86+
87+
}
88+
89+
publicStringgetEventHubLocation(Stringname) {
90+
returneventHubLocations.get(name);
91+
92+
}
93+
94+
publicSet<String>getPeerNames() {
95+
96+
returnCollections.unmodifiableSet(peerLocations.keySet());
97+
}
98+
99+
100+
publicSet<String>getOrdererNames() {
101+
102+
returnCollections.unmodifiableSet(ordererLocations.keySet());
103+
}
104+
105+
publicSet<String>getEventHubNames() {
106+
107+
returnCollections.unmodifiableSet(eventHubLocations.keySet());
108+
}
109+
110+
publicHFCAClientgetCAClient() {
111+
112+
returncaClient;
113+
}
114+
115+
publicvoidsetCAClient(HFCAClientcaClient) {
116+
117+
this.caClient =caClient;
118+
}
119+
120+
publicStringgetName() {
121+
returnname;
122+
}
123+
124+
publicvoidaddUser(SampleUseruser) {
125+
userMap.put(user.getName(),user);
126+
}
127+
128+
publicUsergetUser(Stringname) {
129+
returnuserMap.get(name);
130+
}
131+
132+
publicCollection<String>getOrdererLocations() {
133+
returnCollections.unmodifiableCollection(ordererLocations.values());
134+
}
135+
136+
publicCollection<String>getEventHubLocations() {
137+
returnCollections.unmodifiableCollection(eventHubLocations.values());
138+
}
139+
140+
publicSet<Peer>getPeers() {
141+
returnCollections.unmodifiableSet(peers);
142+
}
143+
144+
publicvoidaddPeer(Peerpeer) {
145+
peers.add(peer);
146+
}
147+
148+
publicvoidsetCAProperties(PropertiescaProperties) {
149+
this.caProperties =caProperties;
150+
}
151+
152+
publicPropertiesgetCAProperties() {
153+
returncaProperties;
154+
}
155+
156+
157+
publicSampleUsergetPeerAdmin() {
158+
returnpeerAdmin;
159+
}
160+
161+
publicvoidsetPeerAdmin(SampleUserpeerAdmin) {
162+
this.peerAdmin =peerAdmin;
163+
}
164+
165+
publicvoidsetDomainName(StringdomainName) {
166+
this.domainName =domainName;
167+
}
168+
169+
publicStringgetDomainName() {
170+
returndomainName;
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
packageijarvis.intelliq.Fabric;
22

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

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

1819
publicclassSampleUserimplementsUser {
19-
20-
privatefinalStringcertFolder;
20+
privatestaticLoggerlogger=Logger.getLogger(SampleUser.class);
21+
publicStringCERTDIR=FabricApp.class.getResource("/").getPath();
22+
privateStringcertfilepath;
23+
privateStringkeyfilepath;
2124
privatefinalStringuserName;
22-
publicSampleUser(StringcertFolder,StringuserName) {
23-
this.certFolder =certFolder;
25+
privateStringmspid;
26+
publicSampleUser(StringUSERTYPE,StringuserName,Stringmspid) {
27+
Stringcertfilepath1;
28+
if("peer".equals(USERTYPE)){
29+
CERTDIR=CERTDIR+"/crypto-config/peerOrganizations";
30+
}elseif ("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+
Fileskfile=newFile(certfilepath);
36+
certfilepath =certfilepath+skfile.listFiles()[0].getName();
37+
38+
keyfilepath =CERTDIR+"/org1.example.com/users/"+userName+"@org1.example.com/msp/keystore/";
39+
Filekeyfile=newFile(keyfilepath);
40+
keyfilepath =keyfilepath+keyfile.listFiles()[0].getName();
41+
}else {
42+
certfilepath =CERTDIR+"/org2.example.com/users/"+userName+"@org2.example.com/msp/signcerts/";
43+
Fileskfile=newFile(certfilepath);
44+
certfilepath =certfilepath+skfile.listFiles()[0].getName();
45+
46+
keyfilepath =CERTDIR+"/org2.example.com/users/"+userName+"@org2.example.com/msp/keystore/";
47+
Filekeyfile=newFile(keyfilepath);
48+
keyfilepath =keyfilepath+keyfile.listFiles()[0].getName();
49+
}
50+
2451
this.userName =userName;
52+
this.mspid=mspid;
2553
}
26-
2754
@Override
2855
publicStringgetName() {
2956
returnuserName;
@@ -51,15 +78,15 @@ public Enrollment getEnrollment() {
5178
@Override
5279
publicPrivateKeygetKey() {
5380
try {
54-
returnloadPrivateKey(Paths.get(certFolder,"/keystore/ea2db84973c9c54436c47d7e10b9b63420f654ecd7c541fab14646e976294393_sk"));
81+
returnloadPrivateKey(Paths.get(keyfilepath));
5582
}catch (Exceptione) {
5683
returnnull;
5784
}
5885
}
5986
@Override
6087
publicStringgetCert() {
6188
try {
62-
returnnewString(Files.readAllBytes(Paths.get(certFolder,"/signcerts/Admin@org1.example.com-cert.pem")));
89+
returnnewString(Files.readAllBytes(Paths.get(certfilepath)));
6390
}catch (Exceptione) {
6491
return"";
6592
}
@@ -69,16 +96,12 @@ public String getCert() {
6996

7097
@Override
7198
publicStringgetMspId() {
72-
return"Org1MSP";
99+
returnthis.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
publicstaticPrivateKeyloadPrivateKey(PathfileName)throwsIOException,GeneralSecurityException {
83106
PrivateKeykey =null;
84107
InputStreamis =null;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp