Skip to content

Commit 0ca1e40

Browse files
committed
script: refactor isMultisigOutput for clarity
1 parent 5be9b04 commit 0ca1e40

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/script.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var REVERSE_OPS = (function () {
1313
return result
1414
})()
1515

16+
const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
17+
1618
function toASM (chunks) {
1719
if (types.Buffer(chunks)) {
1820
chunks = decompile(chunks)
@@ -228,18 +230,18 @@ function isMultisigOutput (script) {
228230
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) return false
229231

230232
var mOp = chunks[0]
231-
if (!types.Number(mOp)) return false
232-
if (mOp < OPS.OP_1) return false
233-
if (mOp > OPS.OP_16) return false
234-
235233
var nOp = chunks[chunks.length - 2]
234+
235+
if (!types.Number(mOp)) return false
236236
if (!types.Number(nOp)) return false
237-
if (nOp < OPS.OP_1) return false
238-
if (nOp > OPS.OP_16) return false
239237

240-
var m = mOp - (OPS.OP_1 - 1)
241-
var n = nOp - (OPS.OP_1 - 1)
242-
if (n < m) return false
238+
var m = mOp - OP_INT_BASE
239+
var n = nOp - OP_INT_BASE
240+
241+
// 0 < m <= n <= 16
242+
if (m <= 0) return false
243+
if (m > n) return false
244+
if (n > 16) return false
243245

244246
var pubKeys = chunks.slice(1, -2)
245247
if (n !== pubKeys.length) return false
@@ -314,9 +316,9 @@ function multisigOutput (m, pubKeys) {
314316
if (n < m) throw new Error('Not enough pubKeys provided')
315317

316318
return compile([].concat(
317-
(OPS.OP_1 - 1) + m,
319+
OP_INT_BASE + m,
318320
pubKeys,
319-
(OPS.OP_1 - 1) + n,
321+
OP_INT_BASE + n,
320322
OPS.OP_CHECKMULTISIG
321323
))
322324
}
@@ -354,8 +356,8 @@ function multisigInput (signatures, scriptPubKey) {
354356

355357
var mOp = chunks[0]
356358
var nOp = chunks[chunks.length - 2]
357-
var m = mOp - (OPS.OP_1 - 1)
358-
var n = nOp - (OPS.OP_1 - 1)
359+
var m = mOp - OP_INT_BASE
360+
var n = nOp - OP_INT_BASE
359361

360362
if (signatures.length < m) throw new Error('Not enough signatures provided')
361363
if (signatures.length > n) throw new Error('Too many signatures provided')

0 commit comments

Comments
 (0)