Skip to content

Commit e347526

Browse files
committed
Fix bugs related to loading of attributes in memory
Also renames method loadUser to setActiveUser in class Client
1 parent 42a8b4d commit e347526

File tree

3 files changed

+27
-44
lines changed

3 files changed

+27
-44
lines changed

src/main/java/com/brunoarruda/hyperdcpabe/Client.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
import com.brunoarruda.hyperdcpabe.blockchain.BlockchainConnection;
1616
import com.brunoarruda.hyperdcpabe.Recording.FileMode;
1717
import com.brunoarruda.hyperdcpabe.io.FileController;
18-
import com.fasterxml.jackson.annotation.JsonProperty;
19-
import com.fasterxml.jackson.core.JsonParseException;
2018
import com.fasterxml.jackson.core.JsonProcessingException;
21-
import com.fasterxml.jackson.databind.JsonMappingException;
2219
import com.fasterxml.jackson.databind.JsonNode;
2320
import com.fasterxml.jackson.databind.node.ArrayNode;
2421
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -91,21 +88,6 @@ public String toString() {
9188

9289
private final Map<String, String> contractAddress;
9390

94-
@JsonProperty("Global Parameters")
95-
public GlobalParameters getGlobalParameters() {
96-
return gp;
97-
}
98-
99-
@JsonProperty("activeUserID")
100-
public String getUserID() {
101-
return user.getID();
102-
}
103-
104-
@JsonProperty("ContractAddress")
105-
public Map<String, String> getContractAddresses() {
106-
return contractAddress;
107-
}
108-
10991
private BlockchainConnection blockchain;
11092
private ServerConnection server;
11193
private static final String DATA_PATH = "data";
@@ -119,15 +101,16 @@ public Client() {
119101
if (clientData != null) {
120102
gp = fc.readFromDir(getClientDirectory(), "clientData.json", "globalParameters", GlobalParameters.class);
121103
contractAddress = fc.readAsMap(getClientDirectory(), "clientData.json", "contractAddress", String.class, String.class);
122-
loadUserData(clientData.get("currentUserID").asText());
123-
this.blockchain.loadContracts(user.getCredentials());
124104
String networkURL = clientData.get("networkURL").asText();
125105
this.blockchain = new BlockchainConnection(networkURL, contractAddress);
126-
127-
fc.writeToDir(getClientDirectory(), "clientData.json", clientData);
106+
if (!clientData.get("currentUserID").asText().equals("")) {
107+
loadUserData(clientData.get("currentUserID").asText());
108+
this.blockchain.loadContracts(user.getCredentials());
109+
}
110+
loadAttributes();
128111
} else {
129112
throw new RuntimeException(
130-
"Execute o comando --init com o endereço para a Blockchain e o endereço do contrato Root.");
113+
"Execute o comando --init informando o endereço de rede para a Blockchain e o endereço do contrato Root.");
131114
}
132115
}
133116

@@ -137,27 +120,29 @@ public Client(String networkURL, String adminName, String adminEmail, String adm
137120
this.blockchain = new BlockchainConnection(networkURL);
138121
gp = DCPABE.globalSetup(160);
139122
contractAddress = deployContracts(adminName, adminEmail, adminPrivateKey);
123+
ObjectNode address = fc.getMapper().createObjectNode();
124+
contractAddress.forEach((key, val) -> address.put(key, val));
140125
ObjectNode gpJSON = fc.getMapper().convertValue(gp, ObjectNode.class);
141-
ObjectNode addressJSON = fc.getMapper().convertValue(contractAddress, ObjectNode.class);
142126
ObjectNode clientData = fc.getMapper().createObjectNode();
143127
clientData.put("currentUserID", "");
128+
clientData.put("networkURL", networkURL);
144129
clientData.set("globalParameters", gpJSON);
145-
clientData.set("contractAddress", addressJSON);
130+
clientData.set("contractAddress", address);
146131
fc.writeToDir(getClientDirectory(), "clientData.json", clientData);
147132
}
148133

