1 | <?php |
||
19 | class HierarchicalKey |
||
20 | { |
||
21 | /** |
||
22 | * @var EcAdapterInterface |
||
23 | */ |
||
24 | private $ecAdapter; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | private $depth; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | private $parentFingerprint; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | private $sequence; |
||
40 | |||
41 | /** |
||
42 | * @var BufferInterface |
||
43 | */ |
||
44 | private $chainCode; |
||
45 | |||
46 | /** |
||
47 | * @var KeyInterface |
||
48 | */ |
||
49 | private $key; |
||
50 | |||
51 | /** |
||
52 | * @param EcAdapterInterface $ecAdapter |
||
53 | * @param int $depth |
||
54 | * @param int $parentFingerprint |
||
55 | * @param int $sequence |
||
56 | * @param BufferInterface $chainCode |
||
57 | * @param KeyInterface $key |
||
58 | * @throws \Exception |
||
59 | */ |
||
60 | 150 | public function __construct(EcAdapterInterface $ecAdapter, $depth, $parentFingerprint, $sequence, BufferInterface $chainCode, KeyInterface $key) |
|
61 | { |
||
62 | 150 | if ($chainCode->getSize() !== 32) { |
|
63 | throw new \RuntimeException('Chaincode should be 32 bytes'); |
||
64 | } |
||
65 | |||
66 | 150 | if (!$key->isCompressed()) { |
|
67 | 6 | throw new \InvalidArgumentException('A HierarchicalKey must always be compressed'); |
|
68 | } |
||
69 | |||
70 | 144 | $this->ecAdapter = $ecAdapter; |
|
71 | 144 | $this->depth = $depth; |
|
72 | 144 | $this->sequence = $sequence; |
|
73 | 144 | $this->parentFingerprint = $parentFingerprint; |
|
74 | 144 | $this->chainCode = $chainCode; |
|
75 | 144 | $this->key = $key; |
|
76 | 144 | } |
|
77 | |||
78 | /** |
||
79 | * Return the depth of this key. This is limited to 256 sequential derivations. |
||
80 | * |
||
81 | * @return int |
||
82 | */ |
||
83 | 66 | public function getDepth() |
|
87 | |||
88 | /** |
||
89 | * Get the sequence number for this address. Hardened keys are |
||
90 | * created with sequence > 0x80000000. a sequence number lower |
||
91 | * than this can be derived with the public key. |
||
92 | * |
||
93 | * @return int |
||
94 | */ |
||
95 | 36 | public function getSequence() |
|
99 | |||
100 | /** |
||
101 | * Get the fingerprint of the parent key. For master keys, this is 00000000. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 36 | public function getFingerprint() |
|
113 | |||
114 | /** |
||
115 | * Return the fingerprint to be used for child keys. |
||
116 | * @return int |
||
117 | */ |
||
118 | 42 | public function getChildFingerprint() |
|
122 | |||
123 | /** |
||
124 | * Return the chain code - a deterministic 'salt' for HMAC-SHA512 |
||
125 | * in child derivations |
||
126 | * |
||
127 | * @return BufferInterface |
||
128 | */ |
||
129 | 48 | public function getChainCode() |
|
133 | |||
134 | /** |
||
135 | * @return PrivateKeyInterface |
||
136 | */ |
||
137 | 78 | public function getPrivateKey() |
|
145 | |||
146 | /** |
||
147 | * Get the public key the private key or public key. |
||
148 | * |
||
149 | * @return PublicKey |
||
150 | */ |
||
151 | 84 | public function getPublicKey() |
|
159 | |||
160 | /** |
||
161 | * @return HierarchicalKey |
||
162 | */ |
||
163 | 24 | public function toPublic() |
|
171 | |||
172 | /** |
||
173 | * Return whether this is a private key |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | 96 | public function isPrivate() |
|
181 | |||
182 | /** |
||
183 | * Return whether the key is hardened |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | 6 | public function isHardened() |
|
191 | |||
192 | /** |
||
193 | * Create a buffer containing data to be hashed hashed to yield the child offset |
||
194 | * |
||
195 | * @param int $sequence |
||
196 | * @return BufferInterface |
||
197 | * @throws \Exception |
||
198 | */ |
||
199 | 42 | public function getHmacSeed($sequence) |
|
215 | |||
216 | /** |
||
217 | * Derive a child key |
||
218 | * |
||
219 | * @param int $sequence |
||
220 | * @return HierarchicalKey |
||
221 | * @throws \Exception |
||
222 | */ |
||
223 | 42 | public function deriveChild($sequence) |
|
249 | |||
250 | 18 | /** |
|
251 | * @param array|\stdClass|\Traversable $list |
||
252 | 18 | * @return HierarchicalKey |
|
253 | */ |
||
254 | public function deriveFromList($list) |
||
267 | |||
268 | /** |
||
269 | * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
||
270 | * |
||
271 | 18 | * @param string $path |
|
272 | * @return HierarchicalKey |
||
273 | 18 | * @throws \Exception |
|
274 | 18 | */ |
|
275 | public function derivePath($path) |
||
280 | |||
281 | /** |
||
282 | 30 | * Serializes the instance according to whether it wraps a private or public key. |
|
283 | * @param NetworkInterface $network |
||
284 | 30 | * @return string |
|
285 | */ |
||
286 | 30 | public function toExtendedKey(NetworkInterface $network = null) |
|
294 | |||
295 | /** |
||
296 | * Explicitly serialize as a private key. Throws an exception if |
||
297 | * the key isn't private. |
||
298 | 30 | * |
|
299 | * @param NetworkInterface $network |
||
300 | 30 | * @return string |
|
301 | 6 | */ |
|
302 | public function toExtendedPrivateKey(NetworkInterface $network = null) |
||
310 | |||
311 | /** |
||
312 | * Explicitly serialize as a public key. This will always work. |
||
313 | 24 | * |
|
314 | * @param NetworkInterface $network |
||
315 | 24 | * @return string |
|
316 | 24 | */ |
|
317 | public function toExtendedPublicKey(NetworkInterface $network = null) |
||
322 | } |
||
323 |
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.