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