add wallets nicknames, handle error messages

This commit is contained in:
Matias Alejo Garcia 2014-04-24 16:35:52 -03:00
commit 5d30a6abea
8 changed files with 98 additions and 48 deletions

View file

@ -25,6 +25,7 @@ function Wallet(opts) {
this.log('creating '+opts.requiredCopayers+' of '+opts.totalCopayers+' wallet');
this.id = opts.id || Wallet.getRandomId();
this.name = opts.name;
this.verbose = opts.verbose;
this.publicKeyRing.walletId = this.id;
this.txProposals.walletId = this.id;
@ -137,6 +138,7 @@ Wallet.prototype._optsToObj = function () {
spendUnconfirmed: this.spendUnconfirmed,
requiredCopayers: this.requiredCopayers,
totalCopayers: this.totalCopayers,
name: this.name,
};
return obj;
@ -172,6 +174,7 @@ Wallet.prototype.netStart = function() {
copayerId: myId,
signingKeyHex: self.privateKey.getSigningKey(),
};
net.start(startOpts, function() {
self.emit('created');
for (var i=0; i<self.publicKeyRing.registeredCopayers(); i++) {
@ -518,6 +521,8 @@ Wallet.prototype.connectTo = function(peerId) {
};
Wallet.prototype.disconnect = function() {
console.log('[Wallet.js.524] DISC'); //TODO
this.network.disconnect();
};

View file

@ -73,7 +73,7 @@ WalletFactory.prototype.read = function(walletId) {
w.privateKey.getExtendedPublicKeyString()
);
} catch (e) {
this.log('NOT NECCESARY AN ERROR:', e); //TODO
// No really an error, just to be sure.
}
this.log('### WALLET OPENED:', w.id);
return w;
@ -129,9 +129,13 @@ WalletFactory.prototype.open = function(walletId, opts) {
return w;
};
WalletFactory.prototype.getWalletIds = function() {
return this.storage.getWalletIds();
}
WalletFactory.prototype.getWallets = function() {
var ret = this.storage.getWallets();
ret.forEach(function(i) {
i.show = i.name ? ( (i.name + ' <'+i.id+'>') ) : i.id;
});
return ret;
};
WalletFactory.prototype.remove = function(walletId) {
// TODO remove wallet contents

View file

@ -192,6 +192,7 @@ Network.prototype._onData = function(data, isInbound, peerId) {
Network.prototype._checkAnyPeer = function() {
if (!this.connectedPeers.length) {
console.log('EMIT openError: no more peers, not even you!');
this._cleanUp();
this.emit('openError');
}
}
@ -251,8 +252,9 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
});
p.on('error', function(err) {
console.log('### PEER ERROR:', err);
//self.disconnect(null, true); // force disconnect
if (!err.message.match(/Could\snot\sconnect\sto peer/)) {
console.log('### PEER ERROR:', err);
}
self._checkAnyPeer();
});
@ -311,11 +313,12 @@ Network.prototype.start = function(opts, openCallback) {
if (this.started) return openCallback();
opts.connectedPeers = opts.connectedPeers || [];
if (!this.copayerId)
if (!this.copayerId)
this.setCopayerId(opts.copayerId);
if (!this.signingKey)
if (!this.signingKey)
this.setSigningKey(opts.signingKeyHex);
console.log('CREATING PEER INSTANCE:', this.peerId); //TODO
this.peer = new Peer(this.peerId, this.opts);
this._setupPeerHandlers(openCallback);
for (var i = 0; i<opts.connectedPeers.length; i++) {
@ -399,27 +402,28 @@ Network.prototype.connectTo = function(copayerId) {
self._setupConnectionHandlers(dataConn, false);
};
Network.prototype._cleanUp = function() {
self.connectedPeers = [];
self.started = false;
self.peerId = null;
self.copayerId = null;
self.signingKey = null;
if (self.peer) {
console.log('## DESTROYING PEER INSTANCE'); //TODO
self.peer.disconnect();
self.peer.destroy();
self.peer = null;
}
self.closing = 0;
};
Network.prototype.disconnect = function(cb, forced) {
var self = this;
self.closing = 1;
var cleanUp = function() {
self.connectedPeers = [];
self.started = false;
self.peerId = null;
if (self.peer) {
self.peer.disconnect();
self.peer.destroy();
self.peer = null;
}
self.closing = 0;
if (typeof cb === 'function') cb();
};
if (!forced) {
this.send(null, { type: 'disconnect' }, cleanUp);
} else {
cleanUp();
}
this.send(null, { type: 'disconnect' });
this._cleanUp();
if (typeof cb === 'function') cb();
};
module.exports = require('soop')(Network);

View file

@ -25,9 +25,9 @@ Storage.prototype._getWalletKeys = function(walletId) {
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
var split = key.split('::');
if (split.length == 2) {
if (split.length == 3) {
if (walletId = split[0])
keys.push(split[1]);
keys.push(split[2]);
}
}
@ -69,30 +69,54 @@ Storage.prototype.remove = function(walletId, k) {
this.removeGlobal(this._key(walletId,k));
};
Storage.prototype.setName = function(walletId, name) {
this.setGlobal('nameFor::'+walletId, name);
};
Storage.prototype.getName = function(walletId) {
return this.getGlobal('nameFor::'+walletId);
};
Storage.prototype.getWalletIds = function() {
var walletIds = [];
var uniq = {};
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
var split = key.split('::');
if (split.length == 2) {
var key = localStorage.key(i);
var split = key.split('::');
if (split.length == 2) {
var walletId = split[0];
if (walletId === 'nameFor') continue;
if (typeof uniq[walletId] === 'undefined' ) {
walletIds.push(walletId);
uniq[walletId] = 1;
}
}
}
}
}
return walletIds;
};
Storage.prototype.getWallets = function() {
var wallets = [];
var uniq = {};
var ids = this.getWalletIds();
for (var i in ids){
wallets.push({
id:ids[i],
name: this.getName(ids[i]),
});
}
return wallets;
};
//obj contains keys to be set
Storage.prototype.setFromObj = function(walletId, obj) {
for (var k in obj) {
this.set(walletId, k, obj[k]);
}
this.setName(walletId, obj.opts.name);
};
// remove all values