@@ -4,11 +4,16 @@ var typeforce = require('typeforce')
44var types = require ( './types' )
55
66var OPS = require ( './opcodes' )
7- var REVERSE_OPS = [ ]
8- for ( var op in OPS ) {
9- var code = OPS [ op ]
10- REVERSE_OPS [ code ] = op
11- }
7+ var REVERSE_OPS = ( function ( ) {
8+ var result = { }
9+ for ( var op in OPS ) {
10+ var code = OPS [ op ]
11+ result [ code ] = op
12+ }
13+ return result
14+ } ) ( )
15+
16+ const OP_INT_BASE = OPS . OP_RESERVED // OP_1 - 1
1217
1318function toASM ( chunks ) {
1419 if ( types . Buffer ( chunks ) ) {
@@ -225,21 +230,21 @@ function isMultisigOutput (script) {
225230 if ( chunks [ chunks . length - 1 ] !== OPS . OP_CHECKMULTISIG ) return false
226231
227232 var mOp = chunks [ 0 ]
228- if ( mOp < OPS . OP_1 ) return false
229- if ( mOp > OPS . OP_16 ) return false
230-
231233 var nOp = chunks [ chunks . length - 2 ]
232- if ( nOp < OPS . OP_1 ) return false
233- if ( nOp > OPS . OP_16 ) return false
234234
235- var m = mOp - ( OPS . OP_1 - 1 )
236- var n = nOp - ( OPS . OP_1 - 1 )
237- if ( n < m ) return false
235+ if ( ! types . Number ( mOp ) ) return false
236+ if ( ! types . Number ( nOp ) ) return false
237+
238+ var m = mOp - OP_INT_BASE
239+ var n = nOp - OP_INT_BASE
238240
239- var pubKeys = chunks . slice ( 1 , - 2 )
240- if ( n !== pubKeys . length ) return false
241+ // 0 < m <= n <= 16
242+ if ( m <= 0 ) return false
243+ if ( m > n ) return false
244+ if ( n > 16 ) return false
245+ if ( n !== chunks . length - 3 ) return false
241246
242- return pubKeys . every ( isCanonicalPubKey )
247+ return chunks . slice ( 1 , - 2 ) . every ( isCanonicalPubKey )
243248}
244249
245250function isNullDataOutput ( script ) {
@@ -309,9 +314,9 @@ function multisigOutput (m, pubKeys) {
309314 if ( n < m ) throw new Error ( 'Not enough pubKeys provided' )
310315
311316 return compile ( [ ] . concat (
312- ( OPS . OP_1 - 1 ) + m ,
317+ OP_INT_BASE + m ,
313318 pubKeys ,
314- ( OPS . OP_1 - 1 ) + n ,
319+ OP_INT_BASE + n ,
315320 OPS . OP_CHECKMULTISIG
316321 ) )
317322}
@@ -349,8 +354,8 @@ function multisigInput (signatures, scriptPubKey) {
349354
350355 var mOp = chunks [ 0 ]
351356 var nOp = chunks [ chunks . length - 2 ]
352- var m = mOp - ( OPS . OP_1 - 1 )
353- var n = nOp - ( OPS . OP_1 - 1 )
357+ var m = mOp - OP_INT_BASE
358+ var n = nOp - OP_INT_BASE
354359
355360 if ( signatures . length < m ) throw new Error ( 'Not enough signatures provided' )
356361 if ( signatures . length > n ) throw new Error ( 'Too many signatures provided' )
0 commit comments