Completed
Pull Request — master (#518)
by thomas
58:22 queued 55:50
created

PayToPubkeyHash::getPubKeyHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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