Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/crypto/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ BN.prototype.toSM = function (opts) {
* @param {number} size The maximum size.
*/
BN.fromScriptNumBuffer = function (buf, fRequireMinimal, size) {
var nMaxNumSize = size || 4
$.checkArgument(buf.length <= nMaxNumSize, new Error('script number overflow'))
if (size) {
$.checkArgument(buf.length <= size, new Error('script number overflow'))
}

if (fRequireMinimal && buf.length > 0) {
// Check that the number is encoded with the minimum possible
// number of bytes.
Expand Down
53 changes: 12 additions & 41 deletions lib/script/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,6 @@ Interpreter.prototype.set = function (obj) {
Interpreter.true = Buffer.from([1])
Interpreter.false = Buffer.from([])

Interpreter.MAX_SCRIPT_ELEMENT_SIZE = 520
Interpreter.MAXIMUM_ELEMENT_SIZE = 4

Interpreter.LOCKTIME_THRESHOLD = 500000000
Interpreter.LOCKTIME_THRESHOLD_BN = new BN(Interpreter.LOCKTIME_THRESHOLD)

Expand Down Expand Up @@ -404,12 +401,7 @@ Interpreter.prototype.checkPubkeyEncoding = function (buf) {
*
*/

Interpreter._isMinimallyEncoded = function (buf, nMaxNumSize) {
nMaxNumSize = nMaxNumSize || Interpreter.MAXIMUM_ELEMENT_SIZE
if (buf.length > nMaxNumSize) {
return false
}

Interpreter._isMinimallyEncoded = function (buf) {
if (buf.length > 0) {
// Check that the number is encoded with the minimum possible number
// of bytes.
Expand Down Expand Up @@ -486,12 +478,6 @@ Interpreter._minimallyEncode = function (buf) {
* bitcoind commit: b5d1b1092998bc95313856d535c632ea5a8f9104
*/
Interpreter.prototype.evaluate = function () {
// TODO: script size should be configurable. no magic numbers
if (this.script.toBuffer().length > 10000) {
this.errstr = 'SCRIPT_ERR_SCRIPT_SIZE'
return false
}

try {
while (this.pc < this.script.chunks.length) {
let thisStep = { pc: this.pc, opcode: Opcode.fromNumber(this.script.chunks[this.pc].opcodenum) }
Expand Down Expand Up @@ -705,16 +691,6 @@ Interpreter.prototype.step = function () {
this.errstr = 'SCRIPT_ERR_UNDEFINED_OPCODE'
return false
}
if (chunk.buf && chunk.buf.length > Interpreter.MAX_SCRIPT_ELEMENT_SIZE) {
this.errstr = 'SCRIPT_ERR_PUSH_SIZE'
return false
}

// Note how Opcode.OP_RESERVED does not count towards the opcode limit.
if (opcodenum > Opcode.OP_16 && ++(this.nOpCount) > 201) {
this.errstr = 'SCRIPT_ERR_OP_COUNT'
return false
}

if (isOpcodeDisabled(opcodenum)) {
this.errstr = 'SCRIPT_ERR_DISABLED_OPCODE'
Expand Down Expand Up @@ -941,9 +917,17 @@ Interpreter.prototype.step = function () {
break

case Opcode.OP_RETURN:
this.errstr = 'SCRIPT_ERR_OP_RETURN'
return false
// break // unreachable
buf = stacktop(-1)
fValue = Interpreter.castToBool(buf)
if (fValue) {
// Successfully terminate the script now
this.pc = this.script.chunks.length
this.vfExec = []
return true
} else {
this.errstr = 'SCRIPT_ERR_VERIFY'
return false
}

//
// Stack ops
Expand Down Expand Up @@ -1559,10 +1543,6 @@ Interpreter.prototype.step = function () {
return false
}
this.nOpCount += nKeysCount
if (this.nOpCount > 201) {
this.errstr = 'SCRIPT_ERR_OP_COUNT'
return false
}
// int ikey = ++i;
var ikey = ++i
i += nKeysCount
Expand Down Expand Up @@ -1691,10 +1671,6 @@ Interpreter.prototype.step = function () {

buf1 = stacktop(-2)
buf2 = stacktop(-1)
if (buf1.length + buf2.length > Interpreter.MAX_SCRIPT_ELEMENT_SIZE) {
this.errstr = 'SCRIPT_ERR_PUSH_SIZE'
return false
}
this.stack[this.stack.length - 2] = Buffer.concat([buf1, buf2])
this.stack.pop()
break
Expand Down Expand Up @@ -1734,11 +1710,6 @@ Interpreter.prototype.step = function () {
}

var size = BN.fromScriptNumBuffer(stacktop(-1), fRequireMinimal).toNumber()
if (size > Interpreter.MAX_SCRIPT_ELEMENT_SIZE) {
this.errstr = 'SCRIPT_ERR_PUSH_SIZE'
return false
}

this.stack.pop()
var rawnum = stacktop(-1)

Expand Down
Loading