Completed
Pull Request — master (#665)
by thomas
15:35
created

PublicKeyFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key\Factory;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
11
use BitWasp\Buffertools\Buffer;
12
use BitWasp\Buffertools\BufferInterface;
13
14
class PublicKeyFactory
15
{
16
    /**
17
     * @var PublicKeySerializerInterface
18
     */
19
    private $serializer;
20
21
    /**
22
     * PublicKeyFactory constructor.
23
     * @param EcAdapterInterface $ecAdapter
24
     */
25
    public function __construct(EcAdapterInterface $ecAdapter)
26
    {
27
        $this->serializer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter);
28
    }
29
30
    /**
31
     * @param string $hex
32
     * @return PublicKeyInterface
33
     * @throws \Exception
34
     */
35
    public function fromHex(string $hex): PublicKeyInterface
36
    {
37
        return $this->fromBuffer(Buffer::hex($hex));
38
    }
39
40
    /**
41
     * @param BufferInterface $buffer
42
     * @return PublicKeyInterface
43
     */
44
    public function fromBuffer(BufferInterface $buffer): PublicKeyInterface
45
    {
46
        return $this->serializer->parse($buffer);
47
    }
48
}
49