Completed
Pull Request — master (#285)
by thomas
21:56
created

PayToPubkey::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 12
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 PublicKeyInterface
14
     */
15
    private $publicKey;
16
17
    /**
18
     * @param ScriptInterface $script
19
     */
20
    public function __construct(ScriptInterface $script)
21
    {
22
        $chunks = $script->getScriptParser()->decode();
23
        if (count($chunks) < 1 || !$chunks[0]->isPush()) {
24
            throw new \InvalidArgumentException('Malformed pay-to-pubkey script');
25
        }
26
        $this->publicKey = PublicKeyFactory::fromHex($chunks[0]->getData());
27
    }
28
29
    /**
30
     * @return int
31
     */
32
    public function getRequiredSigCount()
33
    {
34
        return 1;
35
    }
36
37
    /**
38
     * @return int
39
     */
40
    public function getKeyCount()
41
    {
42
        return 1;
43
    }
44
45
    /**
46
     * @param PublicKeyInterface $publicKey
47
     * @return bool
48
     */
49
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
50
    {
51
        return $publicKey->getBinary() === $this->publicKey->getBinary();
52
    }
53
54
    /**
55
     * @return PublicKeyInterface[]
56
     */
57
    public function getKeys()
58
    {
59
        return [$this->publicKey];
60
    }
61
}
62