Wallet/src/js/controllers/cashScan.js

165 lines
5.1 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.controllers').controller('cashScanController',
2017-09-09 21:14:27 -03:00
function($rootScope, $timeout, $scope, $state, $stateParams, $ionicModal, $ionicScrollDelegate, $window, gettextCatalog, lodash, popupService, ongoingProcess, profileService, walletService, configService, $log, txFormatService, bwcError, pushNotificationsService, bwcService) {
var wallet;
var errors = bwcService.getErrors();
2017-09-09 21:14:27 -03:00
$scope.error = null;
2017-09-11 14:50:14 -03:00
$scope.walletDisabled = '#667';
2017-09-11 14:50:14 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
updateAllWallets();
});
var updateAllWallets = function() {
2017-09-11 14:50:14 -03:00
var walletsBTC = profileService.getWallets({
2017-09-09 21:14:27 -03:00
coin: 'btc',
onlyComplete: true,
network: 'livenet'
});
2017-09-11 14:50:14 -03:00
if (lodash.isEmpty(walletsBTC)) {
2017-09-09 21:14:27 -03:00
$state.go('tabs.home');
return;
}
2017-09-09 21:14:27 -03:00
// Filter out already duplicated wallets
var walletsBCH = profileService.getWallets({
coin: 'bch',
network: 'livenet'
});
var xPubKeyIndex = lodash.indexBy(walletsBCH, "credentials.xPubKey");
2017-09-11 14:50:14 -03:00
walletsBTC = lodash.filter(walletsBTC, function(w) {
2017-09-09 21:14:27 -03:00
return !xPubKeyIndex[w.credentials.xPubKey];
});
2017-09-09 21:14:27 -03:00
// Filter out non BIP44 wallets
2017-09-11 14:50:14 -03:00
var wallets = lodash.filter(walletsBTC, function(w) {
2017-09-09 21:14:27 -03:00
return w.credentials.derivationStrategy == 'BIP44'
});
$scope.wallets = wallets;
2017-09-11 14:50:14 -03:00
$scope.nonBIP44Wallets = lodash.filter(walletsBTC, function(w) {
return w.credentials.derivationStrategy != 'BIP44';
});
var i = wallets.length;
var j = 0;
lodash.each(wallets, function(wallet) {
2017-09-09 21:14:27 -03:00
walletService.getBalance(wallet, {
coin: 'bch'
}, function(err, balance) {
if (err) {
wallet.error = (err === 'WALLET_NOT_REGISTERED') ? gettextCatalog.getString('Wallet not registered') : bwcError.msg(err);
$log.error(err);
return;
2017-09-09 21:14:27 -03:00
}
wallet.error = null;
wallet.bchBalance = txFormatService.formatAmountStr('bch', balance.availableAmount);
if (++j == i) {
//Done
$timeout(function() {
$rootScope.$apply();
}, 10);
}
});
});
};
$scope.duplicate = function(wallet) {
2017-09-09 21:14:27 -03:00
$scope.error = null;
$log.debug('Duplicating wallet for BCH:' + wallet.id + ':' + wallet.name);
var opts = {};
opts.name = wallet.name + '[BCH]';
opts.m = wallet.m;
opts.n = wallet.n;
2017-09-09 21:14:27 -03:00
opts.myName = wallet.credentials.copayerName;
opts.networkName = wallet.network;
opts.coin = 'bch';
2017-09-09 21:14:27 -03:00
opts.walletPrivKey = wallet.credentials.walletPrivKey;
opts.compliantDerivation = wallet.credentials.compliantDerivation;
function setErr(err, cb) {
2017-09-09 21:14:27 -03:00
if (!cb) cb = function() {};
$scope.error = bwcError.cb(err, gettextCatalog.getString('Could not duplicate'), function() {
return cb(err);
});
$timeout(function() {
$rootScope.$apply();
}, 10);
}
function importOrCreate(cb) {
2017-09-09 21:14:27 -03:00
walletService.getStatus(wallet, {}, function(err, status) {
if (err) return cb(err);
2017-09-09 21:14:27 -03:00
opts.singleAddress = status.wallet.singleAddress;
// first try to import
2017-09-09 21:14:27 -03:00
profileService.importExtendedPrivateKey(opts.extendedPrivateKey, opts, function(err, newWallet) {
if (err && !(err instanceof errors.NOT_AUTHORIZED)) {
return setErr(err, cb);
}
if (err) {
// create and store a wallet
2017-09-09 21:14:27 -03:00
return profileService.createWallet(opts, function(err, newWallet) {
if (err) return setErr(err, cb);
2017-09-09 21:14:27 -03:00
return cb(null, newWallet, true);
});
}
2017-09-09 21:14:27 -03:00
return cb(null, newWallet);
});
});
};
2017-09-09 21:14:27 -03:00
// Multisig wallets? add Copayers
function addCopayers(newWallet, isNew, cb) {
if (!isNew) return cb();
if (wallet.n == 1) return cb();
2017-09-09 21:14:27 -03:00
$log.info('Adding copayers for BCH wallet config:' + wallet.m + '-' + wallet.n);
walletService.copyCopayers(wallet, newWallet, function(err) {
if (err) return setErr(err, cb);
return cb();
});
};
2017-09-11 14:50:14 -03:00
walletService.getKeys(wallet, function(err, keys) {
2017-09-09 21:14:27 -03:00
if (err) {
$scope.error = err;
return $timeout(function() {
$rootScope.$apply();
}, 10);
}
opts.extendedPrivateKey = keys.xPrivKey;
ongoingProcess.set('duplicatingWallet', true);
importOrCreate(function(err, newWallet, isNew) {
if (err) {
ongoingProcess.set('duplicatingWallet', false);
return;
}
walletService.updateRemotePreferences(newWallet);
pushNotificationsService.updateSubscription(newWallet);
addCopayers(newWallet, isNew, function(err) {
ongoingProcess.set('duplicatingWallet', false);
if (err)
return setErr(err);
if (isNew)
walletService.startScan(newWallet, function() {});
$state.go('tabs.home');
});
});
});
}
});