Complex classes like HierarchicalKey often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HierarchicalKey, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class HierarchicalKey |
||
24 | { |
||
25 | /** |
||
26 | * @var EcAdapterInterface |
||
27 | */ |
||
28 | protected $ecAdapter; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | private $depth; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $parentFingerprint; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $sequence; |
||
44 | |||
45 | /** |
||
46 | * @var BufferInterface |
||
47 | */ |
||
48 | private $chainCode; |
||
49 | |||
50 | /** |
||
51 | * @var KeyInterface |
||
52 | */ |
||
53 | private $key; |
||
54 | |||
55 | /** |
||
56 | * @var ScriptDataFactory |
||
57 | */ |
||
58 | private $scriptDataFactory; |
||
59 | |||
60 | /** |
||
61 | * @var ScriptAndSignData|null |
||
62 | */ |
||
63 | private $scriptAndSignData; |
||
64 | |||
65 | /** |
||
66 | * @param EcAdapterInterface $ecAdapter |
||
67 | * @param ScriptDataFactory $scriptDataFactory |
||
68 | * @param int $depth |
||
69 | * @param int $parentFingerprint |
||
70 | * @param int $sequence |
||
71 | * @param BufferInterface $chainCode |
||
72 | * @param KeyInterface $key |
||
73 | */ |
||
74 | 71 | public function __construct(EcAdapterInterface $ecAdapter, ScriptDataFactory $scriptDataFactory, int $depth, int $parentFingerprint, int $sequence, BufferInterface $chainCode, KeyInterface $key) |
|
104 | |||
105 | /** |
||
106 | * Return the depth of this key. This is limited to 256 sequential derivations. |
||
107 | * |
||
108 | * @return int |
||
109 | */ |
||
110 | 30 | public function getDepth(): int |
|
114 | |||
115 | /** |
||
116 | * Get the sequence number for this address. Hardened keys are |
||
117 | * created with sequence > 0x80000000. a sequence number lower |
||
118 | * than this can be derived with the public key. |
||
119 | * |
||
120 | * @return int |
||
121 | */ |
||
122 | 25 | public function getSequence(): int |
|
126 | |||
127 | /** |
||
128 | * Get the fingerprint of the parent key. For master keys, this is 00000000. |
||
129 | * |
||
130 | * @return int |
||
131 | */ |
||
132 | 26 | public function getFingerprint(): int |
|
140 | |||
141 | /** |
||
142 | * Return the fingerprint to be used for child keys. |
||
143 | * @return int |
||
144 | */ |
||
145 | 33 | public function getChildFingerprint(): int |
|
146 | { |
||
147 | 33 | $pubKeyHash = $this->getPublicKey()->getPubKeyHash(); |
|
148 | 33 | return (int) $pubKeyHash->slice(0, 4)->getInt(); |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Return the chain code - a deterministic 'salt' for HMAC-SHA512 |
||
153 | * in child derivations |
||
154 | * |
||
155 | * @return BufferInterface |
||
156 | */ |
||
157 | 24 | public function getChainCode(): BufferInterface |
|
158 | { |
||
159 | 24 | return $this->chainCode; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return PrivateKeyInterface |
||
164 | */ |
||
165 | 42 | public function getPrivateKey(): PrivateKeyInterface |
|
166 | { |
||
167 | 42 | if ($this->key->isPrivate()) { |
|
168 | /** @var PrivateKeyInterface $key */ |
||
169 | 40 | $key = $this->key; |
|
170 | 40 | return $key; |
|
171 | } |
||
172 | |||
173 | 2 | throw new \RuntimeException('Unable to get private key, not known'); |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Get the public key the private key or public key. |
||
178 | * |
||
179 | * @return PublicKeyInterface |
||
180 | */ |
||
181 | 44 | public function getPublicKey(): PublicKeyInterface |
|
182 | { |
||
183 | 44 | if ($this->isPrivate()) { |
|
184 | 36 | return $this->getPrivateKey()->getPublicKey(); |
|
185 | } else { |
||
186 | /** @var PublicKeyInterface $key */ |
||
187 | 27 | $key = $this->key; |
|
188 | 27 | return $key; |
|
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @return HierarchicalKey |
||
194 | */ |
||
195 | 21 | public function withoutPrivateKey(): HierarchicalKey |
|
196 | { |
||
197 | 21 | $clone = clone $this; |
|
198 | 21 | $clone->key = $clone->getPublicKey(); |
|
199 | 21 | return $clone; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param ScriptDataFactory $factory |
||
204 | * @return HierarchicalKey |
||
205 | */ |
||
206 | 1 | public function withScriptFactory(ScriptDataFactory $factory) |
|
207 | { |
||
208 | 1 | $clone = clone $this; |
|
209 | 1 | $clone->scriptDataFactory = $factory; |
|
210 | 1 | $clone->scriptAndSignData = null; // we cache, don't forget to clear |
|
211 | 1 | return $clone; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * @return ScriptDataFactory |
||
216 | */ |
||
217 | 24 | public function getScriptDataFactory() |
|
218 | { |
||
219 | 24 | return $this->scriptDataFactory; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return \BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData |
||
224 | */ |
||
225 | 19 | public function getScriptAndSignData() |
|
226 | { |
||
227 | 19 | if (null === $this->scriptAndSignData) { |
|
228 | 19 | $this->scriptAndSignData = $this->scriptDataFactory->convertKey($this->key); |
|
229 | } |
||
230 | |||
231 | 19 | return $this->scriptAndSignData; |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param BaseAddressCreator $addressCreator |
||
236 | * @return \BitWasp\Bitcoin\Address\Address |
||
237 | */ |
||
238 | 13 | public function getAddress(BaseAddressCreator $addressCreator) |
|
239 | { |
||
240 | 13 | return $this->getScriptAndSignData()->getAddress($addressCreator); |
|
241 | } |
||
242 | |||
243 | /** |
||
244 | * Return whether this is a private key |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | 48 | public function isPrivate(): bool |
|
249 | { |
||
250 | 48 | return $this->key->isPrivate(); |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * Return whether the key is hardened |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | 2 | public function isHardened(): bool |
|
259 | { |
||
260 | 2 | return ($this->sequence >> 31) === 1; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Create a buffer containing data to be hashed hashed to yield the child offset |
||
265 | * |
||
266 | * @param int $sequence |
||
267 | * @return BufferInterface |
||
268 | * @throws \Exception |
||
269 | */ |
||
270 | 39 | public function getHmacSeed(int $sequence): BufferInterface |
|
271 | { |
||
272 | 39 | if ($sequence < 0 || $sequence > IntRange::U32_MAX) { |
|
273 | 6 | throw new \InvalidArgumentException("Sequence is outside valid range, must be >= 0 && <= (2^31)-1"); |
|
274 | } |
||
275 | |||
276 | 33 | if (($sequence >> 31) === 1) { |
|
277 | 30 | if ($this->isPrivate() === false) { |
|
278 | 2 | throw new \Exception("Can't derive a hardened key without the private key"); |
|
279 | } |
||
280 | |||
281 | 28 | $data = "\x00{$this->getPrivateKey()->getBinary()}"; |
|
282 | } else { |
||
283 | 31 | $data = $this->getPublicKey()->getBinary(); |
|
284 | } |
||
285 | |||
286 | 31 | return new Buffer($data . pack("N", $sequence)); |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * Derive a child key |
||
291 | * |
||
292 | * @param int $sequence |
||
293 | * @return HierarchicalKey |
||
294 | * @throws \Exception |
||
295 | */ |
||
296 | 36 | public function deriveChild(int $sequence): HierarchicalKey |
|
297 | { |
||
298 | 36 | $nextDepth = $this->depth + 1; |
|
299 | 36 | if ($nextDepth > 255) { |
|
300 | throw new \InvalidArgumentException('Invalid depth for BIP32 key, cannot exceed 255'); |
||
301 | } |
||
302 | |||
303 | 36 | $hash = Hash::hmac('sha512', $this->getHmacSeed($sequence), $this->chainCode); |
|
304 | 31 | $offset = $hash->slice(0, 32); |
|
305 | 31 | $chain = $hash->slice(32); |
|
306 | |||
307 | 31 | if (false === $this->ecAdapter->validatePrivateKey($offset)) { |
|
308 | 1 | return $this->deriveChild($sequence + 1); |
|
309 | } |
||
310 | |||
311 | 31 | $key = $this->isPrivate() ? $this->getPrivateKey() : $this->getPublicKey(); |
|
312 | 31 | $key = $key->tweakAdd($offset->getGmp()); |
|
313 | |||
314 | 31 | return new HierarchicalKey( |
|
315 | 31 | $this->ecAdapter, |
|
316 | 31 | $this->scriptDataFactory, |
|
317 | 31 | $nextDepth, |
|
318 | 31 | $this->getChildFingerprint(), |
|
319 | 31 | $sequence, |
|
320 | 31 | $chain, |
|
321 | 31 | $key |
|
322 | ); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param array|\stdClass|\Traversable $list |
||
327 | * @return HierarchicalKey |
||
328 | * @throws \Exception |
||
329 | */ |
||
330 | 26 | public function deriveFromList($list): HierarchicalKey |
|
343 | |||
344 | /** |
||
345 | * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
||
346 | * |
||
347 | * @param string $path |
||
348 | * @return HierarchicalKey |
||
349 | * @throws \Exception |
||
350 | */ |
||
351 | 26 | public function derivePath(string $path): HierarchicalKey |
|
356 | |||
357 | /** |
||
358 | * Serializes the instance according to whether it wraps a private or public key. |
||
359 | * @param NetworkInterface $network |
||
360 | * @return string |
||
361 | */ |
||
362 | 16 | public function toExtendedKey(NetworkInterface $network = null): string |
|
370 | |||
371 | /** |
||
372 | * Explicitly serialize as a private key. Throws an exception if |
||
373 | * the key isn't private. |
||
374 | * |
||
375 | * @param NetworkInterface $network |
||
376 | * @return string |
||
377 | */ |
||
378 | 16 | public function toExtendedPrivateKey(NetworkInterface $network = null): string |
|
386 | |||
387 | /** |
||
388 | * Explicitly serialize as a public key. This will always work. |
||
389 | * |
||
390 | * @param NetworkInterface $network |
||
391 | * @return string |
||
392 | */ |
||
393 | 13 | public function toExtendedPublicKey(NetworkInterface $network = null): string |
|
397 | } |
||
398 |
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.