1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\SchnorrSignature; |
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\XOnlyPubKeyInterface; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SchnorrSignatureInterface; |
9
|
|
|
use BitWasp\Buffertools\Buffer; |
10
|
|
|
use BitWasp\Buffertools\BufferInterface; |
11
|
|
|
|
12
|
|
|
class XOnlyPublicKey implements XOnlyPubKeyInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var resource |
16
|
|
|
*/ |
17
|
|
|
private $context; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var resource |
21
|
|
|
*/ |
22
|
|
|
private $xonlyKey; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $isCompressed; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* XOnlyPublicKey constructor. |
31
|
|
|
* @param $context |
32
|
|
|
* @param $xonlyKey |
33
|
|
|
* @param bool $isCompressed |
34
|
|
|
*/ |
35
|
|
|
public function __construct($context, $xonlyKey, bool $isCompressed) |
36
|
|
|
{ |
37
|
|
|
if (!is_resource($context) || |
38
|
|
|
!get_resource_type($context) === SECP256K1_TYPE_CONTEXT) { |
39
|
|
|
throw new \InvalidArgumentException('Secp256k1\Key\XOnlyPublicKey expects ' . SECP256K1_TYPE_CONTEXT . ' resource'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!(is_resource($xonlyKey) && get_resource_type($xonlyKey) === SECP256K1_TYPE_XONLY_PUBKEY)) { |
|
|
|
|
43
|
|
|
throw new \InvalidArgumentException('Secp256k1\Key\XOnlyPublicKey expects ' . SECP256K1_TYPE_XONLY_PUBKEY . ' resource'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->context = $context; |
47
|
|
|
$this->xonlyKey = $xonlyKey; |
48
|
|
|
$this->isCompressed = $isCompressed; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function doVerifySchnorr(BufferInterface $msg32, SchnorrSignature $schnorrSig): bool |
52
|
|
|
{ |
53
|
|
|
return (bool) secp256k1_schnorrsig_verify($this->context, $schnorrSig->getResource(), $msg32->getBinary(), $this->xonlyKey); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function verifySchnorr(BufferInterface $msg32, SchnorrSignatureInterface $schnorrSignature): bool |
57
|
|
|
{ |
58
|
|
|
/** @var SchnorrSignature $schnorrSignature */ |
59
|
|
|
return $this->doVerifySchnorr($msg32, $schnorrSignature); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getBuffer(): BufferInterface |
63
|
|
|
{ |
64
|
|
|
$out = ''; |
65
|
|
|
var_dump($this->xonlyKey); |
|
|
|
|
66
|
|
|
if (!secp256k1_xonly_pubkey_serialize($this->context, $out, $this->xonlyKey)) { |
|
|
|
|
67
|
|
|
throw new \RuntimeException("failed to serialize xonly pubkey!"); |
68
|
|
|
} |
69
|
|
|
return new Buffer($out); |
70
|
|
|
} |
71
|
|
|
} |