Completed
Pull Request — master (#524)
by thomas
71:45
created

PayToPubkeyHash::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 3
dl 0
loc 18
ccs 4
cts 4
cp 1
crap 5
rs 8.8571
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\Opcodes;
7
use BitWasp\Bitcoin\Script\Parser\Operation;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
use BitWasp\Bitcoin\Script\ScriptType;
10
use BitWasp\Buffertools\BufferInterface;
11
12
class PayToPubkeyHash
13
{
14
15
    /**
16
     * @var BufferInterface
17
     */
18
    private $hash;
19
20 2
    /**
21
     * @var bool
22 2
     */
23 2
    private $verify;
24
25
    /**
26
     * PayToPubkeyHash constructor.
27
     * @param $opcode
28 2
     * @param BufferInterface $hash160
29 2
     * @param bool $allowVerify
30 2
     */
31
    public function __construct($opcode, BufferInterface $hash160, $allowVerify = false)
32
    {
33
        if ($hash160->getSize() !== 20) {
34
            throw new \RuntimeException('Malformed pay-to-pubkey-hash script');
35 2
        }
36
37 2
        if ($opcode === Opcodes::OP_CHECKSIG) {
38
            $verify = false;
39
        } else if ($allowVerify && $opcode === Opcodes::OP_CHECKSIGVERIFY) {
40
            $verify = true;
41
        } else {
42
            throw new \RuntimeException("Malformed pay-to-pubkey-hash script - invalid opcode");
43 2
        }
44
45 2
        $this->hash = $hash160;
46
        $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...
47
        $this->verify = $verify;
48
    }
49
50
    /**
51
     * @param Operation[] $chunks
52 2
     * @param bool $allowVerify
53
     * @return static
54 2
     */
55
    public static function fromDecodedScript(array $chunks, $allowVerify = false)
56
    {
57
        if (count($chunks) !== 5) {
58
            throw new \RuntimeException('Malformed pay-to-pubkey-hash script');
59
        }
60 2
61
        if ($chunks[0]->getOp() !== Opcodes::OP_DUP
62 2
            || $chunks[1]->getOp() !== Opcodes::OP_HASH160
63
            || $chunks[3]->getOp() !== Opcodes::OP_EQUALVERIFY
64
        ) {
65
            throw new \RuntimeException('Malformed pay-to-pubkey-hash script');
66
        }
67
68
        return new static($chunks[4]->getOp(), $chunks[2]->getData(), $allowVerify);
69
    }
70
71
    /**
72
     * @param ScriptInterface $script
73
     * @param bool $allowVerify
74
     * @return PayToPubkeyHash
75
     */
76
    public static function fromScript(ScriptInterface $script, $allowVerify = false)
77
    {
78
        return self::fromDecodedScript($script->getScriptParser()->decode(), $allowVerify);
79
    }
80
81
    public function getType()
82
    {
83
        return ScriptType::P2PK;
84
    }
85
86
    /**
87
     * @return int
88
     */
89
    public function getRequiredSigCount()
90
    {
91
        return 1;
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function getKeyCount()
98
    {
99
        return 1;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    public function isChecksigVerify()
106
    {
107
        return $this->verify;
108
    }
109
110
    /**
111
     * @param PublicKeyInterface $publicKey
112
     * @return bool
113
     */
114
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
115
    {
116
        return $publicKey->getPubKeyHash()->equals($this->hash);
117
    }
118
119
    /**
120
     * @return BufferInterface
121
     */
122
    public function getPubKeyHash()
123
    {
124
        return $this->hash;
125
    }
126
}
127