script: use typeforce to enforce push-only chunks
This commit is contained in:
parent
0696ca95b6
commit
8df1b45699
1 changed files with 21 additions and 27 deletions
|
@ -14,6 +14,17 @@ var REVERSE_OPS = (function () {
|
||||||
})()
|
})()
|
||||||
var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
|
var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
|
||||||
|
|
||||||
|
function isOPInt (value) {
|
||||||
|
return types.Number(value) &&
|
||||||
|
(value === OPS.OP_0) ||
|
||||||
|
(value >= OPS.OP_1 && value <= OPS.OP_16) ||
|
||||||
|
(value === OPS.OP_1NEGATE)
|
||||||
|
}
|
||||||
|
|
||||||
|
function pushOnlyChunk (value) {
|
||||||
|
return types.oneOf(Buffer, isOPInt)
|
||||||
|
}
|
||||||
|
|
||||||
function compile (chunks) {
|
function compile (chunks) {
|
||||||
// TODO: remove me
|
// TODO: remove me
|
||||||
if (Buffer.isBuffer(chunks)) return chunks
|
if (Buffer.isBuffer(chunks)) return chunks
|
||||||
|
@ -131,37 +142,20 @@ function fromASM (asm) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function decompilePushOnly (script) {
|
function decompilePushOnly (script) {
|
||||||
return decompile(script).map(function (op) {
|
var chunks = decompile(script)
|
||||||
if (op instanceof Buffer) {
|
typeforce([pushOnlyChunk], chunks)
|
||||||
return op
|
|
||||||
}
|
return chunks.map(function (op) {
|
||||||
|
if (Buffer.isBuffer(op)) return op
|
||||||
|
if (op === OPS.OP_0) return new Buffer(0)
|
||||||
|
|
||||||
if (op === OPS.OP_0) {
|
|
||||||
return new Buffer(0)
|
|
||||||
} else if (op === OPS.OP_1NEGATE || op >= OPS.OP_1 && op <= OPS.OP_16) {
|
|
||||||
return scriptNumber.encode(op - OP_INT_BASE)
|
return scriptNumber.encode(op - OP_INT_BASE)
|
||||||
} else {
|
|
||||||
throw new Error('Can only evaluate push-only opcodes')
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function compilePushOnly (set) {
|
function compilePushOnly (chunks) {
|
||||||
return compile(set.map(function (op) {
|
typeforce([pushOnlyChunk], chunks)
|
||||||
if (op.length === 0) {
|
return compile(chunks)
|
||||||
return OPS.OP_0
|
|
||||||
}
|
|
||||||
|
|
||||||
if (op.length === 1) {
|
|
||||||
if (op[0] === 0x81) {
|
|
||||||
return OPS.OP_1NEGATE
|
|
||||||
} else if (op[0] >= 1 && op[0] <= OPS.OP_16) {
|
|
||||||
return op[0] + OP_INT_BASE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return op
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isCanonicalPubKey (buffer) {
|
function isCanonicalPubKey (buffer) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue