2016-11-02 02:30:37 +01:00
|
|
|
// OP_0 [signatures ...]
|
|
|
|
|
|
|
|
var bscript = require('../../script')
|
|
|
|
var typeforce = require('typeforce')
|
|
|
|
var OPS = require('../../opcodes.json')
|
|
|
|
|
|
|
|
function partialSignature (value) {
|
|
|
|
return value === OPS.OP_0 || bscript.isCanonicalSignature(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
function check (script, allowIncomplete) {
|
|
|
|
var chunks = bscript.decompile(script)
|
|
|
|
if (chunks.length < 2) return false
|
|
|
|
if (chunks[0] !== OPS.OP_0) return false
|
|
|
|
|
|
|
|
if (allowIncomplete) {
|
|
|
|
return chunks.slice(1).every(partialSignature)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks.slice(1).every(bscript.isCanonicalSignature)
|
|
|
|
}
|
2016-11-02 04:33:46 +01:00
|
|
|
check.toJSON = function () { return 'multi-sig input' }
|
2016-11-02 02:30:37 +01:00
|
|
|
|
|
|
|
function encode (signatures, scriptPubKey) {
|
|
|
|
typeforce([partialSignature], signatures)
|
|
|
|
|
|
|
|
if (scriptPubKey) {
|
|
|
|
var scriptData = bscript.multisig.output.decode(scriptPubKey)
|
|
|
|
|
|
|
|
if (signatures.length < scriptData.m) {
|
|
|
|
throw new TypeError('Not enough signatures provided')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signatures.length > scriptData.pubKeys.length) {
|
|
|
|
throw new TypeError('Too many signatures provided')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bscript.compile([].concat(OPS.OP_0, signatures))
|
|
|
|
}
|
|
|
|
|
|
|
|
function decode (buffer, allowIncomplete) {
|
|
|
|
var chunks = bscript.decompile(buffer)
|
|
|
|
typeforce(check, chunks, allowIncomplete)
|
|
|
|
|
|
|
|
return {
|
|
|
|
signatures: chunks.slice(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
check: check,
|
|
|
|
decode: decode,
|
|
|
|
encode: encode
|
|
|
|
}
|