Skip to content

Commit 0eadf3d

Browse files
committed
Merge pull request bitcoinjs#482 from bitcoinjs/opint
fix isMultisigOutput allowing m data
2 parents c73c1aa + 173994c commit 0eadf3d

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/script.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ var typeforce = require('typeforce')
44
var types = require('./types')
55

66
var 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

1318
function 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

245250
function 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')

test/fixtures/script.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@
245245
"description": "n > len(pubKeys)",
246246
"scriptPubKey": "OP_1 024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34 OP_2 OP_CHECKMULTISIG"
247247
},
248+
{
249+
"description": "m is data",
250+
"scriptPubKey": "ffff 024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34 OP_1 OP_CHECKMULTISIG"
251+
},
252+
{
253+
"description": "n is data",
254+
"scriptPubKey": "OP_1 024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34 ffff OP_CHECKMULTISIG"
255+
},
248256
{
249257
"description": "non-canonical pubKey (bad length)",
250258
"scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffff OP_1 OP_CHECKMULTISIG"

0 commit comments

Comments
 (0)