|
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
|
56 |
|
public function __construct(EcAdapterInterface $ecAdapter) |
|
49
|
|
|
{ |
|
50
|
56 |
|
$this->ecAdapter = $ecAdapter; |
|
51
|
56 |
|
$this->bytestring4 = Types::bytestring(4); |
|
52
|
56 |
|
$this->uint8 = Types::uint8(); |
|
53
|
56 |
|
$this->uint32 = Types::uint32(); |
|
54
|
56 |
|
$this->bytestring32 = Types::bytestring(32); |
|
55
|
56 |
|
$this->bytestring33 = Types::bytestring(33); |
|
56
|
56 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param RawKeyParams $keyParams |
|
60
|
|
|
* @return BufferInterface |
|
61
|
|
|
* @throws \Exception |
|
62
|
|
|
*/ |
|
63
|
28 |
|
public function serialize(RawKeyParams $keyParams) |
|
64
|
|
|
{ |
|
65
|
28 |
|
return new Buffer( |
|
66
|
28 |
|
pack("H*", $keyParams->getPrefix()) . |
|
67
|
28 |
|
$this->uint8->write($keyParams->getDepth()) . |
|
68
|
28 |
|
$this->uint32->write($keyParams->getParentFingerprint()) . |
|
69
|
28 |
|
$this->uint32->write($keyParams->getSequence()) . |
|
70
|
28 |
|
$this->bytestring32->write($keyParams->getChainCode()) . |
|
71
|
28 |
|
$this->bytestring33->write($keyParams->getKeyData()) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param Parser $parser |
|
77
|
|
|
* @return RawKeyParams |
|
78
|
|
|
* @throws ParserOutOfRange |
|
79
|
|
|
*/ |
|
80
|
40 |
|
public function fromParser(Parser $parser) |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
40 |
|
return new RawKeyParams( |
|
84
|
40 |
|
$this->bytestring4->read($parser)->getHex(), |
|
85
|
38 |
|
$this->uint8->read($parser), |
|
86
|
38 |
|
$this->uint32->read($parser), |
|
87
|
38 |
|
$this->uint32->read($parser), |
|
88
|
38 |
|
$this->bytestring32->read($parser), |
|
89
|
37 |
|
$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
|
|
|
|