can sweep a private key for both btc and bch at the same time
This commit is contained in:
parent
82bd0d5818
commit
b2935a1d1a
2 changed files with 181 additions and 72 deletions
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -4,58 +4,118 @@
|
|||
<ion-nav-back-button>
|
||||
</ion-nav-back-button>
|
||||
<ion-nav-buttons side="secondary">
|
||||
<button ng-disabled="sending || balanceSat <= 0 || noMatchingWallet" class="button no-border" ng-click="sweepWallet()" translate>
|
||||
<button ng-disabled="sending || btcBalanceSat <= 0 && bchBalanceSat <= 0 || noMatchingWallet" class="button no-border" ng-click="sweepWallet()" translate>
|
||||
Sweep
|
||||
</button>
|
||||
</ion-nav-buttons>
|
||||
</ion-nav-bar>
|
||||
<ion-content scroll="false">
|
||||
<div ng-class="ng-hide" ng-show="!noMatchingWallet">
|
||||
<div class="row">
|
||||
<div class="col text-center">
|
||||
<h4 class="text-bold" translate>Funds found:</h4>
|
||||
<div ng-show="balance" class="size-24 ng-hide">{{balance}}</div>
|
||||
<div ng-show="!balance" class="size-24 ng-hide">...</div>
|
||||
<div ng-class="ng-hide" ng-show="btcWallets.length > 0">
|
||||
<div class="row">
|
||||
<div class="col text-center">
|
||||
<h4 class="text-bold" translate>Funds found:</h4>
|
||||
<div ng-show="btcBalance" class="size-24 ng-hide">
|
||||
<span>{{btcBalance}}</span>
|
||||
<span>
|
||||
<button ng-disabled="btcBalanceSat <= 0 || noMatchingWallet" class="button no-border" ng-click="sweepWallet('btc')" translate>
|
||||
Sweep
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
<div ng-show="!btcBalance" class="size-24 ng-hide">...</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center size-12 text-gray">
|
||||
<span translate>Funds will be transferred to</span>:
|
||||
</div>
|
||||
<div class="list card" ng-click="showBtcWalletSelector()" ng-if="btcWallets[0]">
|
||||
<a ng-if="btcWallet" class="item item-sub item-icon-left item-big-icon-left item-icon-right">
|
||||
<i class="icon big-icon-svg" ng-include="'views/includes/walletIcon.html'"></i>
|
||||
<span>
|
||||
{{btcWallet.name || btcWallet.id}}
|
||||
</span>
|
||||
<p>
|
||||
<span ng-if="!btcWallet.balanceHidden"> {{btcWallet.status.totalBalanceStr}} </span>
|
||||
|
||||
<span ng-if="btcWallet.balanceHidden" translate>[Balance Hidden]</span>
|
||||
<span class="tab-home__wallet__multisig-number" ng-if="btcWallet.n > 1">
|
||||
{{btcWallet.m}}-of-{{btcWallet.n}}
|
||||
</span>
|
||||
<span class="assertive" ng-if="wallet.error">{{wallet.error}}</span>
|
||||
|
||||
</p>
|
||||
<i ng-if="!singleBtcWallet" class="icon bp-arrow-right"></i>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center size-12 text-gray">
|
||||
<span translate>Funds will be transferred to</span>:
|
||||
</div>
|
||||
<div class="list card" ng-click="showWalletSelector()" ng-if="wallets[0]">
|
||||
<a ng-if="wallet" class="item item-sub item-icon-left item-big-icon-left item-icon-right">
|
||||
<i class="icon big-icon-svg" ng-include="'views/includes/walletIcon.html'"></i>
|
||||
<span>
|
||||
{{wallet.name || wallet.id}}
|
||||
</span>
|
||||
<p>
|
||||
<span ng-if="!wallet.balanceHidden"> {{wallet.status.totalBalanceStr}} </span>
|
||||
<slide-to-accept-success
|
||||
slide-success-show="sendStatus === 'success'"
|
||||
slide-success-on-confirm="onSuccessConfirm()"
|
||||
slide-success-hide-on-confirm="true">
|
||||
<span translate>Funds transferred</span>
|
||||
</slide-to-accept-success>
|
||||
<div ng-class="ng-hide" ng-show="bchWallets.length > 0">
|
||||
<div class="row">
|
||||
<div class="col text-center">
|
||||
<h4 class="text-bold" translate>Funds found:</h4>
|
||||
<div ng-show="bchBalance" class="size-24 ng-hide">
|
||||
<span>{{bchBalance}}</span>
|
||||
<span>
|
||||
<button ng-disabled="bchBalanceSat <= 0 || noMatchingWallet" class="button no-border" ng-click="sweepWallet('bch')" translate>
|
||||
Sweep
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
<div ng-show="!bchBalance" class="size-24 ng-hide">...</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center size-12 text-gray">
|
||||
<span translate>Funds will be transferred to</span>:
|
||||
</div>
|
||||
<div class="list card" ng-click="showBchWalletSelector()" ng-if="bchWallets[0]">
|
||||
<a ng-if="bchWallet" class="item item-sub item-icon-left item-big-icon-left item-icon-right">
|
||||
<i class="icon big-icon-svg" ng-include="'views/includes/walletIcon.html'"></i>
|
||||
<span>
|
||||
{{bchWallet.name || bchWallet.id}}
|
||||
</span>
|
||||
<p>
|
||||
<span ng-if="!bchWallet.balanceHidden"> {{bchWallet.status.totalBalanceStr}} </span>
|
||||
|
||||
<span ng-if="wallet.balanceHidden" translate>[Balance Hidden]</span>
|
||||
<span class="tab-home__wallet__multisig-number" ng-if="wallet.n > 1">
|
||||
{{wallet.m}}-of-{{wallet.n}}
|
||||
</span>
|
||||
<span class="assertive" ng-if="wallet.error">{{wallet.error}}</span>
|
||||
|
||||
</p>
|
||||
<i ng-if="!singleWallet" class="icon bp-arrow-right"></i>
|
||||
</a>
|
||||
<span ng-if="bchWallet.balanceHidden" translate>[Balance Hidden]</span>
|
||||
<span class="tab-home__wallet__multisig-number" ng-if="bchWallet.n > 1">
|
||||
{{bchWallet.m}}-of-{{bchWallet.n}}
|
||||
</span>
|
||||
<span class="assertive" ng-if="wallet.error">{{wallet.error}}</span>
|
||||
|
||||
</p>
|
||||
<i ng-if="!singleBchWallet" class="icon bp-arrow-right"></i>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<slide-to-accept-success
|
||||
slide-success-show="sendStatus === 'success'"
|
||||
slide-success-on-confirm="onSuccessConfirm()"
|
||||
slide-success-hide-on-confirm="true">
|
||||
<span translate>Funds transferred</span>
|
||||
</slide-to-accept-success>
|
||||
</div>
|
||||
<div class="text-center padding ng-hide" ng-show="noMatchingWallet">
|
||||
<span class="badge badge-energized" translate>No wallets available to receive funds</span>
|
||||
</div>
|
||||
<slide-to-accept-success
|
||||
slide-success-show="sendStatus === 'success'"
|
||||
slide-success-on-confirm="onSuccessConfirm()"
|
||||
slide-success-hide-on-confirm="true">
|
||||
<span translate>Funds transferred</span>
|
||||
</slide-to-accept-success>
|
||||
</ion-content>
|
||||
<wallet-selector
|
||||
wallet-selector-title="walletSelectorTitle"
|
||||
wallet-selector-wallets="wallets"
|
||||
wallet-selector-selected-wallet="wallet"
|
||||
wallet-selector-show="showWallets"
|
||||
wallet-selector-on-select="onWalletSelect">
|
||||
wallet-selector-wallets="btcWallets"
|
||||
wallet-selector-selected-wallet="btcWallet"
|
||||
wallet-selector-show="showBtcWallets"
|
||||
wallet-selector-on-select="onBtcWalletSelect">
|
||||
</wallet-selector>
|
||||
<wallet-selector
|
||||
wallet-selector-title="walletSelectorTitle"
|
||||
wallet-selector-wallets="bchWallets"
|
||||
wallet-selector-selected-wallet="bchWallet"
|
||||
wallet-selector-show="showBchWallets"
|
||||
wallet-selector-on-select="onBchWalletSelect">
|
||||
</wallet-selector>
|
||||
</ion-view>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue