Skip to content

Commit 920848c

Browse files
Merge pull request #20 from com-chain/check_IsActive
Check is active
2 parents 2364479 + a4534a2 commit 920848c

File tree

3 files changed

+112
-17
lines changed

3 files changed

+112
-17
lines changed

api.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,13 @@ function storeTransaction($is_valid_shop, $transaction_ash, $web_hook_status, $a
333333
$query = "INSERT INTO testtransactions (".join(', ',array_keys($fields));
334334
$query = $query.',time) VALUES ('.join(', ',$val).','.time().')';
335335

336-
$cluster = Cassandra::cluster('127.0.0.1') ->withCredentials("transactions_rw", "Private_access_transactions")->build();
337336
$keyspace = 'comchain';
338-
$session1 = $cluster->connect($keyspace);
339-
340-
$session1->execute(new Cassandra\SimpleStatement($query), array('arguments' => $fields));
337+
// for pledge only the other direction is inserted
338+
if ($trans_type!='Pledge') {
339+
$cluster = Cassandra::cluster('127.0.0.1') ->withCredentials("transactions_rw", "Private_access_transactions")->build();
340+
$session1 = $cluster->connect($keyspace);
341+
$session1->execute(new Cassandra\SimpleStatement($query), array('arguments' => $fields));
342+
}
341343

342344
$cluster2 = Cassandra::cluster('127.0.0.1') ->withCredentials("transactions_rw", "Private_access_transactions")->build();
343345
$session2 = $cluster2->connect($keyspace);
@@ -378,6 +380,7 @@ function sendRawTransaction($rawtx,$gethRPC){
378380
// Direct From On Behalf Accept Request
379381
$transfert_NA_functions = ['a5f7c148','58258353','1b6b1ee5','132019f4'];
380382
$transfert_CM_functions = ['60ca9c4c','2ef9ade2','74c421fe','1415707c'];
383+
$pledge_function = '6c343eef';
381384
$transfert_functs = array_merge($transfert_NA_functions,$transfert_CM_functions);
382385

383386
$lock_error = 'Account_Locked_Error';
@@ -421,9 +424,10 @@ function sendRawTransaction($rawtx,$gethRPC){
421424
// get the amount
422425
$amount = hexdec(substr($tr_info,-64));
423426

424-
// get the infos
425-
$from_status = getAccStatus($from_add, $contract);
426-
$to_status = getAccStatus($to_add, $contract);
427+
// get the infos
428+
$status = getAccountStatus(array($from_add, $to_add), $contract);
429+
$from_status = $status[$from_add];
430+
$to_status = $status[$to_add];
427431

428432

429433
$from_Nt_bal = getNTBalance($from_add, $contract);
@@ -481,15 +485,18 @@ function sendRawTransaction($rawtx,$gethRPC){
481485
$contract = getContract1(substr($tr_info,0,40));
482486
$contract2 = '0x'.substr($tr_info,0,40);
483487

488+
// get the dest
489+
$dest = '0x'.substr($tr_info,78,40);
490+
// get the amount
491+
$amount = hexdec(substr($tr_info,-64));
484492

485493
if (in_my_array($funct_address, $transfert_functs)) {
486-
// get the dest
487-
$dest = '0x'.substr($tr_info,78,40);
488-
// get the amount
489-
$amount = hexdec(substr($tr_info,-64));
494+
495+
496+
$status = getAccountStatus(array($sender, $dest), $contract);
497+
$from_status = $status[$sender];
498+
$to_status = $status[$dest];
490499

491-
$from_status = getAccStatus($sender, $contract);
492-
$to_status = getAccStatus($dest, $contract);
493500
if ($from_status==1 && $to_status==1) {
494501
if ($funct_address==$transfert_NA_functions[0] ||
495502
$funct_address==$transfert_CM_functions[0] ||
@@ -582,8 +589,18 @@ function sendRawTransaction($rawtx,$gethRPC){
582589
throw new Exception($lock_error);
583590
}
584591

585-
}
592+
} else if ($pledge_function == $funct_address) {
593+
// We have a pledge:
594+
$trans_type = 'Pledge';
595+
$from_add = 'Admin';
596+
$to_add = $dest;
586597

598+
$acctype = getAccType($sender, $contract);
599+
$status = getAccountStatus(array($sender), $contract);
600+
$curr_stat= $status[$sender];
601+
602+
$need_pending = $acctype==2 && $curr_stat==1;
603+
}
587604
}
588605

589606

@@ -607,9 +624,11 @@ function sendRawTransaction($rawtx,$gethRPC){
607624
$wh_status = 2;
608625
}
609626
}
610-
storeAdditionalData(strlen($shop_url)>0, $data['data'], $wh_status);
627+
// Storing the memos, shop and/or delegate
628+
storeAdditionalData(strlen($shop_url)>0, $data['data'], $wh_status);
611629

612630
if ($need_pending) {
631+
// adding pending transaction
613632
storeTransaction(strlen($shop_url)>0, $data['data'], $wh_status, $amount, $from_add, $to_add, $trans_type);
614633
}
615634

checkAdmin.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,56 @@ function getAccStatus($address, $contract){
8282
}
8383

8484

85+
function isActive($address, $contract){
86+
$url = getServerAddress()."/api.php";
87+
$ch = curl_init();
88+
$ethCall = ['to' =>$contract,
89+
'data' => '0x9f8a13d7000000000000000000000000'.substr($address,2)
90+
];
91+
$fields = ['ethCall'=>$ethCall];
92+
$fields_string = http_build_query($fields);
93+
94+
curl_setopt($ch, CURLOPT_URL, $url);
95+
// Set so curl_exec returns the result instead of outputting it.
96+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
97+
curl_setopt($ch, CURLOPT_POST, count($fields));
98+
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
99+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
100+
101+
// Get the response and close the channel.
102+
$response = curl_exec($ch);
103+
curl_close($ch);
104+
105+
$json = json_decode($response);
106+
$data= $json->{'data'};
107+
108+
return substr($data,-1);
109+
}
110+
111+
function getVersion( $contract){
112+
$url = getServerAddress()."/api.php";
113+
$ch = curl_init();
114+
$ethCall = ['to' =>$contract,
115+
'data' => '0x54fd4d50'
116+
];
117+
$fields = ['ethCall'=>$ethCall];
118+
$fields_string = http_build_query($fields);
119+
120+
curl_setopt($ch, CURLOPT_URL, $url);
121+
// Set so curl_exec returns the result instead of outputting it.
122+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
123+
curl_setopt($ch, CURLOPT_POST, count($fields));
124+
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
125+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
126+
127+
// Get the response and close the channel.
128+
$response = curl_exec($ch);
129+
curl_close($ch);
130+
131+
$json = json_decode($response);
132+
$data= $json->{'data'};
133+
134+
return $data;
85135

86136
function getNumber($address, $contract, $function){
87137
$url = getServerAddress()."/api.php";
@@ -170,6 +220,27 @@ function checkSign($dat, $signature, $caller){
170220
return $caller==personal_ecRecover($dat, $signature);
171221
}
172222

223+
function getAccountStatus($addresses, $contract) {
224+
$version = getVersion($contract);
225+
$result= array();
226+
foreach ( $addresses as $add) {
227+
$result[$add] = 0;
228+
}
229+
if (strlen(strval($version))>2) {
230+
// New Contract use isActive
231+
foreach ( $addresses as $add) {
232+
$result[$add] = isActive($add, $contract);
233+
}
234+
} else {
235+
// Old contract fallback on getAccStatus
236+
foreach ( $addresses as $add) {
237+
$result[$add] = getAccStatus($add, $contract);
238+
}
239+
}
240+
241+
return $result;
242+
}
243+
173244

174245
function checkLegitimateAdmin($dat, $signature, $caller, $server){
175246
$result = false;
@@ -182,7 +253,8 @@ function checkLegitimateAdmin($dat, $signature, $caller, $server){
182253

183254
// Get the caller type and status
184255
$acctype = getAccType($caller, $contract);
185-
$accStatus = getAccStatus($caller, $contract);
256+
$status =getAccountStatus(array($caller), $contract);
257+
$accStatus = $status[$caller];
186258

187259
if ($acctype==2 && $accStatus==1){
188260
$result = true;

ecrecover_helper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ function personal_ecRecoverPublic($msg, $signed) {
5555
$hex = keccak256WithPrefix($personal_prefix_msg);
5656
return ecRecoverPublic($hex, $signed);
5757
}
58+
59+
function keyToAddress($publicKeyString) {
60+
return '0x'. substr(keccak256WithPrefix(hex2bin($publicKeyString)), -40);
61+
}
5862

5963
function ecRecoverPublic($hex, $signed) {
6064
$rHex = substr($signed, 2, 64);
@@ -81,7 +85,7 @@ function ecRecoverPublic($hex, $signed) {
8185
$publicKey = Signature::recoverPublicKey($rGmp, $sGmp, $messageGmp, $recovery);
8286
$publicKeyString = $publicKey["x"] . $publicKey["y"];
8387

84-
return array('0x'. substr(keccak256WithPrefix(hex2bin($publicKeyString)), -40),$publicKeyString);
88+
return array(keyToAddress($publicKeyString),$publicKeyString);
8589
}
8690

8791
function strToHex($string)

0 commit comments

Comments
 (0)