Completed
Push — master ( 3c06bf...ecab00 )
by thomas
43:28 queued 40:12
created

PayToPubkey::checkInvolvesKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\ScriptInfo;
4
5
use BitWasp\Bitcoin\Key\PublicKeyFactory;
6
use BitWasp\Bitcoin\Script\Classifier\OutputClassifier;
7
use BitWasp\Bitcoin\Script\ScriptInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
9
10
class PayToPubkey implements ScriptInfoInterface
11
{
12
    /**
13
     * @var ScriptInterface
14
     */
15
    private $script;
16
17
    /**
18
     * @var PublicKeyInterface
19
     */
20
    private $publicKey;
21
22
    /**
23
     * @param ScriptInterface $script
24
     */
25
    public function __construct(ScriptInterface $script)
26
    {
27
        $this->script = $script;
28
        $chunks = $script->getScriptParser()->decode();
29
        if (count($chunks) < 1 || !$chunks[0]->isPush()) {
30
            throw new \InvalidArgumentException('Malformed pay-to-pubkey script');
31
        }
32
        $this->publicKey = PublicKeyFactory::fromHex($chunks[0]->getData());
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function classification()
39
    {
40
        return OutputClassifier::PAYTOPUBKEY;
41
    }
42
43
    /**
44
     * @return int
45
     */
46
    public function getRequiredSigCount()
47
    {
48
        return 1;
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function getKeyCount()
55
    {
56
        return 1;
57
    }
58
59
    /**
60
     * @param PublicKeyInterface $publicKey
61
     * @return bool
62
     */
63
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
64
    {
65
        return $publicKey->getBinary() === $this->publicKey->getBinary();
66
    }
67
68
    /**
69
     * @return PublicKeyInterface[]
70
     */
71
    public function getKeys()
72
    {
73
        return [$this->publicKey];
74
    }
75
}
76