Wallet/js/services/identityService.js

356 lines
10 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.services')
.factory('identityService', function($rootScope, $location, $timeout, $filter, pluginManager, notification, pendingTxsService, balanceService, applicationService, go) {
2014-12-03 16:01:22 -03:00
// TODO:
// * remove iden from rootScope
// * remove wallet from rootScope
// * create walletService
2014-11-30 00:31:17 -03:00
var root = {};
2014-10-30 18:08:50 -03:00
root.check = function(scope) {
copay.Identity.checkIfExistsAny({
2014-12-05 13:24:46 -03:00
pluginManager: pluginManager.getInstance(config),
2014-10-30 18:08:50 -03:00
}, function(anyProfile) {
copay.Wallet.checkIfExistsAny({
2014-12-05 13:24:46 -03:00
pluginManager: pluginManager.getInstance(config),
2014-10-30 18:08:50 -03:00
}, function(anyWallet) {
2014-10-31 11:24:16 -03:00
scope.loading = false;
2014-10-30 18:08:50 -03:00
scope.anyProfile = anyProfile ? true : false;
scope.anyWallet = anyWallet ? true : false;
if (!scope.anyProfile) {
$location.path('/createProfile');
}
});
});
};
2014-11-30 03:58:38 -03:00
root.create = function(email, password, cb) {
2014-10-30 18:08:50 -03:00
copay.Identity.create({
2014-11-30 03:58:38 -03:00
email: email,
password: password,
2014-12-05 13:24:46 -03:00
pluginManager: pluginManager.getInstance(config),
2014-10-27 11:10:32 -03:00
network: config.network,
networkName: config.networkName,
walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig,
2014-10-30 18:08:50 -03:00
failIfExists: true,
}, function(err, iden) {
2014-11-30 00:31:17 -03:00
if (err) return cb(err);
preconditions.checkState(iden);
2014-11-30 03:58:38 -03:00
root.bind(iden);
2014-11-30 00:31:17 -03:00
2014-12-03 16:01:22 -03:00
return cb(null);
2014-10-27 11:10:32 -03:00
});
2014-12-03 16:01:22 -03:00
};
root.createDefaultWallet = function(cb) {
var iden = $rootScope.iden;
2014-10-30 18:08:50 -03:00
2014-12-03 16:01:22 -03:00
var walletOptions = {
nickname: iden.fullName,
networkName: config.networkName,
requiredCopayers: 1,
totalCopayers: 1,
password: iden.password,
name: 'My wallet',
};
iden.createWallet(walletOptions, function(err, wallet) {
return cb(err);
});
2014-10-27 11:10:32 -03:00
};
2014-12-18 09:37:07 -03:00
root.resendVerificationEmail = function (cb) {
2014-12-18 15:50:42 -03:00
var iden = $rootScope.iden;
iden.resendVerificationEmail(cb);
2014-12-18 09:37:07 -03:00
};
2014-12-01 17:21:39 -03:00
root.setServerStatus = function(headers) {
2014-11-26 12:08:26 -03:00
if (!headers)
2014-12-01 17:21:39 -03:00
return;
2014-11-26 12:08:26 -03:00
if (headers['X-Email-Needs-Validation'])
$rootScope.needsEmailConfirmation = true;
2014-12-01 17:21:39 -03:00
else
2014-11-26 12:08:26 -03:00
$rootScope.needsEmailConfirmation = null;
2014-12-01 17:21:39 -03:00
2014-11-26 12:08:26 -03:00
if (headers['X-Quota-Per-Item'])
$rootScope.quotaPerItem = parseInt(headers['X-Quota-Per-Item']);
2014-12-01 17:21:39 -03:00
2014-11-26 12:08:26 -03:00
if (headers['X-Quota-Items-Limit'])
$rootScope.quotaItems = parseInt(headers['X-Quota-Items-Limit']);
2014-12-01 17:21:39 -03:00
};
2014-10-24 11:58:22 -03:00
2014-11-30 00:31:17 -03:00
root.open = function(email, password, cb) {
var opts = {
email: email,
password: password,
2014-12-05 13:24:46 -03:00
pluginManager: pluginManager.getInstance(config),
2014-10-27 11:10:32 -03:00
network: config.network,
networkName: config.networkName,
walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig,
2014-11-30 00:31:17 -03:00
};
2014-11-28 13:10:15 -03:00
2014-12-01 16:19:34 -03:00
copay.Identity.open(opts, function(err, iden, headers) {
2014-11-30 00:31:17 -03:00
if (err) return cb(err);
2014-12-01 17:21:39 -03:00
root.setServerStatus(headers);
2014-11-30 00:31:17 -03:00
root.bind(iden);
2014-12-01 15:33:16 -03:00
return cb(null, iden);
2014-11-29 18:35:48 -03:00
});
};
2014-11-26 12:08:26 -03:00
root.deleteProfile = function(cb) {
2014-12-01 14:44:36 -03:00
$rootScope.iden.remove(null, cb);
};
2014-11-30 23:08:16 -03:00
root.deleteWallet = function(w, cb) {
2014-12-09 19:29:56 -03:00
$rootScope.iden.deleteWallet(w.getId(), cb);
2014-10-30 18:08:50 -03:00
};
2014-11-29 18:35:48 -03:00
root.isFocused = function(wid) {
return $rootScope.wallet && wid === $rootScope.wallet.getId();
};
root.setupGlobalVariables = function(iden) {
$rootScope.reconnecting = false;
$rootScope.iden = iden;
};
2014-12-02 16:53:24 -03:00
root.noFocusedWallet = function() {
$rootScope.wallet = null;
$timeout(function() {
$rootScope.$digest();
})
};
2014-11-30 01:02:18 -03:00
root.setFocusedWallet = function(w, dontUpdateIt) {
2014-11-29 18:35:48 -03:00
if (!_.isObject(w))
w = $rootScope.iden.getWalletById(w);
preconditions.checkState(w && _.isObject(w));
2014-11-30 00:31:17 -03:00
copay.logger.debug('Set focus:', w.getName());
2014-11-29 18:35:48 -03:00
$rootScope.wallet = w;
2014-11-30 00:31:17 -03:00
2014-11-30 01:02:18 -03:00
if (!dontUpdateIt)
$rootScope.iden.updateFocusedTimestamp(w.getId());
2014-11-29 18:35:48 -03:00
2014-11-30 01:02:18 -03:00
pendingTxsService.update();
2014-11-30 00:31:17 -03:00
$timeout(function() {
2014-11-29 18:35:48 -03:00
$rootScope.$digest();
2014-11-30 00:31:17 -03:00
})
2014-11-29 18:35:48 -03:00
};
2014-12-10 18:18:28 -03:00
root.notifyTxProposalEvent = function(w, e) {
if (e.cId == w.getMyCopayerId())
2014-12-10 18:18:28 -03:00
return;
var user = w.publicKeyRing.nicknameForCopayer(e.cId);
var name = w.getName();
switch (e.type) {
case copay.Wallet.TX_NEW:
notification.info('[' + name + '] New Transaction',
$filter('translate')('You received a transaction proposal from') + ' ' + user);
break;
case copay.Wallet.TX_SIGNED:
notification.success('[' + name + '] Transaction Signed',
$filter('translate')('A transaction was signed by') + ' ' + user);
break;
case copay.Wallet.TX_BROADCASTED:
notification.success('[' + name + '] Transaction Approved',
$filter('translate')('A transaction was broadcasted by') + ' ' + user);
break;
case copay.Wallet.TX_REJECTED:
notification.warning('[' + name + '] Transaction Rejected',
$filter('translate')('A transaction was rejected by') + ' ' + user);
break;
}
};
2014-11-30 00:31:17 -03:00
root.installWalletHandlers = function(w) {
2014-11-29 18:35:48 -03:00
var wid = w.getId();
w.on('connectionError', function() {
if (root.isFocused(wid)) {
var message = "Could not connect to the Insight server. Check your settings and network configuration";
notification.error('Networking Error', message);
}
});
w.on('corrupt', function(peerId) {
2014-12-10 18:18:28 -03:00
copay.logger.warn('Received corrupt message from ' + peerId);
2014-11-29 18:35:48 -03:00
});
2014-12-02 17:32:32 -03:00
w.on('publicKeyRingUpdated', function() {
$rootScope.$digest();
});
2014-11-29 18:35:48 -03:00
w.on('ready', function() {
2014-11-30 00:31:17 -03:00
var isFocused = root.isFocused(wid);
2014-12-12 11:21:48 -03:00
copay.logger.debug('Wallet:' + w.getName() +
' is ready. Focused:', isFocused);
2014-11-30 00:31:17 -03:00
balanceService.update(w, function() {
$rootScope.$digest();
}, isFocused);
2014-11-29 18:35:48 -03:00
});
w.on('tx', function(address, isChange) {
if (!isChange) {
notification.funds('Funds received on ' + w.getName(), address);
}
balanceService.update(w, function() {
$rootScope.$digest();
}, root.isFocused(wid));
});
w.on('balanceUpdated', function() {
balanceService.update(w, function() {
$rootScope.$digest();
}, root.isFocused(wid));
});
w.on('insightReconnected', function() {
$rootScope.reconnecting = false;
balanceService.update(w, function() {
$rootScope.$digest();
}, root.isFocused(wid));
});
w.on('insightError', function() {
if (root.isFocused(wid)) {
$rootScope.reconnecting = true;
$rootScope.$digest();
}
});
w.on('newAddresses', function() {
2014-11-30 18:35:30 -03:00
// Nothing yet
2014-11-29 18:35:48 -03:00
});
2014-12-10 18:18:28 -03:00
// Disabled for now, does not seens to have much value for the user
// w.on('paymentACK', function(memo) {
// notification.success('Payment Acknowledged', memo);
// });
2014-12-10 18:18:28 -03:00
w.on('txProposalEvent', function(ev) {
2014-11-29 18:35:48 -03:00
if (root.isFocused(wid)) {
pendingTxsService.update();
2014-11-29 18:35:48 -03:00
}
2014-12-10 18:18:28 -03:00
// TODO aqui lo unico que cambia son los locked
// se puede optimizar
balanceService.update(w, function() {
$rootScope.$digest();
}, root.isFocused(wid));
2014-12-10 18:18:28 -03:00
root.notifyTxProposalEvent(w, ev);
$timeout(function() {
2014-12-10 18:18:28 -03:00
$rootScope.$digest();
});
2014-11-29 18:35:48 -03:00
});
2014-12-10 18:18:28 -03:00
2014-11-29 18:35:48 -03:00
w.on('addressBookUpdated', function(dontDigest) {
if (root.isFocused(wid)) {
if (!dontDigest) {
$rootScope.$digest();
}
}
});
w.on('connect', function(peerID) {
$rootScope.$digest();
});
// TODO?
// w.on('close', );
// w.on('locked',);
};
2014-11-30 00:31:17 -03:00
root.bind = function(iden) {
preconditions.checkArgument(_.isObject(iden));
2014-11-30 00:44:25 -03:00
copay.logger.debug('Binding profile...');
2014-11-30 00:31:17 -03:00
var self = this;
2014-11-29 18:35:48 -03:00
root.setupGlobalVariables(iden);
2014-11-30 00:31:17 -03:00
iden.on('newWallet', function(wid) {
var w = iden.getWalletById(wid);
copay.logger.debug('newWallet:',
w.getName(), wid, iden.getLastFocusedWalletId());
2014-11-30 00:31:17 -03:00
root.installWalletHandlers(w);
if (wid == iden.getLastFocusedWalletId()) {
2014-11-30 01:02:18 -03:00
copay.logger.debug('GOT Focused wallet:', w.getName());
root.setFocusedWallet(w, true);
go.walletHome();
2014-11-30 00:31:17 -03:00
}
2014-11-30 01:02:18 -03:00
// At the end (after all handlers are in place)...start the wallet.
w.netStart();
2014-11-30 00:31:17 -03:00
});
iden.on('noWallets', function() {
2014-12-10 18:18:28 -03:00
notification.warning('No Wallets', 'Your profile has no wallets. Create one here');
$rootScope.starting = false;
2014-11-29 18:35:48 -03:00
$location.path('/create');
$timeout(function() {
$rootScope.$digest();
}, 1);
2014-11-30 00:31:17 -03:00
});
2014-11-29 18:35:48 -03:00
2014-12-02 16:53:24 -03:00
iden.on('walletDeleted', function(wid) {
// do nothing. this is handled 'on sync' on controller.
2014-11-30 00:31:17 -03:00
});
2014-11-29 18:35:48 -03:00
iden.on('walletStorageError', function(wid, message) {
notification.error('Error storing wallet', message);
});
2014-11-30 00:31:17 -03:00
iden.on('closed', function() {
delete $rootScope['wallet'];
delete $rootScope['iden'];
applicationService.restart();
});
};
2014-11-29 18:35:48 -03:00
2014-11-30 00:31:17 -03:00
root.signout = function() {
2014-12-06 21:07:26 -03:00
$rootScope.signingOut = true;
2014-11-30 00:31:17 -03:00
if ($rootScope.iden) {
2014-12-04 12:25:21 -03:00
$rootScope.iden.store({
noWallets: true
}, function() {
2014-12-06 21:07:26 -03:00
$rootScope.signingOut = false;
$rootScope.iden.close(); // Will trigger 'closed'
2014-12-04 12:25:21 -03:00
});
2014-11-29 18:35:48 -03:00
}
};
root.createWallet = function(opts, cb) {
2014-11-30 00:31:17 -03:00
$rootScope.iden.createWallet(opts, cb);
2014-11-29 18:35:48 -03:00
};
root.importWallet = function(encryptedObj, pass, opts, cb) {
2014-12-02 17:38:31 -03:00
copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj, pass, opts, cb);
2014-11-29 18:35:48 -03:00
};
root.joinWallet = function(opts, cb) {
$rootScope.iden.joinWallet(opts, function(err, w) {
return cb(err);
});
};
2014-11-30 01:02:18 -03:00
root.importProfile = function(str, password, cb) {
2014-11-30 00:31:17 -03:00
copay.Identity.importFromEncryptedFullJson(str, password, {
2014-12-05 13:24:46 -03:00
pluginManager: pluginManager.getInstance(config),
2014-11-30 00:31:17 -03:00
network: config.network,
networkName: config.networkName,
walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig,
2014-11-30 04:42:39 -03:00
}, function(err, iden, walletObjs) {
2014-11-30 00:31:17 -03:00
if (err) return cb(err);
2014-11-30 03:58:38 -03:00
root.bind(iden);
2014-11-30 04:42:39 -03:00
iden.importMultipleWalletsFromObj(walletObjs);
2014-11-30 03:58:38 -03:00
return cb();
2014-11-30 00:31:17 -03:00
});
};
2014-11-29 18:35:48 -03:00
2014-10-27 11:10:32 -03:00
return root;
});