Completed
Pull Request — master (#286)
by thomas
30:16
created

PayToPubkeyHash   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 80%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 56
wmc 8
lcom 1
cbo 5
ccs 12
cts 15
cp 0.8
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequiredSigCount() 0 4 1
A getKeyCount() 0 4 1
A checkInvolvesKey() 0 4 1
A getKeys() 0 4 1
A __construct() 0 11 4
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 implements ScriptInfoInterface
10
{
11
12
    /**
13
     * @var BufferInterface
14
     */
15
    private $hash;
16
17
    /**
18
     * @param ScriptInterface $script
19
     */
20 6
    public function __construct(ScriptInterface $script)
21
    {
22 6
        $chunks = $script->getScriptParser()->decode();
23 6
        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 6
        $hash = $chunks[2]->getData();
29 6
        $this->hash = $hash;
30 6
    }
31
32
    /**
33
     * @return int
34
     */
35 6
    public function getRequiredSigCount()
36
    {
37 6
        return 1;
38
    }
39
40
    /**
41
     * @return int
42
     */
43 6
    public function getKeyCount()
44
    {
45 6
        return 1;
46
    }
47
48
    /**
49
     * @param PublicKeyInterface $publicKey
50
     * @return bool
51
     */
52 6
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
53
    {
54 6
        return $publicKey->getPubKeyHash()->getBinary() === $this->hash->getBinary();
55
    }
56
57
    /**
58
     * @return PublicKeyInterface[]
59
     */
60
    public function getKeys()
61
    {
62
        return [];
63
    }
64
}
65