run js-beautify on everything

...with two spaces. Command: js-beautify -s 2 -r [filename]
This commit is contained in:
Ryan X. Charles 2014-06-24 08:36:32 -07:00
commit ea2e2d4e19
49 changed files with 859 additions and 682 deletions

View file

@ -1,17 +1,17 @@
'use strict';
var imports = require('soop').imports();
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks;
var util = bitcore.util;
var Structure = require('./Structure');
var imports = require('soop').imports();
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks;
var util = bitcore.util;
var Structure = require('./Structure');
function PrivateKey(opts) {
opts = opts || {};
this.network = opts.networkName === 'testnet' ?
this.network = opts.networkName === 'testnet' ?
networks.testnet : networks.livenet;
var init = opts.extendedPrivateKeyString || this.network.name;
this.bip = opts.HK || new HK(init);
@ -81,15 +81,19 @@ PrivateKey.prototype.getForPaths = function(paths) {
PrivateKey.prototype.getForPath = function(path) {
var pk = this.privateKeyCache[path];
if (!pk) {
var derivedHK = this._getHK(path);
var derivedHK = this._getHK(path);
pk = this.privateKeyCache[path] = derivedHK.eckey.private.toString('hex');
}
var wk = new WalletKey({network: this.network});
wk.fromObj({priv: pk});
var wk = new WalletKey({
network: this.network
});
wk.fromObj({
priv: pk
});
return wk;
};
PrivateKey.prototype.get = function(index,isChange) {
PrivateKey.prototype.get = function(index, isChange) {
var path = Structure.FullBranch(index, isChange);
return this.getForPath(path);
};
@ -99,11 +103,11 @@ PrivateKey.prototype.getAll = function(receiveIndex, changeIndex) {
throw new Error('Invalid parameters');
var ret = [];
for(var i=0;i<receiveIndex; i++) {
ret.push(this.get(i,false));
for (var i = 0; i < receiveIndex; i++) {
ret.push(this.get(i, false));
}
for(var i=0; i<changeIndex; i++) {
ret.push(this.get(i,true));
for (var i = 0; i < changeIndex; i++) {
ret.push(this.get(i, true));
}
return ret;
};

View file

@ -1,24 +1,23 @@
'use strict';
var imports = require('soop').imports();
var imports = require('soop').imports();
var preconditions = require('preconditions').instance();
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var PrivateKey = require('./PrivateKey');
var Structure = require('./Structure');
var AddressIndex= require('./AddressIndex');
var Address = bitcore.Address;
var Script = bitcore.Script;
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var PrivateKey = require('./PrivateKey');
var Structure = require('./Structure');
var AddressIndex = require('./AddressIndex');
var Address = bitcore.Address;
var Script = bitcore.Script;
function PublicKeyRing(opts) {
opts = opts || {};
this.walletId = opts.walletId;
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
this.requiredCopayers = opts.requiredCopayers || 3;
this.totalCopayers = opts.totalCopayers || 5;
@ -33,11 +32,11 @@ function PublicKeyRing(opts) {
this.addressToPath = {};
}
PublicKeyRing.fromObj = function (data) {
PublicKeyRing.fromObj = function(data) {
if (data instanceof PublicKeyRing) {
throw new Error('bad data format: Did you use .toObj()?');
}
var ret = new PublicKeyRing(data);
var ret = new PublicKeyRing(data);
for (var k in data.copayersExtPubKeys) {
ret.addCopayer(data.copayersExtPubKeys[k]);
@ -54,8 +53,8 @@ PublicKeyRing.prototype.toObj = function() {
totalCopayers: this.totalCopayers,
indexes: this.indexes.toObj(),
copayersExtPubKeys: this.copayersHK.map( function (b) {
return b.extendedPublicKeyString();
copayersExtPubKeys: this.copayersHK.map(function(b) {
return b.extendedPublicKeyString();
}),
nicknameFor: this.nicknameFor,
publicKeysCache: this.publicKeysCache
@ -67,11 +66,11 @@ PublicKeyRing.prototype.getCopayerId = function(i) {
return this.copayerIds[i];
};
PublicKeyRing.prototype.registeredCopayers = function () {
PublicKeyRing.prototype.registeredCopayers = function() {
return this.copayersHK.length;
};
PublicKeyRing.prototype.isComplete = function () {
PublicKeyRing.prototype.isComplete = function() {
return this.registeredCopayers() === this.totalCopayers;
};
@ -86,21 +85,23 @@ PublicKeyRing.prototype.myCopayerId = function(i) {
PublicKeyRing.prototype._checkKeys = function() {
if (!this.isComplete())
throw new Error('dont have required keys yet');
throw new Error('dont have required keys yet');
};
PublicKeyRing.prototype._newExtendedPublicKey = function () {
return new PrivateKey({networkName: this.network.name})
PublicKeyRing.prototype._newExtendedPublicKey = function() {
return new PrivateKey({
networkName: this.network.name
})
.deriveBIP45Branch()
.extendedPublicKeyString();
};
PublicKeyRing.prototype._updateBip = function (index) {
PublicKeyRing.prototype._updateBip = function(index) {
var hk = this.copayersHK[index].derive(Structure.IdBranch);
this.copayerIds[index]= hk.eckey.public.toString('hex');
this.copayerIds[index] = hk.eckey.public.toString('hex');
};
PublicKeyRing.prototype._setNicknameForIndex = function (index, nickname) {
PublicKeyRing.prototype._setNicknameForIndex = function(index, nickname) {
this.nicknameFor[this.copayerIds[index]] = nickname;
};
@ -114,9 +115,9 @@ PublicKeyRing.prototype.nicknameForCopayer = function(copayerId) {
PublicKeyRing.prototype.addCopayer = function(newEpk, nickname) {
if (this.isComplete())
throw new Error('PKR already has all required key:' + this.totalCopayers);
throw new Error('PKR already has all required key:' + this.totalCopayers);
this.copayersHK.forEach(function(b){
this.copayersHK.forEach(function(b) {
if (b.extendedPublicKeyString() === newEpk)
throw new Error('PKR already has that key');
});
@ -129,7 +130,7 @@ PublicKeyRing.prototype.addCopayer = function(newEpk, nickname) {
var bip = new HK(newEpk);
this.copayersHK.push(bip);
this._updateBip(i);
if (nickname) {
if (nickname) {
this._setNicknameForIndex(i, nickname);
}
return newEpk;
@ -138,19 +139,22 @@ PublicKeyRing.prototype.addCopayer = function(newEpk, nickname) {
PublicKeyRing.prototype.getPubKeys = function(index, isChange) {
this._checkKeys();
var path = Structure.Branch(index, isChange);
var path = Structure.Branch(index, isChange);
var pubKeys = this.publicKeysCache[path];
if (!pubKeys) {
pubKeys = [];
var l = this.copayersHK.length;
for(var i=0; i<l; i++) {
for (var i = 0; i < l; i++) {
var hk = this.copayersHK[i].derive(path);
pubKeys[i] = hk.eckey.public;
}
this.publicKeysCache[path] = pubKeys.map(function(pk){return pk.toString('hex');});
}
else {
pubKeys = pubKeys.map(function(s){return new Buffer(s,'hex');});
this.publicKeysCache[path] = pubKeys.map(function(pk) {
return pk.toString('hex');
});
} else {
pubKeys = pubKeys.map(function(s) {
return new Buffer(s, 'hex');
});
}
@ -158,15 +162,15 @@ PublicKeyRing.prototype.getPubKeys = function(index, isChange) {
};
// TODO this could be cached
PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
PublicKeyRing.prototype.getRedeemScript = function(index, isChange) {
var pubKeys = this.getPubKeys(index, isChange);
var script = Script.createMultisig(this.requiredCopayers, pubKeys);
var script = Script.createMultisig(this.requiredCopayers, pubKeys);
return script;
};
// TODO this could be cached
PublicKeyRing.prototype.getAddress = function (index, isChange) {
var script = this.getRedeemScript(index,isChange);
PublicKeyRing.prototype.getAddress = function(index, isChange) {
var script = this.getRedeemScript(index, isChange);
var address = Address.fromScript(script, this.network.name);
this.addressToPath[address.toString()] = Structure.FullBranch(index, isChange);
return address;
@ -174,13 +178,13 @@ PublicKeyRing.prototype.getAddress = function (index, isChange) {
PublicKeyRing.prototype.pathForAddress = function(address) {
var path = this.addressToPath[address];
if (!path) throw new Error('Couldn\'t find path for address '+address);
if (!path) throw new Error('Couldn\'t find path for address ' + address);
return path;
};
// TODO this could be cached
PublicKeyRing.prototype.getScriptPubKeyHex = function (index, isChange) {
var addr = this.getAddress(index,isChange);
PublicKeyRing.prototype.getScriptPubKeyHex = function(index, isChange) {
var addr = this.getAddress(index, isChange);
return Script.createP2SH(addr.payload()).getBuffer().toString('hex');
};
@ -204,18 +208,18 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts) {
var ret = [];
if (!opts.excludeChange) {
for (var i=0; i<this.indexes.getChangeIndex(); i++) {
for (var i = 0; i < this.indexes.getChangeIndex(); i++) {
ret.unshift({
address: this.getAddress(i,true),
address: this.getAddress(i, true),
isChange: true
});
}
}
if (!opts.excludeMain) {
for (var i=0; i<this.indexes.getReceiveIndex(); i++) {
for (var i = 0; i < this.indexes.getReceiveIndex(); i++) {
ret.unshift({
address: this.getAddress(i,false),
address: this.getAddress(i, false),
isChange: false
});
}
@ -225,59 +229,59 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts) {
};
// TODO this could be cached
PublicKeyRing.prototype._addScriptMap = function (map, index, isChange) {
var script = this.getRedeemScript(index,isChange);
PublicKeyRing.prototype._addScriptMap = function(map, index, isChange) {
var script = this.getRedeemScript(index, isChange);
map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex');
};
PublicKeyRing.prototype.getRedeemScriptMap = function () {
PublicKeyRing.prototype.getRedeemScriptMap = function() {
var ret = {};
for (var i=0; i<this.indexes.getChangeIndex(); i++) {
this._addScriptMap(ret,i,true);
for (var i = 0; i < this.indexes.getChangeIndex(); i++) {
this._addScriptMap(ret, i, true);
}
for (var i=0; i<this.indexes.getReceiveIndex(); i++) {
this._addScriptMap(ret,i,false);
for (var i = 0; i < this.indexes.getReceiveIndex(); i++) {
this._addScriptMap(ret, i, false);
}
return ret;
};
PublicKeyRing.prototype._checkInPKR = function(inPKR, ignoreId) {
if (!ignoreId && this.walletId !== inPKR.walletId) {
if (!ignoreId && this.walletId !== inPKR.walletId) {
throw new Error('inPKR walletId mismatch');
}
if (this.network.name !== inPKR.network.name) {
throw new Error('Network mismatch. Should be '+this.network.name +
' and found '+inPKR.network.name);
throw new Error('Network mismatch. Should be ' + this.network.name +
' and found ' + inPKR.network.name);
}
if (
this.requiredCopayers && inPKR.requiredCopayers &&
(this.requiredCopayers !== inPKR.requiredCopayers))
throw new Error('inPKR requiredCopayers mismatch '+this.requiredCopayers+'!='+inPKR.requiredCopayers);
throw new Error('inPKR requiredCopayers mismatch ' + this.requiredCopayers + '!=' + inPKR.requiredCopayers);
if (
this.totalCopayers && inPKR.totalCopayers &&
(this.totalCopayers !== inPKR.totalCopayers))
throw new Error('inPKR totalCopayers mismatch'+this.totalCopayers+'!='+inPKR.requiredCopayers);
throw new Error('inPKR totalCopayers mismatch' + this.totalCopayers + '!=' + inPKR.requiredCopayers);
};
PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
var self = this;
var hasChanged = false;
var l= self.copayersHK.length;
if (self.isComplete())
var l = self.copayersHK.length;
if (self.isComplete())
return;
inPKR.copayersHK.forEach( function(b) {
inPKR.copayersHK.forEach(function(b) {
var haveIt = false;
var epk = b.extendedPublicKeyString();
for(var j=0; j<l; j++) {
var epk = b.extendedPublicKeyString();
for (var j = 0; j < l; j++) {
if (self.copayersHK[j].extendedPublicKeyString() === epk) {
haveIt=true;
haveIt = true;
break;
}
}
@ -289,8 +293,8 @@ PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
self.copayersHK.push(new HK(epk));
self._updateBip(l2);
if (inPKR.nicknameFor[self.getCopayerId(l2)])
self._setNicknameForIndex(l2,inPKR.nicknameFor[self.getCopayerId(l2)]);
hasChanged=true;
self._setNicknameForIndex(l2, inPKR.nicknameFor[self.getCopayerId(l2)]);
hasChanged = true;
}
});
return hasChanged;

View file

@ -77,7 +77,7 @@ TxProposal.prototype.mergeMetadata = function(v1, author) {
Object.keys(v1.seenBy).forEach(function(k) {
if (!v0.seenBy[k]) {
if (k != author) throw new Error('Non authoritative seenBy change by '+author);
if (k != author) throw new Error('Non authoritative seenBy change by ' + author);
v0.seenBy[k] = v1.seenBy[k];
events.push({
type: 'seen',
@ -89,7 +89,7 @@ TxProposal.prototype.mergeMetadata = function(v1, author) {
Object.keys(v1.signedBy).forEach(function(k) {
if (!v0.signedBy[k]) {
if (k != author) throw new Error('Non authoritative signedBy change by '+author);
if (k != author) throw new Error('Non authoritative signedBy change by ' + author);
v0.signedBy[k] = v1.signedBy[k];
events.push({
type: 'signed',
@ -101,7 +101,7 @@ TxProposal.prototype.mergeMetadata = function(v1, author) {
Object.keys(v1.rejectedBy).forEach(function(k) {
if (!v0.rejectedBy[k]) {
if (k != author) throw new Error('Non authoritative rejectedBy change by '+author);
if (k != author) throw new Error('Non authoritative rejectedBy change by ' + author);
v0.rejectedBy[k] = v1.rejectedBy[k];
events.push({
type: 'rejected',

View file

@ -44,7 +44,7 @@ function Wallet(opts) {
this.token = opts.token;
this.tokenTime = opts.tokenTime;
}
this.verbose = opts.verbose;
this.publicKeyRing.walletId = this.id;
this.txProposals.walletId = this.id;
@ -142,12 +142,11 @@ Wallet.prototype._handleAddressBook = function(senderId, data, isInbound) {
this.log('RECV ADDRESSBOOK:', data);
var rcv = data.addressBook;
var hasChange;
for(var key in rcv) {
for (var key in rcv) {
if (!this.addressBook[key]) {
this.addressBook[key] = rcv[key];
hasChange = true;
}
else {
} else {
if (rcv[key].createdTs > this.addressBook[key].createdTs) {
this.addressBook[key] = rcv[key];
hasChange = true;
@ -395,7 +394,7 @@ Wallet.prototype.sendAllTxProposals = function(recipients) {
Wallet.prototype.sendTxProposal = function(ntxid, recipients) {
preconditions.checkArgument(ntxid);
preconditions.checkState(this.txProposals.txps[ntxid]);
this.log('### SENDING txProposal '+ntxid+' TO:', recipients || 'All', this.txProposals);
this.log('### SENDING txProposal ' + ntxid + ' TO:', recipients || 'All', this.txProposals);
this.network.send(recipients, {
type: 'txProposal',
txProposal: this.txProposals.txps[ntxid].toObj(),
@ -771,7 +770,7 @@ Wallet.prototype.updateIndexes = function(callback) {
Wallet.prototype.deriveAddresses = function(index, amout, isChange) {
var ret = new Array(amout);
for(var i = 0; i < amout; i++) {
for (var i = 0; i < amout; i++) {
ret[i] = this.publicKeyRing.getAddress(index + i, isChange).toString();
}
return ret;
@ -791,7 +790,7 @@ Wallet.prototype.indexDiscovery = function(start, change, gap, cb) {
// Optimize window to minimize the derivations.
var scanWindow = (lastActive == -1) ? gap : gap - (scanIndex - lastActive) + 1;
var addresses = self.deriveAddresses(scanIndex, scanWindow, change);
self.blockchain.checkActivity(addresses, function(err, actives){
self.blockchain.checkActivity(addresses, function(err, actives) {
if (err) throw err;
// Check for new activities in the newlly scanned addresses
@ -804,7 +803,9 @@ Wallet.prototype.indexDiscovery = function(start, change, gap, cb) {
next();
});
},
function _while() { return hasActivity; },
function _while() {
return hasActivity;
},
function _finnaly(err) {
if (err) return cb(err);
cb(null, lastActive);