Browse Source

script: use typeforce to enforce push-only chunks

hk-custom-address
Daniel Cousens 8 years ago
committed by Daniel Cousens
parent
commit
8df1b45699
  1. 48
      src/script.js

48
src/script.js

@ -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
}
if (op === OPS.OP_0) { return chunks.map(function (op) {
return new Buffer(0) if (Buffer.isBuffer(op)) return op
} else if (op === OPS.OP_1NEGATE || op >= OPS.OP_1 && op <= OPS.OP_16) { if (op === OPS.OP_0) return new Buffer(0)
return scriptNumber.encode(op - OP_INT_BASE)
} else { return scriptNumber.encode(op - OP_INT_BASE)
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…
Cancel
Save