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

PayToPubkeyHash::classification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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