Completed
Pull Request — master (#593)
by thomas
15:02
created

PayToPubkeyHash   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 73.52%

Importance

Changes 0
Metric Value
dl 0
loc 118
ccs 25
cts 34
cp 0.7352
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 5

9 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 18 5
A fromScript() 0 4 1
A getRequiredSigCount() 0 4 1
A getKeyCount() 0 4 1
A checkInvolvesKey() 0 4 1
A getPubKeyHash() 0 4 1
B fromDecodedScript() 0 15 5
A getType() 0 4 1
A isChecksigVerify() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Script\ScriptInfo;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
8
use BitWasp\Bitcoin\Script\Opcodes;
9
use BitWasp\Bitcoin\Script\Parser\Operation;
10
use BitWasp\Bitcoin\Script\ScriptInterface;
11
use BitWasp\Bitcoin\Script\ScriptType;
12
use BitWasp\Buffertools\BufferInterface;
13
14
class PayToPubkeyHash
15
{
16
17
    /**
18
     * @var BufferInterface
19
     */
20
    private $hash;
21
22
    /**
23
     * @var bool
24
     */
25
    private $verify;
26
27
    /**
28
     * PayToPubkeyHash constructor.
29
     * @param int $opcode
30
     * @param BufferInterface $hash160
31
     * @param bool $allowVerify
32
     */
33 2
    public function __construct(int $opcode, BufferInterface $hash160, bool $allowVerify = false)
34
    {
35 2
        if ($hash160->getSize() !== 20) {
36
            throw new \RuntimeException('Malformed pay-to-pubkey-hash script');
37
        }
38
39 2
        if ($opcode === Opcodes::OP_CHECKSIG) {
40 2
            $verify = false;
41
        } else if ($allowVerify && $opcode === Opcodes::OP_CHECKSIGVERIFY) {
42
            $verify = true;
43
        } else {
44
            throw new \RuntimeException("Malformed pay-to-pubkey-hash script - invalid opcode");
45
        }
46
47 2
        $this->hash = $hash160;
48 2
        $this->opcode = $opcode;
0 ignored issues
show
Bug introduced by
The property opcode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49 2
        $this->verify = $verify;
50 2
    }
51
52
    /**
53
     * @param Operation[] $chunks
54
     * @param bool $allowVerify
55
     * @return static
56
     */
57 4
    public static function fromDecodedScript(array $chunks, bool $allowVerify = false)
58
    {
59 4
        if (count($chunks) !== 5) {
60 3
            throw new \RuntimeException('Malformed pay-to-pubkey-hash script');
61
        }
62
63 2
        if ($chunks[0]->getOp() !== Opcodes::OP_DUP
64 2
            || $chunks[1]->getOp() !== Opcodes::OP_HASH160
65 2
            || $chunks[3]->getOp() !== Opcodes::OP_EQUALVERIFY
66
        ) {
67
            throw new \RuntimeException('Malformed pay-to-pubkey-hash script');
68
        }
69
70 2
        return new static($chunks[4]->getOp(), $chunks[2]->getData(), $allowVerify);
71
    }
72
73
    /**
74
     * @param ScriptInterface $script
75
     * @param bool $allowVerify
76
     * @return PayToPubkeyHash
77
     */
78 1
    public static function fromScript(ScriptInterface $script, bool $allowVerify = false)
79
    {
80 1
        return self::fromDecodedScript($script->getScriptParser()->decode(), $allowVerify);
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getType(): string
87
    {
88
        return ScriptType::P2PK;
89
    }
90
91
    /**
92
     * @return int
93
     */
94 2
    public function getRequiredSigCount(): int
95
    {
96 2
        return 1;
97
    }
98
99
    /**
100
     * @return int
101
     */
102 1
    public function getKeyCount(): int
103
    {
104 1
        return 1;
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    public function isChecksigVerify(): bool
111
    {
112
        return $this->verify;
113
    }
114
115
    /**
116
     * @param PublicKeyInterface $publicKey
117
     * @return bool
118
     */
119 1
    public function checkInvolvesKey(PublicKeyInterface $publicKey): bool
120
    {
121 1
        return $publicKey->getPubKeyHash()->equals($this->hash);
122
    }
123
124
    /**
125
     * @return BufferInterface
126
     */
127 2
    public function getPubKeyHash(): BufferInterface
128
    {
129 2
        return $this->hash;
130
    }
131
}
132