@@ -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. ' );
0 commit comments