Completed
Push — 0.0.35 ( b2fc74...d8d049 )
by thomas
28:12 queued 09:10
created

PrivateKeySerializer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 4 1
A fromParser() 0 4 1
A parse() 0 4 1
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\Impl\PhpEcc\Key\PrivateKey;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PrivateKeySerializerInterface;
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 62
    public function __construct(EcAdapter $ecAdapter)
24
    {
25 62
        $this->ecAdapter = $ecAdapter;
26 62
    }
27
28
    /**
29
     * @param PrivateKeyInterface $privateKey
30
     * @return BufferInterface
31
     */
32 52
    public function serialize(PrivateKeyInterface $privateKey)
33
    {
34 52
        return Buffer::int(gmp_strval($privateKey->getSecret(), 10), 32, $this->ecAdapter->getMath());
35
    }
36
37
    /**
38
     * @param Parser $parser
39
     * @return PrivateKey
40
     */
41 27
    public function fromParser(Parser $parser)
42
    {
43 27
        return $this->ecAdapter->getPrivateKey($parser->readBytes(32)->getGmp());
44
    }
45
46
    /**
47
     * @param BufferInterface|string $string
48
     * @return PrivateKey
49
     */
50 27
    public function parse($string)
51
    {
52 27
        return $this->fromParser(new Parser($string));
53
    }
54
}
55