-
Notifications
You must be signed in to change notification settings - Fork 3
/
GovID.bal
756 lines (648 loc) · 28.5 KB
/
GovID.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// 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 ballerina/mysql;
import ballerina/sql;
import wso2/ethereum;
import wso2/utils;
import ballerina/runtime;
import ballerina/time;
listener http:Listener uiGovIDLogin = new(9090);
mysql:Client ssiDB = new({
host: "192.168.32.1",
port: 3306,
name: "ssidb",
username: "test",
password: "test",
dbOptions: { useSSL: false }
});
map<string> sessionMap = {};
map<boolean> authenticatedMap = {};
map<string> userMap = {"carol": "123", "dion" : "456"};
string chatBuffer = "";
string pk = "";
string randomKey = "";
ethereum:EthereumConfiguration ethereumConfig = {
jsonRpcEndpoint: "http://192.168.32.1:8081",
jsonRpcVersion: "2.0",
networkId: "2000"
};
string ethereumAccount = "0xee0727d9e94dbb230233ea4bd362e4c081aabf38";
string jsonRpcEndpoint = ethereumConfig.jsonRpcEndpoint;
http:Client ethereumClient = new(jsonRpcEndpoint, config = ethereumConfig.clientConfig);
@http:ServiceConfig { basePath:"/",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
service uiServiceGovIDLogin on uiGovIDLogin {
@http:ResourceConfig {
methods:["GET"],
path:"/",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function displayLoginPage(http:Caller caller, http:Request req, string name, string message) {
string buffer = readFile("web/govid-login.html");
http:Response res = new;
if (caller.localAddress.host != "") {
buffer= buffer.replace("localhost", caller.localAddress.host);
}
res.setPayload(untaint buffer);
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");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["POST"],
path:"/authenticate",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function processLogin(http:Caller caller, http:Request req, string name, string message) returns error?{
var requestVariableMap = check req.getFormParams();
string username = requestVariableMap["username"] ?: "";
string password = requestVariableMap["pwd"] ?: "";
var authenticated = false;
foreach var x in userMap {
if (username.equalsIgnoreCase(x[0]) && password.equalsIgnoreCase(x[1])) {
io:println("Welcome " + username);
authenticatedMap[username] = true;
var result = caller->respond("success");
if (result is error) {
log:printError("Error sending response", err = result);
} else {
authenticated = true;
}
break;
}
}
if (!authenticated) {
var result = caller->respond("failed");
if (result is error) {
log:printError("Error sending response", err = result);
}
}
return;
}
@http:ResourceConfig {
methods:["GET"],
path:"/home",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function displayLoginPage2(http:Caller caller, http:Request req, string name, string message) returns error? {
var requestVariableMap = req.getQueryParams();
string username = requestVariableMap["username"] ?: "";
if ((!(username.equalsIgnoreCase(""))) && authenticatedMap[username] == true) {
string buffer = readFile("web/govid-request.html");
http:Response res = new;
if (caller.localAddress.host != "") {
buffer= buffer.replace("localhost", caller.localAddress.host);
buffer= buffer.replace("EMPTYUNAME", username);
}
res.setPayload(untaint buffer);
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");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
} else {
io:println("Error login");
string buffer = readFile("web/error.html");
string hostname = "localhost";
if (caller.localAddress.host != "") {
hostname= caller.localAddress.host;
}
buffer = buffer.replace("MSG", "Re-try login via : <a href='http://" + caller.localAddress.host + ":9090'>Login Page</a>");
http:Response res = new;
res.setPayload(untaint buffer);
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");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
}
@http:ResourceConfig {
methods:["GET"],
path:"/govid-request.css",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function sendGOVIDRequest(http:Caller caller, http:Request req, string name, string message) {
http:Response res = new;
res.setFileAsPayload("web/govid-request.css", contentType = "text/css");
res.setContentType("text/css; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["GET"],
path:"/bundle.js",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function sendBundle(http:Caller caller, http:Request req, string name, string message) {
http:Response res = new;
res.setFileAsPayload("web/bundle.js", contentType = "text/javascript");
res.setContentType("text/javascript; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["GET"],
path:"/FileSaver.js",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function sendFileSaver(http:Caller caller, http:Request req, string name, string message) {
http:Response res = new;
res.setFileAsPayload("web/FileSaver.js", contentType = "text/javascript");
res.setContentType("text/javascript; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["POST"],
path:"/submit",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function processRegistration(http:Caller caller, http:Request req, string name, string message) returns error?{
var requestVariableMap = check req.getFormParams();
string firstName = requestVariableMap["firstName"] ?: "";
string lastName = requestVariableMap["lastName"] ?: "";
string streetAddress = requestVariableMap["streetAddress"] ?: "";
string city = requestVariableMap["city"] ?: "";
string state = requestVariableMap["state"] ?: "";
string postcode = requestVariableMap["postcode"] ?: "";
string country = requestVariableMap["country"] ?: "";
string date = requestVariableMap["date"] ?: "";
string did = requestVariableMap["did"] ?: "";
int index = did.indexOf("\"id\": \"did:ethr:") + 16;
did = did.substring(index, index+64);
//io:println(("insert into ssidb.govid(firstname, lastname, streetaddress, city, state, country, postcode, dob ,did) " + "values ('"+ firstName +"', '" + lastName + "', '" + streetAddress + "', '"+ city +"', '"+ state +"', '" + postcode + "', '" + country + "', '" + date + "', '" + did + "');"));
var selectRet = ssiDB->select(untaint "select country, firstname from ssidb.govid where (did LIKE '"+ untaint did +"');", ());
if (selectRet is table<record {}>) {
if (selectRet.hasNext()) {
var result = caller->respond("did-exists");
if (result is error) {
log:printError("Error sending response", err = result);
}
return;
} else {
var ret = ssiDB->update(untaint ("insert into ssidb.govid(firstname, lastname, streetaddress, city, state, postcode, country, dob, did) " + "values ('"+ firstName +"', '" + lastName + "', '" + streetAddress + "', '"+ city +"', '"+ state +"', '" + postcode + "', '" + country + "','" + date + "', '" + did + "');"));
var result = caller->respond("done");
if (result is error) {
log:printError("Error sending response", err = result);
}
return;
}
}
}
@http:ResourceConfig {
methods:["GET"],
path:"/logout",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function logout(http:Caller caller, http:Request req, string name, string message) {
var requestVariableMap = req.getQueryParams();
string username = requestVariableMap["username"] ?: "";
if ((!(username.equalsIgnoreCase(""))) && authenticatedMap[username] == true) {
authenticatedMap[username] = false;
}
string buffer = readFile("web/govid-login.html");
http:Response res = new;
if (caller.localAddress.host != "") {
buffer= buffer.replace("localhost", caller.localAddress.host);
}
res.setPayload(untaint buffer);
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");
var result = caller->redirect(res, http:REDIRECT_TEMPORARY_REDIRECT_307,
["http://localhost:9090/"]);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["POST"],
path:"/api",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function api(http:Caller caller, http:Request req, string name, string message) returns error?{
var requestVariableMap = check req.getFormParams();
if (requestVariableMap["command"] == "cmd1") {
if (requestVariableMap.hasKey("secureToken") && (!randomKey.equalsIgnoreCase(requestVariableMap["secureToken"] ?: ""))) {
io:println("incorrect sec token");
var result = caller->respond("incorrect-token");
if (result is error) {
log:printError("Error sending response", err = result);
}
} else {
if (requestVariableMap.hasKey("did")) {
pk = untaint requestVariableMap["did"] ?: "";
var result = caller->respond("done");
if (result is error) {
log:printError("Error sending response", err = result);
}
}
}
} else if (requestVariableMap["command"] == "requestvc") {
var did = requestVariableMap["did"] ?: "";
did = did.replace("%2C", ",");
did = utils:binaryStringToString(did);
int index2 = did.indexOf("\"id\": \"did:ethr:") + 16;
string didmid = did.substring(index2, index2+64);
index2 = did.indexOf("-----BEGIN PUBLIC KEY-----") + 26;
int index3 = did.indexOf("-----END PUBLIC KEY-----");
var publicKey = did.substring(index2, index3);
didmid = "0x" + didmid;
http:Request request = new;
request.setHeader("Content-Type", "application/json");
request.setJsonPayload({"jsonrpc":"2.0", "id":"2000", "method":"eth_getTransactionByHash", "params":[untaint didmid]});
string finalResult = "";
string pkHash = "";
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) {
finalResult = jsonResponse.result.toString();
pkHash = jsonResponse.result["input"].toString();
} 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;
}
string hexEncodedString = "0x" + utils:hashSHA256("-----BEGIN PUBLIC KEY-----" + publicKey + "-----END PUBLIC KEY-----");
if (hexEncodedString == pkHash) {
string randKey = generateRandomKey(16);
sessionMap[didmid] = randKey;
finalResult = utils:encryptRSAWithPublicKey(publicKey, randKey);
} else {
finalResult = "Failure in Key Verification";
}
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);
}
} else if (requestVariableMap["command"] == "encresponse") {
var did = requestVariableMap["did"] ?: "";
var encryptedval = requestVariableMap["encryptedval"] ?: "";
//var publicKey = requestVariableMap["publicKey"] ?: "";
did = did.replace("%2C", ",");
did = utils:binaryStringToString(did);
int index2 = did.indexOf("\"id\": \"did:ethr:") + 16;
string didmid = did.substring(index2, index2+64);
index2 = did.indexOf("-----BEGIN PUBLIC KEY-----") + 26;
int index3 = did.indexOf("-----END PUBLIC KEY-----");
var publicKey = did.substring(index2, index3);
var didmidOrg = didmid;
didmid = "0x" + didmid;
string randKey = sessionMap[didmid]?: "";
if (encryptedval === randKey) {
var verifiableCredentialsList = getVerifiableCredentials(didmidOrg);
http:Response res = new;
// A util method that can be used to set string payload.
res.setPayload(untaint verifiableCredentialsList);
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);
}
} else {
io:println("Challenge response authentication failed.");
}
}
}
@http:ResourceConfig {
methods:["POST"],
path:"/secureTokenAPI",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function secTokenAPI(http:Caller caller, http:Request req, string name, string message) returns error?{
var requestVariableMap = check req.getFormParams();
if (requestVariableMap.hasKey("randomKey")) {
randomKey = untaint requestVariableMap["randomKey"] ?: "";
}
var result = caller->respond("done");
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) {
while(true) {
var err = caller->pushText(pk);
if (err is error) {
log:printError("Error occurred when sending text", err = err);
}
runtime:sleep(1000);
}
}
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") {
var err = 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");
}
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 readFile (string filePath) returns (string) {
io:ReadableByteChannel readableByteChannel = io:openReadableFile(filePath);
var readableCharChannel = new io:ReadableCharacterChannel(readableByteChannel, "UTF-8");
var readableRecordsChannel = new io:ReadableTextRecordChannel(readableCharChannel);
string buffer = "";
while (readableRecordsChannel.hasNext()) {
var result = readableRecordsChannel.getNext();
if (result is string[]) {
string item = string.convert(result[0]);
buffer += item;
} else {
io:println("Error");
}
}
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;
}
public function getVerifiableCredentials(string didmid) returns (string) {
time: Time currentTime = time:currentTime();
string customTimeString = currentTime.format("dd-MM-yyyy");
string country = "";
string firstname = "";
var selectRet = ssiDB->select(untaint "select country, firstname from ssidb.govid where (did LIKE '"+ untaint didmid +"');", ());
if (selectRet is table<record {}>) {
io:println("\nConvert the table into json");
var jsonConversionRet = json.convert(selectRet);
if (jsonConversionRet is json) {
if (jsonConversionRet.length() > 0) {
country = jsonConversionRet[0]["country"].toString();
firstname = jsonConversionRet[0]["firstname"].toString();
} else {
return "none";
}
}
}
string finalResult = sendTransactionAndgetHash(country);
string countryCredential = "|||" + didmid + "," + finalResult + ",http://ip6-localhost:9090/api,CountryCredential" + "||| " + "{
// set the context, which establishes the special terms we will be using
// such as 'issuer' and 'alumniOf'.
\"@context\": [
\"https://www.w3.org/2018/credentials/v1\",
\"https://www.w3.org/2018/credentials/examples/v1\"
],
// specify the identifier for the credential
\"id\": \"http://localhost:9090/credentials/1\",
// the credential types, which declare what data to expect in the credential
\"type\": [\"VerifiableCredential\", \"CountryCredential\"],
// the entity that issued the credential
\"issuer\": \"http://ip6-localhost:9090/api\",
// when the credential was issued
\"issuanceDate\": \"" + customTimeString + "\",
// claims about the subjects of the credential
\"credentialSubject\": {
// identifier for the only subject of the credential
\"id\": \"did:ethr:" + didmid + "\",
// assertion about the only subject of the credential
\"homeCountry\": {
\"id\": \"did:ethr:" + finalResult + "\",
\"name\": [{
\"value\": \"home country\",
\"lang\": \"en\"
}, {
\"value\": \"" + country + "\",
\"lang\": \"en\"
}]
}
},
// digital proof that makes the credential tamper-evident
// see the NOTE at end of this section for more detail
\"proof\": {
// the cryptographic signature suite that was used to generate the signature
\"type\": \"RsaSignature2018\",
// the date the signature was created
\"created\": \"2017-06-18T21:19:10Z\",
// purpose of this proof
\"proofPurpose\": \"assertionMethod\",
// the identifier of the public key that can verify the signature
\"verificationMethod\": \"https://example.edu/issuers/keys/1\",
// the digital signature value
\"jws\": \"eyJhbGciOiJSUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..TCYt5X
sITJX1CxPCT8yAV-TVkIEq_PbChOMqsLfRoPsnsgw5WEuts01mq-pQy7UJiN5mgRxD-WUc
X16dUEMGlv50aqzpqh4Qktb3rk-BuQy72IFLOqV0G_zS245-kronKb78cPN25DGlcTwLtj
PAYuNzVBAh4vGHSrQyHUdBBPM\"
}
}";
return countryCredential;
}
public function sendTransactionAndgetHash(string data) returns (string) {
http:Request request2 = new;
request2.setHeader("Content-Type", "application/json");
request2.setJsonPayload({"jsonrpc":"2.0", "id":"2000", "method":"personal_unlockAccount", "params": [ethereumAccount,"1234",null]});
string finalResult2 = "";
boolean errorFlag2 = false;
var httpResponse2 = ethereumClient -> post("/", request2);
if (httpResponse2 is http:Response) {
int statusCode = httpResponse2.statusCode;
var jsonResponse = httpResponse2.getJsonPayload();
if (jsonResponse is json) {
if (jsonResponse["error"] == null) {
finalResult2 = jsonResponse.result.toString();
} else {
error err = error("(wso2/ethereum)EthereumError",
{ message: "Error occurred while accessing the JSON payload of the response" });
finalResult2 = jsonResponse["error"].toString();
errorFlag2 = true;
}
} else {
error err = error("(wso2/ethereum)EthereumError",
{ message: "Error occurred while accessing the JSON payload of the response" });
finalResult2 = jsonResponse.reason();
errorFlag2 = true;
}
} else {
error err = error("(wso2/ethereum)EthereumError", { message: "Error occurred while invoking the Ethererum API" });
errorFlag2 = true;
}
string hexEncodedString = "0x" + utils:hashSHA256(data);
http:Request request = new;
request.setHeader("Content-Type", "application/json");
request.setJsonPayload({"jsonrpc":"2.0", "id":"2000", "method":"eth_sendTransaction", "params":[{"from": ethereumAccount, "to":"0x6814412628addef8989ee696a67b0fad5d62735e", "data": hexEncodedString}]});
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) {
finalResult = jsonResponse.result.toString();
} 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;
}
return finalResult;
}