Completed
Pull Request — master (#275)
by thomas
146:52 queued 142:54
created

PayToPubkey   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 66
ccs 0
cts 29
cp 0
rs 10
c 2
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A classification() 0 4 1
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\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