Merge pull request #82 from Dcousens/jshint

Basic pass through JSHint
This commit is contained in:
Wei Lu 2014-03-24 09:50:56 +08:00
commit c42e0fb8e0
6 changed files with 92 additions and 68 deletions

View file

@ -1,11 +1,9 @@
// https://en.bitcoin.it/wiki/Base58Check_encoding // https://en.bitcoin.it/wiki/Base58Check_encoding
var BigInteger = require('./jsbn/jsbn'); var BigInteger = require('./jsbn/jsbn');
var Crypto = require('crypto-js'); var Crypto = require('crypto-js');
var SHA256 = Crypto.SHA256;
var WordArray = Crypto.lib.WordArray;
var convert = require('./convert'); var convert = require('./convert');
var SHA256 = Crypto.SHA256;
var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var base = BigInteger.valueOf(58); var base = BigInteger.valueOf(58);
@ -38,7 +36,7 @@ module.exports.encode = function (input) {
} }
return chars.reverse().join(''); return chars.reverse().join('');
}, }
// decode a base58 string into a byte array // decode a base58 string into a byte array
// input should be a base58 encoded string // input should be a base58 encoded string
@ -90,10 +88,13 @@ module.exports.checkDecode = function(input) {
var bytes = module.exports.decode(input), var bytes = module.exports.decode(input),
front = bytes.slice(0,bytes.length-4), front = bytes.slice(0,bytes.length-4),
back = bytes.slice(bytes.length-4); back = bytes.slice(bytes.length-4);
var checksum = getChecksum(front) var checksum = getChecksum(front)
if (""+checksum != ""+back) { if (""+checksum != ""+back) {
throw new Error("Checksum failed"); throw new Error("Checksum failed");
} }
var o = front.slice(1); var o = front.slice(1);
o.version = front[0]; o.version = front[0];
return o; return o;
@ -105,4 +106,3 @@ function getChecksum(bytes) {
} }
module.exports.getChecksum = getChecksum module.exports.getChecksum = getChecksum

View file

