Completed
Push — master ( a8a46d...6b0176 )
by thomas
23:45
created

PublicKeyFactory::fromHex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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