refactor.

This commit is contained in:
Christopher Jeffrey 2016-03-15 13:31:54 -07:00
parent 3495b6fbf9
commit 5fb16d29e6
3 changed files with 21 additions and 83 deletions

View File

@ -23,7 +23,7 @@ function Witness(items) {
}
Witness.prototype.inspect = function inspect() {
return bcoin.script.format(this.items);
return Script.format(this.items);
};
Witness.prototype.encode = function encode() {
@ -130,7 +130,7 @@ function Stack(items) {
}
Stack.prototype.inspect = function inspect() {
return bcoin.script.format(this.items);
return Script.format(this.items);
};
Stack.prototype.__defineGetter__('length', function() {
@ -1193,7 +1193,7 @@ Script.prototype.removeData = function removeData(data) {
};
Script.checkPush = function checkPush(value, flags) {
var op;
var pushdata = value.pushdata;
if (flags == null)
flags = constants.flags.STANDARD_VERIFY_FLAGS;
@ -1201,14 +1201,9 @@ Script.checkPush = function checkPush(value, flags) {
if (!(flags & constants.flags.VERIFY_MINIMALDATA))
return true;
if (!value.pushdata)
if (!pushdata)
return true;
op = value.pushdata.opcode;
if (!op)
op = value.pushdata.len;
if (value.length === 1 && value[0] === 0)
return false;
@ -1219,13 +1214,13 @@ Script.checkPush = function checkPush(value, flags) {
return false;
if (value.length <= 75)
return op === value.length;
return pushdata.opcode == null && pushdata.len === value.length;
if (value.length <= 255)
return op === 'pushdata1';
return pushdata.opcode === 'pushdata1';
if (value.length <= 65535)
return op === 'pushdata2';
return pushdata.opcode === 'pushdata2';
return true;
};
@ -2471,22 +2466,22 @@ Script.decode = function decode(buf) {
var off = 0;
var b, opcode, len;
if (!buf)
return [];
assert(Buffer.isBuffer(buf));
// NOTE: We can't use a BufferReader here since
// script parsing was originally non-strict.
// script parsing was originally non-strict/ridiculous.
// Something could do a direct push of 30 bytes with
// only 20 bytes after it.
while (off < buf.length) {
b = buf[off++];
// OP_0, OP_FALSE
// Special case: this is an empty array
// because it can be seen as an empty pushdata.
if (b === 0x00) {
code.push(0);
continue;
}
// Direct Push
// Next `b` bytes should be pushed to stack
if (b >= 0x01 && b <= 0x4b) {
code.push(utils.slice(buf, off, off + b));
@ -2554,6 +2549,8 @@ Script.encode = function encode(code) {
var i = 0;
var op;
assert(Array.isArray(code));
for (i = 0; i < code.length; i++) {
op = code[i];

View File

@ -200,25 +200,17 @@ utils.isBase58 = function isBase58(msg) {
};
utils.ripemd160 = function ripemd160(data, enc) {
var result;
if (!crypto)
return new Buffer(hash.ripemd160().update(data, enc).digest());
result = crypto.createHash('ripemd160').update(data, enc).digest();
return result;
return crypto.createHash('ripemd160').update(data, enc).digest();
};
utils.sha1 = function sha1(data, enc) {
var result;
if (!crypto)
return new Buffer(hash.sha1().update(data, enc).digest());
result = crypto.createHash('sha1').update(data, enc).digest();
return result;
return crypto.createHash('sha1').update(data, enc).digest();
};
utils.ripesha = function ripesha(data, enc) {
@ -230,14 +222,10 @@ utils.checksum = function checksum(data, enc) {
};
utils.sha256 = function sha256(data, enc) {
var result;
if (!crypto)
return new Buffer(hash.sha256().update(data, enc).digest());
result = crypto.createHash('sha256').update(data, enc).digest();
return result;
return crypto.createHash('sha256').update(data, enc).digest();
};
utils.dsha256 = function dsha256(data, enc) {
@ -284,8 +272,11 @@ utils.encrypt = function encrypt(data, passphrase) {
utils.decrypt = function decrypt(data, passphrase) {
var decipher, out;
if (!crypto)
if (!crypto) {
if (data[0] === ':')
throw new Error('Cannot decrypt.');
return data;
}
if (data[0] !== ':')
return data;
@ -873,10 +864,6 @@ utils.fromCompact = function fromCompact(compact) {
if (negative)
num.ineg();
// var overflow = mantissa !== 0 && ((exponent > 34)
// || (mantissa > 0xff && exponent > 33)
// || (mantissa > 0xffff && exponent > 32));
return num;
};
@ -925,28 +912,6 @@ utils.host = function host(addr) {
return addr.split(':')[0];
};
utils.hash = function hash(obj, enc) {
if (obj == null)
throw new Error('Cannot get hash of null');
if (typeof obj === 'string')
return enc === 'hex' ? obj : new Buffer(obj, 'hex');
if (Buffer.isBuffer(obj))
return enc === 'hex' ? utils.toHex(obj) : obj;
if (typeof obj.hash === 'function')
return obj.hash(enc);
if (obj.hash)
return hash(obj.hash, enc);
if (obj._hash)
return hash(obj._hash, enc);
throw new Error('Cannot get hash of object');
};
utils.U64 = new bn('ffffffffffffffff', 'hex');
utils.nonce = function nonce() {
@ -1441,9 +1406,6 @@ utils.ccmp = function(a, b) {
assert(Buffer.isBuffer(a));
assert(Buffer.isBuffer(b));
if (a.length !== b.length)
return false;
for (i = 0; i < a.length; i++)
res |= a[i] ^ b[i];

View File

@ -42,25 +42,4 @@ describe('Utils', function() {
btc = utils.fromBTC('546.0');
assert(btc.cmp(new bn(5460).mul(new bn(10000000))) === 0);
});
it('should convert objects to hashes', function() {
var b1 = '00';
var b2 = new Buffer([0]);
var b3 = { hash: function(enc) { return enc === 'hex' ? '00' : new Buffer([0]); } };
var b4 = { hash: '00' };
var b5 = { _hash: '00' };
var b6 = { hash: new Buffer([0]) };
var b7 = { _hash: new Buffer([0]) };
[b1, b2, b3, b4, b5, b6, b7].forEach(function(b, i) {
utils.assert.equal(utils.hash(b, 'hex'), '00');
utils.assert(utils.isEqual(utils.hash(b), [0]));
});
var thrown = true;
try {
utils.hash(1, 'hex');
} catch (e) {
thrown = true;
}
assert.equal(thrown, true);
});
});