Merge pull request #3290 from javierbitpay/ref/paperWallet

Ref/paper wallet
This commit is contained in:
Gustavo Maximiliano Cortez 2015-10-08 09:43:05 -03:00
commit ca100dc983
2 changed files with 97 additions and 92 deletions

View file

@ -25,23 +25,25 @@
<div ng-show="!paperWallet.balance" class="row"> <div ng-show="!paperWallet.balance" class="row">
<div class="large-12 medium-12 columns"> <div class="large-12 medium-12 columns">
<div class="input"> <div class="input">
<label for="privateKey" translate>Paper Wallet Private Key</label> <label for="inputData" translate>Paper Wallet Private Key</label>
<input type="text" placeholder="{{'Paste your paper wallet private key here'|translate}}" ng-model="privateKey" id="privateKey"> <input type="text" placeholder="{{'Paste your paper wallet private key here'|translate}}" ng-model="inputData" id="inputData" ng-change="paperWallet.onData(inputData)">
<div class="qr-scanner-input"> <div class="qr-scanner-input">
<qr-scanner on-scan="paperWallet.onQrCodeScanned(data)"></qr-scanner> <qr-scanner on-scan="paperWallet.onQrCodeScanned(data)"></qr-scanner>
</div> </div>
<label for="passphrase"> <div ng-show="paperWallet.isPkEncrypted">
<span translate>Passphrase (if you have one)</span> <label for="passphrase">
</label> <span translate>Passphrase</span>
<input id="passphrase" type="password" name="passphrase" placeholder="{{'Passphrase'|translate}}" ng-model="passphrase"> </label>
<p ng-show="index.isCordova" translate class="size-12 text-gray"> <input id="passphrase" type="password" name="passphrase" placeholder="{{'Passphrase'|translate}}" ng-model="passphrase">
Decrypting a paper wallet could take around 5 minutes on this device. please be patient and keep the app open. <p ng-show="index.isCordova" translate class="size-12 text-gray">
</p> Decrypting a paper wallet could take around 5 minutes on this device. please be patient and keep the app open.
</p>
</div>
<button <button
ng-disabled="paperWallet.scanning || !privateKey" ng-disabled="paperWallet.scanning || !paperWallet.scannedKey"
ng-style="{'background-color':index.backgroundColor}" ng-style="{'background-color':index.backgroundColor}"
class="button black round expand" class="button black round expand"
ng-click="paperWallet.createTx(privateKey, passphrase)" ng-click="paperWallet.scanFunds()"
translate>Scan Wallet Funds translate>Scan Wallet Funds
</button> </button>
</div> </div>
@ -57,10 +59,10 @@
</div> </div>
<button <button
ng-disabled="paperWallet.sending" ng-disabled="paperWallet.sending || paperWallet.balanceSat <= 0"
ng-style="{'background-color':index.backgroundColor}" ng-style="{'background-color':index.backgroundColor}"
class="button black round expand" class="button black round expand"
ng-click="paperWallet.transaction()" ng-click="paperWallet.sweepWallet()"
translate>Sweep Wallet translate>Sweep Wallet
</button> </button>
<div class="text-center"> <div class="text-center">

View file

