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

PayToPubkeyHash   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 69
ccs 0
cts 30
cp 0
rs 10
c 3
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 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\Crypto\EcAdapter\Key\PublicKeyInterface;
6
use BitWasp\Bitcoin\Script\Classifier\OutputClassifier;
7
use BitWasp\Bitcoin\Script\ScriptInterface;
8
use BitWasp\Buffertools\BufferInterface;
9
10
class PayToPubkeyHash implements ScriptInfoInterface
11
{
12
    /**
13
     * @var ScriptInterface
14
     */
15
    private $script;
16
17
    /**
18
     * @var BufferInterface
19
     */
20
    private $hash;
21
22
    /**
23
     * @param ScriptInterface $script
24
     */
25
    public function __construct(ScriptInterface $script)
26
    {
27
        $this->script = $script;
28
        $chunks = $this->script->getScriptParser()->decode();
29
        if (count($chunks) < 5 || !$chunks[2]->isPush()) {
30
            throw new \RuntimeException('Malformed pay-to-pubkey-hash script');
31
        }
32
33
        /** @var BufferInterface $hash */
34
        $hash = $chunks[2]->getData();
35
        $this->hash = $hash;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function classification()
42
    {
43
        return OutputClassifier::PAYTOPUBKEYHASH;
44
    }
45
46
    /**
47
     * @return int
48
     */
49
    public function getRequiredSigCount()
50
    {
51
        return 1;
52
    }
53
54
    /**
55
     * @return int
56
     */
57
    public function getKeyCount()
58
    {
59
        return 1;
60
    }
61
62
    /**
63
     * @param PublicKeyInterface $publicKey
64
     * @return bool
65
     */
66
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
67
    {
68
        return $publicKey->getPubKeyHash()->getBinary() === $this->hash->getBinary();
69
    }
70
71
    /**
72
     * @return PublicKeyInterface[]
73
     */
74
    public function getKeys()
75
    {
76
        return [];
77
    }
78
}
79