Wallet/src/js/controllers/paperWallet.js

139 lines
4.5 KiB
JavaScript
Raw Normal View History

2015-10-02 11:27:17 -03:00
angular.module('copayApp.controllers').controller('paperWalletController',
2016-09-09 13:03:58 -03:00
function($scope, $timeout, $log, $ionicModal, $ionicHistory, popupService, gettextCatalog, platformInfo, configService, profileService, $state, bitcore, ongoingProcess, txFormatService, $stateParams, walletService) {
2016-06-16 16:06:43 -03:00
$scope.onQrCodeScanned = function(data) {
$scope.formData.inputData = data;
2016-06-16 16:06:43 -03:00
$scope.onData(data);
};
2015-10-07 15:32:52 -03:00
2016-06-16 16:06:43 -03:00
$scope.onData = function(data) {
$scope.scannedKey = data;
$scope.isPkEncrypted = (data.substring(0, 2) == '6P');
};
2015-10-02 11:27:17 -03:00
2016-06-16 16:06:43 -03:00
function _scanFunds(cb) {
function getPrivateKey(scannedKey, isPkEncrypted, passphrase, cb) {
if (!isPkEncrypted) return cb(null, scannedKey);
2016-08-19 13:09:27 -03:00
wallet.decryptBIP38PrivateKey(scannedKey, passphrase, null, cb);
};
2015-10-02 15:56:59 -03:00
function getBalance(privateKey, cb) {
2016-08-19 13:09:27 -03:00
wallet.getBalanceFromPrivateKey(privateKey, cb);
};
function checkPrivateKey(privateKey) {
try {
new bitcore.PrivateKey(privateKey, 'livenet');
} catch (err) {
return false;
}
return true;
2016-06-16 16:06:43 -03:00
};
2015-10-02 15:56:59 -03:00
2016-06-16 16:06:43 -03:00
getPrivateKey($scope.scannedKey, $scope.isPkEncrypted, $scope.passphrase, function(err, privateKey) {
2015-10-07 17:49:34 -03:00
if (err) return cb(err);
if (!checkPrivateKey(privateKey)) return cb(new Error('Invalid private key'));
getBalance(privateKey, function(err, balance) {
if (err) return cb(err);
return cb(null, privateKey, balance);
});
});
2016-06-16 16:06:43 -03:00
};
2015-10-07 17:49:34 -03:00
2016-06-16 16:06:43 -03:00
$scope.scanFunds = function() {
$scope.privateKey = '';
$scope.balanceSat = 0;
2015-10-07 17:49:34 -03:00
2016-06-13 15:25:40 -03:00
ongoingProcess.set('scanning', true);
2015-10-02 13:24:43 -03:00
$timeout(function() {
2016-06-16 16:06:43 -03:00
_scanFunds(function(err, privateKey, balance) {
2016-06-13 15:25:40 -03:00
ongoingProcess.set('scanning', false);
if (err) {
$log.error(err);
2016-09-08 19:58:00 -03:00
popupService.showAlert(gettextCatalog.getString('Error scanning funds:'), err || err.toString());
2015-10-07 17:49:34 -03:00
} else {
2016-06-16 16:06:43 -03:00
$scope.privateKey = privateKey;
$scope.balanceSat = balance;
var config = configService.getSync().wallet.settings;
2016-08-18 11:45:30 -03:00
$scope.balance = txFormatService.formatAmount(balance) + ' ' + config.unitName;
$scope.scanned = true;
2015-10-07 17:49:34 -03:00
}
2015-10-07 18:17:51 -03:00
$scope.$apply();
2015-10-02 13:24:43 -03:00
});
}, 100);
2016-06-16 16:06:43 -03:00
};
2015-10-02 15:56:59 -03:00
2016-06-16 16:06:43 -03:00
function _sweepWallet(cb) {
2016-09-09 13:03:58 -03:00
walletService.getAddress(wallet, true, function(err, destinationAddress) {
2015-10-07 17:49:34 -03:00
if (err) return cb(err);
2016-08-19 13:09:27 -03:00
wallet.buildTxFromPrivateKey($scope.privateKey, destinationAddress, null, function(err, tx) {
2015-10-07 17:49:34 -03:00
if (err) return cb(err);
2016-08-19 13:09:27 -03:00
wallet.broadcastRawTx({
2015-10-07 17:49:34 -03:00
rawTx: tx.serialize(),
network: 'livenet'
}, function(err, txid) {
if (err) return cb(err);
return cb(null, destinationAddress, txid);
});
});
});
};
2016-06-16 16:06:43 -03:00
$scope.sweepWallet = function() {
2016-06-13 15:25:40 -03:00
ongoingProcess.set('sweepingWallet', true);
2016-06-16 16:06:43 -03:00
$scope.sending = true;
2015-10-07 17:49:34 -03:00
$timeout(function() {
2016-06-16 16:06:43 -03:00
_sweepWallet(function(err, destinationAddress, txid) {
2016-06-13 15:25:40 -03:00
ongoingProcess.set('sweepingWallet', false);
2016-09-09 13:03:58 -03:00
$scope.sending = false;
if (err) {
$log.error(err);
2016-09-08 19:58:00 -03:00
popupService.showAlert(gettextCatalog.getString('Error sweeping wallet:'), err || err.toString());
2015-10-07 18:17:51 -03:00
} else {
2016-09-09 13:03:58 -03:00
$scope.openStatusModal('broadcasted', function() {
2016-09-23 12:42:33 -03:00
$ionicHistory.removeBackView();
2016-08-19 13:07:18 -03:00
$state.go('tabs.home');
2015-10-07 18:17:51 -03:00
});
}
2015-10-07 18:17:51 -03:00
$scope.$apply();
2015-10-02 11:27:17 -03:00
});
2015-10-02 16:04:11 -03:00
}, 100);
2016-06-16 16:06:43 -03:00
};
2016-09-09 13:03:58 -03:00
$scope.openStatusModal = function(type, cb) {
$scope.tx = {};
$scope.tx.amountStr = $scope.balance;
$scope.type = type;
2016-08-19 13:09:27 -03:00
$scope.color = wallet.backgroundColor;
$scope.cb = cb;
$ionicModal.fromTemplateUrl('views/modals/tx-status.html', {
2016-09-09 13:03:58 -03:00
scope: $scope
}).then(function(modal) {
$scope.txStatusModal = modal;
$scope.txStatusModal.show();
});
};
2016-10-27 12:33:08 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
var wallet = profileService.getWallet($stateParams.walletId);
$scope.wallet = wallet;
$scope.needsBackup = wallet.needsBackup;
$scope.walletAlias = wallet.name;
$scope.walletName = wallet.credentials.walletName;
$scope.formData = {};
$scope.formData.inputData = null;
$scope.scannedKey = null;
$scope.balance = null;
$scope.balanceSat = null;
$scope.scanned = false;
$timeout(function() {
$scope.$apply();
}, 10);
});
2015-10-02 15:23:44 -03:00
});