refactor. script encoding checks.

This commit is contained in:
Christopher Jeffrey 2016-03-31 02:31:09 -07:00
parent 2b5c1369da
commit b084913680
2 changed files with 19 additions and 3 deletions

View File

@ -2814,6 +2814,8 @@ Script.encode = function encode(code) {
p.writeU8(opcodes.OP_PUSHDATA4);
p.writeU32(op.pushdata.size);
p.writeBytes(op);
} else {
assert(false, 'Bad pushdata op.');
}
continue;
}
@ -2821,6 +2823,18 @@ Script.encode = function encode(code) {
if (op.length === 0) {
p.writeU8(opcodes.OP_0);
} else if (op.length <= 0x4b) {
if (op.length === 1) {
if (op[0] === 0) {
p.writeU8(opcodes.OP_0);
continue;
} else if (op[0] >= 1 && op[0] <= 16) {
p.writeU8(op[0] + 0x50);
continue;
} else if (op[0] === 0xff) {
p.writeU8(opcodes.OP_1NEGATE);
continue;
}
}
p.writeU8(op.length);
p.writeBytes(op);
} else if (op.length <= 0xff) {
@ -2831,10 +2845,12 @@ Script.encode = function encode(code) {
p.writeU8(opcodes.OP_PUSHDATA2);
p.writeU16(op.length);
p.writeBytes(op);
} else {
} else if (op.length <= 0xffffffff) {
p.writeU8(opcodes.OP_PUSHDATA4);
p.writeU32(op.length);
p.writeBytes(op);
} else {
assert(false, 'Bad pushdata op.');
}
continue;
}

View File

@ -1501,7 +1501,7 @@ utils.writeVarint = function writeVarint(dst, num, off) {
off = off >>> 0;
if (bn.isBN(num)) {
if (num.cmpn(0xffffffff) > 0) {
if (num.cmp(utils.U32) > 0) {
dst[off] = 0xff;
utils.writeU64(dst, num, off + 1);
return 9;
@ -1540,7 +1540,7 @@ utils.writeVarint = function writeVarint(dst, num, off) {
utils.sizeVarint = function sizeVarint(num) {
if (bn.isBN(num)) {
if (num.cmpn(0xffffffff) > 0)
if (num.cmp(utils.U32) > 0)
return 9;
num = num.toNumber();
}