Completed
Push — master ( a22894...37130c )
by thomas
62:03 queued 58:09
created

PrivateKeySerializer::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0156

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 6
cts 8
cp 0.75
crap 1.0156
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Serializer\Key;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PrivateKeySerializerInterface;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PrivateKey;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
9
use BitWasp\Buffertools\Buffer;
10
use BitWasp\Buffertools\BufferInterface;
11
use BitWasp\Buffertools\Parser;
12
13
class PrivateKeySerializer implements PrivateKeySerializerInterface
14
{
15
    /**
16
     * @var EcAdapter
17
     */
18
    private $ecAdapter;
19
20
    /**
21
     * @var bool
22
     */
23
    private $haveNextCompressed = false;
24
25
    /**
26
     * @param EcAdapter $ecAdapter
27
     */
28 110
    public function __construct(EcAdapter $ecAdapter)
29
    {
30 110
        $this->ecAdapter = $ecAdapter;
31 110
    }
32
33
    /**
34
     * @param PrivateKeyInterface $privateKey
35
     * @return BufferInterface
36
     */
37 70
    public function serialize(PrivateKeyInterface $privateKey)
38
    {
39 70
        return Buffer::int(
40 70
            $privateKey->getSecretMultiplier(),
41 70
            32,
42 70
            $this->ecAdapter->getMath()
43 35
        );
44
    }
45
46
    /**
47
     * @return $this
48
     */
49
    public function setNextCompressed()
50
    {
51
        $this->haveNextCompressed = true;
52
        return $this;
53
    }
54
55
    /**
56
     * @param Parser $parser
57
     * @return PrivateKey
58
     */
59 56
    public function fromParser(Parser $parser)
60
    {
61 56
        $compressed = $this->haveNextCompressed;
62 56
        $this->haveNextCompressed = false;
63 56
        return $this->ecAdapter->getPrivateKey($parser->readBytes(32)->getInt(), $compressed);
64
    }
65
66
    /**
67
     * @param BufferInterface|string $string
68
     * @return PrivateKey
69
     */
70 56
    public function parse($string)
71
    {
72 56
        return $this->fromParser(new Parser($string));
73
    }
74
}
75