Wallet/src/js/controllers/paperWallet.js

80 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-10-02 11:27:17 -03:00
angular.module('copayApp.controllers').controller('paperWalletController',
2015-10-02 11:58:20 -03:00
function($scope, $http, $timeout, profileService, go, addressService) {
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) self.error = "Enter privateKey or scann for one";
2015-10-02 11:58:20 -03:00
self.getRawTx(privateKey, passphrase, function(err, rawtx, utxos) {
2015-10-02 11:27:17 -03:00
if (err) self.error = err.toString();
else {
self.balance = (utxos / 1e8).toFixed(8);
2015-10-02 11:58:20 -03:00
rawTx = rawtx;
2015-10-02 11:27:17 -03:00
}
2015-10-02 11:58:20 -03:00
$timeout(function() {
$scope.$apply();
}, 1);
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 11:58:20 -03:00
self.doTransaction(rawTx).then(function(err, response) {
self.goHome();
2015-10-02 11:27:17 -03:00
},
function(err) {
self.error = err;
});
};
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
});
};
});