Completed
Pull Request — master (#286)
by thomas
37:17
created

PayToPubkey::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

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