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 ;
18
20
import com .fasterxml .jackson .core .JsonProcessingException ;
21
+ import com .fasterxml .jackson .databind .JsonMappingException ;
19
22
import com .fasterxml .jackson .databind .JsonNode ;
20
23
import com .fasterxml .jackson .databind .node .ArrayNode ;
21
24
import com .fasterxml .jackson .databind .node .ObjectNode ;
@@ -86,6 +89,23 @@ public String toString() {
86
89
private User user ;
87
90
private Certifier certifier ;
88
91
92
+ private final Map <String , String > contractAddress ;
93
+
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
+
89
109
private BlockchainConnection blockchain ;
90
110
private ServerConnection server ;
91
111
private static final String DATA_PATH = "data" ;
@@ -94,62 +114,41 @@ public String toString() {
94
114
95
115
public Client () {
96
116
fc = FileController .getInstance ().configure (DATA_PATH );
117
+ this .server = new ServerConnection (SERVER_PORT );
97
118
ObjectNode clientData = (ObjectNode ) fc .loadAsJSON (getClientDirectory (), "clientData.json" );
98
119
if (clientData != null ) {
120
+ gp = fc .readFromDir (fc .getDataDirectory (), "clientData.json" , "globalParameters" , GlobalParameters .class );
121
+ contractAddress = fc .readAsMap (fc .getDataDirectory (), "clientData.json" , "contractAddress" , String .class , String .class );
122
+ loadUserData (clientData .get ("currentUserID" ).asText ());
123
+ this .blockchain .loadContracts (user .getCredentials ());
99
124
String networkURL = clientData .get ("networkURL" ).asText ();
100
- if (networkURL .equals ("null" )) {
101
- throw new RuntimeException (
102
- "Execute o comando --init informando o endereço de rede" + " para conexão com a blockchain" );
103
- }
104
- String contractAuthorityAddress = clientData .get ("contractAuthorityAddress" ).asText ();
105
- String contractFilesAddress = clientData .get ("contractFilesAddress" ).asText ();
106
- String contractKeysAddress = clientData .get ("contractKeysAddress" ).asText ();
107
- String contractRequestsAddress = clientData .get ("contractRequestsAddress" ).asText ();
108
- String contractUsersAddress = clientData .get ("contractUsersAddress" ).asText ();
109
- this .blockchain = new BlockchainConnection (networkURL , contractAuthorityAddress , contractFilesAddress ,
110
- contractKeysAddress , contractRequestsAddress , contractUsersAddress );
111
- loadUserData (clientData .get ("userID" ).asText ());
125
+ this .blockchain = new BlockchainConnection (networkURL , contractAddress );
126
+
112
127
fc .writeToDir (getClientDirectory (), "clientData.json" , clientData );
113
128
} else {
114
129
throw new RuntimeException (
115
- "Execute o comando --init informando o endereço de rede para conexão com a blockchain " );
130
+ "Execute o comando --init com o endereço para a Blockchain e o endereço do contrato Root. " );
116
131
}
117
- this .server = new ServerConnection (SERVER_PORT );
118
- gp = fc .readFromDir (fc .getDataDirectory (), "globalParameters.json" , GlobalParameters .class );
119
132
}
120
133
121
- public Client (String url ) {
122
- this (url , null , null , null , null , null );
123
- }
124
-
125
- public Client (String networkURL , String contractAuthorityAddress , String contractFilesAddress ,
126
- String contractKeysAddress , String contractRequestsAddress , String contractUsersAddress ) {
127
- fc = FileController .getInstance ();
128
- this .blockchain = new BlockchainConnection (networkURL , contractAuthorityAddress , contractFilesAddress ,
129
- contractKeysAddress , contractRequestsAddress , contractUsersAddress );
130
- ObjectNode clientData = (ObjectNode ) fc .loadAsJSON (getClientDirectory (), "clientData.json" );
131
- if (clientData != null && clientData .get ("userID" ) != null ) {
132
- loadUserData (clientData .get ("userID" ).asText ());
133
- } else if (clientData == null ) {
134
- clientData = (ObjectNode ) fc .getMapper ().createObjectNode ();
135
- }
136
- loadAttributes ();
134
+ public Client (String networkURL , String adminName , String adminEmail , String adminPrivateKey ) {
135
+ fc = FileController .getInstance ().configure (DATA_PATH );
136
+ this .server = new ServerConnection (SERVER_PORT );
137
+ this .blockchain = new BlockchainConnection (networkURL );
137
138
gp = DCPABE .globalSetup (160 );
138
- fc .writeToDir (fc .getDataDirectory (), "globalParameters.json" , gp );
139
- // UGLY: refactor contract configuration
140
- clientData .put ("networkURL" , networkURL );
141
- clientData .put ("contractAuthorityAddress" , contractAuthorityAddress );
142
- clientData .put ("contractFilesAddress" , contractFilesAddress );
143
- clientData .put ("contractKeysAddress" , contractKeysAddress );
144
- clientData .put ("contractRequestsAddress" , contractRequestsAddress );
145
- clientData .put ("contractUsersAddress" , contractUsersAddress );
139
+ contractAddress = deployContracts (adminName , adminEmail , adminPrivateKey );
140
+ ObjectNode gpJSON = fc .getMapper ().convertValue (gp , ObjectNode .class );
141
+ ObjectNode addressJSON = fc .getMapper ().convertValue (contractAddress , ObjectNode .class );
142
+ ObjectNode clientData = fc .getMapper ().createObjectNode ();
143
+ clientData .put ("currentUserID" , "" );
144
+ clientData .set ("globalParameters" , gpJSON );
145
+ clientData .set ("contractAddress" , addressJSON );
146
146
fc .writeToDir (getClientDirectory (), "clientData.json" , clientData );
147
- this .server = new ServerConnection (SERVER_PORT );
148
147
}
149
148
150
149
public void changeUser (String userID ) {
151
150
ObjectNode clientData = (ObjectNode ) fc .loadAsJSON (getClientDirectory (), "clientData.json" );
152
- clientData .put ("userID " , userID );
151
+ clientData .put ("currentUserID " , userID );
153
152
fc .writeToDir (getClientDirectory (), "clientData.json" , clientData );
154
153
loadUserData (userID );
155
154
}
@@ -162,16 +161,10 @@ public void createUser(String name, String email, String privateKey) {
162
161
this .blockchain .loadContracts (user .getCredentials ());
163
162
}
164
163
165
- public void deployContract (String userID ) {
166
- String path = getClientDirectory () + userID ;
167
- user = fc .readFromDir (path , "user.json" , User .class );
168
- certifier = fc .readFromDir (path , "Certifier.json" , Certifier .class );
169
- user .setRecordings (fc .readAsList (path , "recordings.json" , Recording .class ));
170
- PersonalKeysJSON ABEKeys = fc .readFromDir (path , "personalKeys.json" , PersonalKeysJSON .class );
171
- if (ABEKeys != null ) {
172
- user .setABEKeys (ABEKeys );
173
- }
174
- this .blockchain .deployContracts (user .getCredentials ());
164
+ public Map <String , String > deployContracts (String name , String email , String privateKey ) {
165
+ ECKey keys = this .blockchain .generateECKeys (privateKey );
166
+ user = new User (name , email , keys );
167
+ return this .blockchain .deployContracts (user .getCredentials ());
175
168
}
176
169
177
170
private String getClientDirectory () {
@@ -335,7 +328,6 @@ public void loadUserData(String userID) {
335
328
user .setABEKeys (ABEKeys );
336
329
}
337
330
338
- this .blockchain .loadContracts (user .getCredentials ());
339
331
System .out .println ("Client - user data loaded: " + userID );
340
332
}
341
333
0 commit comments