Completed
Pull Request — master (#286)
by thomas
30:16
created

PayToPubkey   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 92.86%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 52
wmc 8
lcom 1
cbo 5
ccs 13
cts 14
cp 0.9286
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 4
A getRequiredSigCount() 0 4 1
A getKeyCount() 0 4 1
A checkInvolvesKey() 0 4 1
A getKeys() 0 4 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\ScriptInfo;
4
5
use BitWasp\Bitcoin\Key\PublicKeyFactory;
6
use BitWasp\Bitcoin\Script\Opcodes;
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 6
    public function __construct(ScriptInterface $script)
21
    {
22 6
        $chunks = $script->getScriptParser()->decode();
23 6
        if (count($chunks) !== 2 || !$chunks[0]->isPush() || $chunks[1]->getOp() !== Opcodes::OP_CHECKSIG) {
24
            throw new \InvalidArgumentException('Malformed pay-to-pubkey script');
25
        }
26 6
        $this->publicKey = PublicKeyFactory::fromHex($chunks[0]->getData());
27 6
    }
28
29
    /**
30
     * @return int
31
     */
32 6
    public function getRequiredSigCount()
33
    {
34 6
        return 1;
35
    }
36
37
    /**
38
     * @return int
39
     */
40 6
    public function getKeyCount()
41
    {
42 6
        return 1;
43
    }
44
45
    /**
46
     * @param PublicKeyInterface $publicKey
47
     * @return bool
48
     */
49 6
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
50
    {
51 6
        return $publicKey->getBinary() === $this->publicKey->getBinary();
52
    }
53
54
    /**
55
     * @return PublicKeyInterface[]
56
     */
57 6
    public function getKeys()
58
    {
59 6
        return [$this->publicKey];
60
    }
61
}
62