Completed
Pull Request — master (#758)
by thomas
22:04
created

XOnlyPublicKey::getBuffer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 6
rs 10
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)) {
0 ignored issues
show
Bug introduced by
The constant BitWasp\Bitcoin\Crypto\E...256K1_TYPE_XONLY_PUBKEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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);
0 ignored issues
show
Bug introduced by
The function secp256k1_schnorrsig_verify was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        return (bool) /** @scrutinizer ignore-call */ secp256k1_schnorrsig_verify($this->context, $schnorrSig->getResource(), $msg32->getBinary(), $this->xonlyKey);
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        if (1 !== /** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_serialize($context, $serialized, $this->xonlyKey)) {
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_parse was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        if (1 !== /** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_parse($context, $clone, $serialized)) {
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_tweak_add was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        if (!/** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_tweak_add($this->context, $tweaked, $pubkey, $tweak32)) {
Loading history...
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);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->xonlyKey) looks like debug code. Are you sure you do not want to remove it?
Loading history...
88
        if (!secp256k1_xonly_pubkey_serialize($this->context, $out, $this->xonlyKey)) {
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        if (!/** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_serialize($this->context, $out, $this->xonlyKey)) {
Loading history...
89
            throw new \RuntimeException("failed to serialize xonly pubkey!");
90
        }
91
        return new Buffer($out);
92
    }
93
}