1 | <?php |
||
18 | class PrivateKey extends Key implements PrivateKeyInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var \GMP |
||
22 | */ |
||
23 | private $secret; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $secretBin; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $compressed; |
||
34 | |||
35 | /** |
||
36 | * @var PublicKey |
||
37 | */ |
||
38 | private $publicKey; |
||
39 | |||
40 | /** |
||
41 | * @var EcAdapter |
||
42 | */ |
||
43 | private $ecAdapter; |
||
44 | |||
45 | /** |
||
46 | * @param EcAdapter $adapter |
||
47 | * @param \GMP $secret |
||
48 | * @param bool|false $compressed |
||
49 | * @throws \Exception |
||
50 | */ |
||
51 | 59 | public function __construct(EcAdapter $adapter, \GMP $secret, $compressed = false) |
|
52 | { |
||
53 | 59 | $buffer = Buffer::int(gmp_strval($secret, 10), 32, $adapter->getMath()); |
|
54 | 59 | if (!$adapter->validatePrivateKey($buffer)) { |
|
55 | 2 | throw new InvalidPrivateKey('Invalid private key'); |
|
56 | } |
||
57 | |||
58 | 57 | if (false === is_bool($compressed)) { |
|
59 | throw new \InvalidArgumentException('PrivateKey: Compressed argument must be a boolean'); |
||
60 | } |
||
61 | |||
62 | 57 | $this->ecAdapter = $adapter; |
|
63 | 57 | $this->secret = $secret; |
|
64 | 57 | $this->secretBin = $buffer->getBinary(); |
|
65 | 57 | $this->compressed = $compressed; |
|
66 | 57 | } |
|
67 | |||
68 | /** |
||
69 | * @param BufferInterface $msg32 |
||
70 | * @param RbgInterface|null $rbgInterface |
||
71 | * @return Signature |
||
72 | */ |
||
73 | public function sign(BufferInterface $msg32, RbgInterface $rbgInterface = null) |
||
74 | { |
||
75 | return $this->ecAdapter->sign($msg32, $this, $rbgInterface); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return bool|false |
||
80 | */ |
||
81 | 49 | public function isCompressed() |
|
85 | |||
86 | /** |
||
87 | * @return int|string |
||
88 | */ |
||
89 | 37 | public function getSecret() |
|
93 | |||
94 | /** |
||
95 | * @return string |
||
96 | */ |
||
97 | 72 | public function getSecretBinary() |
|
101 | |||
102 | /** |
||
103 | * @return PublicKey |
||
104 | */ |
||
105 | 66 | public function getPublicKey() |
|
120 | |||
121 | /** |
||
122 | * @param \GMP $tweak |
||
123 | * @return PrivateKey |
||
124 | */ |
||
125 | 8 | public function tweakAdd(\GMP $tweak) |
|
145 | |||
146 | /** |
||
147 | * @param \GMP $tweak |
||
148 | * @return PrivateKey |
||
149 | */ |
||
150 | 1 | public function tweakMul(\GMP $tweak) |
|
169 | |||
170 | /** |
||
171 | * @param NetworkInterface $network |
||
172 | * @return string |
||
173 | */ |
||
174 | 3 | public function toWif(NetworkInterface $network = null) |
|
180 | |||
181 | /** |
||
182 | * @return BufferInterface |
||
183 | */ |
||
184 | 70 | public function getBuffer() |
|
188 | } |
||
189 |