Completed
Push — master ( 6f9908...2cb166 )
by thomas
71:43 queued 12s
created

PrivateKeySerializer::fromParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 4
cp 0.75
crap 1.0156
rs 10
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
     * @param EcAdapter $ecAdapter
22
     */
23
    public function __construct(EcAdapter $ecAdapter)
24
    {
25
        $this->ecAdapter = $ecAdapter;
26
    }
27
28 134
    /**
29
     * @param PrivateKeyInterface $privateKey
30 134
     * @return BufferInterface
31 134
     */
32
    public function serialize(PrivateKeyInterface $privateKey)
33
    {
34
        return Buffer::int(gmp_strval($privateKey->getSecret(), 10), 32, $this->ecAdapter->getMath());
35
    }
36
37 98
    /**
38
     * @param Parser $parser
39 98
     * @return PrivateKey
40 98
     */
41 98
    public function fromParser(Parser $parser)
42 98
    {
43 49
        return $this->ecAdapter->getPrivateKey($parser->readBytes(32)->getGmp());
44
    }
45
46
    /**
47
     * @param BufferInterface|string $string
48
     * @return PrivateKey
49
     */
50
    public function parse($string)
51
    {
52
        return $this->fromParser(new Parser($string));
53
    }
54
}
55