even more bufffer work.

This commit is contained in:
Christopher Jeffrey 2016-01-08 04:10:30 -08:00
parent 465b23013d
commit d56f7a3733
9 changed files with 56 additions and 41 deletions

View File

@ -445,7 +445,7 @@ Chain.prototype.byHeight = function byHeight(height) {
};
Chain.prototype.byHash = function byHash(hash) {
if (utils.isArray(hash))
if (utils.isBuffer(hash))
hash = utils.toHex(hash);
else if (hash.hash)
hash = hash.hash('hex');
@ -480,7 +480,7 @@ Chain.prototype.getBlock = function getBlock(hash) {
};
Chain.prototype.getOrphan = function getOrphan(hash) {
if (utils.isArray(hash))
if (utils.isBuffer(hash))
hash = utils.toHex(hash);
else if (hash.hash)
hash = hash.hash('hex');
@ -533,7 +533,7 @@ Chain.prototype.locatorHashes = function locatorHashes(start) {
var i;
if (start) {
if (utils.isArray(start))
if (utils.isBuffer(start))
start = utils.toHex(start);
else if (start.hash)
start = start.hash('hex');
@ -577,7 +577,7 @@ Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) {
var self = this;
var root = hash;
if (utils.isArray(hash))
if (utils.isBuffer(hash))
hash = utils.toHex(hash);
else if (hash.hash)
hash = hash.hash('hex');

View File

@ -138,7 +138,7 @@ function HDPrivateKey(options) {
if (options.seed
&& typeof options.seed === 'object'
&& !Array.isArray(options.seed)
&& !utils.isBuffer(options.seed)
&& !(options.seed instanceof bcoin.hd.seed)) {
options.seed = bcoin.hd.seed(options.seed);
}

View File

@ -1030,7 +1030,7 @@ Pool.prototype.search = function search(id, range, e) {
// Optional id argument
if ((id !== null
&& typeof id === 'object'
&& !Array.isArray(id))
&& !utils.isBuffer(id))
|| typeof id === 'number') {
range = id;
id = null;

View File

@ -197,7 +197,7 @@ Parser.prototype.parseInvList = function parseInvList(p) {
for (i = 0, off = 0; i < count; i++, off += 36) {
items.push({
type: constants.invByVal[readU32(p, off)],
hash: p.slice(off + 4, off + 36)
hash: utils.toArray(p.slice(off + 4, off + 36))
});
}
@ -220,7 +220,7 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
hashes = new Array(hashCount);
for (i = 0; i < hashCount; i++)
hashes[i] = p.slice(off + i * 32, off + (i + 1) * 32);
hashes[i] = utils.toArray(p.slice(off + i * 32, off + (i + 1) * 32));
off = off + 32 * hashCount;
flagCount = utils.readIntv(p, off);
@ -230,12 +230,12 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
if (off + flagCount > p.length)
return this._error('Invalid flag count');
flags = p.slice(off, off + flagCount);
flags = utils.toArray(p.slice(off, off + flagCount));
return {
version: readU32(p, 0),
prevBlock: p.slice(4, 36),
merkleRoot: p.slice(36, 68),
prevBlock: utils.toArray(p.slice(4, 36)),
merkleRoot: utils.toArray(p.slice(36, 68)),
ts: readU32(p, 68),
bits: readU32(p, 72),
nonce: readU32(p, 76),
@ -266,9 +266,9 @@ Parser.prototype.parseHeaders = function parseHeaders(p) {
start = off;
header.version = readU32(p, off);
off += 4;
header.prevBlock = p.slice(off, off + 32);
header.prevBlock = utils.toArray(p.slice(off, off + 32));
off += 32;
header.merkleRoot = p.slice(off, off + 32);
header.merkleRoot = utils.toArray(p.slice(off, off + 32));
off += 32;
header.ts = readU32(p, off);
off += 4;
@ -307,8 +307,8 @@ Parser.prototype.parseBlock = function parseBlock(p) {
return {
version: readU32(p, 0),
prevBlock: p.slice(4, 36),
merkleRoot: p.slice(36, 68),
prevBlock: utils.toArray(p.slice(4, 36)),
merkleRoot: utils.toArray(p.slice(36, 68)),
ts: readU32(p, 68),
bits: readU32(p, 72),
nonce: readU32(p, 76),
@ -335,10 +335,9 @@ Parser.prototype.parseTXIn = function parseTXIn(p) {
return {
size: off + scriptLen + 4,
out: {
hash: p.slice(0, 32),
hash: utils.toArray(p.slice(0, 32)),
index: readU32(p, 32)
},
// script: bcoin.script.decode(p.slice(off, off + scriptLen)),
script: bcoin.script.decode(utils.toArray(p.slice(off, off + scriptLen))),
seq: readU32(p, off + scriptLen)
};
@ -359,8 +358,6 @@ Parser.prototype.parseTXOut = function parseTXOut(p) {
return {
size: off + scriptLen,
// value: new bn(p.slice(0, 8).reverse()),
// script: bcoin.script.decode(p.slice(off, off + scriptLen))
value: new bn(utils.toArray(p.slice(0, 8)).reverse()),
script: bcoin.script.decode(utils.toArray(p.slice(off, off + scriptLen)))
};
@ -461,7 +458,7 @@ Parser.prototype.parseReject = function parseReject(p) {
off += reasonLen;
data = p.slice(off, off + 32);
data = utils.toArray(p.slice(off, off + 32));
return {
message: message,

View File

@ -1020,7 +1020,7 @@ script.isMultisig = function isMultisig(s, pubs) {
return false;
// compat
if (pubs && !Array.isArray(pubs[0]))
if (pubs && !utils.isBuffer(pubs[0]))
pubs = [pubs];
m = s[0];
@ -1321,7 +1321,7 @@ script.isCoinbase = function isCoinbase(s, block, strict) {
};
script.isSig = function isSig(sig, allowZero) {
if (!Array.isArray(sig))
if (!utils.isBuffer(sig))
return false;
if (allowZero && sig.length === 0)
@ -1344,7 +1344,7 @@ script.isSig = function isSig(sig, allowZero) {
script.isValidSig = function isValidSig(sig, allowZero) {
var lenR, lenS;
if (!Array.isArray(sig))
if (!utils.isBuffer(sig))
return false;
// Empty signature. Not strictly DER encoded, but allowed to provide a

View File

@ -243,7 +243,7 @@ TXPool.prototype.unspent = function unspent() {
TXPool.prototype.hasUnspent = function hasUnspent(hash, unspent) {
var has;
if (Array.isArray(hash) && hash.length && typeof hash[0] !== 'number') {
if (utils.isBuffer(hash) && hash.length && typeof hash[0] !== 'number') {
unspent = this.unspent();
has = hash.map(function(hash) {
var h = this.hasUnspent(hash, unspent);
@ -256,7 +256,7 @@ TXPool.prototype.hasUnspent = function hasUnspent(hash, unspent) {
return has;
}
if (Array.isArray(hash))
if (utils.isBuffer(hash))
hash = utils.toHex(hash);
else if (hash.out)
hash = hash.out.hash;

View File

@ -99,7 +99,7 @@ TX.prototype._input = function _input(obj, index) {
if (obj instanceof TX)
options = { tx: obj, index: index };
else if (typeof obj === 'string' || Array.isArray(obj))
else if (typeof obj === 'string' || utils.isBuffer(obj))
options = { hash: obj, index: index };
else
options = obj;
@ -325,7 +325,7 @@ TX.prototype.signInput = function signInput(input, key, type) {
// Build the scriptSig and sign it
TX.prototype.scriptSig = function scriptSig(input, key, pub, type) {
if (!Array.isArray(pub)) {
if (!utils.isBuffer(pub)) {
type = pub;
pub = key.getPublic(true, 'array');
}

View File

@ -150,19 +150,24 @@ utils.isBase58 = function isBase58(msg) {
};
utils.ripemd160 = function ripemd160(data, enc) {
// return hash.ripemd160().update(data, enc).digest();
var hash;
if (Array.isArray(data))
data = new Buffer(data);
return crypto.createHash('ripemd160').update(data, enc).digest();
return hash.ripemd160().update(data, enc).digest();
hash = crypto.createHash('ripemd160').update(data, enc).digest();
return utils.toArray(hash);
};
utils.sha1 = function sha1(data, enc) {
return crypto.createHash('sha1').update(data, enc).digest();
return hash.sha1().update(data, enc).digest();
// return hash.sha1().update(data, enc).digest();
var hash;
if (Array.isArray(data))
data = new Buffer(data);
hash = crypto.createHash('sha1').update(data, enc).digest();
return utils.toArray(hash);
};
utils.ripesha = function ripesha(data, enc) {
// return hash.ripemd160().update(utils.sha256(data, enc)).digest();
return utils.ripemd160(utils.sha256(data, enc));
};
@ -171,10 +176,12 @@ utils.checksum = function checksum(data, enc) {
};
utils.sha256 = function sha256(data, enc) {
// return hash.sha256().update(data, enc).digest();
var hash;
if (Array.isArray(data))
data = new Buffer(data);
return crypto.createHash('sha256').update(data, enc).digest();
return hash.sha256().update(data, enc).digest();
hash = crypto.createHash('sha256').update(data, enc).digest();
return utils.toArray(hash);
};
utils.dsha256 = function dsha256(data, enc) {
@ -353,10 +360,15 @@ utils.array2ascii = function array2ascii(arr, printable) {
};
utils.array2utf8 = function array2utf8(arr) {
if (Buffer.isBuffer(arr))
return arr.toString('utf8');
return new Buffer(arr).toString('utf8');
};
utils.copy = function copy(src, dst, off, force) {
if (Buffer.isBuffer(src) && Buffer.isBuffer(dst) && !force)
return src.copy(dst, off, 0, src.length);
var len = src.length;
var i = 0;
@ -373,6 +385,9 @@ utils.stringify = function stringify(arr) {
var res = '';
var i = 0;
if (Buffer.isBuffer(arr))
return arr.toString('ascii');
for (; i < arr.length; i++)
res += String.fromCharCode(arr[i]);
@ -614,6 +629,8 @@ utils.isSatoshi = function isSatoshi(val) {
return new bn(val, 10);
if (utils.isHex(val))
return new bn(val, 'hex');
if (Buffer.isBuffer(val))
return new bn(utils.toArray(val));
if (Array.isArray(val))
return new bn(val);
return false;
@ -658,11 +675,12 @@ utils.isArrayLike = function isArrayLike(msg) {
};
utils.isArray = function isArray(msg) {
if (Array.isArray(msg))
return true;
if (Buffer.isBuffer(msg))
return true;
return false;
return Array.isArray(msg);
};
utils.isBuffer = function isBuffer(msg) {
utils.assert(!Buffer.isBuffer(msg));
return Array.isArray(msg);
};
utils.toKeyArray = function toKeyArray(msg) {
@ -869,7 +887,7 @@ utils.hash = function hash(obj, enc) {
if (typeof obj === 'string')
return enc === 'hex' ? obj : utils.toArray(obj, 'hex');
if (Array.isArray(obj))
if (utils.isBuffer(obj))
return enc === 'hex' ? utils.toHex(obj) : obj;
if (typeof obj.hash === 'function')

View File

@ -328,7 +328,7 @@ Wallet.addr2hash = function addr2hash(addr, prefix) {
if (prefix == null && typeof addr === 'string')
prefix = Wallet.prefixes[addr[0]];
if (!Array.isArray(addr))
if (!utils.isBuffer(addr))
addr = utils.fromBase58(addr);
prefix = network.prefixes[prefix || 'pubkeyhash'];