Completed
Pull Request — 0.0.35 (#659)
by thomas
26:42
created

KeyToScriptDataFactory::convertKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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