Wallet/src/js/controllers/paperWallet.js

145 lines
5.1 KiB
JavaScript
Raw Normal View History

2015-10-02 11:27:17 -03:00
angular.module('copayApp.controllers').controller('paperWalletController',
function($scope, $timeout, $log, $ionicModal, $ionicHistory, feeService, popupService, gettextCatalog, platformInfo, configService, profileService, $state, bitcore, ongoingProcess, txFormatService, $stateParams, walletService) {
2016-06-16 16:06:43 -03:00
function _scanFunds(cb) {
function getPrivateKey(scannedKey, isPkEncrypted, passphrase, cb) {
if (!isPkEncrypted) return cb(null, scannedKey);
2016-11-17 17:14:20 -03:00
$scope.wallet.decryptBIP38PrivateKey(scannedKey, passphrase, null, cb);
};
2015-10-02 15:56:59 -03:00
function getBalance(privateKey, cb) {
2016-11-17 17:14:20 -03:00
$scope.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() {
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());
2016-11-22 11:37:19 -03:00
$state.go('tabs.home');
2015-10-07 17:49:34 -03:00
} else {
2016-06-16 16:06:43 -03:00
$scope.privateKey = privateKey;
$scope.balanceSat = balance;
2016-11-22 11:37:19 -03:00
if ($scope.balanceSat <= 0)
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Not funds found'));
var config = configService.getSync().wallet.settings;
2016-08-18 11:45:30 -03:00
$scope.balance = txFormatService.formatAmount(balance) + ' ' + config.unitName;
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-11-17 17:14:20 -03:00
walletService.getAddress($scope.wallet, true, function(err, destinationAddress) {
2015-10-07 17:49:34 -03:00
if (err) return cb(err);
$scope.wallet.buildTxFromPrivateKey($scope.privateKey, destinationAddress, null, function(err, testTx) {
2015-10-07 17:49:34 -03:00
if (err) return cb(err);
var rawTxLength = testTx.serialize().length;
2017-07-03 09:41:40 -03:00
feeService.getCurrentFeeRate('livenet', function(err, feePerKB) {
var opts = {};
opts.fee = Math.round((feePerKB * rawTxLength) / 2000);
$scope.wallet.buildTxFromPrivateKey($scope.privateKey, destinationAddress, opts, function(err, tx) {
if (err) return cb(err);
$scope.wallet.broadcastRawTx({
rawTx: tx.serialize(),
network: 'livenet'
}, function(err, txid) {
if (err) return cb(err);
return cb(null, destinationAddress, txid);
});
});
2015-10-07 17:49:34 -03:00
});
});
});
};
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-11-22 11:37:19 -03:00
$scope.sendStatus = 'success';
}
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-11-22 11:37:19 -03:00
$scope.onSuccessConfirm = function() {
$state.go('tabs.home');
};
$scope.onWalletSelect = function(wallet) {
$scope.wallet = wallet;
};
$scope.showWalletSelector = function() {
if ($scope.singleWallet) return;
$scope.walletSelectorTitle = gettextCatalog.getString('Transfer to');
$scope.showWallets = true;
};
2016-11-22 11:37:19 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.scannedKey = (data.stateParams && data.stateParams.privateKey) ? data.stateParams.privateKey : null;
$scope.isPkEncrypted = $scope.scannedKey ? ($scope.scannedKey.substring(0, 2) == '6P') : null;
$scope.sendStatus = null;
$scope.error = false;
$scope.wallets = profileService.getWallets({
onlyComplete: true,
network: 'livenet',
});
$scope.singleWallet = $scope.wallets.length == 1;
2016-11-22 11:37:19 -03:00
if (!$scope.wallets || !$scope.wallets.length) {
$scope.noMatchingWallet = true;
return;
}
});
$scope.$on("$ionicView.enter", function(event, data) {
$scope.wallet = $scope.wallets[0];
if (!$scope.wallet) return;
if (!$scope.isPkEncrypted) $scope.scanFunds();
else {
var message = gettextCatalog.getString('Private key encrypted. Enter password');
popupService.showPrompt(null, message, null, function(res) {
$scope.passphrase = res;
$scope.scanFunds();
});
}
});
2015-10-02 15:23:44 -03:00
});