2014-10-24 07:37:03 -07:00
|
|
|
'use strict';
|
|
|
|
angular.module('copayApp.services')
|
2014-11-29 19:31:17 -08:00
|
|
|
.factory('identityService', function($rootScope, $location, $timeout, $filter, pluginManager, notification, pendingTxsService, balanceService, applicationService) {
|
|
|
|
notification.enableHtml5Mode(); // for chrome: if support, enable it
|
2014-10-24 07:37:03 -07:00
|
|
|
|
2014-11-29 19:31:17 -08:00
|
|
|
var root = {};
|
2014-10-30 14:08:50 -07:00
|
|
|
root.check = function(scope) {
|
|
|
|
copay.Identity.checkIfExistsAny({
|
|
|
|
pluginManager: pluginManager,
|
|
|
|
}, function(anyProfile) {
|
|
|
|
copay.Wallet.checkIfExistsAny({
|
|
|
|
pluginManager: pluginManager,
|
|
|
|
}, function(anyWallet) {
|
2014-10-31 07:24:16 -07:00
|
|
|
scope.loading = false;
|
2014-10-30 14:08:50 -07:00
|
|
|
scope.anyProfile = anyProfile ? true : false;
|
|
|
|
scope.anyWallet = anyWallet ? true : false;
|
|
|
|
|
|
|
|
if (!scope.anyProfile) {
|
|
|
|
$location.path('/createProfile');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-11-29 13:35:48 -08:00
|
|
|
root.goWalletHome = function() {
|
|
|
|
var w = $rootScope.wallet;
|
|
|
|
if (w) {
|
2014-11-29 19:31:17 -08:00
|
|
|
if (!w.isComplete()) {
|
2014-11-29 13:35:48 -08:00
|
|
|
$location.path('/copayers');
|
|
|
|
} else {
|
|
|
|
if ($rootScope.pendingPayment) {
|
|
|
|
$location.path('paymentIntent');
|
|
|
|
} else {
|
|
|
|
$location.path('homeWallet');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-29 22:58:38 -08:00
|
|
|
root.create = function(email, password, cb) {
|
2014-10-30 14:08:50 -07:00
|
|
|
copay.Identity.create({
|
2014-11-29 22:58:38 -08:00
|
|
|
email: email,
|
|
|
|
password: password,
|
2014-10-27 07:10:32 -07:00
|
|
|
pluginManager: pluginManager,
|
|
|
|
network: config.network,
|
|
|
|
networkName: config.networkName,
|
|
|
|
walletDefaults: config.wallet,
|
|
|
|
passphraseConfig: config.passphraseConfig,
|
2014-10-30 14:08:50 -07:00
|
|
|
failIfExists: true,
|
|
|
|
}, function(err, iden) {
|
2014-11-29 19:31:17 -08:00
|
|
|
if (err) return cb(err);
|
|
|
|
preconditions.checkState(iden);
|
2014-11-29 22:58:38 -08:00
|
|
|
root.bind(iden);
|
2014-11-29 19:31:17 -08:00
|
|
|
|
2014-10-30 14:08:50 -07: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) {
|
2014-11-29 22:58:38 -08:00
|
|
|
return cb(err);
|
2014-10-30 14:08:50 -07:00
|
|
|
});
|
2014-10-27 07:10:32 -07:00
|
|
|
});
|
2014-10-30 14:08:50 -07:00
|
|
|
|
2014-10-27 07:10:32 -07:00
|
|
|
};
|
|
|
|
|
2014-10-24 07:58:22 -07:00
|
|
|
|
2014-11-29 19:31:17 -08:00
|
|
|
root.open = function(email, password, cb) {
|
|
|
|
var opts = {
|
|
|
|
email: email,
|
|
|
|
password: password,
|
2014-10-27 07:10:32 -07:00
|
|
|
pluginManager: pluginManager,
|
|
|
|
network: config.network,
|
|
|
|
networkName: config.networkName,
|
|
|
|
walletDefaults: config.wallet,
|
|
|
|
passphraseConfig: config.passphraseConfig,
|
2014-11-29 19:31:17 -08:00
|
|
|
};
|
2014-11-28 08:10:15 -08:00
|
|
|
|
2014-11-29 19:31:17 -08:00
|
|
|
copay.Identity.open(opts, function(err, iden) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
root.bind(iden);
|
2014-11-29 19:44:25 -08:00
|
|
|
iden.openWallets();
|
|
|
|
return cb();
|
2014-11-29 13:35:48 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
root.deleteWallet = function($scope, iden, w) {
|
2014-11-29 19:31:17 -08:00
|
|
|
$rootScope.iden.deleteWallet(w.id);
|
2014-10-30 14:08:50 -07:00
|
|
|
};
|
2014-10-24 07:37:03 -07:00
|
|
|
|
2014-11-29 13:35:48 -08:00
|
|
|
root.isFocused = function(wid) {
|
|
|
|
return $rootScope.wallet && wid === $rootScope.wallet.getId();
|
|
|
|
};
|
|
|
|
|
|
|
|
root.setupGlobalVariables = function(iden) {
|
|
|
|
$rootScope.pendingTxCount = 0;
|
|
|
|
$rootScope.reconnecting = false;
|
|
|
|
$rootScope.iden = iden;
|
|
|
|
};
|
|
|
|
|
|
|
|
root.setPaymentWallet = function(w) {
|
|
|
|
root.setFocusedWallet(w);
|
|
|
|
$location.path('/send');
|
|
|
|
};
|
|
|
|
|
2014-11-29 20:02:18 -08:00
|
|
|
root.setFocusedWallet = function(w, dontUpdateIt) {
|
2014-11-29 13:35:48 -08:00
|
|
|
if (!_.isObject(w))
|
|
|
|
w = $rootScope.iden.getWalletById(w);
|
|
|
|
preconditions.checkState(w && _.isObject(w));
|
2014-11-29 19:31:17 -08:00
|
|
|
|
|
|
|
copay.logger.debug('Set focus:', w.getName());
|
2014-11-29 13:35:48 -08:00
|
|
|
$rootScope.wallet = w;
|
2014-11-29 19:31:17 -08:00
|
|
|
|
2014-11-29 20:02:18 -08:00
|
|
|
if (!dontUpdateIt)
|
|
|
|
$rootScope.iden.updateFocusedTimestamp(w.getId());
|
2014-11-29 13:35:48 -08:00
|
|
|
|
2014-11-29 20:02:18 -08:00
|
|
|
pendingTxsService.update();
|
2014-11-29 19:31:17 -08:00
|
|
|
$timeout(function() {
|
2014-11-29 13:35:48 -08:00
|
|
|
$rootScope.$digest();
|
2014-11-29 19:31:17 -08:00
|
|
|
})
|
2014-11-29 13:35:48 -08:00
|
|
|
};
|
|
|
|
|
2014-11-29 19:31:17 -08:00
|
|
|
root.installWalletHandlers = function(w) {
|
2014-11-29 13:35:48 -08:00
|
|
|
var wid = w.getId();
|
|
|
|
w.on('connectionError', function() {
|
|
|
|
console.log('err', w.getName()); //TODO
|
|
|
|
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) {
|
|
|
|
console.log('corr', w.getName()); //TODO
|
|
|
|
if (root.isFocused(wid)) {
|
|
|
|
notification.error('Error', $filter('translate')('Received corrupt message from ') + peerId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.on('ready', function() {
|
2014-11-29 19:31:17 -08:00
|
|
|
var isFocused = root.isFocused(wid);
|
2014-11-29 20:02:18 -08:00
|
|
|
copay.logger.debug('Wallet:' + w.getName() + ' is ready. Focused:', isFocused);
|
2014-11-29 19:31:17 -08:00
|
|
|
|
|
|
|
balanceService.update(w, function() {
|
|
|
|
$rootScope.$digest();
|
|
|
|
}, isFocused);
|
2014-11-29 13:35:48 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
w.on('tx', function(address, isChange) {
|
|
|
|
console.log('tx', w.getName()); //TODO
|
|
|
|
if (!isChange) {
|
|
|
|
notification.funds('Funds received on ' + w.getName(), address);
|
|
|
|
}
|
|
|
|
balanceService.update(w, function() {
|
|
|
|
$rootScope.$digest();
|
|
|
|
}, root.isFocused(wid));
|
|
|
|
});
|
|
|
|
|
|
|
|
w.on('balanceUpdated', function() {
|
|
|
|
console.log('b', w.getName()); //TODO
|
|
|
|
balanceService.update(w, function() {
|
|
|
|
$rootScope.$digest();
|
|
|
|
}, root.isFocused(wid));
|
|
|
|
});
|
|
|
|
|
|
|
|
w.on('insightReconnected', function() {
|
|
|
|
console.log('i', w.getName()); //TODO
|
|
|
|
$rootScope.reconnecting = false;
|
|
|
|
balanceService.update(w, function() {
|
|
|
|
$rootScope.$digest();
|
|
|
|
}, root.isFocused(wid));
|
|
|
|
});
|
|
|
|
|
|
|
|
w.on('insightError', function() {
|
|
|
|
console.log('i', w.getName()); //TODO
|
|
|
|
if (root.isFocused(wid)) {
|
|
|
|
$rootScope.reconnecting = true;
|
|
|
|
$rootScope.$digest();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.on('newAddresses', function() {
|
|
|
|
console.log('newAddress', w.getName()); //TODO
|
|
|
|
});
|
|
|
|
|
|
|
|
w.on('txProposalsUpdated', function() {
|
|
|
|
if (root.isFocused(wid)) {
|
|
|
|
pendingTxsService.update();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
w.on('paymentACK', function(memo) {
|
|
|
|
notification.success('Payment Acknowledged', memo);
|
|
|
|
});
|
|
|
|
|
|
|
|
w.on('txProposalEvent', function(e) {
|
|
|
|
if (root.isFocused(wid)) {
|
|
|
|
pendingTxsService.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add wallet name notification
|
|
|
|
var user = w.publicKeyRing.nicknameForCopayer(e.cId);
|
|
|
|
var name = w.getName();
|
|
|
|
switch (e.type) {
|
|
|
|
case 'new':
|
|
|
|
notification.info('[' + name + '] New Transaction',
|
|
|
|
$filter('translate')('You received a transaction proposal from') + ' ' + user);
|
|
|
|
break;
|
|
|
|
case 'signed':
|
|
|
|
notification.info('[' + name + '] Transaction Signed',
|
|
|
|
$filter('translate')('A transaction was signed by') + ' ' + user);
|
|
|
|
break;
|
|
|
|
case 'signedAndBroadcasted':
|
|
|
|
notification.info('[' + name + '] Transaction Approved',
|
|
|
|
$filter('translate')('A transaction was signed and broadcasted by') + ' ' + user);
|
|
|
|
break;
|
|
|
|
case 'rejected':
|
|
|
|
notification.info('[' + name + '] Transaction Rejected',
|
|
|
|
$filter('translate')('A transaction was rejected by') + ' ' + user);
|
|
|
|
break;
|
|
|
|
case 'corrupt':
|
|
|
|
notification.error('[' + name + '] Transaction Error',
|
|
|
|
$filter('translate')('Received corrupt transaction from') + ' ' + user);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$rootScope.$digest();
|
|
|
|
});
|
|
|
|
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-29 19:31:17 -08:00
|
|
|
root.bind = function(iden) {
|
|
|
|
preconditions.checkArgument(_.isObject(iden));
|
2014-11-29 19:44:25 -08:00
|
|
|
copay.logger.debug('Binding profile...');
|
2014-11-29 19:31:17 -08:00
|
|
|
|
|
|
|
var self = this;
|
2014-11-29 13:35:48 -08:00
|
|
|
root.setupGlobalVariables(iden);
|
2014-11-29 19:31:17 -08:00
|
|
|
|
|
|
|
iden.on('newWallet', function(wid) {
|
|
|
|
var w = iden.getWalletById(wid);
|
2014-11-29 20:02:18 -08:00
|
|
|
copay.logger.debug('newWallet:', w.getName(), wid, iden.getLastFocusedWalletId());
|
2014-11-29 19:31:17 -08:00
|
|
|
root.installWalletHandlers(w);
|
|
|
|
if (wid == iden.getLastFocusedWalletId()) {
|
2014-11-29 20:11:00 -08:00
|
|
|
$rootScope.starting = false;
|
2014-11-29 20:02:18 -08:00
|
|
|
copay.logger.debug('GOT Focused wallet:', w.getName());
|
|
|
|
root.setFocusedWallet(w, true);
|
2014-11-29 19:31:17 -08:00
|
|
|
root.goWalletHome();
|
|
|
|
}
|
2014-11-29 20:02:18 -08:00
|
|
|
// At the end (after all handlers are in place)...start the wallet.
|
|
|
|
w.netStart();
|
2014-11-29 19:31:17 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
iden.on('noWallets', function() {
|
2014-11-29 13:35:48 -08:00
|
|
|
$location.path('/create');
|
|
|
|
$rootScope.$digest()
|
2014-11-29 19:31:17 -08:00
|
|
|
});
|
2014-11-29 13:35:48 -08:00
|
|
|
|
2014-11-29 19:31:17 -08:00
|
|
|
iden.on('deletedWallet', function(wid) {
|
|
|
|
notification.info('Wallet deleted', $filter('translate')('This wallet was deleted'));
|
|
|
|
if ($rootScope.wallet.id === wid) {
|
|
|
|
$rootScope.wallet = null;
|
|
|
|
var lastFocused = iden.getLastFocusedWalletId();
|
2014-11-29 20:02:18 -08:00
|
|
|
root.setFocusedWallet(lastFocused);
|
2014-11-29 19:31:17 -08:00
|
|
|
}
|
|
|
|
});
|
2014-11-29 13:35:48 -08:00
|
|
|
|
2014-11-29 19:31:17 -08:00
|
|
|
iden.on('closed', function() {
|
|
|
|
delete $rootScope['wallet'];
|
|
|
|
delete $rootScope['iden'];
|
|
|
|
applicationService.restart();
|
|
|
|
});
|
|
|
|
};
|
2014-11-29 13:35:48 -08:00
|
|
|
|
2014-11-29 19:31:17 -08:00
|
|
|
root.signout = function() {
|
|
|
|
if ($rootScope.iden) {
|
|
|
|
$rootScope.iden.close();
|
2014-11-29 13:35:48 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
root.createWallet = function(opts, cb) {
|
2014-11-29 19:31:17 -08:00
|
|
|
$rootScope.iden.createWallet(opts, cb);
|
2014-11-29 13:35:48 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
root.importWallet = function(encryptedObj, pass, opts, cb) {
|
2014-11-29 19:31:17 -08:00
|
|
|
copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj, pass, opts);
|
2014-11-29 13:35:48 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
root.joinWallet = function(opts, cb) {
|
|
|
|
$rootScope.iden.joinWallet(opts, function(err, w) {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-11-29 20:02:18 -08:00
|
|
|
root.importProfile = function(str, password, cb) {
|
2014-11-29 19:31:17 -08:00
|
|
|
copay.Identity.importFromEncryptedFullJson(str, password, {
|
|
|
|
pluginManager: pluginManager,
|
|
|
|
network: config.network,
|
|
|
|
networkName: config.networkName,
|
|
|
|
walletDefaults: config.wallet,
|
|
|
|
passphraseConfig: config.passphraseConfig,
|
|
|
|
}, function(err, iden) {
|
|
|
|
if (err) return cb(err);
|
2014-11-29 22:58:38 -08:00
|
|
|
root.bind(iden);
|
|
|
|
iden.openWallets();
|
|
|
|
return cb();
|
2014-11-29 19:31:17 -08:00
|
|
|
});
|
|
|
|
};
|
2014-11-29 13:35:48 -08:00
|
|
|
|
2014-10-27 07:10:32 -07:00
|
|
|
return root;
|
|
|
|
});
|