This commit is contained in:
Christopher Jeffrey 2016-05-15 00:40:21 -07:00
parent dd5c9096a7
commit a7f3d2aa8f
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 73 additions and 176 deletions

View File

@ -398,7 +398,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
// Remove all signatures.
for (i = 0; i < copy.inputs.length; i++)
copy.inputs[i].script = new Script([]);
copy.inputs[i].script = new Script();
// Remove all code separators.
prev = prev.removeSeparators();
@ -427,7 +427,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
// Null outputs that are not the at current input index.
for (i = 0; i < copy.outputs.length; i++) {
if (i !== index) {
copy.outputs[i].script = new Script([]);
copy.outputs[i].script = new Script();
copy.outputs[i].value = -1;
}
}

View File

@ -1582,32 +1582,17 @@ utils.writeU64NBE = function writeU64NBE(dst, num, off) {
utils.MAX_SAFE_INTEGER = 0x1fffffffffffff;
/**
* Max safe integer as a big number (53 bits).
* @type BN
* @const
*/
utils.MAX_SAFE_BN = new bn(utils.MAX_SAFE_INTEGER);
/**
* Most-significant 4 bytes of max safe integer.
* @type Number
* @const
*/
utils.MAX_SAFE_HI = 0x1fffff;
/**
* Write a javascript number as an int64le (faster than big numbers).
* @param {Number} value
* @throws on num > MAX_SAFE_INTEGER
*/
utils.write64N = function write64N(dst, num, off) {
var neg, hi, lo, one, i, b;
utils.write64N = function write64N(dst, num, off, be) {
var neg, hi, lo;
assert(typeof num === 'number');
num = +num;
off = off >>> 0;
assert(num <= utils.MAX_SAFE_INTEGER, 'Number exceeds 2^53-1');
@ -1617,25 +1602,35 @@ utils.write64N = function write64N(dst, num, off) {
num = num < 0 ? -num : num;
if (neg)
num--;
hi = num / 0x100000000 | 0;
lo = num % 0x100000000;
dst[off + 0] = (lo >>> 0) & 0xff;
dst[off + 1] = (lo >>> 8) & 0xff;
dst[off + 2] = (lo >>> 16) & 0xff;
dst[off + 3] = (lo >>> 24) & 0xff;
dst[off + 4] = (hi >>> 0) & 0xff;
dst[off + 5] = (hi >>> 8) & 0xff;
dst[off + 6] = (hi >>> 16) & 0xff;
dst[off + 7] = (hi >>> 24) & 0xff;
if (neg) {
one = 1;
for (i = off; i < off + 8; i++) {
b = (dst[i] ^ 0xff) + one;
dst[i] = b & 0xff;
one = b >>> 8;
}
hi = ~hi >>> 0;
lo = ~lo >>> 0;
}
if (be) {
dst[off + 0] = (hi >>> 24) & 0xff;
dst[off + 1] = (hi >>> 16) & 0xff;
dst[off + 2] = (hi >>> 8) & 0xff;
dst[off + 3] = (hi >>> 0) & 0xff;
dst[off + 4] = (lo >>> 24) & 0xff;
dst[off + 5] = (lo >>> 16) & 0xff;
dst[off + 6] = (lo >>> 8) & 0xff;
dst[off + 7] = (lo >>> 0) & 0xff;
} else {
dst[off + 0] = (lo >>> 0) & 0xff;
dst[off + 1] = (lo >>> 8) & 0xff;
dst[off + 2] = (lo >>> 16) & 0xff;
dst[off + 3] = (lo >>> 24) & 0xff;
dst[off + 4] = (hi >>> 0) & 0xff;
dst[off + 5] = (hi >>> 8) & 0xff;
dst[off + 6] = (hi >>> 16) & 0xff;
dst[off + 7] = (hi >>> 24) & 0xff;
}
return 8;
@ -1648,40 +1643,7 @@ utils.write64N = function write64N(dst, num, off) {
*/
utils.write64NBE = function write64NBE(dst, num, off) {
var neg, hi, lo, one, i, b;
num = +num;
off = off >>> 0;
assert(num <= utils.MAX_SAFE_INTEGER, 'Number exceeds 2^53-1');
if (num < 0)
neg = true;
num = num < 0 ? -num : num;
hi = num / 0x100000000 | 0;
lo = num % 0x100000000;
dst[off + 7] = (lo >>> 0) & 0xff;
dst[off + 6] = (lo >>> 8) & 0xff;
dst[off + 5] = (lo >>> 16) & 0xff;
dst[off + 4] = (lo >>> 24) & 0xff;
dst[off + 3] = (hi >>> 0) & 0xff;
dst[off + 2] = (hi >>> 8) & 0xff;
dst[off + 1] = (hi >>> 16) & 0xff;
dst[off + 0] = (hi >>> 24) & 0xff;
if (neg) {
one = 1;
for (i = off + 7; i >= off; i--) {
b = (dst[i] ^ 0xff) + one;
dst[i] = b & 0xff;
one = b >>> 8;
}
}
return 8;
return utils.write64N(dst, num, off, true);
};
/**
@ -1693,13 +1655,24 @@ utils.write64NBE = function write64NBE(dst, num, off) {
* @throws on num > MAX_SAFE_INTEGER
*/
utils.readU64N = function readU64N(data, off, force53) {
utils.readU64N = function readU64N(data, off, force53, be) {
var hi, lo;
off = off >>> 0;
var hi = utils.readU32(data, off + 4);
var lo = utils.readU32(data, off);
if (be) {
hi = utils.readU32(data, off);
lo = utils.readU32(data, off + 4);
} else {
hi = utils.readU32(data, off + 4);
lo = utils.readU32(data, off);
}
if (force53)
hi &= utils.MAX_SAFE_HI;
assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1');
hi &= 0x1fffff;
assert((hi & 0xffe00000) === 0, 'Number exceeds 2^53-1');
return (hi * 0x100000000) + lo;
};
@ -1713,13 +1686,7 @@ utils.readU64N = function readU64N(data, off, force53) {
*/
utils.readU64NBE = function readU64NBE(data, off, force53) {
off = off >>> 0;
var hi = utils.readU32BE(data, off);
var lo = utils.readU32BE(data, off + 4);
if (force53)
hi &= utils.MAX_SAFE_HI;
assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1');
return (hi * 0x100000000) + lo;
return utils.readU64N(data, off, force53, true);
};
/**
@ -1731,54 +1698,32 @@ utils.readU64NBE = function readU64NBE(data, off, force53) {
* @throws on num > MAX_SAFE_INTEGER
*/
utils.read64N = function read64N(data, off, force53) {
var hi, lo, result, one, b, v;
utils.read64N = function read64N(data, off, force53, be) {
var hi, lo;
off = off >>> 0;
if (data[off + 7] & 0x80) {
result = 0;
one = 1;
for (i = 0; i < 8; i++) {
b = (data[i + off] ^ 0xff) + one;
v = b & 0xff;
// We can't have any bits in the msb.
if (i === 7 && v !== 0) {
assert(force53, 'Number exceeds 2^53-1');
v = 0;
}
// We can only have 5 bits in the second msb.
if (i === 6 && v > 0x1f) {
assert(force53, 'Number exceeds 2^53-1');
v &= 0x1f;
}
// We need to shift left by `i` bytes,
// but we can't because this will
// exceed 32 bits. So we multiply by
// `Math.pow(2, shiftBits)` instead.
// Equivalent to `v <<= 8 * i`.
v *= Math.pow(2, 8 * i);
// We can't OR so we add.
result += v;
one = b >>> 8;
}
return -result;
if (be) {
hi = utils.readU32(data, off);
lo = utils.readU32(data, off + 4);
} else {
hi = utils.readU32(data, off + 4);
lo = utils.readU32(data, off);
}
hi = utils.readU32(data, off + 4);
lo = utils.readU32(data, off);
if (hi & 0x80000000) {
hi = ~hi >>> 0;
lo = ~lo >>> 0;
if (force53)
hi &= utils.MAX_SAFE_HI;
if (force53)
hi &= 0x1fffff;
assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1');
assert((hi & 0xffe00000) === 0, 'Number exceeds 2^53-1');
return -(hi * 0x100000000 + lo + 1);
}
assert((hi & 0xffe00000) === 0, 'Number exceeds 2^53-1');
return (hi * 0x100000000) + lo;
};
@ -1793,55 +1738,7 @@ utils.read64N = function read64N(data, off, force53) {
*/
utils.read64NBE = function read64NBE(data, off, force53) {
var hi, lo, result, one, b, v;
off = off >>> 0;
if (data[off] & 0x80) {
result = 0;
one = 1;
for (i = 7; i >= 0; i--) {
b = (data[i + off] ^ 0xff) + one;
v = b & 0xff;
// We can't have any bits on the msb.
if (i === 0 && v !== 0) {
assert(force53, 'Number exceeds 2^53-1');
v = 0;
}
// We can only have 5 bits on the second msb.
if (i === 1 && v > 0x1f) {
assert(force53, 'Number exceeds 2^53-1');
v &= 0x1f;
}
// We need to shift left by `7 - i` bytes,
// but we can't because this will exceed
// 32 bits. So we multiply by
// `Math.pow(2, shiftBits)` instead.
// Equivalent to `v <<= 8 * (7 - i)`.
v *= Math.pow(2, 8 * (7 - i));
// We can't OR so we add.
result += v;
one = b >>> 8;
}
return -result;
}
hi = utils.readU32(data, off);
lo = utils.readU32(data, off + 4);
if (force53)
hi &= utils.MAX_SAFE_HI;
assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1');
return (hi * 0x100000000) + lo;
return utils.read64N(data, off, force53, true);
};
/**
@ -1986,7 +1883,7 @@ utils.write32BE = function write32BE(dst, num, off) {
utils.write64 = function write64(dst, num, off) {
var i;
if (!bn.isBN(num))
if (typeof num === 'number')
return utils.write64N(dst, num, off);
off = off >>> 0;
@ -2016,7 +1913,7 @@ utils.write64 = function write64(dst, num, off) {
utils.write64BE = function write64BE(dst, num, off) {
var i;
if (!bn.isBN(num))
if (typeof num === 'number')
return utils.write64NBE(dst, num, off);
off = off >>> 0;
@ -2093,7 +1990,7 @@ utils.writeVarint = function writeVarint(dst, num, off) {
off = off >>> 0;
if (bn.isBN(num)) {
if (num.cmp(utils.U32) > 0) {
if (num.bitLength() > 32) {
dst[off] = 0xff;
utils.writeU64(dst, num, off + 1);
return 9;
@ -2138,7 +2035,7 @@ utils.writeVarint = function writeVarint(dst, num, off) {
utils.sizeVarint = function sizeVarint(num) {
if (bn.isBN(num)) {
if (num.cmp(utils.U32) > 0)
if (num.bitLength() > 32)
return 9;
num = num.toNumber();
}