Completed
Pull Request — 0.0.35 (#659)
by thomas
25:58
created

KeyToScriptDataFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 40
ccs 9
cts 9
cp 1
rs 10
wmc 4
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
convertKeyToScriptData() 0 1 ?
A convertKey() 0 9 2
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\KeyInterface;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
10
use BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData;
11
use BitWasp\Bitcoin\Key\KeyToScript\ScriptDataFactory;
12
13
abstract class KeyToScriptDataFactory extends ScriptDataFactory
14
{
15
    /**
16
     * @var PublicKeySerializerInterface
17
     */
18
    protected $pubKeySerializer;
19
20
    /**
21
     * KeyToP2PKScriptFactory constructor.
22
     * @param PublicKeySerializerInterface|null $pubKeySerializer
23
     */
24 29
    public function __construct(PublicKeySerializerInterface $pubKeySerializer = null)
25
    {
26 29
        if (null === $pubKeySerializer) {
27 29
            $pubKeySerializer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, true);
28
        }
29
30 29
        $this->pubKeySerializer = $pubKeySerializer;
31 29
    }
32
33
    /**
34
     * @param PublicKeyInterface $publicKey
35
     * @return ScriptAndSignData
36
     */
37
    abstract protected function convertKeyToScriptData(PublicKeyInterface $publicKey);
38
39
    /**
40
     * @param KeyInterface $key
41
     * @return ScriptAndSignData
42
     */
43 14
    public function convertKey(KeyInterface $key)
44
    {
45 14
        if ($key->isPrivate()) {
46
            /** @var PrivateKeyInterface $key */
47 1
            $key = $key->getPublicKey();
48
        }
49
        /** @var PublicKeyInterface $key */
50 14
        return $this->convertKeyToScriptData($key);
51
    }
52
}
53