Completed
Pull Request — 0.0.35 (#659)
by thomas
30:11 queued 21:19
created

ExtendedKeyWithScriptSerializer::fromParser()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 3
nop 2
dl 0
loc 28
ccs 0
cts 19
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
4
namespace BitWasp\Bitcoin\Serializer\Key\ScriptedHierarchicalKey;
5
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
7
use BitWasp\Bitcoin\Key\Deterministic\HdPrefix\GlobalPrefixConfig;
8
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKey;
9
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyScriptDecorator;
10
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
11
use BitWasp\Bitcoin\Key\PublicKeyFactory;
12
use BitWasp\Bitcoin\Network\NetworkInterface;
13
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\RawExtendedKeySerializer;
14
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\RawKeyParams;
15
use BitWasp\Buffertools\Buffer;
16
use BitWasp\Buffertools\BufferInterface;
17
use BitWasp\Buffertools\Exceptions\ParserOutOfRange;
18
use BitWasp\Buffertools\Parser;
19
20
class ExtendedKeyWithScriptSerializer
21
{
22
23
    /**
24
     * @var EcAdapterInterface
25
     */
26
    private $ecAdapter;
27
28
    /**
29
     * @var RawExtendedKeySerializer
30
     */
31
    private $rawSerializer;
32
33
    /**
34
     * @var GlobalPrefixConfig
35
     */
36
    private $config;
37
38
    /**
39
     * ExtendedKeyWithScriptSerializer constructor.
40
     * @param EcAdapterInterface $ecAdapter
41
     * @param GlobalPrefixConfig $hdPrefixConfig
42
     */
43 2
    public function __construct(EcAdapterInterface $ecAdapter, GlobalPrefixConfig $hdPrefixConfig)
44
    {
45 2
        $this->ecAdapter = $ecAdapter;
46 2
        $this->rawSerializer = new RawExtendedKeySerializer($ecAdapter);
47 2
        $this->config = $hdPrefixConfig;
48 2
    }
49
50
    /**
51
     * @param NetworkInterface $network
52
     * @param HierarchicalKeyScriptDecorator $key
53
     * @return BufferInterface
54
     * @throws \Exception
55
     */
56 2
    public function serialize(NetworkInterface $network, HierarchicalKeyScriptDecorator $key)
57
    {
58 2
        $scriptConfig = $this->config
59 2
            ->getNetworkConfig($network)
60 2
            ->getConfigForScriptType($key->getScriptDataFactory()->getScriptType())
61
        ;
62
63 2
        $hdKey = $key->getHdKey();
64 2
        if ($hdKey->isPrivate()) {
65 2
            $prefix = $scriptConfig->getPrivatePrefix();
66 2
            $keyData = new Buffer("\x00" . $hdKey->getPrivateKey()->getBinary());
67
        } else {
68 2
            $prefix = $scriptConfig->getPublicPrefix();
69 2
            $keyData = $hdKey->getPublicKey()->getBuffer();
70
        }
71
72 2
        return $this->rawSerializer->serialize(new RawKeyParams(
73 2
            $prefix,
74 2
            $hdKey->getDepth(),
75 2
            $hdKey->getFingerprint(),
76 2
            $hdKey->getSequence(),
77 2
            $hdKey->getChainCode(),
78 2
            $keyData
79
        ));
80
    }
81
82
    /**
83
     * @param NetworkInterface $network
84
     * @param Parser $parser
85
     * @return HierarchicalKeyScriptDecorator
86
     * @throws ParserOutOfRange
87
     * @throws \Exception
88
     */
89
    public function fromParser(NetworkInterface $network, Parser $parser)
90
    {
91
        $params = $this->rawSerializer->fromParser($parser);
92
        $scriptConfig = $this->config
93
            ->getNetworkConfig($network)
94
            ->getConfigForPrefix($params->getPrefix())
95
        ;
96
97
        if ($params->getPrefix() === $scriptConfig->getPublicPrefix()) {
98
            $key = PublicKeyFactory::fromHex($params->getKeyData(), $this->ecAdapter);
99
        } else if ($params->getPrefix() === $scriptConfig->getPrivatePrefix()) {
100
            $key = PrivateKeyFactory::fromHex($params->getKeyData()->slice(1), true, $this->ecAdapter);
101
        } else {
102
            throw new \InvalidArgumentException('Invalid prefix for extended key');
103
        }
104
105
        return new HierarchicalKeyScriptDecorator(
106
            $scriptConfig->getScriptDataFactory(),
107
            new HierarchicalKey(
108
                $this->ecAdapter,
109
                $params->getDepth(),
110
                $params->getParentFingerprint(),
111
                $params->getSequence(),
112
                $params->getChainCode(),
113
                $key
114
            )
115
        );
116
    }
117
118
    /**
119
     * @param NetworkInterface $network
120
     * @param BufferInterface $buffer
121
     * @return HierarchicalKeyScriptDecorator
122
     * @throws ParserOutOfRange
123
     */
124
    public function parse(NetworkInterface $network, BufferInterface $buffer)
125
    {
126
        return $this->fromParser($network, new Parser($buffer));
127
    }
128
}
129