can sweep a private key for both btc and bch at the same time

This commit is contained in:
Kadir Sekha 2017-10-31 14:53:15 +09:00
commit b2935a1d1a
2 changed files with 181 additions and 72 deletions

View file

@ -1,32 +1,58 @@
angular.module('copayApp.controllers').controller('paperWalletController',
function($scope, $timeout, $log, $ionicModal, $ionicHistory, feeService, popupService, gettextCatalog, platformInfo, configService, profileService, $state, bitcore, ongoingProcess, txFormatService, $stateParams, walletService) {
function($scope, $timeout, $log, $ionicModal, $ionicHistory, feeService, popupService, gettextCatalog, platformInfo, configService, profileService, $state, lodash, bitcore, bitcoreCash, ongoingProcess, txFormatService, $stateParams, walletService) {
var _ = lodash;
function _scanFunds(cb) {
function getPrivateKey(scannedKey, isPkEncrypted, passphrase, cb) {
if (!isPkEncrypted) return cb(null, scannedKey);
$scope.wallet.decryptBIP38PrivateKey(scannedKey, passphrase, null, cb);
if ($scope.btcWallet) {
$scope.btcWallet.decryptBIP38PrivateKey(scannedKey, passphrase, null, cb);
} else if ($scope.bchWallet) {
$scope.bchWallet.decryptBIP38PrivateKey(scannedKey, passphrase, null, cb);
}
};
function getBalance(privateKey, cb) {
$scope.wallet.getBalanceFromPrivateKey(privateKey, cb);
};
function getBalance(privateKey, coin, cb) {
if (coin == 'btc') {
if (!$scope.btcWallet) return cb(null, 0);
$scope.btcWallet.getBalanceFromPrivateKey(privateKey, cb);
} else {
if (!$scope.bchWallet) return cb(null, 0);
$scope.bchWallet.getBalanceFromPrivateKey(privateKey, cb);
}
}
function getBalances(privateKey, cb) {
getBalance(privateKey, 'btc', function (err, btcBalance) {
if (err) return cb(err);
getBalance(privateKey, 'bch', function (err, bchBalance) {
if (err) return cb(err);
return cb(null, btcBalance, bchBalance);
});
});
}
function checkPrivateKey(privateKey) {
try {
new bitcore.PrivateKey(privateKey, 'livenet');
} catch (err) {
return false;
try {
new bitcoreCash.PrivateKey(privateKey, 'livenet');
} catch (err) {
return false;
}
}
return true;
};
}
getPrivateKey($scope.scannedKey, $scope.isPkEncrypted, $scope.passphrase, function(err, privateKey) {
if (err) return cb(err);
if (!checkPrivateKey(privateKey)) return cb(new Error('Invalid private key'));
getBalance(privateKey, function(err, balance) {
getBalances(privateKey, function(err, btcBalance, bchBalance) {
if (err) return cb(err);
return cb(null, privateKey, balance);
return cb(null, privateKey, btcBalance, bchBalance);
});
});
};
@ -34,7 +60,7 @@ angular.module('copayApp.controllers').controller('paperWalletController',
$scope.scanFunds = function() {
ongoingProcess.set('scanning', true);
$timeout(function() {
_scanFunds(function(err, privateKey, balance) {
_scanFunds(function(err, privateKey, btcBalance, bchBalance) {
ongoingProcess.set('scanning', false);
if (err) {
$log.error(err);
@ -42,31 +68,35 @@ angular.module('copayApp.controllers').controller('paperWalletController',
$state.go('tabs.home');
} else {
$scope.privateKey = privateKey;
$scope.balanceSat = balance;
$scope.btcBalanceSat = btcBalance;
$scope.bchBalanceSat = bchBalance;
if ($scope.balanceSat <= 0)
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Not funds found'));
$scope.balance = txFormatService.formatAmountStr($scope.wallet.coin, balance);
if ($scope.btcWallet)
$scope.btcBalance = txFormatService.formatAmountStr($scope.btcWallet.coin, btcBalance);
if ($scope.bchWallet)
$scope.bchBalance = txFormatService.formatAmountStr($scope.bchWallet.coin, bchBalance);
}
$scope.$apply();
});
}, 100);
};
function _sweepWallet(cb) {
walletService.getAddress($scope.wallet, true, function(err, destinationAddress) {
if (err) return cb(err);
function _sweepWallet(wallet, cb) {
var opts = { 'coin': wallet.coin };
$scope.wallet.buildTxFromPrivateKey($scope.privateKey, destinationAddress, null, function(err, testTx) {
walletService.getAddress(wallet, true, function(err, destinationAddress) {
if (err) return cb(err);
wallet.buildTxFromPrivateKey($scope.privateKey, destinationAddress, opts, function(err, testTx) {
if (err) return cb(err);
var rawTxLength = testTx.serialize().length;
feeService.getCurrentFeeRate('btc', 'livenet', function(err, feePerKb) {
var opts = {};
feeService.getCurrentFeeRate(wallet.coin, wallet.network, function(err, feePerKb) {
opts.fee = Math.round((feePerKb * rawTxLength) / 2000);
$scope.wallet.buildTxFromPrivateKey($scope.privateKey, destinationAddress, opts, function(err, tx) {
wallet.buildTxFromPrivateKey($scope.privateKey, destinationAddress, opts, function(err, tx) {
if (err) return cb(err);
$scope.wallet.broadcastRawTx({
wallet.broadcastRawTx({
rawTx: tx.serialize(),
network: 'livenet'
network: wallet.network
}, function(err, txid) {
if (err) return cb(err);
return cb(null, destinationAddress, txid);
@ -77,12 +107,13 @@ angular.module('copayApp.controllers').controller('paperWalletController',
});
};
$scope.sweepWallet = function() {
$scope.sweepWallet = function(coin) {
var wallet = coin == 'btc' ? $scope.btcWallet : $scope.bchWallet;
ongoingProcess.set('sweepingWallet', true);
$scope.sending = true;
$timeout(function() {
_sweepWallet(function(err, destinationAddress, txid) {
_sweepWallet(wallet, function(err, destinationAddress, txid) {
ongoingProcess.set('sweepingWallet', false);
$scope.sending = false;
if (err) {
@ -94,21 +125,31 @@ angular.module('copayApp.controllers').controller('paperWalletController',
$scope.$apply();
});
}, 100);
};
}
$scope.onSuccessConfirm = function() {
$state.go('tabs.home');
};
$scope.onWalletSelect = function(wallet) {
$scope.wallet = wallet;
};
$scope.onBtcWalletSelect = function(wallet) {
$scope.btcWallet = wallet;
}
$scope.showWalletSelector = function() {
if ($scope.singleWallet) return;
$scope.onBchWalletSelect = function(wallet) {
$scope.bchWallet = wallet;
}
$scope.showBtcWalletSelector = function() {
if ($scope.singleBtcWallet) return;
$scope.walletSelectorTitle = gettextCatalog.getString('Transfer to');
$scope.showWallets = true;
};
$scope.showBtcWallets = true;
}
$scope.showBchWalletSelector = function() {
if ($scope.singleBchWallet) return;
$scope.walletSelectorTitle = gettextCatalog.getString('Transfer to');
$scope.showBchWallets = true;
}
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.scannedKey = (data.stateParams && data.stateParams.privateKey) ? data.stateParams.privateKey : null;
@ -116,21 +157,29 @@ angular.module('copayApp.controllers').controller('paperWalletController',
$scope.sendStatus = null;
$scope.error = false;
$scope.wallets = profileService.getWallets({
var wallets = profileService.getWallets({
onlyComplete: true,
network: 'livenet',
});
$scope.singleWallet = $scope.wallets.length == 1;
if (!$scope.wallets || !$scope.wallets.length) {
$scope.noMatchingWallet = false;
if (!wallets || wallets.length == 0) {
$scope.noMatchingWallet = true;
return;
}
$scope.wallets = wallets;
$scope.btcWallets = _.filter($scope.wallets, function(w) { return w.coin == 'btc'; });
$scope.bchWallets = _.filter($scope.wallets, function(w) { return w.coin == 'bch'; });
$scope.singleBtcWallet = $scope.btcWallets.length == 1;
$scope.singleBchWallet = $scope.bchWallets.length == 1;
});
$scope.$on("$ionicView.enter", function(event, data) {
$scope.wallet = $scope.wallets[0];
if (!$scope.wallet) return;
$scope.btcWallet = $scope.btcWallets[0];
$scope.bchWallet = $scope.bchWallets[0];
if (!$scope.btcWallet && !$scope.bchWallet) return;
if (!$scope.isPkEncrypted) $scope.scanFunds();
else {
var message = gettextCatalog.getString('Private key encrypted. Enter password');