Completed
Pull Request — 0.0.35 (#659)
by thomas
26:42
created

ExtendedKeyWithScriptSerializer::serialize()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

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