Completed
Pull Request — 0.0.35 (#659)
by thomas
25:58
created

ExtendedKeySerializer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 107
ccs 41
cts 41
cp 1
rs 9.1666
wmc 6
lcom 1
cbo 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B serialize() 0 25 2
B fromParser() 0 26 2
A parse() 0 4 1
1
<?php
2
3
4
namespace BitWasp\Bitcoin\Serializer\Key\ScriptDecoratedHierarchicalKey;
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 ExtendedKeySerializer
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 3
    public function __construct(EcAdapterInterface $ecAdapter, GlobalPrefixConfig $hdPrefixConfig)
44
    {
45 3
        $this->ecAdapter = $ecAdapter;
46 3
        $this->rawSerializer = new RawExtendedKeySerializer($ecAdapter);
47 3
        $this->config = $hdPrefixConfig;
48 3
    }
49
50
    /**
51
     * @param NetworkInterface $network
52
     * @param HierarchicalKeyScriptDecorator $key
53
     * @return BufferInterface
54
     * @throws \Exception
55
     */
56 3
    public function serialize(NetworkInterface $network, HierarchicalKeyScriptDecorator $key)
57
    {
58 3
        $scriptConfig = $this->config
59 3
            ->getNetworkConfig($network)
60 3
            ->getConfigForScriptType($key->getScriptDataFactory()->getScriptType())
61
        ;
62
63 3
        $hdKey = $key->getHdKey();
64 3
        if ($hdKey->isPrivate()) {
65 3
            $prefix = $scriptConfig->getPrivatePrefix();
66 3
            $keyData = new Buffer("\x00" . $hdKey->getPrivateKey()->getBinary());
67
        } else {
68 3
            $prefix = $scriptConfig->getPublicPrefix();
69 3
            $keyData = $hdKey->getPublicKey()->getBuffer();
70
        }
71
72 3
        return $this->rawSerializer->serialize(new RawKeyParams(
73 3
            $prefix,
74 3
            $hdKey->getDepth(),
75 3
            $hdKey->getFingerprint(),
76 3
            $hdKey->getSequence(),
77 3
            $hdKey->getChainCode(),
78 3
            $keyData
79
        ));
80
    }
81
82
    /**
83
     * @param NetworkInterface $network
84
     * @param Parser $parser
85
     * @return HierarchicalKeyScriptDecorator
86
     * @throws ParserOutOfRange
87
     * @throws \Exception
88
     */
89 1
    public function fromParser(NetworkInterface $network, Parser $parser)
90
    {
91 1
        $params = $this->rawSerializer->fromParser($parser);
92 1
        $scriptConfig = $this->config
93 1
            ->getNetworkConfig($network)
94 1
            ->getConfigForPrefix($params->getPrefix())
95
        ;
96
97 1
        if ($params->getPrefix() === $scriptConfig->getPrivatePrefix()) {
98 1
            $key = PrivateKeyFactory::fromHex($params->getKeyData()->slice(1), true, $this->ecAdapter);
99
        } else {
100 1
            $key = PublicKeyFactory::fromHex($params->getKeyData(), $this->ecAdapter);
101
        }
102
103 1
        return new HierarchicalKeyScriptDecorator(
104 1
            $scriptConfig->getScriptDataFactory(),
105 1
            new HierarchicalKey(
106 1
                $this->ecAdapter,
107 1
                $params->getDepth(),
108 1
                $params->getParentFingerprint(),
109 1
                $params->getSequence(),
110 1
                $params->getChainCode(),
111 1
                $key
112
            )
113
        );
114
    }
115
116
    /**
117
     * @param NetworkInterface $network
118
     * @param BufferInterface $buffer
119
     * @return HierarchicalKeyScriptDecorator
120
     * @throws ParserOutOfRange
121
     */
122 1
    public function parse(NetworkInterface $network, BufferInterface $buffer)
123
    {
124 1
        return $this->fromParser($network, new Parser($buffer));
125
    }
126
}
127