@ -53,8 +53,8 @@ exports.base64ToBytes = function(base64) {
// Remove non-base-64 characters // Remove non-base-64 characters
base64 = base64.replace(/[^A-Z0-9+\/]/ig, ''); base64 = base64.replace(/[^A-Z0-9+\/]/ig, '');
var bytes = [] var bytes = [];
, imod4 = 0 var imod4 = 0;
for (var i = 0; i < base64.length; imod4 = ++i % 4) { for (var i = 0; i < base64.length; imod4 = ++i % 4) {
if (!imod4) continue if (!imod4) continue

View file

@ -248,7 +248,7 @@ var ECDSA = {
var alpha = x.multiply(x).multiply(x).add(a.multiply(x)).add(b).mod(p); var alpha = x.multiply(x).multiply(x).add(a.multiply(x)).add(b).mod(p);
var beta = alpha.modPow(P_OVER_FOUR, p); var beta = alpha.modPow(P_OVER_FOUR, p);
var xorOdd = beta.isEven() ? (i % 2) : ((i+1) % 2); // var xorOdd = beta.isEven() ? (i % 2) : ((i+1) % 2);
// If beta is even, but y isn't or vice versa, then convert it, // If beta is even, but y isn't or vice versa, then convert it,
// otherwise we're done and y == beta. // otherwise we're done and y == beta.
var y = (beta.isEven() ? !isYEven : isYEven) ? beta : p.subtract(beta); var y = (beta.isEven() ? !isYEven : isYEven) ? beta : p.subtract(beta);

View file

@ -63,7 +63,7 @@ ECKey.prototype.getPub = function(compressed) {
* @deprecated Reserved keyword, factory pattern. Use toHex, toBytes, etc. * @deprecated Reserved keyword, factory pattern. Use toHex, toBytes, etc.
*/ */
ECKey.prototype['export'] = function(format) { ECKey.prototype['export'] = function(format) {
format || (format = 'hex') var format = format || 'hex'
return this['to' + format.substr(0, 1).toUpperCase() + format.substr(1)]() return this['to' + format.substr(0, 1).toUpperCase() + format.substr(1)]()
} }
@ -77,7 +77,7 @@ ECKey.version_bytes = {
} }
ECKey.prototype.toWif = function(version) { ECKey.prototype.toWif = function(version) {
var version = version || Network.mainnet.addressVersion; version = version || Network.mainnet.addressVersion;
return base58.checkEncode(this.toBytes(), ECKey.version_bytes[version]) return base58.checkEncode(this.toBytes(), ECKey.version_bytes[version])
} }
@ -147,7 +147,7 @@ ECPubKey.prototype.multiply = function(key) {
} }
ECPubKey.prototype['export'] = function(format) { ECPubKey.prototype['export'] = function(format) {
format || (format = 'hex') var format = format || 'hex';
return this['to' + format.substr(0, 1).toUpperCase() + format.substr(1)]() return this['to' + format.substr(0, 1).toUpperCase() + format.substr(1)]()
} }
@ -165,7 +165,7 @@ ECPubKey.prototype.toBin = function() {
} }
ECPubKey.prototype.toWif = function(version) { ECPubKey.prototype.toWif = function(version) {
var version = version || Network.mainnet.addressVersion; version = version || Network.mainnet.addressVersion;
return base58.checkEncode(this.toBytes(), version) return base58.checkEncode(this.toBytes(), version)
} }
@ -173,7 +173,7 @@ ECPubKey.prototype.toWif = function(version) {
ECPubKey.prototype.toString = ECPubKey.prototype.toHex ECPubKey.prototype.toString = ECPubKey.prototype.toHex
ECPubKey.prototype.getAddress = function(version) { ECPubKey.prototype.getAddress = function(version) {
var version = version || Network.mainnet.addressVersion; version = version || Network.mainnet.addressVersion;
return new Address(util.sha256ripe160(this.toBytes()), version); return new Address(util.sha256ripe160(this.toBytes()), version);
} }

View file

@ -1,4 +1,4 @@
Opcode = { var Opcode = {
map: { map: {
// push value // push value
OP_0 : 0, OP_0 : 0,
@ -144,4 +144,4 @@ for(var i in Opcode.map) {
Opcode.reverseMap[Opcode.map[i]] = i Opcode.reverseMap[Opcode.map[i]] = i
} }
module.exports = Opcode module.exports = Opcode;

View file

@ -5,7 +5,6 @@ var convert = require('./convert');
var ECKey = require('./eckey').ECKey; var ECKey = require('./eckey').ECKey;
var ECDSA = require('./ecdsa'); var ECDSA = require('./ecdsa');
var Address = require('./address'); var Address = require('./address');
var Message = require('./message');
var SHA256 = require('crypto-js/sha256'); var SHA256 = require('crypto-js/sha256');
var Transaction = function (doc) { var Transaction = function (doc) {
@ -14,27 +13,29 @@ var Transaction = function (doc) {
this.locktime = 0; this.locktime = 0;
this.ins = []; this.ins = [];
this.outs = []; this.outs = [];
this.defaultSequence = [255, 255, 255, 255] // 0xFFFFFFFF this.defaultSequence = [255, 255, 255, 255]; // 0xFFFFFFFF
if (doc) { if (doc) {
if (typeof doc == "string" || Array.isArray(doc)) { if (typeof doc == "string" || Array.isArray(doc)) {
doc = Transaction.deserialize(doc) doc = Transaction.deserialize(doc);
} }
if (doc.hash) this.hash = doc.hash; if (doc.hash) this.hash = doc.hash;
if (doc.version) this.version = doc.version; if (doc.version) this.version = doc.version;
if (doc.locktime) this.locktime = doc.locktime; if (doc.locktime) this.locktime = doc.locktime;
if (doc.ins && doc.ins.length) { if (doc.ins && doc.ins.length) {
for (var i = 0; i < doc.ins.length; i++) { doc.ins.forEach(function(input) {
this.addInput(new TransactionIn(doc.ins[i])); this.addInput(new TransactionIn(input));
} }, this);
}
if (doc.outs && doc.outs.length) {
for (var i = 0; i < doc.outs.length; i++) {
this.addOutput(new TransactionOut(doc.outs[i]));
}
} }
this.hash = this.hash || this.getHash() if (doc.outs && doc.outs.length) {
doc.outs.forEach(function(output) {
this.addOutput(new TransactionOut(output));
}, this);
}
this.hash = this.hash || this.getHash();
} }
}; };
@ -59,8 +60,9 @@ Transaction.prototype.addInput = function (tx, outIndex) {
return this.addInput(args[0], args[1]); return this.addInput(args[0], args[1]);
} }
else { else {
var hash = typeof tx === "string" ? tx : tx.hash var hash = typeof tx === "string" ? tx : tx.hash;
var hash = Array.isArray(hash) ? convert.bytesToHex(hash) : hash hash = Array.isArray(hash) ? convert.bytesToHex(hash) : hash;
this.ins.push(new TransactionIn({ this.ins.push(new TransactionIn({
outpoint: { outpoint: {
hash: hash, hash: hash,
@ -87,11 +89,13 @@ Transaction.prototype.addOutput = function (address, value) {
this.outs.push(arguments[0]); this.outs.push(arguments[0]);
return; return;
} }
if (arguments[0].indexOf(':') >= 0) { if (arguments[0].indexOf(':') >= 0) {
var args = arguments[0].split(':'); var args = arguments[0].split(':');
address = args[0]; address = args[0];
value = parseInt(args[1]); value = parseInt(args[1]);
} }
this.outs.push(new TransactionOut({ this.outs.push(new TransactionOut({
value: value, value: value,
script: Script.createOutputScript(address) script: Script.createOutputScript(address)
@ -107,30 +111,33 @@ Transaction.prototype.addOutput = function (address, value) {
*/ */
Transaction.prototype.serialize = function () { Transaction.prototype.serialize = function () {
var buffer = []; var buffer = [];
buffer = buffer.concat(convert.numToBytes(parseInt(this.version),4)); buffer = buffer.concat(convert.numToBytes(parseInt(this.version), 4));
buffer = buffer.concat(convert.numToVarInt(this.ins.length)); buffer = buffer.concat(convert.numToVarInt(this.ins.length));
for (var i = 0; i < this.ins.length; i++) {
var txin = this.ins[i];
this.ins.forEach(function(txin) {
// Why do blockchain.info, blockexplorer.com, sx and just about everybody // Why do blockchain.info, blockexplorer.com, sx and just about everybody
// else use little-endian hashes? No idea... // else use little-endian hashes? No idea...
buffer = buffer.concat(convert.hexToBytes(txin.outpoint.hash).reverse()); buffer = buffer.concat(convert.hexToBytes(txin.outpoint.hash).reverse());
buffer = buffer.concat(convert.numToBytes(parseInt(txin.outpoint.index),4)); buffer = buffer.concat(convert.numToBytes(parseInt(txin.outpoint.index), 4));
var scriptBytes = txin.script.buffer; var scriptBytes = txin.script.buffer;
buffer = buffer.concat(convert.numToVarInt(scriptBytes.length)); buffer = buffer.concat(convert.numToVarInt(scriptBytes.length));
buffer = buffer.concat(scriptBytes); buffer = buffer.concat(scriptBytes);
buffer = buffer.concat(txin.sequence); buffer = buffer.concat(txin.sequence);
} });
buffer = buffer.concat(convert.numToVarInt(this.outs.length)); buffer = buffer.concat(convert.numToVarInt(this.outs.length));
for (var i = 0; i < this.outs.length; i++) {
var txout = this.outs[i]; this.outs.forEach(function(txout) {
buffer = buffer.concat(convert.numToBytes(txout.value,8)); buffer = buffer.concat(convert.numToBytes(txout.value,8));
var scriptBytes = txout.script.buffer; var scriptBytes = txout.script.buffer;
buffer = buffer.concat(convert.numToVarInt(scriptBytes.length)); buffer = buffer.concat(convert.numToVarInt(scriptBytes.length));
buffer = buffer.concat(scriptBytes); buffer = buffer.concat(scriptBytes);
} });
buffer = buffer.concat(convert.numToBytes(parseInt(this.locktime),4));
buffer = buffer.concat(convert.numToBytes(parseInt(this.locktime), 4));
return buffer; return buffer;
}; };
@ -139,7 +146,7 @@ Transaction.prototype.serializeHex = function() {
return convert.bytesToHex(this.serialize()); return convert.bytesToHex(this.serialize());
} }
var OP_CODESEPARATOR = 171; //var OP_CODESEPARATOR = 171;
var SIGHASH_ALL = 1; var SIGHASH_ALL = 1;
var SIGHASH_NONE = 2; var SIGHASH_NONE = 2;
@ -167,9 +174,9 @@ function (connectedScript, inIndex, hashType)
});*/ });*/
// Blank out other inputs' signatures // Blank out other inputs' signatures
for (var i = 0; i < txTmp.ins.length; i++) { txTmp.ins.forEach(function(txin) {
txTmp.ins[i].script = new Script(); txin.script = new Script();
} });
txTmp.ins[inIndex].script = connectedScript; txTmp.ins[inIndex].script = connectedScript;
@ -178,9 +185,12 @@ function (connectedScript, inIndex, hashType)
txTmp.outs = []; txTmp.outs = [];
// Let the others update at will // Let the others update at will
for (var i = 0; i < txTmp.ins.length; i++) txTmp.ins.forEach(function(txin, i) {
if (i != inIndex) if (i != inIndex) {
txTmp.ins[i].sequence = 0; txTmp.ins[i].sequence = 0;
}
});
} else if ((hashType & 0x1f) == SIGHASH_SINGLE) { } else if ((hashType & 0x1f) == SIGHASH_SINGLE) {
// TODO: Implement // TODO: Implement
} }
@ -192,9 +202,9 @@ function (connectedScript, inIndex, hashType)
var buffer = txTmp.serialize(); var buffer = txTmp.serialize();
buffer = buffer.concat(convert.numToBytes(parseInt(hashType),4)); buffer = buffer.concat(convert.numToBytes(parseInt(hashType), 4));
buffer = convert.bytesToWordArray(buffer); buffer = convert.bytesToWordArray(buffer);
return convert.wordArrayToBytes(SHA256(SHA256(buffer))); return convert.wordArrayToBytes(SHA256(SHA256(buffer)));
}; };
@ -217,14 +227,15 @@ Transaction.prototype.clone = function ()
var newTx = new Transaction(); var newTx = new Transaction();
newTx.version = this.version; newTx.version = this.version;
newTx.locktime = this.locktime; newTx.locktime = this.locktime;
for (var i = 0; i < this.ins.length; i++) {
var txin = this.ins[i].clone(); this.ins.forEach(function(txin) {
newTx.addInput(txin); newTx.addInput(txin.clone());
} });
for (var i = 0; i < this.outs.length; i++) {
var txout = this.outs[i].clone(); this.outs.forEach(function(txout) {
newTx.addOutput(txout); newTx.addOutput(txout.clone());
} });
return newTx; return newTx;
}; };
@ -238,7 +249,7 @@ Transaction.deserialize = function(buffer) {
} }
var pos = 0; var pos = 0;
var readAsInt = function(bytes) { var readAsInt = function(bytes) {
if (bytes == 0) return 0; if (bytes === 0) return 0;
pos++; pos++;
return buffer[pos-1] + readAsInt(bytes-1) * 256; return buffer[pos-1] + readAsInt(bytes-1) * 256;
} }
@ -263,7 +274,9 @@ Transaction.deserialize = function(buffer) {
} }
obj.version = readAsInt(4); obj.version = readAsInt(4);
var ins = readVarInt(); var ins = readVarInt();
for (var i = 0; i < ins; i++) { var i;
for (i = 0; i < ins; i++) {
obj.ins.push({ obj.ins.push({
outpoint: { outpoint: {
hash: convert.bytesToHex(readBytes(32).reverse()), hash: convert.bytesToHex(readBytes(32).reverse()),
@ -274,12 +287,14 @@ Transaction.deserialize = function(buffer) {
}); });
} }
var outs = readVarInt(); var outs = readVarInt();
for (var i = 0; i < outs; i++) {
for (i = 0; i < outs; i++) {
obj.outs.push({ obj.outs.push({
value: convert.bytesToNum(readBytes(8)), value: convert.bytesToNum(readBytes(8)),
script: new Script(readVarString()) script: new Script(readVarString())
}); });
} }
obj.locktime = readAsInt(4); obj.locktime = readAsInt(4);
return new Transaction(obj); return new Transaction(obj);
@ -292,7 +307,7 @@ Transaction.deserialize = function(buffer) {
Transaction.prototype.sign = function(index, key, type) { Transaction.prototype.sign = function(index, key, type) {
type = type || SIGHASH_ALL; type = type || SIGHASH_ALL;
key = new ECKey(key); key = new ECKey(key);
// TODO: getPub is slow, sha256ripe160 probably is too. // TODO: getPub is slow, sha256ripe160 probably is too.
// This could be sped up a lot by providing these as inputs. // This could be sped up a lot by providing these as inputs.
var pub = key.getPub().export('bytes'), var pub = key.getPub().export('bytes'),
@ -306,6 +321,7 @@ Transaction.prototype.sign = function(index, key, type) {
// Takes outputs of the form [{ output: 'txhash:index', address: 'address' },...] // Takes outputs of the form [{ output: 'txhash:index', address: 'address' },...]
Transaction.prototype.signWithKeys = function(keys, outputs, type) { Transaction.prototype.signWithKeys = function(keys, outputs, type) {
type = type || SIGHASH_ALL; type = type || SIGHASH_ALL;
var addrdata = keys.map(function(key) { var addrdata = keys.map(function(key) {
key = new ECKey(key); key = new ECKey(key);
return { return {
@ -313,18 +329,24 @@ Transaction.prototype.signWithKeys = function(keys, outputs, type) {
address: key.getAddress().toString() address: key.getAddress().toString()
} }
}); });
var hmap = {}; var hmap = {};
for (var o in outputs) { outputs.forEach(function(o) {
hmap[outputs[o].output] = outputs[o]; hmap[o.output] = o;
} });
for (var i = 0; i < this.ins.length; i++) { for (var i = 0; i < this.ins.length; i++) {
var outpoint = this.ins[i].outpoint.hash+':'+this.ins[i].outpoint.index, var outpoint = this.ins[i].outpoint.hash + ':' + this.ins[i].outpoint.index;
histItem = hmap[outpoint]; var histItem = hmap[outpoint];
if (!histItem) continue; if (!histItem) continue;
var thisInputAddrdata = addrdata.filter(function(a) { var thisInputAddrdata = addrdata.filter(function(a) {
return a.address == histItem.address; return a.address == histItem.address;
}); });
if (thisInputAddrdata.length == 0) continue;
if (thisInputAddrdata.length === 0) continue;
this.sign(i,thisInputAddrdata[0].key); this.sign(i,thisInputAddrdata[0].key);
} }
} }
@ -344,7 +366,7 @@ Transaction.prototype.p2shsign = function(index, script, key, type) {
Transaction.prototype.multisign = Transaction.prototype.p2shsign; Transaction.prototype.multisign = Transaction.prototype.p2shsign;
Transaction.prototype.applyMultisigs = function(index, script, sigs, type) { Transaction.prototype.applyMultisigs = function(index, script, sigs/*, type*/) {
this.ins[index].script = Script.createMultiSigInputScript(sigs, script); this.ins[index].script = Script.createMultiSigInputScript(sigs, script);
} }
@ -411,6 +433,8 @@ TransactionOut.prototype.clone = function ()
return newTxout; return newTxout;
}; };
module.exports.Transaction = Transaction; module.exports = {
module.exports.TransactionIn = TransactionIn; Transaction: Transaction,
module.exports.TransactionOut = TransactionOut; TransactionIn: TransactionIn,
TransactionOut: TransactionOut
}