Completed
Push — master ( 4963c9...d80e29 )
by thomas
18:26 queued 14:36
created

PrivateKey::isCompressed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Serializer\Key\PrivateKeySerializer;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\Key;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
10
use BitWasp\Bitcoin\Crypto\Random\RbgInterface;
11
use BitWasp\Bitcoin\Exceptions\InvalidPrivateKey;
12
use BitWasp\Bitcoin\Network\NetworkInterface;
13
use BitWasp\Bitcoin\Serializer\Key\PrivateKey\WifPrivateKeySerializer;
14
use BitWasp\Buffertools\Buffer;
15
use BitWasp\Buffertools\BufferInterface;
16
17
class PrivateKey extends Key implements PrivateKeyInterface
18
{
19
    /**
20
     * @var int|string
21
     */
22
    private $secretMultiplier;
23
24
    /**
25
     * @var bool
26
     */
27
    private $compressed;
28
29
    /**
30
     * @var PublicKey
31
     */
32
    private $publicKey;
33
34
    /**
35
     * @var EcAdapter
36
     */
37
    private $ecAdapter;
38
39
    /**
40
     * @param EcAdapter $ecAdapter
41
     * @param $int
42
     * @param bool $compressed
43
     * @throws InvalidPrivateKey
44
     */
45 222
    public function __construct(EcAdapter $ecAdapter, $int, $compressed = false)
46
    {
47 222
        if (false === $ecAdapter->validatePrivateKey(Buffer::int($int, 32, $ecAdapter->getMath()))) {
48 3
            throw new InvalidPrivateKey('Invalid private key - must be less than curve order.');
49
        }
50 219
        if (false === is_numeric($int)) {
51
            throw new \InvalidArgumentException('PrivateKey: Secret must be an integer');
52
        }
53 219
        if (false === is_bool($compressed)) {
54
            throw new \InvalidArgumentException('PrivateKey: Compressed argument must be a boolean');
55
        }
56 219
        $this->ecAdapter = $ecAdapter;
57 219
        $this->secretMultiplier = $int;
0 ignored issues
show
Documentation Bug introduced by
It seems like $int can also be of type double. However, the property $secretMultiplier is declared as type integer|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58 219
        $this->compressed = $compressed;
59 219
    }
60
61
    /**
62
     * @return int|string
63
     */
64 198
    public function getSecretMultiplier()
65
    {
66 198
        return $this->secretMultiplier;
67
    }
68
69
    /**
70
     * @param BufferInterface $msg32
71
     * @param RbgInterface|null $rbg
72
     * @return \BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface
73
     */
74
    public function sign(BufferInterface $msg32, RbgInterface $rbg = null)
75
    {
76
        return $this->ecAdapter->sign($msg32, $this, $rbg);
77
    }
78
79
    /**
80
     * @param int|string $tweak
81
     * @return PrivateKeyInterface
82
     */
83 18
    public function tweakAdd($tweak)
84
    {
85 18
        $adapter = $this->ecAdapter;
86 18
        return $adapter->getPrivateKey(
87
            $adapter
88 18
                ->getMath()
89 18
                ->getModularArithmetic(
90
                    $adapter
91 18
                        ->getGenerator()
92 18
                        ->getOrder()
93 18
                )
94 18
                ->add(
95 18
                    $tweak,
96 18
                    $this->getSecretMultiplier()
97 18
                ),
98 18
            $this->compressed
99 18
        );
100
    }
101
102
    /**
103
     * @param int|string $tweak
104
     * @return PrivateKeyInterface
105
     */
106 3
    public function tweakMul($tweak)
107
    {
108 3
        $adapter = $this->ecAdapter;
109 3
        return $adapter->getPrivateKey(
110
            $adapter
111 3
            ->getMath()
112 3
            ->getModularArithmetic(
113
                $adapter
114 3
                    ->getGenerator()
115 3
                    ->getOrder()
116 3
            )
117 3
            ->mul(
118 3
                $tweak,
119 3
                $this->getSecretMultiplier()
120 3
            ),
121 3
            $this->compressed
122 3
        );
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128 102
    public function isCompressed()
129
    {
130 102
        return $this->compressed;
131
    }
132
133
    /**
134
     * Return the public key
135
     *
136
     * @return PublicKey
137
     */
138 225
    public function getPublicKey()
139
    {
140 225
        if (null === $this->publicKey) {
141 189
            $adapter = $this->ecAdapter;
142 189
            $this->publicKey = $adapter->getPublicKey(
143
                $adapter
144 189
                    ->getGenerator()
145 189
                    ->mul($this->secretMultiplier),
146 189
                $this->compressed
147 189
            );
148 189
        }
149
150 225
        return $this->publicKey;
151
    }
152
153
    /**
154
     * @param NetworkInterface $network
155
     * @return string
156
     */
157 12
    public function toWif(NetworkInterface $network = null)
158
    {
159 12
        $network = $network ?: Bitcoin::getNetwork();
160 12
        $serializer = new WifPrivateKeySerializer(
161 12
            $this->ecAdapter->getMath(),
162 12
            new PrivateKeySerializer($this->ecAdapter)
163 12
        );
164
165 12
        return $serializer->serialize($network, $this);
166
    }
167
168
    /**
169
     * @return \BitWasp\Buffertools\BufferInterface
170
     */
171 126
    public function getBuffer()
172
    {
173 126
        return (new PrivateKeySerializer($this->ecAdapter))->serialize($this);
174
    }
175
}
176