forked from miyurud/blockchain-iam-group-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EthereumInterface.bal
317 lines (269 loc) · 11 KB
/
EthereumInterface.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright (c) 2019 Miyuru Dayarathna All Rights Reserved.
//
// Miyuru Dayarathna licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import ballerina/http;
import ballerina/log;
import ballerina/io;
import ballerina/config;
import ballerina/math;
import wso2/ethereum;
import wso2/utils;
listener http:Listener blockChainInterfaceEP = new(9093);
map<string> sessionMap = {};
map<boolean> authenticatedMap = {};
string chatBuffer = "";
ethereum:EthereumConfiguration ethereumConfig = {
jsonRpcEndpoint: "http://192.168.32.1:8081",
jsonRpcVersion: "2.0",
networkId: "2000"
};
string jsonRpcEndpoint = ethereumConfig.jsonRpcEndpoint;
http:Client ethereumClient = new(jsonRpcEndpoint, config = ethereumConfig.clientConfig);
@http:ServiceConfig { basePath:"/",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
service chainPage on blockChainInterfaceEP {
public function constructRequest (string jsonRPCVersion, int networkId, string method, json params) returns http:Request {
http:Request request = new;
request.setHeader("Content-Type", "application/json");
request.setJsonPayload({"jsonrpc":jsonRPCVersion, "id":networkId, "method":method, "params":params});
return request;
}
public function resultToString(json jsonPayload) returns string {
string result = jsonPayload["result"] != null ? jsonPayload["result"].toString() : "";
return result;
}
public function setResponseError(json jsonResponse) returns error {
map<string> details = { message: jsonResponse["error"].message.toString() };
error err = error("(wso2/ethereum)EthereumError", details);
return err;
}
@http:ResourceConfig {
methods:["POST"],
path:"/",
cors: {
allowOrigins: ["*"]
}
}
resource function respond(http:Caller caller, http:Request req, string name, string message) returns error? {
var requestVariableMap = check req.getFormParams();
string encryptedval = requestVariableMap["encryptedval"] ?: "";
string username = requestVariableMap["username"] ?: "";
var randKey = sessionMap[username] ?: "";
string decryptedval = utils:decryptAes(encryptedval, randKey);
if (decryptedval == "ack") {
authenticatedMap[username] = true;
} else{
authenticatedMap[username] = false;
}
return;
}
@http:ResourceConfig {
methods:["GET"],
path:"/",
cors: {
allowOrigins: ["*"]
}
}
resource function sayHello(http:Caller caller, http:Request req, string name, string message) {
string resultBuffer = "";
map<string> requestVariableMap = req. getQueryParams();
var logoutFlag = requestVariableMap["logout"] ?: "false";
boolean flg = boolean.convert(logoutFlag);
string uname = requestVariableMap["username"] ?: "";
string hostname = "localhost";
if (caller.localAddress.host != "") {
hostname= caller.localAddress.host;
}
string buffer = "http://" + hostname + ":9093";
if (flg) {
authenticatedMap[uname] = false;
sessionMap[uname] = "";
io:println(uname + " logged out.");
http:Response res = new;
res.setPayload(untaint buffer);
res.setContentType("text/html; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
return;
}
string functionToCall = functionMap[uname] ?: "";
http:Request request = new;
request.setHeader("Content-Type", "application/json");
request.setJsonPayload({"jsonrpc":"2.0", "id":"2000", "method":"eth_call", "params":[{"from": "0x7fb124c09dc0fc5e7f1028b37d06c893bbc71b5b", "to":"0x03c3a8b5266bf2e0cba7aa0a1df4e232418f6b47", "data": functionToCall}, "latest"]});
string finalResult = "";
boolean errorFlag = false;
var httpResponse = ethereumClient -> post("/", request);
if (httpResponse is http:Response) {
int statusCode = httpResponse.statusCode;
var jsonResponse = httpResponse.getJsonPayload();
if (jsonResponse is json) {
if (jsonResponse["error"] == null) {
string inputString = jsonResponse.result.toString();
finalResult = convertHexStringToString(inputString);
} else {
error err = error("(wso2/ethereum)EthereumError",
{ message: "Error occurred while accessing the JSON payload of the response" });
finalResult = jsonResponse["error"].toString();
errorFlag = true;
}
} else {
error err = error("(wso2/ethereum)EthereumError",
{ message: "Error occurred while accessing the JSON payload of the response" });
finalResult = jsonResponse.reason();
errorFlag = true;
}
} else {
error err = error("(wso2/ethereum)EthereumError", { message: "Error occurred while invoking the Ethererum API" });
errorFlag = true;
}
if (!errorFlag) {
string hashKey = untaint finalResult;
io:ReadableCharacterChannel sourceChannel = new (io:openReadableFile("key-db/" + untaint uname + "/" + hashKey), "UTF-8");
var readableRecordsChannel = new io:ReadableTextRecordChannel(sourceChannel, fs = ",", rs = "\n");
while (readableRecordsChannel.hasNext()) {
var result = readableRecordsChannel.getNext();
if (result is string[]) {
string randKey = generateRandomKey(16);
sessionMap[uname] = randKey;
finalResult = utils:encryptRSAWithPublicKey(result[0], randKey);
}
}
} else {
io:println("An error has ocurred.");
}
http:Response res = new;
// A util method that can be used to set string payload.
res.setPayload(untaint finalResult);
res.setContentType("text/html; charset=utf-8");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE");
res.setHeader("Access-Control-Allow-Headers", "Authorization, Lang");
// Sends the response back to the client.
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
}
@http:WebSocketServiceConfig {
path: "/basic/ws",
subProtocols: ["xml", "json"],
idleTimeoutInSeconds: 120
}
service basic on new http:WebSocketListener(9095) {
string ping = "ping";
byte[] pingData = ping.toByteArray("UTF-8");
resource function onOpen(http:WebSocketCaller caller) {
var err = caller->pushText(chatBuffer);
if (err is error) {
log:printError("Error occurred when sending text", err = err);
}
}
resource function onText(http:WebSocketCaller caller, string text,
boolean finalFrame) {
if (text == "ping") {
io:println("Pinging...");
var err = caller->ping(self.pingData);
if (err is error) {
log:printError("Error sending ping", err = err);
}
} else if (text == "closeMe") {
_ = caller->close(statusCode = 1001,
reason = "You asked me to close the connection",
timeoutInSecs = 0);
} else {
chatBuffer += untaint text + "\r\n";
var err = caller->pushText(chatBuffer);
if (err is error) {
log:printError("Error occurred when sending text", err = err);
}
io:println("Pinging...");
var err2 = caller->ping(self.pingData);
if (err2 is error) {
log:printError("Error sending ping", err = err);
}
}
}
resource function onBinary(http:WebSocketCaller caller, byte[] b) {
io:println("\nNew binary message received");
io:print("UTF-8 decoded binary message: ");
io:println(b);
var err = caller->pushBinary(b);
if (err is error) {
log:printError("Error occurred when sending binary", err = err);
}
}
resource function onPing(http:WebSocketCaller caller, byte[] data) {
var err = caller->pong(data);
if (err is error) {
log:printError("Error occurred when closing the connection",
err = err);
}
}
resource function onPong(http:WebSocketCaller caller, byte[] data) {
io:println("Pong received");
}
resource function onIdleTimeout(http:WebSocketCaller caller) {
io:println("\nReached idle timeout");
io:println("Closing connection " + caller.id);
var err = caller->close(statusCode = 1001, reason =
"Connection timeout");
if (err is error) {
log:printError("Error occured when closing the connection",
err = err);
}
}
resource function onError(http:WebSocketCaller caller, error err) {
log:printError("Error occurred ", err = err);
}
resource function onClose(http:WebSocketCaller caller, int statusCode,
string reason) {
io:println(string `Client left with {{statusCode}} because
{{reason}}`);
}
}
public function convertHexStringToString(string inputString) returns (string) {
int len = inputString.length();
int counter = 0;
string buffer = "";
while (counter < len) {
string s3 = inputString.substring(counter, counter+2);
if (s3.equalsIgnoreCase("0x")) {
counter += 2;
continue;
}
var res1 = utils:hexToChar(s3);
if (res1.length() != 0) {
buffer += res1;
}
counter += 2;
}
return buffer;
}
public function generateRandomKey(int keyLen) returns (string){
string buffer = "";
int counter = 0;
while (counter < keyLen) {
buffer += utils:decToChar(65 + math:randomInRange(0, 26));
counter += 1;
}
return buffer;
}