Skip to content

Commit a3f6f36

Browse files
committed
script decode fix
1 parent 484902b commit a3f6f36

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

src/Script/ScriptPubKey.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,11 @@ public function isPayToScriptHash(): bool
116116
*/
117117
public function isMultisig(): bool
118118
{
119-
$sigs = ord($this->data[0]);
120-
$keys = ord($this->data[-2]);
121-
122119
// check pattern
123120
if (!($this->size >= 37 &&
124-
$sigs >= Opcodes::OP_1 && $sigs <= Opcodes::OP_16 &&
125-
$keys >= Opcodes::OP_1 && $keys <= Opcodes::OP_16 &&
126-
$keys >= $sigs &&
121+
ord($this->data[0]) >= Opcodes::OP_1 && ord($this->data[0]) <= Opcodes::OP_16 &&
122+
ord($this->data[-2]) >= Opcodes::OP_1 && ord($this->data[-2]) <= Opcodes::OP_16 &&
123+
ord($this->data[-2]) >= ord($this->data[0]) &&
127124
ord($this->data[-1]) == Opcodes::OP_CHECKMULTISIG)) {
128125
return false;
129126
}
@@ -135,7 +132,7 @@ public function isMultisig(): bool
135132
$i += $size + 1;
136133
}
137134

138-
return $k > 0 && $keys - Opcodes::OP_1 + 1 == $j;
135+
return $k > 0 && ord($this->data[-2]) - Opcodes::OP_1 + 1 == $j;
139136
}
140137

141138
/**
@@ -144,10 +141,8 @@ public function isMultisig(): bool
144141
*/
145142
public function isPayToWitnessPubKeyHash(): bool
146143
{
147-
$version = ord($this->data[0]);
148-
149144
return $this->size == 22 &&
150-
$version >= Opcodes::OP_0 && $version <= Opcodes::OP_16 &&
145+
ord($this->data[0]) >= Opcodes::OP_0 && ord($this->data[0]) <= Opcodes::OP_16 &&
151146
ord($this->data[1]) == 20;
152147
}
153148

@@ -157,10 +152,8 @@ public function isPayToWitnessPubKeyHash(): bool
157152
*/
158153
public function isPayToWitnessScriptHash(): bool
159154
{
160-
$version = ord($this->data[0]);
161-
162155
return $this->size == 34 &&
163-
$version >= Opcodes::OP_0 && $version <= Opcodes::OP_16 &&
156+
ord($this->data[0]) >= Opcodes::OP_0 && ord($this->data[0]) <= Opcodes::OP_16 &&
164157
ord($this->data[1]) == 32;
165158
}
166159

@@ -191,7 +184,7 @@ public function getOutputAddress(NetworkInterface $network = null): string
191184
}
192185

193186
if (!PublicKey::isFullyValid($pubKey)) {
194-
throw new ScriptException('Invalid public key.');
187+
throw new ScriptException('Unable to decode output address (invalid public key).');
195188
}
196189

197190
return $addressSerializer->getPayToPubKey($pubKey);
@@ -206,21 +199,21 @@ public function getOutputAddress(NetworkInterface $network = null): string
206199
}
207200

208201
if ($this->isMultisig()) {
209-
throw new ScriptException('Unable to decode output script (multisig).');
202+
throw new ScriptException('Unable to decode output address (multisig).');
210203
}
211204

212205
if ($this->isReturn()) {
213-
throw new ScriptException('Unable to decode output script (OP_RETURN).');
206+
throw new ScriptException('Unable to decode output address (OP_RETURN).');
214207
}
215208

216209
if ($this->isEmpty()) {
217-
throw new ScriptException('Unable to decode output script (empty).');
210+
throw new ScriptException('Unable to decode output address (empty).');
218211
}
219212

220213
if ($this->isPayToPubKeyHashAlt()) {
221214
return $addressSerializer->getPayToPubKeyHash(substr($this->data, 4, 20));
222215
}
223216

224-
throw new ScriptException('Unable to decode output script.');
217+
throw new ScriptException('Unable to decode output address.');
225218
}
226219
}

0 commit comments

Comments
 (0)