Completed
Pull Request — master (#239)
by thomas
49:01 queued 45:29
created

OutputClassifier::isPayToScriptHash()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8.0155

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
ccs 15
cts 16
cp 0.9375
rs 5.3846
cc 8
eloc 14
nc 5
nop 1
crap 8.0155
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Classifier;
4
5
use BitWasp\Bitcoin\Script\Parser\Operation;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PublicKey;
7
use BitWasp\Bitcoin\Script\Opcodes;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
use BitWasp\Buffertools\BufferInterface;
10
11
class OutputClassifier
12
{
13
    const PAYTOPUBKEY = 'pubkey';
14
    const PAYTOPUBKEYHASH = 'pubkeyhash';
15
    const PAYTOSCRIPTHASH = 'scripthash';
16
    const WITNESS_V0_KEYHASH = 'witness_v0_keyhash';
17
    const WITNESS_V0_SCRIPTHASH = 'witness_v0_scripthash';
18
    const MULTISIG = 'multisig';
19
    const UNKNOWN = 'unknown';
20 340
    const NONSTANDARD = 'nonstandard';
21
22 340
    /**
23 340
     * @var \BitWasp\Bitcoin\Script\Parser\Operation[]
24
     */
25
    private $decoded;
26
27
    /**
28 202
     * @var ScriptInterface
29
     */
30 202
    private $script;
31 160
32
    /**
33
     * @param ScriptInterface $script
34 60
     */
35 60
    public function __construct(ScriptInterface $script)
36 42
    {
37 42
        $this->script = $script;
38 42
        $this->decoded = $script->getScriptParser()->decode();
39
    }
40
41
    /**
42 18
     * @param BufferInterface|null $publicKey
43
     * @return bool
44
     */
45
    public function isPayToPublicKey(& $publicKey = null)
46
    {
47
        if (count($this->decoded) < 1 || !$this->decoded[0]->isPush()) {
48 160
            return false;
49
        }
50 160
51 60
        $size = $this->decoded[0]->getDataSize();
52
        if ($size === 33 || $size === 65) {
53
            $op = $this->decoded[1];
54 112
            if (!$op->isPush() && $op->getOp() === Opcodes::OP_CHECKSIG) {
55 112
                $publicKey = $this->decoded[0]->getData();
56 112
                return true;
57 112
            }
58 112
        }
59
60 112
        return false;
61
    }
62 112
63 24
    /**
64
     * @param BufferInterface|null $pubKeyHash
65 112
     * @return bool
66
     */
67 88
    public function isPayToPublicKeyHash(& $pubKeyHash = null)
68 88
    {
69 88
        if (count($this->decoded) !== 5) {
70 88
            return false;
71 88
        }
72
73
        $dup = $this->decoded[0];
74
        $hash = $this->decoded[1];
75
        $buf = $this->decoded[2];
76
        $eq = $this->decoded[3];
77 292
        $checksig = $this->decoded[4];
78
79 292
        foreach ([$dup, $hash, $eq, $checksig] as $op) {
80 238
            /** @var Operation $op */
81
            if ($op->isPush()) {
82
                return false;
83 108
            }
84 108
        }
85 18
86
        if ($dup->getOp() === Opcodes::OP_DUP
87
        && $hash->getOp() === Opcodes::OP_HASH160
88 90
        && $buf->isPush() && $buf->getDataSize() === 20
89 90
        && $eq->getOp() === Opcodes::OP_EQUALVERIFY
90 6
        && $checksig->getOp() === Opcodes::OP_CHECKSIG) {
91
            $pubKeyHash = $this->decoded[2]->getData();
92
            return true;
93 84
        }
94 84
95
        return false;
96
    }
97
98
    /**
99
     * @param BufferInterface|null $scriptHash
100 172
     * @return bool
101
     */
102 172
    public function isPayToScriptHash(& $scriptHash = null)
103 172
    {
104 60
        if (count($this->decoded) !== 3) {
105
            return false;
106
        }
107 112
108 112
        $hash = $this->decoded[0];
109 112
        if ($hash->isPush() || !$hash->getOp() === Opcodes::OP_HASH160) {
110 112
            return false;
111
        }
112
113
        $buffer = $this->decoded[1];
114
        if (!$buffer->isPush() || $buffer->getDataSize() !== 20) {
115 112
            return false;
116 112
        }
117 112
118 58
119
        $eq = $this->decoded[2];
120 54
        if (!$eq->isPush() && $eq->getOp() === Opcodes::OP_EQUAL) {
121
            $scriptHash = $this->decoded[1]->getData();
122 54
            return true;
123 54
        }
124 54
125
        return false;
126
    }
127
128
    /**
129
     * @param BufferInterface[] $keys
130 102
     * @return bool
131
     */
132 102
    public function isMultisig(& $keys = [])
133 6
    {
134 96
        $count = count($this->decoded);
135 30
        if ($count <= 3) {
136 78
            return false;
137 18
        }
138 66
139 48
        $mOp = $this->decoded[0];
140
        $nOp = $this->decoded[$count - 2];
141
        $checksig = $this->decoded[$count - 1];
142 18
        if ($mOp->isPush() || $nOp->isPush() || $checksig->isPush()) {
143
            return false;
144
        }
145
146
        /** @var Operation[] $vKeys */
147
        $vKeys = array_slice($this->decoded, 1, -2);
148
        $solutions = [];
149
        foreach ($vKeys as $key) {
150
            if (!$key->isPush() || !PublicKey::isCompressedOrUncompressed($key->getData())) {
151
                return false;
152
            }
153
            $solutions[] = $key->getData();
154
        }
155
156
        if ($mOp->getOp() >= Opcodes::OP_0
157
            && $nOp->getOp() <= Opcodes::OP_16
158
            && $checksig->getOp() === Opcodes::OP_CHECKMULTISIG) {
159
            $keys = $solutions;
160
            return true;
161
        }
162
163
        return false;
164
    }
165
166
    /**
167
     * @param BufferInterface $programHash
168
     * @return bool
169
     */
170
    public function isWitness(& $programHash = null)
171
    {
172
        $buffer = $this->script->getBuffer();
173
        $size = $buffer->getSize();
174
175
        if ($size < 4 || $size > 34) {
176
            return false;
177
        }
178
179
        $parser = $this->script->getScriptParser();
180
        $script = $parser->decode();
181
        if (count($script) !== 2 || !$script[1]->isPush()) {
182
            return false;
183
        }
184
185
        $version = $script[0]->getOp();
186
        if ($version != Opcodes::OP_0 && ($version < Opcodes::OP_1 || $version > Opcodes::OP_16)) {
187
            return false;
188
        }
189
190
        $witness = $script[1];
191
        if ($size === $witness->getDataSize() + 2) {
192
            $programHash = $witness->getData();
193
            return true;
194
        }
195
196
        return false;
197
    }
198
199
    /**
200
     * @param BufferInterface|BufferInterface[] $solutions
201
     * @return string
202
     */
203
    public function classify(&$solutions = null)
204
    {
205
        $type = self::UNKNOWN;
206
        $solution = null;
207
        if ($this->isPayToScriptHash($solution)) {
208
            /** @var BufferInterface $solution */
209
            $type = self::PAYTOSCRIPTHASH;
210
        } elseif ($this->isWitness($solution)) {
211
            /** @var BufferInterface $solution */
212
            if ($solution->getSize() == 20) {
213
                $type = self::WITNESS_V0_KEYHASH;
214
            } else {
215
                $type = self::WITNESS_V0_SCRIPTHASH;
216
            }
217
        } elseif ($this->isPayToPublicKey($solution)) {
218
            /** @var BufferInterface $solution */
219
            $type = self::PAYTOPUBKEY;
220
        } elseif ($this->isPayToPublicKeyHash($solution)) {
221
            /** @var BufferInterface $solution */
222
            $type = self::PAYTOPUBKEYHASH;
223
        } elseif ($this->isMultisig($solution)) {
0 ignored issues
show
Documentation introduced by
$solution is of type object<BitWasp\Buffertools\BufferInterface>|null, but the function expects a array<integer,object<Bit...tools\BufferInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
224
            /** @var BufferInterface[] $solution */
225
            $type = self::MULTISIG;
226
        }
227
228
        $solutions = $solution;
229
230
        return $type;
231
    }
232
}
233