Skip to content

Commit

Permalink
running
Browse files Browse the repository at this point in the history
  • Loading branch information
FDubath committed Dec 11, 2019
1 parent 11573b1 commit 96af2cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
31 changes: 19 additions & 12 deletions Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,42 @@ function createWebhookMessage($tr_hash, $server_name, $store_id, $store_ref, $fr
$base_link .= "://";
$base_link .= $_SERVER['HTTP_HOST'];

$link = {'href'=>$base_link.'/api.php?hash='.$tr_hash,
'method'=>'GET'};
$link = array (
"href"=>$base_link."/api.php?hash=".$tr_hash,
"method"=>"GET"
);

$amount = { 'sent'=> $amount,
'type'=> $type_tr,
'currency' => $server_name
};
$amount = array (
'sent'=> $amount,
'type'=> $type_tr,
'currency' => $server_name
);


$resources = {'id'=>$tr_hash,
$resources = array (
'id'=>$tr_hash,
'create_time'=> $time,
'state'='completed',
'state'=>'completed',
'store_id' => $store_id,
'reference' => $store_ref,
'links'=>[$link],
'addr_from'=> $from_add,

'addr_to' => $dest,
'amount'=>$amount
};
);
if (strlen($from_add)>0) {
$resources['addr_from']=$from_add;
}


$data = {'id'=>$tr_hash,
$data = array ('id'=>$tr_hash,
'create_time'=>$time,
'resource_type'=>'sale',
'event_type'=> 'PAYMENT.SALE.COMPLETED',
'summary'=>'A sale has been completed. The payement has been processed.',
'links'=>[$link],
'resources'=>$resources
};
);

return $data;
}
Expand Down
11 changes: 5 additions & 6 deletions api.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
header('Access-Control-Allow-Origin: *');
include './checkAdmin.php';
include './ecrecover_helper.php';
include './Webhook.php';
require_once 'libs/jsonRPCClient.php';

Expand Down Expand Up @@ -260,13 +259,13 @@ function sendRawTransaction($rawtx,$gethRPC){
// if so get the dest
$dest = '0x'.$rawtx.substr(110,40);
// get the sender
$sender = TransactionEcRecover($rawtx)[0];
// TODO $sender = TransactionEcRecover($rawtx)[0];

// get the amount
$amount = hexdec($rawtx.substr(150,64);
$amount = hexdec($rawtx.substr(150,64));
// get the balances for dest
$to_bal = getBalance($dest, $contract);
$from_bal = getBalance($sender, $contract);
// TODO $from_bal = getBalance($sender, $contract);
$wh_status = 1;
}

Expand All @@ -276,11 +275,11 @@ function sendRawTransaction($rawtx,$gethRPC){
// get the balances check if changes compatible the the amount
$to_bal_after = getBalance($dest, $contract);
$from_bal_after = getBalance($sender, $contract);
if ($to_bal_after - $to_bal >= $amount && $from_bal - $from_bal_after >= $amount) {
if ($to_bal_after - $to_bal >= $amount) { // TODO} && $from_bal - $from_bal_after >= $amount) {
// if so : send the webhook
$message = createWebhookMessage($data['data'], $_REQUEST['serverName'],
$_REQUEST['shopId'], $_REQUEST['txId'],
$sender, $rawtx);
"", $rawtx); // TODO ""=> $sender
$res = sendWebhook($shop_url, $message);
if ($res) {
$wh_status = 3;
Expand Down
7 changes: 4 additions & 3 deletions ecrecover_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_once './Keccak.php';
use kornrunner\Keccak;


function TransactionEcRecover($rawTx) {
// get the signature, last 134 chars
$len = strlen($rawTx);
Expand All @@ -30,7 +31,7 @@ function ecRecover($hex, $signed) {

function personal_ecRecoverPublic($msg, $signed) {
$personal_prefix_msg = "\x19Ethereum Signed Message:\n". strlen($msg). $msg;
$hex = keccak256($personal_prefix_msg);
$hex = keccak256WithPrefix($personal_prefix_msg);
return ecRecoverPublic($hex, $signed);
}

Expand Down Expand Up @@ -59,7 +60,7 @@ function ecRecoverPublic($hex, $signed) {
$publicKey = Signature::recoverPublicKey($rGmp, $sGmp, $messageGmp, $recovery);
$publicKeyString = $publicKey["x"] . $publicKey["y"];

return array('0x'. substr(keccak256(hex2bin($publicKeyString)), -40),$publicKeyString);
return array('0x'. substr(keccak256WithPrefix(hex2bin($publicKeyString)), -40),$publicKeyString);
}

function strToHex($string)
Expand All @@ -68,7 +69,7 @@ function strToHex($string)
return '0x' . array_shift($hex);
}

function keccak256($str) {
function keccak256WithPrefix($str) {
return '0x'. Keccak::hash($str, 256);
}

Expand Down

0 comments on commit 96af2cb

Please sign in to comment.