Completed
Push — master ( abcf31...a8a46d )
by thomas
28:08 queued 25:46
created

KeyToScriptDataFactory::convertKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 9.6666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key\KeyToScript\Factory;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
11
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
12
use BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData;
13
use BitWasp\Bitcoin\Key\KeyToScript\ScriptDataFactory;
14
15
abstract class KeyToScriptDataFactory extends ScriptDataFactory
16
{
17
    /**
18
     * @var PublicKeySerializerInterface
19
     */
20
    protected $pubKeySerializer;
21
22
    /**
23
     * KeyToP2PKScriptFactory constructor.
24
     * @param PublicKeySerializerInterface|null $pubKeySerializer
25
     */
26 108
    public function __construct(PublicKeySerializerInterface $pubKeySerializer = null)
27
    {
28 108
        if (null === $pubKeySerializer) {
29 85
            $pubKeySerializer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, true);
30
        }
31
32 108
        $this->pubKeySerializer = $pubKeySerializer;
33 108
    }
34
35
    /**
36
     * @param PublicKeyInterface $publicKey
37
     * @return ScriptAndSignData
38
     */
39
    abstract protected function convertKeyToScriptData(PublicKeyInterface $publicKey): ScriptAndSignData;
40
41
    /**
42
     * @param KeyInterface $key
43
     * @return ScriptAndSignData
44
     */
45 32
    public function convertKey(KeyInterface $key): ScriptAndSignData
46
    {
47 32
        if ($key->isPrivate()) {
48
            /** @var PrivateKeyInterface $key */
49 23
            $key = $key->getPublicKey();
50
        }
51
        /** @var PublicKeyInterface $key */
52 32
        return $this->convertKeyToScriptData($key);
53
    }
54
}
55