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

RawExtendedKeySerializer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 7

3 Methods

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