Hello, I’m developping in php and this is my function:
$body = [
'type' => 'send',
'to' => $address,
'amount' => $amount,
'currency' => $currency,
'idem' => uniqid('CB-SENT-')
];
try {
$client = new \GuzzleHttp\Client();
$account_id = $this->getAccountId($client, $currency);
$path = '/v2/accounts/' . $account_id . '/transactions';
$time = time();
$signature = $this->signature(false, $path, $time, $body, 'POST');
if ($account_id == null) {
// Crypto account not found
$this->addFlash('notice', 'We could not send you crypto. Please wait while for an operation proceed');
return $this->redirectToRoute('cryptoexchange');
}
$response = $client->request('POST', 'https://api.coinbase.com' . $path, [
'headers' => [
'Content-Type' => 'application/json',
'CB-ACCESS-KEY' => $this->key,
'CB-ACCESS-SIGN' => $signature,
'CB-ACCESS-TIMESTAMP' => $time,
'CB-VERSION' => '2022-03-28',
]
]);
$body = json_decode($response->getBody(), true);
dd($body);
} catch (\Throwable $th) {
dd($th);
}
And my signature function is:
public function signature(bool $cbPro, string $request_path, $timestamp, $body = '', $method = 'GET')
{
$body = is_array($body) ? json_encode($body) : $body;
$what = $timestamp . $method . $request_path . $body;
if ($cbPro)
return base64_encode(hash_hmac("sha256", $what, base64_decode($this->secret)));
else
return hash_hmac('SHA256', $what, $this->secret);
}
Whether the $cbPro bool in the signature is true or not, i always get an invalid signature error when sending crypto. But the getAccountId() works and they have same signatures.
public function getAccountId(\GuzzleHttp\Client $client, string $currency): string | null
{
try {
$path = '/v2/accounts/' . $currency;
$time = time();
$signature = $this->signature(false, $path, $time);
$response = $client->request('GET', 'https://api.coinbase.com' . $path, [
'headers' => [
'Content-Type' => 'application/json',
'CB-ACCESS-KEY' => $this->key,
'CB-ACCESS-SIGN' => $signature,
'CB-ACCESS-TIMESTAMP' => $time,
'CB-VERSION' => '2022-03-28',
]
]);
$body = json_decode($response->getBody(), true);
return $body['data']['id'];
} catch (\Throwable $th) {
// dd($th);
return null;
}
}
I am sorry that my message is long but can i know what it the problem with my function ? Which “code” is better for sending crypto please