1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Serializer\Key\HierarchicalKey; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
8
|
|
|
use BitWasp\Bitcoin\Serializer\Types; |
9
|
|
|
use BitWasp\Buffertools\Buffer; |
10
|
|
|
use BitWasp\Buffertools\BufferInterface; |
11
|
|
|
use BitWasp\Buffertools\Exceptions\ParserOutOfRange; |
12
|
|
|
use BitWasp\Buffertools\Parser; |
13
|
|
|
|
14
|
|
|
class RawExtendedKeySerializer |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EcAdapterInterface |
18
|
|
|
*/ |
19
|
|
|
private $ecAdapter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \BitWasp\Buffertools\Types\ByteString |
23
|
|
|
*/ |
24
|
|
|
private $bytestring4; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \BitWasp\Buffertools\Types\Uint8 |
28
|
|
|
*/ |
29
|
|
|
private $uint8; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \BitWasp\Buffertools\Types\Uint32 |
33
|
|
|
*/ |
34
|
|
|
private $uint32; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \BitWasp\Buffertools\Types\ByteString |
38
|
|
|
*/ |
39
|
|
|
private $bytestring32; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \BitWasp\Buffertools\Types\ByteString |
43
|
|
|
*/ |
44
|
|
|
private $bytestring33; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* RawExtendedKeySerializer constructor. |
48
|
|
|
* @param EcAdapterInterface $ecAdapter |
49
|
|
|
*/ |
50
|
56 |
|
public function __construct(EcAdapterInterface $ecAdapter) |
51
|
|
|
{ |
52
|
56 |
|
$this->ecAdapter = $ecAdapter; |
53
|
56 |
|
$this->bytestring4 = Types::bytestring(4); |
54
|
56 |
|
$this->uint8 = Types::uint8(); |
55
|
56 |
|
$this->uint32 = Types::uint32(); |
56
|
56 |
|
$this->bytestring32 = Types::bytestring(32); |
57
|
56 |
|
$this->bytestring33 = Types::bytestring(33); |
58
|
56 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param RawKeyParams $keyParams |
62
|
|
|
* @return BufferInterface |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
24 |
|
public function serialize(RawKeyParams $keyParams): BufferInterface |
66
|
|
|
{ |
67
|
24 |
|
return new Buffer( |
68
|
24 |
|
pack("H*", $keyParams->getPrefix()) . |
69
|
24 |
|
$this->uint8->write($keyParams->getDepth()) . |
70
|
24 |
|
$this->uint32->write($keyParams->getParentFingerprint()) . |
71
|
24 |
|
$this->uint32->write($keyParams->getSequence()) . |
72
|
24 |
|
$this->bytestring32->write($keyParams->getChainCode()) . |
|
|
|
|
73
|
24 |
|
$this->bytestring33->write($keyParams->getKeyData()) |
|
|
|
|
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Parser $parser |
79
|
|
|
* @return RawKeyParams |
80
|
|
|
* @throws ParserOutOfRange |
81
|
|
|
*/ |
82
|
36 |
|
public function fromParser(Parser $parser): RawKeyParams |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
36 |
|
return new RawKeyParams( |
86
|
36 |
|
$this->bytestring4->read($parser)->getHex(), |
87
|
34 |
|
(int) $this->uint8->read($parser), |
88
|
34 |
|
(int) $this->uint32->read($parser), |
89
|
34 |
|
(int) $this->uint32->read($parser), |
90
|
34 |
|
$this->bytestring32->read($parser), |
91
|
33 |
|
$this->bytestring33->read($parser) |
92
|
|
|
); |
93
|
3 |
|
} catch (ParserOutOfRange $e) { |
94
|
3 |
|
throw new ParserOutOfRange('Failed to extract HierarchicalKey from parser'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.