Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completion some client interface #41

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ function getCoinDetails(string $coin)
return $this->request("GET", "/v1/custody/coin_info/", $params);
}

/***
* Get Support Coins
* @return mixed
* @throws Exception
*/
function getSupportedCoins()
{
$params = [];
return $this->request("GET", "/v1/custody/get_supported_coins/", $params);
}

/***
* Get New Deposit Address
* @param string $coin
Expand Down Expand Up @@ -307,6 +318,17 @@ function getPendingTransactions(array $params = [])
return $this->request("GET", "/v1/custody/pending_transactions/", $params);
}

/***
* Get transactions by requestIds
* @param string $requestIds
* @return mixed|string
* @throws Exception
*/
function getTransactionsByRequestid(string $requestIds)
{
return $this->request("GET", "/v1/custody/transactions_by_request_ids/", ["request_ids" => $requestIds]);
}

/***
* Get transactions by txid
* @param string $txid
Expand Down Expand Up @@ -477,6 +499,4 @@ function getStakingHistory(array $params = [])
{
return $this->request("GET", "/v1/custody/staking_history/", $params);
}


}
14 changes: 14 additions & 0 deletions MPCClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,18 @@ function signMessageByCoboIds(string $coboIds)

return $this->request("GET", "/v1/custody/mpc/sign_messages_by_cobo_ids/", $params);
}

/***
* retry double check
* string $requestId
* @return mixed|string
*/
function retryDoubleCheck(string $requestId)
{
$params = [
"request_id" => $requestId,
];

return $this->request("POST", "/v1/custody/mpc/retry_double_check/", $params);
}
}
187 changes: 187 additions & 0 deletions MPCPrimeBrokerClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

namespace Cobo\Custody;

use BI\BigInteger;
use Elliptic\EC;
use PHPUnit\Runner\Exception;


class MPCPrimeBrokerClient
{
private $apiSigner;
private $apiKey;
private $coboPub;
private $host;
private $debug;

public function __construct(ApiSigner $apiSigner, array $config, bool $debug = false)
{
$this->apiKey = $apiSigner->getPublicKey();
$this->apiSigner = $apiSigner;
$this->coboPub = $config['coboPub'];
$this->host = $config['host'];
$this->debug = $debug;
}

/**
* @throws Exception
*/
function request(string $method, string $path, array $data)
{
$ch = curl_init();
$sorted_data = $this->sortData($data);
list($microsecond, $second) = explode(' ', microtime());
$nonce = (float)sprintf('%.0f', (floatval($microsecond) + floatval($second)) * 1000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Biz-Api-Key:" . $this->apiKey,
"Biz-Api-Nonce:" . $nonce,
"Biz-Api-Signature:" . $this->apiSigner->sign(join("|", [$method, $path, $nonce, $sorted_data]))
]);


if ($method == "POST") {
curl_setopt($ch, CURLOPT_URL, $this->host . $path);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($ch, CURLOPT_URL, $this->host . $path . "?" . $sorted_data);
}
if ($this->debug) {
echo "request >>>>>>>>\n";
echo join("|", [$method, $path, $nonce, $sorted_data]), "\n";
}

list($header, $body) = explode("\r\n\r\n", curl_exec($ch), 2);
preg_match("/biz_timestamp: (?<timestamp>[0-9]*)/i", $header, $match);
$timestamp = $match["timestamp"];
preg_match("/biz_resp_signature: (?<signature>[0-9abcdef]*)/i", $header, $match);
$signature = $match["signature"];

if ($this->debug) {
echo "response <<<<<<<<\n";
echo "$body|$timestamp", "\n";
echo "$signature", "\n";
}
if ($this->verifyEcdsa($body, $timestamp, $signature) != 1) {
throw new Exception("signature verify fail");
}
curl_close($ch);
return json_decode($body);
}

private function sortData(array $data): string
{
ksort($data);
$result = [];
foreach ($data as $key => $val) {
array_push($result, $key . "=" . urlencode($val));
}
return join("&", $result);
}

function verifyEcdsa(string $message, string $timestamp, string $signature): bool
{
$message = hash("sha256", hash("sha256", "$message|$timestamp", True), True);
$ec = new EC('secp256k1');
$key = $ec->keyFromPublic($this->coboPub, "hex");
return $key->verify(bin2hex($message), $signature);
}

/***
* create binding
* @param string $userId
* @return mixed|string
*/
function createBinding(string $userId)
{
$params = [
"user_id" => $userId,
];
return $this->request("POST", "/v1/custody/guard/create_binding/", $params);
}

/***
* query binding
* @param string $binderId
* @return mixed|string
*/
function queryBinding(string $binderId)
{
$params = [
"binder_id" => $binderId,
];
return $this->request("GET", "/v1/custody/guard/query_binding/", $params);
}

/***
* query user auth
* @param string $userId
* @return mixed|string
*/
function queryUserAuth(string $userId)
{
$params = [
"user_id" => $userId,
];
return $this->request("GET", "/v1/custody/guard/query_user_auth/", $params);
}

/***
* bind addresses
* @param string $userId
* @param string $addresses
* @return mixed|string
*/
function bindAddresses(string $userId, string $addresses)
{
$params = [
"user_id" => $userId,
"addresses" => $addresses,
];
return $this->request("POST", "/v1/custody/guard/bind_addresses/", $params);
}

/***
* change binding
* @param string $userId
* @return mixed|string
*/
function changeBinding(string $userId)
{
$params = [
"user_id" => $userId,
];
return $this->request("POST", "/v1/custody/guard/change_binding/", $params);
}

/***
* unbind binding
* @param string $userId
* @return mixed|string
*/
function unbindBinding(string $userId)
{
$params = [
"user_id" => $userId,
];
return $this->request("POST", "/v1/custody/guard/unbind_binding/", $params);
}

/***
* query statement
* @param string $statementId
* @return mixed|string
*/
function queryStatement(string $statementId)
{
$params = [
"statement_id" => $statementId,
];
return $this->request("GET", "/v1/custody/guard/query_statement/", $params);
}
}
Loading