Wallet/src/js/controllers/paperWallet.js

128 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-10-02 11:27:17 -03:00
angular.module('copayApp.controllers').controller('paperWalletController',
2016-08-19 14:01:50 -03:00
function($scope, $timeout, $log, $ionicModal, configService, profileService, $state, addressService, txStatus, bitcore, ongoingProcess, txFormatService, $stateParams) {
2016-08-19 13:09:27 -03:00
var wallet = profileService.getWallet($stateParams.walletId);
2015-10-02 11:27:17 -03:00
var rawTx;
2016-06-16 16:06:43 -03:00
$scope.onQrCodeScanned = function(data) {
2015-10-07 15:32:52 -03:00
$scope.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.error = null;
$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;
$scope.error = null;
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-06-16 16:06:43 -03:00
$scope.error = err.message || 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;
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-08-19 13:09:27 -03:00
addressService.getAddress(wallet.credentials.walletId, 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;
$scope.error = null;
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);
2015-10-07 18:17:51 -03:00
if (err) {
2016-06-16 16:06:43 -03:00
$scope.error = err.message || err.toString();
$log.error(err);
2015-10-07 18:17:51 -03:00
} else {
var type = txStatus.notify(txp);
$scope.openStatusModal(type, txp, function() {
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
};
$scope.openStatusModal = function(type, txp, cb) {
$scope.type = type;
$scope.tx = txFormatService.processTx(txp);
2016-08-19 13:09:27 -03:00
$scope.color = wallet.backgroundColor;
$scope.cb = cb;
$ionicModal.fromTemplateUrl('views/modals/tx-status.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.txStatusModal = modal;
$scope.txStatusModal.show();
});
};
2015-10-02 15:23:44 -03:00
});