1 | <?php |
||
18 | class HierarchicalKey |
||
19 | { |
||
20 | /** |
||
21 | * @var EcAdapterInterface |
||
22 | */ |
||
23 | private $ecAdapter; |
||
24 | |||
25 | /** |
||
26 | * @var int|string |
||
27 | */ |
||
28 | private $depth; |
||
29 | |||
30 | /** |
||
31 | * @var int|string |
||
32 | */ |
||
33 | private $parentFingerprint; |
||
34 | |||
35 | /** |
||
36 | * @var int|string |
||
37 | */ |
||
38 | private $sequence; |
||
39 | |||
40 | /** |
||
41 | * @var int|string |
||
42 | */ |
||
43 | private $chainCode; |
||
44 | |||
45 | /** |
||
46 | * @var KeyInterface |
||
47 | */ |
||
48 | private $key; |
||
49 | |||
50 | /** |
||
51 | * @param EcAdapterInterface $ecAdapter |
||
52 | * @param integer|string $depth |
||
53 | * @param integer|string $parentFingerprint |
||
54 | * @param integer|string $sequence |
||
55 | * @param integer|string $chainCode |
||
56 | * @param KeyInterface $key |
||
57 | * @throws \Exception |
||
58 | */ |
||
59 | 150 | public function __construct(EcAdapterInterface $ecAdapter, $depth, $parentFingerprint, $sequence, $chainCode, KeyInterface $key) |
|
73 | |||
74 | /** |
||
75 | * Return the depth of this key. This is limited to 256 sequential derivations. |
||
76 | * |
||
77 | * @return int |
||
78 | */ |
||
79 | 66 | public function getDepth() |
|
83 | |||
84 | /** |
||
85 | * Get the sequence number for this address. Hardened keys are |
||
86 | * created with sequence > 0x80000000. a sequence number lower |
||
87 | * than this can be derived with the public key. |
||
88 | * |
||
89 | * @return int |
||
90 | */ |
||
91 | 36 | public function getSequence() |
|
95 | |||
96 | /** |
||
97 | * Get the fingerprint of the parent key. For master keys, this is 00000000. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | 36 | public function getFingerprint() |
|
109 | |||
110 | /** |
||
111 | * Return the fingerprint to be used for child keys. |
||
112 | * @return string |
||
113 | */ |
||
114 | 42 | public function getChildFingerprint() |
|
118 | |||
119 | /** |
||
120 | * Return the chain code - a deterministic 'salt' for HMAC-SHA512 |
||
121 | * in child derivations |
||
122 | * |
||
123 | * @return integer |
||
124 | */ |
||
125 | 54 | public function getChainCode() |
|
129 | |||
130 | /** |
||
131 | * @return PrivateKeyInterface |
||
132 | */ |
||
133 | 78 | public function getPrivateKey() |
|
141 | |||
142 | /** |
||
143 | * Get the public key the private key or public key. |
||
144 | * |
||
145 | * @return PublicKey |
||
146 | */ |
||
147 | 78 | public function getPublicKey() |
|
155 | |||
156 | /** |
||
157 | * @return HierarchicalKey |
||
158 | */ |
||
159 | 24 | public function toPublic() |
|
167 | |||
168 | /** |
||
169 | * Return whether this is a private key |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | 90 | public function isPrivate() |
|
177 | |||
178 | /** |
||
179 | * Return whether the key is hardened |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | 6 | public function isHardened() |
|
188 | |||
189 | /** |
||
190 | * Create a buffer containing data to be hashed hashed to yield the child offset |
||
191 | * |
||
192 | * @param integer|string $sequence |
||
193 | * @return Buffer |
||
194 | * @throws \Exception |
||
195 | */ |
||
196 | 42 | public function getHmacSeed($sequence) |
|
197 | { |
||
198 | 42 | $hardened = $this->ecAdapter->getMath()->getBinaryMath()->isNegative($sequence, 32); |
|
199 | |||
200 | 42 | if ($hardened) { |
|
201 | 30 | if ($this->isPrivate() === false) { |
|
202 | 6 | throw new \Exception("Can't derive a hardened key without the private key"); |
|
203 | } |
||
204 | |||
205 | 24 | $buffer = Buffertools::concat(new Buffer("\x00"), $this->getPrivateKey()->getBuffer()); |
|
206 | 24 | } else { |
|
207 | 36 | $buffer = $this->getPublicKey()->getBuffer(); |
|
208 | } |
||
209 | |||
210 | 36 | return (new Parser($buffer)) |
|
211 | 36 | ->writeBytes(4, Buffer::int($sequence, 4)) |
|
212 | 36 | ->getBuffer(); |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Derive a child key |
||
217 | * |
||
218 | * @param $sequence |
||
219 | * @return HierarchicalKey |
||
220 | * @throws \Exception |
||
221 | */ |
||
222 | 42 | public function deriveChild($sequence) |
|
244 | |||
245 | /** |
||
246 | * @param array|\stdClass|\Traversable $list |
||
247 | * @return HierarchicalKey |
||
248 | */ |
||
249 | 18 | public function deriveFromList($list) |
|
262 | |||
263 | /** |
||
264 | * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
||
265 | * |
||
266 | * @param string $path |
||
267 | * @return HierarchicalKey |
||
268 | * @throws \Exception |
||
269 | */ |
||
270 | 18 | public function derivePath($path) |
|
275 | |||
276 | /** |
||
277 | * Serializes the instance according to whether it wraps a private or public key. |
||
278 | * @param NetworkInterface $network |
||
279 | * @return string |
||
280 | */ |
||
281 | 30 | public function toExtendedKey(NetworkInterface $network = null) |
|
289 | |||
290 | /** |
||
291 | * Explicitly serialize as a private key. Throws an exception if |
||
292 | * the key isn't private. |
||
293 | * |
||
294 | * @param NetworkInterface $network |
||
295 | * @return string |
||
296 | */ |
||
297 | 30 | public function toExtendedPrivateKey(NetworkInterface $network = null) |
|
305 | |||
306 | /** |
||
307 | * Explicitly serialize as a public key. This will always work. |
||
308 | * |
||
309 | * @param NetworkInterface $network |
||
310 | * @return string |
||
311 | */ |
||
312 | 24 | public function toExtendedPublicKey(NetworkInterface $network = null) |
|
317 | } |
||
318 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.