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

KeyToScriptDataFactory::convertKeyToScriptData()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
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\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