149-
public void changeUser(String userID) {
134+
public void setActiveUser(String userID) {
150135
ObjectNode clientData = (ObjectNode) fc.loadAsJSON(getClientDirectory(), "clientData.json");
151136
clientData.put("currentUserID", userID);
152137
fc.writeToDir(getClientDirectory(), "clientData.json", clientData);
153-
loadUserData(userID);
154138
}
155139

156140
// NOTE: createUser generates a pubkey from privateKey. It's only for this that I'm using Ethereum Library. Maybe this class may migrate the data to the Web3j ECKeyPair.
157141
public void createUser(String name, String email, String privateKey) {
158142
ECKey keys = this.blockchain.generateECKeys(privateKey);
159143
user = new User(name, email, keys);
160144
fc.writeToDir(fc.getUserDirectory(user), "user.json", user);
145+
setActiveUser(user.getID());
161146
this.blockchain.loadContracts(user.getCredentials());
162147
}
163148

@@ -208,10 +193,12 @@ public void createCertifier(String name, String email, String privateKey) {
208193
ECKey keys = this.blockchain.generateECKeys(privateKey);
209194
certifier = new Certifier(name, email, keys);
210195
fc.writeToDir(fc.getUserDirectory(user), "Certifier.json", certifier);
196+
setActiveUser(user.getID());
211197
}
212198

213199
public void createCertifier() {
214200
if (user != null) {
201+
setActiveUser(user.getID());
215202
certifier = new Certifier(user);
216203
} else {
217204
System.out.println("Crie um usuário ou informe nome e e-mail");
@@ -276,7 +263,8 @@ public void loadAttributes() {
276263
Map<String, PublicKey> attributes = new HashMap<String, PublicKey>();
277264
if (folder.exists()) {
278265
for (String json : folder.list()) {
279-
attributes = fc.readAsMap(path, json, String.class, PublicKey.class);
266+
// UGLY: repeated json parameter here. That file should be included inside client config. data.
267+
attributes = fc.readAsMap(path, json, json.split("\\.")[0], String.class, PublicKey.class);
280268
String authority = json.split("\\.")[0];
281269
publishedAttributes.put(authority, attributes);
282270
}
@@ -327,7 +315,6 @@ public void loadUserData(String userID) {
327315
if (ABEKeys != null) {
328316
user.setABEKeys(ABEKeys);
329317
}
330-
331318
System.out.println("Client - user data loaded: " + userID);
332319
}
333320

src/main/java/com/brunoarruda/hyperdcpabe/CommandLine.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.net.URL;
99
import java.util.Hashtable;
1010
import java.util.Map;
11-
import java.util.Scanner;
1211

1312
import com.brunoarruda.hyperdcpabe.Client.RequestStatus;
1413

@@ -20,7 +19,6 @@ public class CommandLine {
2019
private static final Map<String, String> COMMAND_ALIAS = new Hashtable<>();
2120
private static final int BUFFER_SIZE = 1024;
2221
private static Client client;
23-
private static Scanner sc;
2422

2523
static {
2624
populateCommands();
@@ -109,7 +107,7 @@ public static void createCertifier(String[] args){
109107
}
110108

111109
public static void load(String[] args){
112-
client.changeUser(args[1]);
110+
client.setActiveUser(args[1]);
113111
}
114112

115113
// DCPABE commands
@@ -203,7 +201,6 @@ public static void milestone(String[] args){
203201
choices[i] = Integer.parseInt(numberSplit[i]);
204202
}
205203
runMilestone(choices);
206-
sc.close();
207204
}
208205

209206
public static void help(String[] args){
@@ -284,9 +281,9 @@ public static void runMilestone(int[] choice) {
284281
String args;
285282
if (choice[0] == 1) {
286283
// pre-setup to deliver file to be encrypted to user folder
287-
String path = "data\\client\\Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47\\";
284+
String path = "data/client/Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47/";
288285
new File(path).mkdirs();
289-
getFileFromResources(path, "test\\lorem_ipsum.md");
286+
getFileFromResources(path, "test/lorem_ipsum.md", "lorem_ipsum.md");
290287

291288
// admin inicia o sistema e cria os contratos
292289
args = "--init http://127.0.0.1:7545 admin [email protected] ";
@@ -335,9 +332,9 @@ public static void runMilestone(int[] choice) {
335332

336333
if (choice[0] == 2) {
337334
// pre-setup to deliver file to be encrypted to user folder
338-
String path = "data\\client\\Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47\\";
335+
String path = "data/client/Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47/";
339336
new File(path).mkdirs();
340-
getFileFromResources(path, "test\\lorem_ipsum2.md");
337+
getFileFromResources(path, "test/lorem_ipsum2.md", "lorem_ipsum2.md");
341338

342339
/**
343340
* cenário: novo prontuário
@@ -367,7 +364,7 @@ public static void runMilestone(int[] choice) {
367364
*/
368365
if (choice[1] == 2) {
369366
runMilestone(new int[] { 1 });
370-
getFileFromResources(path, "test\\lorem_ipsum-edit.md", "test\\lorem_ipsum.md");
367+
getFileFromResources(path, "test/lorem_ipsum-edit.md", "lorem_ipsum.md");
371368
runCommand("--load Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47".split(" "));
372369
runCommand("--encrypt lorem_ipsum.md atributo1 CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0".split(" "));
373370
runCommand("--send lorem_ipsum.md".split(" "));
@@ -400,10 +397,6 @@ public static void runMilestone(int[] choice) {
400397
}
401398
}
402399

403-
private static void getFileFromResources(String path, String fileName) {
404-
getFileFromResources(path, fileName, fileName);
405-
}
406-
407400
private static void getFileFromResources(String path, String inputFileName, String outputFileName) {
408401
ClassLoader classLoader = CommandLine.class.getClassLoader();
409402

src/main/java/com/brunoarruda/hyperdcpabe/blockchain/BlockchainConnection.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ public BlockchainConnection(String networkURL) {
7878
// cycles
7979
public BlockchainConnection(String networkURL, Map<String, String> contractAddress) {
8080
this.networkURL = networkURL;
81-
contractAddress = new HashMap<String, String>();
8281
web3j = Web3j.build(new HttpService(networkURL));
8382
fc = FileController.getInstance();
8483
dgp = new DefaultGasProvider();
85-
this.contractAddress = contractAddress;
84+
if (contractAddress == null) {
85+
this.contractAddress = new HashMap<String, String>();
86+
} else {
87+
this.contractAddress = contractAddress;
88+
}
8689
}
8790

8891
public Map<String, String> deployContracts(Credentials credentials) {

0 commit comments

Comments
 (0)