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

PayToPubkeyHash   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeyCount() 0 4 1
A checkInvolvesKey() 0 4 1
A getRequiredSigCount() 0 4 1
A __construct() 0 11 4
A getPubKeyHash() 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\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