diff --git a/api.php b/api.php index f58f163..6a3f1d0 100644 --- a/api.php +++ b/api.php @@ -333,11 +333,13 @@ function storeTransaction($is_valid_shop, $transaction_ash, $web_hook_status, $a $query = "INSERT INTO testtransactions (".join(', ',array_keys($fields)); $query = $query.',time) VALUES ('.join(', ',$val).','.time().')'; - $cluster = Cassandra::cluster('127.0.0.1') ->withCredentials("transactions_rw", "Private_access_transactions")->build(); $keyspace = 'comchain'; - $session1 = $cluster->connect($keyspace); - - $session1->execute(new Cassandra\SimpleStatement($query), array('arguments' => $fields)); + // for pledge only the other direction is inserted + if ($trans_type!='Pledge') { + $cluster = Cassandra::cluster('127.0.0.1') ->withCredentials("transactions_rw", "Private_access_transactions")->build(); + $session1 = $cluster->connect($keyspace); + $session1->execute(new Cassandra\SimpleStatement($query), array('arguments' => $fields)); + } $cluster2 = Cassandra::cluster('127.0.0.1') ->withCredentials("transactions_rw", "Private_access_transactions")->build(); $session2 = $cluster2->connect($keyspace); @@ -378,6 +380,7 @@ function sendRawTransaction($rawtx,$gethRPC){ // Direct From On Behalf Accept Request $transfert_NA_functions = ['a5f7c148','58258353','1b6b1ee5','132019f4']; $transfert_CM_functions = ['60ca9c4c','2ef9ade2','74c421fe','1415707c']; + $pledge_function = '6c343eef'; $transfert_functs = array_merge($transfert_NA_functions,$transfert_CM_functions); $lock_error = 'Account_Locked_Error'; @@ -421,9 +424,10 @@ function sendRawTransaction($rawtx,$gethRPC){ // get the amount $amount = hexdec(substr($tr_info,-64)); - // get the infos - $from_status = getAccStatus($from_add, $contract); - $to_status = getAccStatus($to_add, $contract); + // get the infos + $status = getAccountStatus(array($from_add, $to_add), $contract); + $from_status = $status[$from_add]; + $to_status = $status[$to_add]; $from_Nt_bal = getNTBalance($from_add, $contract); @@ -481,15 +485,18 @@ function sendRawTransaction($rawtx,$gethRPC){ $contract = getContract1(substr($tr_info,0,40)); $contract2 = '0x'.substr($tr_info,0,40); + // get the dest + $dest = '0x'.substr($tr_info,78,40); + // get the amount + $amount = hexdec(substr($tr_info,-64)); if (in_my_array($funct_address, $transfert_functs)) { - // get the dest - $dest = '0x'.substr($tr_info,78,40); - // get the amount - $amount = hexdec(substr($tr_info,-64)); + + + $status = getAccountStatus(array($sender, $dest), $contract); + $from_status = $status[$sender]; + $to_status = $status[$dest]; - $from_status = getAccStatus($sender, $contract); - $to_status = getAccStatus($dest, $contract); if ($from_status==1 && $to_status==1) { if ($funct_address==$transfert_NA_functions[0] || $funct_address==$transfert_CM_functions[0] || @@ -582,8 +589,18 @@ function sendRawTransaction($rawtx,$gethRPC){ throw new Exception($lock_error); } - } + } else if ($pledge_function == $funct_address) { + // We have a pledge: + $trans_type = 'Pledge'; + $from_add = 'Admin'; + $to_add = $dest; + $acctype = getAccType($sender, $contract); + $status = getAccountStatus(array($sender), $contract); + $curr_stat= $status[$sender]; + + $need_pending = $acctype==2 && $curr_stat==1; + } } @@ -607,9 +624,11 @@ function sendRawTransaction($rawtx,$gethRPC){ $wh_status = 2; } } - storeAdditionalData(strlen($shop_url)>0, $data['data'], $wh_status); + // Storing the memos, shop and/or delegate + storeAdditionalData(strlen($shop_url)>0, $data['data'], $wh_status); if ($need_pending) { + // adding pending transaction storeTransaction(strlen($shop_url)>0, $data['data'], $wh_status, $amount, $from_add, $to_add, $trans_type); } diff --git a/checkAdmin.php b/checkAdmin.php index 1259f09..b4ee016 100644 --- a/checkAdmin.php +++ b/checkAdmin.php @@ -82,6 +82,56 @@ function getAccStatus($address, $contract){ } +function isActive($address, $contract){ + $url = getServerAddress()."/api.php"; + $ch = curl_init(); + $ethCall = ['to' =>$contract, + 'data' => '0x9f8a13d7000000000000000000000000'.substr($address,2) + ]; + $fields = ['ethCall'=>$ethCall]; + $fields_string = http_build_query($fields); + + curl_setopt($ch, CURLOPT_URL, $url); + // Set so curl_exec returns the result instead of outputting it. + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, count($fields)); + curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + + // Get the response and close the channel. + $response = curl_exec($ch); + curl_close($ch); + + $json = json_decode($response); + $data= $json->{'data'}; + + return substr($data,-1); +} + +function getVersion( $contract){ + $url = getServerAddress()."/api.php"; + $ch = curl_init(); + $ethCall = ['to' =>$contract, + 'data' => '0x54fd4d50' + ]; + $fields = ['ethCall'=>$ethCall]; + $fields_string = http_build_query($fields); + + curl_setopt($ch, CURLOPT_URL, $url); + // Set so curl_exec returns the result instead of outputting it. + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, count($fields)); + curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + + // Get the response and close the channel. + $response = curl_exec($ch); + curl_close($ch); + + $json = json_decode($response); + $data= $json->{'data'}; + + return $data; function getNumber($address, $contract, $function){ $url = getServerAddress()."/api.php"; @@ -170,6 +220,27 @@ function checkSign($dat, $signature, $caller){ return $caller==personal_ecRecover($dat, $signature); } +function getAccountStatus($addresses, $contract) { + $version = getVersion($contract); + $result= array(); + foreach ( $addresses as $add) { + $result[$add] = 0; + } + if (strlen(strval($version))>2) { + // New Contract use isActive + foreach ( $addresses as $add) { + $result[$add] = isActive($add, $contract); + } + } else { + // Old contract fallback on getAccStatus + foreach ( $addresses as $add) { + $result[$add] = getAccStatus($add, $contract); + } + } + + return $result; +} + function checkLegitimateAdmin($dat, $signature, $caller, $server){ $result = false; @@ -182,7 +253,8 @@ function checkLegitimateAdmin($dat, $signature, $caller, $server){ // Get the caller type and status $acctype = getAccType($caller, $contract); - $accStatus = getAccStatus($caller, $contract); + $status =getAccountStatus(array($caller), $contract); + $accStatus = $status[$caller]; if ($acctype==2 && $accStatus==1){ $result = true; diff --git a/ecrecover_helper.php b/ecrecover_helper.php index 2217551..848ac34 100644 --- a/ecrecover_helper.php +++ b/ecrecover_helper.php @@ -55,6 +55,10 @@ function personal_ecRecoverPublic($msg, $signed) { $hex = keccak256WithPrefix($personal_prefix_msg); return ecRecoverPublic($hex, $signed); } + +function keyToAddress($publicKeyString) { + return '0x'. substr(keccak256WithPrefix(hex2bin($publicKeyString)), -40); +} function ecRecoverPublic($hex, $signed) { $rHex = substr($signed, 2, 64); @@ -81,7 +85,7 @@ function ecRecoverPublic($hex, $signed) { $publicKey = Signature::recoverPublicKey($rGmp, $sGmp, $messageGmp, $recovery); $publicKeyString = $publicKey["x"] . $publicKey["y"]; - return array('0x'. substr(keccak256WithPrefix(hex2bin($publicKeyString)), -40),$publicKeyString); + return array(keyToAddress($publicKeyString),$publicKeyString); } function strToHex($string)