|
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
|
|
|
* @param resource $context |
|
26
|
|
|
* @param resource $xonlyKey |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($context, $xonlyKey) |
|
29
|
|
|
{ |
|
30
|
|
|
if (!is_resource($context) || |
|
31
|
|
|
!get_resource_type($context) === SECP256K1_TYPE_CONTEXT) { |
|
32
|
|
|
throw new \InvalidArgumentException('Secp256k1\Key\XOnlyPublicKey expects ' . SECP256K1_TYPE_CONTEXT . ' resource'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (!(is_resource($xonlyKey) && get_resource_type($xonlyKey) === SECP256K1_TYPE_XONLY_PUBKEY)) { |
|
|
|
|
|
|
36
|
|
|
throw new \InvalidArgumentException('Secp256k1\Key\XOnlyPublicKey expects ' . SECP256K1_TYPE_XONLY_PUBKEY . ' resource'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->context = $context; |
|
40
|
|
|
$this->xonlyKey = $xonlyKey; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function doVerifySchnorr(BufferInterface $msg32, SchnorrSignature $schnorrSig): bool |
|
44
|
|
|
{ |
|
45
|
|
|
return (bool) secp256k1_schnorrsig_verify($this->context, $schnorrSig->getResource(), $msg32->getBinary(), $this->xonlyKey); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function verifySchnorr(BufferInterface $msg32, SchnorrSignatureInterface $schnorrSignature): bool |
|
49
|
|
|
{ |
|
50
|
|
|
/** @var SchnorrSignature $schnorrSignature */ |
|
51
|
|
|
return $this->doVerifySchnorr($msg32, $schnorrSignature); |
|
52
|
|
|
} |
|
53
|
|
|
/** |
|
54
|
|
|
* @return resource |
|
55
|
|
|
* @throws \Exception |
|
56
|
|
|
*/ |
|
57
|
|
|
private function clonePubkey() |
|
58
|
|
|
{ |
|
59
|
|
|
$context = $this->context; |
|
60
|
|
|
$serialized = ''; |
|
61
|
|
|
if (1 !== secp256k1_xonly_pubkey_serialize($context, $serialized, $this->xonlyKey)) { |
|
|
|
|
|
|
62
|
|
|
throw new \Exception('failed to serialize xonly pubkey for clone'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** @var resource $clone */ |
|
66
|
|
|
$clone = null; |
|
67
|
|
|
if (1 !== secp256k1_xonly_pubkey_parse($context, $clone, $serialized)) { |
|
|
|
|
|
|
68
|
|
|
throw new \Exception('failed to parse xonly pubkey'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $clone; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function tweakAdd(BufferInterface $tweak32): XOnlyPublicKey |
|
75
|
|
|
{ |
|
76
|
|
|
$pubkey = $this->clonePubkey(); |
|
77
|
|
|
$tweaked = null; |
|
78
|
|
|
if (!secp256k1_xonly_pubkey_tweak_add($this->context, $tweaked, $pubkey, $tweak32)) { |
|
|
|
|
|
|
79
|
|
|
throw new \RuntimeException("failed to tweak pubkey"); |
|
80
|
|
|
} |
|
81
|
|
|
return new XOnlyPublicKey($this->context, $pubkey); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getBuffer(): BufferInterface |
|
85
|
|
|
{ |
|
86
|
|
|
$out = ''; |
|
87
|
|
|
var_dump($this->xonlyKey); |
|
|
|
|
|
|
88
|
|
|
if (!secp256k1_xonly_pubkey_serialize($this->context, $out, $this->xonlyKey)) { |
|
|
|
|
|
|
89
|
|
|
throw new \RuntimeException("failed to serialize xonly pubkey!"); |
|
90
|
|
|
} |
|
91
|
|
|
return new Buffer($out); |
|
92
|
|
|
} |
|
93
|
|
|
} |