Skip to content

Commit 4b47056

Browse files
committed
multisig out fix
1 parent 218bea4 commit 4b47056

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/Script/ScriptPubKey.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ public function isPayToScriptHash(): bool
9999
*/
100100
public function isMultisig(): bool
101101
{
102-
$keys = ord($this->data[0]);
103-
$sigs = ord($this->data[-2]);
102+
$sigs = ord($this->data[0]);
103+
$keys = ord($this->data[-2]);
104104

105105
if (!($this->size >= 24 &&
106+
$sigs >= Opcodes::OP_1 && $sigs <= Opcodes::OP_16 &&
106107
$keys >= Opcodes::OP_1 && $keys <= Opcodes::OP_16 &&
107-
$sigs && $sigs <= Opcodes::OP_16 &&
108108
$keys >= $sigs &&
109109
ord($this->data[-1]) == Opcodes::OP_CHECKMULTISIG)) {
110110
return false;
@@ -154,6 +154,14 @@ public function getOutputAddress(NetworkInterface $network = null): string
154154
{
155155
$addressSerializer = new AddressSerializer($network);
156156

157+
if ($this->isPayToPubKeyHash()) {
158+
return $addressSerializer->getPayToPubKeyHash(substr($this->data, 3, 20));
159+
}
160+
161+
if ($this->isPayToScriptHash()) {
162+
return $addressSerializer->getPayToScriptHash(substr($this->data, 2, 20));
163+
}
164+
157165
if ($this->isPayToPubKey()) {
158166
if ($this->size == 35) {
159167
$pubKey = substr($this->data, 1, 33);
@@ -168,24 +176,28 @@ public function getOutputAddress(NetworkInterface $network = null): string
168176
return $addressSerializer->getPayToPubKey($pubKey);
169177
}
170178

171-
if ($this->isPayToPubKeyHash()) {
172-
return $addressSerializer->getPayToPubKeyHash(substr($this->data, 3, 20));
179+
if ($this->isPayToWitnessPubKeyHash()) {
180+
return $addressSerializer->getPayToWitnessPubKeyHash(substr($this->data, 2, 20));
173181
}
174182

175-
if ($this->isPayToPubKeyHashAlt()) {
176-
return $addressSerializer->getPayToPubKeyHash(substr($this->data, 4, 20));
183+
if ($this->isPayToWitnessScriptHash()) {
184+
return $addressSerializer->getPayToWitnessScriptHash(substr($this->data, 2, 32));
177185
}
178186

179-
if ($this->isPayToScriptHash()) {
180-
return $addressSerializer->getPayToScriptHash(substr($this->data, 2, 20));
187+
if ($this->isMultisig()) {
188+
throw new ScriptException('Unable to decode output script (multisig).');
181189
}
182190

183-
if ($this->isPayToWitnessPubKeyHash()) {
184-
return $addressSerializer->getPayToWitnessPubKeyHash(substr($this->data, 2, 20));
191+
if ($this->isReturn()) {
192+
throw new ScriptException('Unable to decode output script (OP_RETURN).');
185193
}
186194

187-
if ($this->isPayToWitnessScriptHash()) {
188-
return $addressSerializer->getPayToWitnessScriptHash(substr($this->data, 2, 32));
195+
if ($this->isEmpty()) {
196+
throw new ScriptException('Unable to decode output script (empty).');
197+
}
198+
199+
if ($this->isPayToPubKeyHashAlt()) {
200+
return $addressSerializer->getPayToPubKeyHash(substr($this->data, 4, 20));
189201
}
190202

191203
throw new ScriptException('Unable to decode output script.');

tests/ScriptPubKeyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ public function testParseP2SH()
9393
// [numsigs] [...pubkeys...] [numpubkeys] OP_CHECKMULTISIG
9494
public function testParseMultisig()
9595
{
96-
$hex = '53'; // 3
96+
$hex = '52'; // 2
9797
$hex .= '14'; // 20
9898
$hex .= '91b24bf9f5288532960ac687abb035127b1d28a5'; // pubkey hash
9999
$hex .= '14'; // 20
100100
$hex .= 'd6c8e828c1eca1bba065e1b83e1dc2a36e387a42'; // pubkey hash
101101
$hex .= '14'; // 20
102102
$hex .= 'ec7eced2c57ed1292bc4eb9bfd13c9f7603bc338'; // pubkey hash
103-
$hex .= '52'; // 2
103+
$hex .= '53'; // 3
104104
$hex .= 'ae'; // OP_CHECKMULTISIG
105105

106106
$script = new ScriptPubKey(hex2bin($hex));

0 commit comments

Comments
 (0)