@ -1,110 +1,113 @@
angular.module('copayApp.controllers').controller('paperWalletController', angular.module('copayApp.controllers').controller('paperWalletController',
function($scope, $http, $timeout, configService, profileService, go, addressService, bitcore) { function($scope, $http, $timeout, $log, configService, profileService, go, addressService, txStatus, bitcore) {
self = this; self = this;
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
var rawTx; var rawTx;
self.onQrCodeScanned = function(data) { self.onQrCodeScanned = function(data) {
$scope.privateKey = data; $scope.inputData = data;
self.onData(data);
} }
self.createTx = function(privateKey, passphrase) { self.onData = function(data) {
if (privateKey.charAt(0) != '6') { self.error = '';
var isValidKey = self.checkPrivateKey(privateKey); self.scannedKey = data;
self.isPkEncrypted = (data.charAt(0) == '6');
}
if (!isValidKey) return; self._scanFunds = function(cb) {
function getPrivateKey(scannedKey, isPkEncrypted, passphrase, cb) {
if (!isPkEncrypted) return cb(null, scannedKey);
fc.decryptBIP38PrivateKey(scannedKey, passphrase, null, cb);
};
function getBalance(privateKey, cb) {
fc.getBalanceFromPrivateKey(privateKey, cb);
};
function checkPrivateKey(privateKey) {
try {
new bitcore.PrivateKey(privateKey, 'livenet');
} catch (err) {
return false;
}
return true;
} }
var config = configService.getSync().wallet.settings; getPrivateKey(self.scannedKey, self.isPkEncrypted, $scope.passphrase, function(err, privateKey) {
self.error = null; if (err) return cb(err);
self.scanning = true; if (!checkPrivateKey(privateKey)) return cb(new Error('Invalid private key'));
$timeout(function() {
self.getRawTx(privateKey, passphrase, function(err, rawtx, utxos) {
self.scanning = false;
if (err) getBalance(privateKey, function(err, balance) {
self.error = err.toString(); if (err) return cb(err);
else { return cb(null, privateKey, balance);
self.balance = profileService.formatAmount(utxos) + ' ' + config.unitName; });
rawTx = rawtx; });
}
self.scanFunds = function() {
self.scanning = true;
self.privateKey = '';
self.balanceSat = 0;
self.error = '';
$timeout(function() {
self._scanFunds(function(err, privateKey, balance) {
self.scanning = false;
if (err) {
$log.error(err);
self.error = err.message || err.toString();
} else {
self.privateKey = privateKey;
self.balanceSat = balance;
var config = configService.getSync().wallet.settings;
self.balance = profileService.formatAmount(balance) + ' ' + config.unitName;
} }
$timeout(function() { $scope.$apply();
$scope.$apply();
}, 1);
}); });
}, 100); }, 100);
};
self.checkPrivateKey = function(privateKey) {
try {
new bitcore.PrivateKey(privateKey, 'livenet');
} catch (err) {
self.error = err.toString();
return false;
}
return true;
} }
self.getRawTx = function(privateKey, passphrase, cb) { self._sweepWallet = function(cb) {
if (privateKey.charAt(0) == 6) { addressService.getAddress(fc.credentials.walletId, true, function(err, destinationAddress) {
fc.decryptBIP38PrivateKey(privateKey, passphrase, null, function(err, privateKey) { if (err) return cb(err);
fc.buildTxFromPrivateKey(self.privateKey, destinationAddress, null, function(err, tx) {
if (err) return cb(err); if (err) return cb(err);
fc.getBalanceFromPrivateKey(privateKey, function(err, utxos) { fc.broadcastRawTx({
rawTx: tx.serialize(),
network: 'livenet'
}, function(err, txid) {
if (err) return cb(err); if (err) return cb(err);
return cb(null, destinationAddress, txid);
addressService.getAddress(fc.credentials.walletId, true, function(err, destinationAddress) {
if (err) return cb(err);
fc.buildTxFromPrivateKey(privateKey, destinationAddress, null, function(err, tx) {
if (err) return cb(err);
return cb(null, tx.serialize(), utxos);
});
});
}); });
}); });
} else {
fc.getBalanceFromPrivateKey(privateKey, function(err, utxos) {
if (err) return cb(err)
addressService.getAddress(fc.credentials.walletId, true, function(err, destinationAddress) {
if (err) return cb(err);
fc.buildTxFromPrivateKey(privateKey, destinationAddress, null, function(err, tx) {
if (err) return cb(err);
return cb(null, tx.serialize(), utxos);
});
});
});
}
};
self.transaction = function() {
self.error = null;
self.sending = true;
$timeout(function() {
self.doTransaction(rawTx).then(function(err, response) {
self.sending = false;
self.goHome();
},
function(err) {
self.sending = false;
self.error = err.toString();
$timeout(function() {
$scope.$apply();
}, 1);
});
}, 100);
};
self.goHome = function() {
go.walletHome();
};
self.doTransaction = function(rawTx) {
return $http.post('https://insight.bitpay.com/api/tx/send', {
rawtx: rawTx
}); });
}; };
self.sweepWallet = function() {
self.sending = true;
self.error = '';
$timeout(function() {
self._sweepWallet(function(err, destinationAddress, txid) {
self.sending = false;
if (err) {
self.error = err.message || err.toString();
$log.error(err);
} else {
txStatus.notify({
status: 'broadcasted'
}, function() {
go.walletHome();
});
}
$scope.$apply();
});
}, 100);
}
}); });