15
15
import com .brunoarruda .hyperdcpabe .blockchain .BlockchainConnection ;
16
16
import com .brunoarruda .hyperdcpabe .Recording .FileMode ;
17
17
import com .brunoarruda .hyperdcpabe .io .FileController ;
18
- import com .fasterxml .jackson .annotation .JsonProperty ;
19
- import com .fasterxml .jackson .core .JsonParseException ;
20
18
import com .fasterxml .jackson .core .JsonProcessingException ;
21
- import com .fasterxml .jackson .databind .JsonMappingException ;
22
19
import com .fasterxml .jackson .databind .JsonNode ;
23
20
import com .fasterxml .jackson .databind .node .ArrayNode ;
24
21
import com .fasterxml .jackson .databind .node .ObjectNode ;
@@ -91,21 +88,6 @@ public String toString() {
91
88
92
89
private final Map <String , String > contractAddress ;
93
90
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
-
109
91
private BlockchainConnection blockchain ;
110
92
private ServerConnection server ;
111
93
private static final String DATA_PATH = "data" ;
@@ -119,15 +101,16 @@ public Client() {
119
101
if (clientData != null ) {
120
102
gp = fc .readFromDir (getClientDirectory (), "clientData.json" , "globalParameters" , GlobalParameters .class );
121
103
contractAddress = fc .readAsMap (getClientDirectory (), "clientData.json" , "contractAddress" , String .class , String .class );
122
- loadUserData (clientData .get ("currentUserID" ).asText ());
123
- this .blockchain .loadContracts (user .getCredentials ());
124
104
String networkURL = clientData .get ("networkURL" ).asText ();
125
105
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 ();
128
111
} else {
129
112
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." );
131
114
}
132
115
}
133
116
@@ -137,27 +120,29 @@ public Client(String networkURL, String adminName, String adminEmail, String adm
137
120
this .blockchain = new BlockchainConnection (networkURL );
138
121
gp = DCPABE .globalSetup (160 );
139
122
contractAddress = deployContracts (adminName , adminEmail , adminPrivateKey );
123
+ ObjectNode address = fc .getMapper ().createObjectNode ();
124
+ contractAddress .forEach ((key , val ) -> address .put (key , val ));
140
125
ObjectNode gpJSON = fc .getMapper ().convertValue (gp , ObjectNode .class );
141
- ObjectNode addressJSON = fc .getMapper ().convertValue (contractAddress , ObjectNode .class );
142
126
ObjectNode clientData = fc .getMapper ().createObjectNode ();
143
127
clientData .put ("currentUserID" , "" );
128
+ clientData .put ("networkURL" , networkURL );
144
129
clientData .set ("globalParameters" , gpJSON );
145
- clientData .set ("contractAddress" , addressJSON );
130
+ clientData .set ("contractAddress" , address );
146
131
fc .writeToDir (getClientDirectory (), "clientData.json" , clientData );
147
132
}
148
133
149
- public void changeUser (String userID ) {
134
+ public void setActiveUser (String userID ) {
150
135
ObjectNode clientData = (ObjectNode ) fc .loadAsJSON (getClientDirectory (), "clientData.json" );
151
136
clientData .put ("currentUserID" , userID );
152
137
fc .writeToDir (getClientDirectory (), "clientData.json" , clientData );
153
- loadUserData (userID );
154
138
}
155
139
156
140
// 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.
157
141
public void createUser (String name , String email , String privateKey ) {
158
142
ECKey keys = this .blockchain .generateECKeys (privateKey );
159
143
user = new User (name , email , keys );
160
144
fc .writeToDir (fc .getUserDirectory (user ), "user.json" , user );
145
+ setActiveUser (user .getID ());
161
146
this .blockchain .loadContracts (user .getCredentials ());
162
147
}
163
148
@@ -208,10 +193,12 @@ public void createCertifier(String name, String email, String privateKey) {
208
193
ECKey keys = this .blockchain .generateECKeys (privateKey );
209
194
certifier = new Certifier (name , email , keys );
210
195
fc .writeToDir (fc .getUserDirectory (user ), "Certifier.json" , certifier );
196
+ setActiveUser (user .getID ());
211
197
}
212
198
213
199
public void createCertifier () {
214
200
if (user != null ) {
201
+ setActiveUser (user .getID ());
215
202
certifier = new Certifier (user );
216
203
} else {
217
204
System .out .println ("Crie um usuário ou informe nome e e-mail" );
@@ -276,7 +263,8 @@ public void loadAttributes() {
276
263
Map <String , PublicKey > attributes = new HashMap <String , PublicKey >();
277
264
if (folder .exists ()) {
278
265
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 );
280
268
String authority = json .split ("\\ ." )[0 ];
281
269
publishedAttributes .put (authority , attributes );
282
270
}
@@ -327,7 +315,6 @@ public void loadUserData(String userID) {
327
315
if (ABEKeys != null ) {
328
316
user .setABEKeys (ABEKeys );
329
317
}
330
-
331
318
System .out .println ("Client - user data loaded: " + userID );
332
319
}
333
320
0 commit comments