delete Wallet WIP

This commit is contained in:
Matias Alejo Garcia 2014-06-16 15:51:19 -03:00
commit 46feadf57c
6 changed files with 134 additions and 83 deletions

View file

@ -1,9 +1,9 @@
'use strict';
var imports = require('soop').imports();
var Storage = imports.Storage;
var Network = imports.Network;
var Blockchain = imports.Blockchain;
var imports = require('soop').imports();
var Storage = imports.Storage;
var Network = imports.Network;
var Blockchain = imports.Blockchain;
var TxProposals = require('./TxProposals');
var PublicKeyRing = require('./PublicKeyRing');
@ -23,13 +23,13 @@ function WalletFactory(config, version) {
this.network = new Network(config.network);
this.blockchain = new Blockchain(config.blockchain);
this.networkName = config.networkName;
this.verbose = config.verbose;
this.networkName = config.networkName;
this.verbose = config.verbose;
this.walletDefaults = config.wallet;
this.version = version;
this.version = version;
}
WalletFactory.prototype.log = function(){
WalletFactory.prototype.log = function() {
if (!this.verbose) return;
if (console) {
console.log.apply(console, arguments);
@ -39,18 +39,18 @@ WalletFactory.prototype.log = function(){
WalletFactory.prototype._checkRead = function(walletId) {
var s = this.storage;
var ret =
s.get(walletId, 'publicKeyRing') &&
s.get(walletId, 'txProposals') &&
s.get(walletId, 'opts') &&
s.get(walletId, 'privateKey');
var ret =
s.get(walletId, 'publicKeyRing') &&
s.get(walletId, 'txProposals') &&
s.get(walletId, 'opts') &&
s.get(walletId, 'privateKey');
return !!ret;
};
WalletFactory.prototype.fromObj = function(obj) {
// not stored options
obj.opts.reconnectDelay = this.walletDefaults.reconnectDelay;
obj.opts.reconnectDelay = this.walletDefaults.reconnectDelay;
var w = Wallet.fromObj(obj, this.storage, this.network, this.blockchain);
if (!w) return false;
@ -69,33 +69,35 @@ WalletFactory.prototype.fromEncryptedObj = function(base64, password) {
};
WalletFactory.prototype.read = function(walletId) {
if (! this._checkRead(walletId))
if (!this._checkRead(walletId))
return false;
var obj = {};
var s = this.storage;
obj.id = walletId;
obj.opts = s.get(walletId, 'opts');
obj.publicKeyRing = s.get(walletId, 'publicKeyRing');
obj.txProposals = s.get(walletId, 'txProposals');
obj.privateKey = s.get(walletId, 'privateKey');
obj.opts = s.get(walletId, 'opts');
obj.publicKeyRing = s.get(walletId, 'publicKeyRing');
obj.txProposals = s.get(walletId, 'txProposals');
obj.privateKey = s.get(walletId, 'privateKey');
var w = this.fromObj(obj);
return w;
};
WalletFactory.prototype.create = function(opts) {
opts = opts || {};
this.log('### CREATING NEW WALLET.' +
(opts.id ? ' USING ID: ' + opts.id : ' NEW ID') +
(opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey')
);
opts = opts || {};
this.log('### CREATING NEW WALLET.' +
(opts.id ? ' USING ID: ' + opts.id : ' NEW ID') +
(opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey')
);
opts.privateKey = opts.privateKey || new PrivateKey({ networkName: this.networkName });
opts.privateKey = opts.privateKey || new PrivateKey({
networkName: this.networkName
});
var requiredCopayers = opts.requiredCopayers || this.walletDefaults.requiredCopayers;
var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers;
var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers;
opts.publicKeyRing = opts.publicKeyRing || new PublicKeyRing({
networkName: this.networkName,
@ -120,10 +122,10 @@ WalletFactory.prototype.create = function(opts) {
opts.verbose = this.verbose;
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
opts.reconnectDelay = opts.reconnectDelay || this.walletDefaults.reconnectDelay;
opts.reconnectDelay = opts.reconnectDelay || this.walletDefaults.reconnectDelay;
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
opts.version = opts.version || this.version;
opts.totalCopayers = totalCopayers;
opts.version = opts.version || this.version;
var w = new Wallet(opts);
w.store();
return w;
@ -133,27 +135,22 @@ WalletFactory.prototype.create = function(opts) {
WalletFactory.prototype._checkVersion = function(inVersion) {
var thisV = this.version.split('.');
var thisV0 = parseInt(thisV[0]);
var inV = inVersion.split('.');
var inV0 = parseInt(inV[0]);
var inV = inVersion.split('.');
var inV0 = parseInt(inV[0]);
//We only check for major version differences
if( thisV0 < inV0 ) {
if (thisV0 < inV0) {
throw new Error('Major difference in software versions' +
'. Received:' + inVersion +
'. Current version:' + this.version +
'. Aborting.');
'. Received:' + inVersion +
'. Current version:' + this.version +
'. Aborting.');
}
};
WalletFactory.prototype._checkNetwork = function(inNetworkName) {
if( this.networkName !== inNetworkName ) {
throw new Error('This Wallet is configured for '
+ inNetworkName
+ ' while currently Copay is configured for: '
+ this.networkName
+ '. Check your settings.'
);
if (this.networkName !== inNetworkName) {
throw new Error('This Wallet is configured for ' + inNetworkName + ' while currently Copay is configured for: ' + this.networkName + '. Check your settings.');
}
};
@ -175,14 +172,16 @@ WalletFactory.prototype.open = function(walletId, opts) {
WalletFactory.prototype.getWallets = function() {
var ret = this.storage.getWallets();
ret.forEach(function(i) {
i.show = i.name ? ( (i.name + ' <'+i.id+'>') ) : i.id;
i.show = i.name ? ((i.name + ' <' + i.id + '>')) : i.id;
});
return ret;
};
WalletFactory.prototype.remove = function(walletId) {
// TODO remove wallet contents
this.log('TODO: remove wallet contents');
WalletFactory.prototype.remove = function(walletId, cb) {
var s = this.storage;
this.log('## DELETING WALLET ID:'+ walletId); //TODO
s.get(walletId, 'opts');
return cb();
};
WalletFactory.prototype.decodeSecret = function(secret) {
@ -198,9 +197,11 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
var s = self.decodeSecret(secret);
if (!s) return cb('badSecret');
//Create our PrivateK
var privateKey = new PrivateKey({ networkName: this.networkName });
var privateKey = new PrivateKey({
networkName: this.networkName
});
this.log('\t### PrivateKey Initialized');
var opts = {
copayerId: privateKey.getId(),
@ -219,13 +220,13 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
return cb(connectedOnce ? 'walletFull' : 'joinError');
});
self.network.on('data', function(sender, data) {
if (data.type ==='walletId') {
if (data.networkName !== self.networkName ){
if (data.type === 'walletId') {
if (data.networkName !== self.networkName) {
return cb('badNetwork');
}
data.opts.privateKey = privateKey;
data.opts.nickname = nickname;
data.opts.nickname = nickname;
data.opts.passphrase = passphrase;
data.opts.id = data.walletId;
var w = self.create(data.opts);

View file

@ -147,6 +147,27 @@ Storage.prototype.getWallets = function() {
return wallets;
};
Storage.prototype.deleteWallet = function(walletId) {
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 walletId = split[0];
if (walletId === 'nameFor') continue;
if (typeof uniq[walletId] === 'undefined') {
walletIds.push(walletId);
uniq[walletId] = 1;
}
}
}
};
//obj contains keys to be set
Storage.prototype.setFromObj = function(walletId, obj) {
for (var k in obj) {