|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Serializer\Key\HierarchicalKey; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
|
6
|
|
|
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKey; |
|
7
|
|
|
use BitWasp\Bitcoin\Key\PrivateKeyFactory; |
|
8
|
|
|
use BitWasp\Bitcoin\Key\PublicKeyFactory; |
|
9
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
|
10
|
|
|
use BitWasp\Buffertools\Buffer; |
|
11
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
12
|
|
|
use BitWasp\Buffertools\Exceptions\ParserOutOfRange; |
|
13
|
|
|
use BitWasp\Buffertools\Parser; |
|
14
|
|
|
|
|
15
|
|
|
class ExtendedKeySerializer |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EcAdapterInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $ecAdapter; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var RawExtendedKeySerializer |
|
24
|
|
|
*/ |
|
25
|
|
|
private $rawSerializer; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param EcAdapterInterface $ecAdapter |
|
29
|
|
|
* @throws \Exception |
|
30
|
|
|
*/ |
|
31
|
44 |
|
public function __construct(EcAdapterInterface $ecAdapter) |
|
32
|
|
|
{ |
|
33
|
44 |
|
$this->ecAdapter = $ecAdapter; |
|
34
|
44 |
|
$this->rawSerializer = new RawExtendedKeySerializer($ecAdapter); |
|
35
|
44 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param NetworkInterface $network |
|
39
|
|
|
* @param HierarchicalKey $key |
|
40
|
|
|
* @return BufferInterface |
|
41
|
|
|
*/ |
|
42
|
12 |
|
public function serialize(NetworkInterface $network, HierarchicalKey $key) |
|
43
|
|
|
{ |
|
44
|
12 |
|
if ($key->isPrivate()) { |
|
45
|
12 |
|
$prefix = $network->getHDPrivByte(); |
|
46
|
12 |
|
$keyData = new Buffer("\x00" . $key->getPrivateKey()->getBinary()); |
|
47
|
|
|
} else { |
|
48
|
9 |
|
$prefix = $network->getHDPubByte(); |
|
49
|
9 |
|
$keyData = $key->getPublicKey()->getBuffer(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
12 |
|
return $this->rawSerializer->serialize( |
|
53
|
12 |
|
new RawKeyParams( |
|
54
|
12 |
|
$prefix, |
|
55
|
12 |
|
$key->getDepth(), |
|
56
|
12 |
|
$key->getFingerprint(), |
|
57
|
12 |
|
$key->getSequence(), |
|
58
|
12 |
|
$key->getChainCode(), |
|
59
|
12 |
|
$keyData |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param NetworkInterface $network |
|
66
|
|
|
* @param Parser $parser |
|
67
|
|
|
* @return HierarchicalKey |
|
68
|
|
|
* @throws ParserOutOfRange |
|
69
|
|
|
*/ |
|
70
|
36 |
|
public function fromParser(NetworkInterface $network, Parser $parser) |
|
71
|
|
|
{ |
|
72
|
36 |
|
$params = $this->rawSerializer->fromParser($parser); |
|
73
|
|
|
|
|
74
|
33 |
|
if ($params->getPrefix() === $network->getHDPubByte()) { |
|
75
|
20 |
|
$key = PublicKeyFactory::fromHex($params->getKeyData(), $this->ecAdapter); |
|
76
|
17 |
|
} else if ($params->getPrefix() === $network->getHDPrivByte()) { |
|
77
|
16 |
|
$key = PrivateKeyFactory::fromHex($params->getKeyData()->slice(1), true, $this->ecAdapter); |
|
78
|
|
|
} else { |
|
79
|
1 |
|
throw new \InvalidArgumentException('HD key magic bytes do not match network magic bytes'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
32 |
|
return new HierarchicalKey( |
|
83
|
32 |
|
$this->ecAdapter, |
|
84
|
32 |
|
$params->getDepth(), |
|
85
|
32 |
|
$params->getParentFingerprint(), |
|
86
|
32 |
|
$params->getSequence(), |
|
87
|
32 |
|
$params->getChainCode(), |
|
88
|
32 |
|
$key |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param NetworkInterface $network |
|
94
|
|
|
* @param BufferInterface $buffer |
|
95
|
|
|
* @return HierarchicalKey |
|
96
|
|
|
*/ |
|
97
|
36 |
|
public function parse(NetworkInterface $network, BufferInterface $buffer) |
|
98
|
|
|
{ |
|
99
|
36 |
|
return $this->fromParser($network, new Parser($buffer)); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|