forked from CoboGlobal/cobo-php-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalSigner.php
44 lines (34 loc) · 974 Bytes
/
LocalSigner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace Cobo\Custody;
use Elliptic\EC;
require "ApiSigner.php";
class LocalSigner implements ApiSigner
{
public $secretKey;
public function __construct($secretKey)
{
$this->secretKey = $secretKey;
}
public static function generateKeyPair(): array
{
$ec = new EC('secp256k1');
$key = $ec->genKeyPair();
return [
"apiSecret" => $key->getPrivate('hex'),
"apiKey" => $key->getPublic(true, 'hex')
];
}
public function sign($message): string
{
$message = hash("sha256", hash("sha256", $message, True), True);
$ec = new EC('secp256k1');
$key = $ec->keyFromPrivate($this->secretKey);
return $key->sign(bin2hex($message))->toDER('hex');
}
public function getPublicKey(): string
{
$ec = new EC('secp256k1');
$key = $ec->keyFromPrivate($this->secretKey);
return $key->getPublic(true, 'hex');
}
}