Completed
Push — master ( 545d02...a22894 )
by thomas
14:41
created

PrivateKey::sign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 65
    public function __construct(EcAdapter $ecAdapter, $int, $compressed = false)
46
    {
47 65
        if (false === $ecAdapter->validatePrivateKey(Buffer::int($int, 32, $ecAdapter->getMath()))) {
48 1
            throw new InvalidPrivateKey('Invalid private key - must be less than curve order.');
49
        }
50
51 64
        if (false === is_bool($compressed)) {
52
            throw new \InvalidArgumentException('PrivateKey: Compressed argument must be a boolean');
53
        }
54 64
        $this->ecAdapter = $ecAdapter;
55 64
        $this->secretMultiplier = $int;
56 64
        $this->compressed = $compressed;
57 64
    }
58
59
    /**
60
     * @return int|string
61
     */
62 56
    public function getSecretMultiplier()
63
    {
64 56
        return $this->secretMultiplier;
65
    }
66
67
    /**
68
     * @param BufferInterface $msg32
69
     * @param RbgInterface|null $rbg
70
     * @return \BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface
71
     */
72
    public function sign(BufferInterface $msg32, RbgInterface $rbg = null)
73
    {
74
        return $this->ecAdapter->sign($msg32, $this, $rbg);
75
    }
76
77
    /**
78
     * @param int|string $tweak
79
     * @return PrivateKeyInterface
80
     */
81 7
    public function tweakAdd($tweak)
82
    {
83 7
        $adapter = $this->ecAdapter;
84 7
        return $adapter->getPrivateKey(
85
            $adapter
86 7
                ->getMath()
87 7
                ->getModularArithmetic(
88
                    $adapter
89 7
                        ->getGenerator()
90 7
                        ->getOrder()
91
                )
92 7
                ->add(
93
                    $tweak,
94 7
                    $this->getSecretMultiplier()
95
                ),
96 7
            $this->compressed
97
        );
98
    }
99
100
    /**
101
     * @param int|string $tweak
102
     * @return PrivateKeyInterface
103
     */
104 1
    public function tweakMul($tweak)
105
    {
106 1
        $adapter = $this->ecAdapter;
107 1
        return $adapter->getPrivateKey(
108
            $adapter
109 1
            ->getMath()
110 1
            ->getModularArithmetic(
111
                $adapter
112 1
                    ->getGenerator()
113 1
                    ->getOrder()
114
            )
115 1
            ->mul(
116
                $tweak,
117 1
                $this->getSecretMultiplier()
118
            ),
119 1
            $this->compressed
120
        );
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126 33
    public function isCompressed()
127
    {
128 33
        return $this->compressed;
129
    }
130
131
    /**
132
     * Return the public key
133
     *
134
     * @return PublicKey
135
     */
136 67
    public function getPublicKey()
137
    {
138 67
        if (null === $this->publicKey) {
139 55
            $adapter = $this->ecAdapter;
140 55
            $this->publicKey = $adapter->getPublicKey(
141
                $adapter
142 55
                    ->getGenerator()
143 55
                    ->mul($this->secretMultiplier),
144 55
                $this->compressed
145 3
            );
146 3
        }
147
148 67
        return $this->publicKey;
149
    }
150
151
    /**
152
     * @param NetworkInterface $network
153
     * @return string
154
     */
155 3
    public function toWif(NetworkInterface $network = null)
156
    {
157 3
        $network = $network ?: Bitcoin::getNetwork();
158 3
        $serializer = new WifPrivateKeySerializer(
159 3
            $this->ecAdapter->getMath(),
160 3
            new PrivateKeySerializer($this->ecAdapter)
161
        );
162
163 3
        return $serializer->serialize($network, $this);
164
    }
165
166
    /**
167
     * @return \BitWasp\Buffertools\BufferInterface
168
     */
169 33
    public function getBuffer()
170
    {
171 33
        return (new PrivateKeySerializer($this->ecAdapter))->serialize($this);
172
    }
173
}
174