Skip to content

Commit c3560fd

Browse files
committed
Add the API for the request messages
1 parent 2379585 commit c3560fd

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

requestMessages.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
require_once './Keccak.php';
3+
require_once './ecrecover_helper.php';
4+
use kornrunner\Keccak;
5+
6+
header('Access-Control-Allow-Origin: *');
7+
/*
8+
9+
UseCases:
10+
Add a reference:
11+
12+
POST with:
13+
data = {'add_req'='0x123...', 'add_to'='0x123...', 'ref_to'='0x123...', 'ref_req'='0x123...'}
14+
sign = 0x123..
15+
16+
Read a reference :
17+
GET with:
18+
add_req = 0x123..
19+
add_to = 0x123..
20+
*/
21+
22+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
23+
// POST case
24+
25+
// Check signature
26+
$input_data = $_POST['data'];
27+
$input_obj = json_decode($_POST['data']);
28+
29+
$input_sign = $_POST['sign'];
30+
$ec_recover_result = personal_ecRecover($input_data, $input_sign);
31+
if ($ec_recover_result !== $input_obj->{'add_req'}){
32+
// wrong signature
33+
exit("Bye!");
34+
}
35+
36+
// insert data
37+
$add_from = $ec_recover_result;
38+
$add_to = preg_replace("/[^a-zA-Z0-9]+/", "", $input_obj->{'add_cli'});
39+
$ref_from = preg_replace("/[^a-zA-Z0-9]+/", "", $input_obj->{'ref_req'});
40+
$ref_to = preg_replace("/[^a-zA-Z0-9]+/", "", $input_obj->{'ref_cli'});
41+
42+
43+
$session = getDBSession();
44+
$query = "INSERT INTO request_reference (add_from, add_to, ref_from, ref_to) VALUES (?,?,?,?)";
45+
$options = array('arguments' => array($add_from, $add_to, $ref_from, $ref_to));
46+
47+
$session->execute(new Cassandra\SimpleStatement($query), $options);
48+
echo '{"result":"OK"}';
49+
50+
51+
} else {
52+
// GET case
53+
54+
// Check inputs
55+
$addr_from = strtolower(preg_replace("/[^a-zA-Z0-9]+/", "", $_GET['add_req']));
56+
if (strlen($addr_from) != 42) {
57+
exit("Bye!");
58+
}
59+
$addr_cli = strtolower(preg_replace("/[^a-zA-Z0-9]+/", "", $_GET['add_cli']));
60+
if (strlen($addr_cli) != 42) {
61+
exit("Bye!");
62+
}
63+
64+
// get the data from the DB
65+
$session = getDBSession();
66+
67+
$query = "SELECT ref_from, ref_to FROM request_reference WHERE add_from = '$addr_from' and add_to = '$addr_cli'";
68+
69+
70+
// the address is a primary key it should be only 0 or 1 row
71+
$counter=0;
72+
foreach ($session->execute(new Cassandra\SimpleStatement($query)) as $row) {
73+
$string[$counter] = json_encode($row);
74+
$counter++;
75+
}
76+
77+
// Return empty object if address pait is not found
78+
isset($string) or exit("[]");
79+
80+
// return the keys
81+
echo $string[0];
82+
}
83+
84+
85+
86+
87+
/*
88+
FUNCTIONS
89+
*/
90+
91+
function getDBSession() {
92+
$cluster = Cassandra::cluster('127.0.0.1') ->withCredentials("webhook_rw", "Private_access_transactions")->build();
93+
$keyspace = 'comchain';
94+
return $cluster->connect($keyspace);
95+
}
96+
97+
?>

0 commit comments

Comments
 (0)