Completed
Pull Request — master (#446)
by thomas
105:00 queued 101:56
created

PrivateKeySerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 55%

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 11
cts 20
cp 0.55
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A doSerialize() 0 4 1
A serialize() 0 5 1
A fromParser() 0 4 1
A parse() 0 4 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Key;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\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
/**
14
 * Private Key Serializer - specific to secp256k1
15
 */
16
class PrivateKeySerializer implements PrivateKeySerializerInterface
17
{
18
    /**
19
     * @var EcAdapter
20
     */
21
    private $ecAdapter;
22
23
    /**
24
     * @param EcAdapter $ecAdapter
25
     */
26 83
    public function __construct(EcAdapter $ecAdapter)
27
    {
28 83
        $this->ecAdapter = $ecAdapter;
29 83
    }
30
31
    /**
32
     * @param PrivateKey $privateKey
33
     * @return BufferInterface
34
     */
35 68
    private function doSerialize(PrivateKey $privateKey)
36
    {
37 68
        return new Buffer($privateKey->getSecretBinary(), 32, $this->ecAdapter->getMath());
38
    }
39
40
    /**
41
     * @param PrivateKeyInterface $privateKey
42
     * @return BufferInterface
43
     */
44 68
    public function serialize(PrivateKeyInterface $privateKey)
45
    {
46
        /** @var PrivateKey $privateKey */
47 68
        return $this->doSerialize($privateKey);
48
    }
49
50
    /**
51
     * @param Parser $parser
52
     * @return PrivateKey
53
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
54
     */
55 40
    public function fromParser(Parser $parser)
56
    {
57 40
        return $this->ecAdapter->getPrivateKey($parser->readBytes(32)->getGmp());
58
    }
59
60
    /**
61
     * @param \BitWasp\Buffertools\BufferInterface|string $data
62
     * @return PrivateKey
63
     */
64 40
    public function parse($data)
65
    {
66 40
        return $this->fromParser(new Parser($data, $this->ecAdapter->getMath()));
67
    }
68
}
69