Wallet/src/js/controllers/paperWallet.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-10-02 11:27:17 -03:00
angular.module('copayApp.controllers').controller('paperWalletController',
function($scope, $http, $timeout, configService, profileService, go, addressService, bitcore) {
2015-10-02 11:27:17 -03:00
self = this;
var fc = profileService.focusedClient;
var rawTx;
self.onQrCodeScanned = function(data) {
$scope.privateKey = data;
}
self.createTx = function(privateKey, passphrase) {
if (privateKey.charAt(0) != '6') {
2015-10-02 16:30:20 -03:00
var isValidKey = self.checkPrivateKey(privateKey);
2015-10-02 15:56:59 -03:00
2015-10-02 16:30:20 -03:00
if (!isValidKey) return;
2015-10-02 15:56:59 -03:00
}
var config = configService.getSync().wallet.settings;
2015-10-02 13:24:43 -03:00
self.error = null;
self.scanning = true;
$timeout(function() {
self.getRawTx(privateKey, passphrase, function(err, rawtx, utxos) {
self.scanning = false;
if (err)
self.error = err.toString();
else {
self.balance = profileService.formatAmount(utxos) + ' ' + config.unitName;
2015-10-02 13:24:43 -03:00
rawTx = rawtx;
}
$timeout(function() {
$scope.$apply();
}, 1);
});
}, 100);
2015-10-02 11:27:17 -03:00
};
2015-10-02 15:56:59 -03:00
self.checkPrivateKey = function(privateKey) {
try {
new bitcore.PrivateKey(privateKey, 'livenet');
} catch (err) {
2015-10-02 16:30:20 -03:00
self.error = err.toString();
return false;
2015-10-02 15:56:59 -03:00
}
return true;
}
2015-10-02 11:27:17 -03:00
self.getRawTx = function(privateKey, passphrase, cb) {
if (privateKey.charAt(0) == 6) {
fc.decryptBIP38PrivateKey(privateKey, passphrase, null, function(err, privateKey) {
if (err) return cb(err);
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);
});
});
});
});
} 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() {
2015-10-02 16:04:11 -03:00
self.error = null;
self.sending = true;
$timeout(function() {
self.doTransaction(rawTx).then(function(err, response) {
2015-10-02 16:30:20 -03:00
self.sending = false;
self.goHome();
},
function(err) {
self.sending = false;
self.error = err.toString();
$timeout(function() {
$scope.$apply();
}, 1);
});
2015-10-02 16:04:11 -03:00
}, 100);
2015-10-02 11:27:17 -03:00
};
2015-10-02 11:58:20 -03:00
self.goHome = function() {
go.walletHome();
};
2015-10-02 11:27:17 -03:00
self.doTransaction = function(rawTx) {
return $http.post('https://insight.bitpay.com/api/tx/send', {
rawtx: rawTx
});
};
2015-10-02 15:23:44 -03:00
});