Completed
Pull Request — master (#758)
by thomas
35:59
created

XOnlyPublicKey   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 5
A verifySchnorr() 0 4 1
A getBuffer() 0 8 2
A doVerifySchnorr() 0 3 1
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)) {
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...
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);
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

53
        return (bool) /** @scrutinizer ignore-call */ secp256k1_schnorrsig_verify($this->context, $schnorrSig->getResource(), $msg32->getBinary(), $this->xonlyKey);
Loading history...
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);
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...
66
        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

66
        if (!/** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_serialize($this->context, $out, $this->xonlyKey)) {
Loading history...
67
            throw new \RuntimeException("failed to serialize xonly pubkey!");
68
        }
69
        return new Buffer($out);
70
    }
71
}