Wallet/src/js/controllers/tab-send.js

217 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-08-12 12:44:16 -03:00
'use strict';
2016-08-24 18:06:17 -03:00
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $ionicModal, $log, $timeout, addressbookService, profileService, lodash, $state, walletService, bitcore) {
2016-08-12 15:11:52 -03:00
2016-08-18 19:25:30 -03:00
var originalList;
2016-08-16 18:38:18 -03:00
2016-08-12 15:11:52 -03:00
$scope.init = function() {
2016-08-18 19:25:30 -03:00
originalList = [];
2016-08-16 18:38:18 -03:00
var wallets = profileService.getWallets({onlyComplete: true});
2016-08-15 18:11:36 -03:00
lodash.each(wallets, function(v) {
originalList.push({
color: v.color,
label: v.name,
isWallet: true,
2016-08-16 18:38:18 -03:00
getAddress: function(cb) {
walletService.getAddress(v, false, cb);
},
2016-08-15 18:11:36 -03:00
});
2016-08-12 15:11:52 -03:00
});
2016-08-15 18:11:36 -03:00
addressbookService.list(function(err, ab) {
if (err) $log.error(err);
2016-08-12 15:11:52 -03:00
2016-08-18 19:25:30 -03:00
$scope.isEmptyList = lodash.isEmpty(ab);
2016-08-15 18:11:36 -03:00
var contacts = [];
lodash.each(ab, function(v, k) {
contacts.push({
2016-08-18 19:25:30 -03:00
label: v,
address: k,
2016-08-16 18:38:18 -03:00
getAddress: function(cb) {
2016-08-18 19:25:30 -03:00
return cb(null,k);
2016-08-16 18:38:18 -03:00
},
2016-08-15 18:11:36 -03:00
});
});
2016-08-12 15:11:52 -03:00
2016-08-15 18:11:36 -03:00
originalList = originalList.concat(contacts);
$scope.list = lodash.clone(originalList);
});
2016-08-12 16:30:50 -03:00
};
2016-08-24 18:06:17 -03:00
var setFromUri = function(uri) {
function sanitizeUri(uri) {
// Fixes when a region uses comma to separate decimals
var regex = /[\?\&]amount=(\d+([\,\.]\d+)?)/i;
var match = regex.exec(uri);
if (!match || match.length === 0) {
return uri;
}
var value = match[0].replace(',', '.');
var newUri = uri.replace(regex, value);
return newUri;
};
var satToUnit = 1 / this.unitToSatoshi;
// URI extensions for Payment Protocol with non-backwards-compatible request
if ((/^bitcoin:\?r=[\w+]/).exec(uri)) {
uri = decodeURIComponent(uri.replace('bitcoin:?r=', ''));
setFromPayPro(uri, function(err) {
if (err) {
return err;
}
});
} else {
uri = sanitizeUri(uri);
if (!bitcore.URI.isValid(uri)) {
return uri;
}
var parsed = new bitcore.URI(uri);
var addr = parsed.address ? parsed.address.toString() : '';
var message = parsed.message;
var amount = parsed.amount ?
(parsed.amount.toFixed(0) * satToUnit).toFixed(this.unitDecimals) : 0;
if (parsed.r) {
setFromPayPro(parsed.r, function(err) {
if (err && addr && amount) {
// TODO
$state.go('confirm', {toAmount: amount, toAddress: addr, message:message})
return addr;
}
});
} else {
//
$state.go('confirm', {toAmount: amount, toAddress: addr, message:message})
return addr;
}
}
};
2016-08-18 19:25:30 -03:00
$scope.findContact = function(search, opts) {
opts = opts || {};
2016-08-15 18:11:36 -03:00
2016-08-24 18:06:17 -03:00
if (search.indexOf('bitcoin:') === 0) {
return setFromUri(search);
} else if (/^https?:\/\//.test(search)) {
return setFromPayPro(search);
}
2016-08-18 19:25:30 -03:00
if (!search || search.length < 2) {
2016-08-15 18:11:36 -03:00
$scope.list = originalList;
$timeout(function() {
$scope.$apply();
2016-08-16 18:38:18 -03:00
}, 10);
2016-08-15 18:11:36 -03:00
return;
}
2016-08-12 16:30:50 -03:00
var result = lodash.filter($scope.list, function(item) {
2016-08-18 19:25:30 -03:00
if (opts && opts.onlyContacts && item.isWallet) return;
2016-08-12 16:30:50 -03:00
var val = item.label || item.alias || item.name;
2016-08-18 19:25:30 -03:00
return lodash.includes(val.toLowerCase(), search.toLowerCase());
2016-08-12 16:30:50 -03:00
});
2016-08-12 16:30:50 -03:00
$scope.list = result;
2016-08-12 15:11:52 -03:00
};
2016-08-16 18:38:18 -03:00
$scope.goToAmount = function(item) {
item.getAddress(function(err,addr){
if (err|| !addr) {
$log.error(err);
return;
}
$log.debug('Got toAddress:' + addr + ' | ' + item.label)
return $state.transitionTo('send.amount', { toAddress: addr, toName: item.label})
2016-08-12 12:44:16 -03:00
});
};
2016-08-18 19:25:30 -03:00
/*
* Modal Addressbook
*/
$ionicModal.fromTemplateUrl('views/modals/addressbook.html', {
scope: $scope
}).then(function(modal) {
$scope.addressbookModal = modal;
});
$scope.openAddressbookModal = function() {
$scope.addressbookModal.show();
};
$scope.closeAddressbookModal = function() {
$scope.cleanAddressbookEntry();
$scope.addAddressbookEntry = false;
$scope.addressbookModal.hide();
};
$scope.onQrCodeScanned = function(data, addressbookForm) {
$timeout(function() {
var form = addressbookForm;
if (data && form) {
data = data.replace('bitcoin:', '');
form.address.$setViewValue(data);
form.address.$isValid = true;
form.address.$render();
}
$scope.$digest();
}, 100);
};
$scope.cleanAddressbookEntry = function() {
$scope.addressbookEntry = {
'address': '',
'label': ''
};
};
$scope.toggleAddAddressbookEntry = function() {
$scope.cleanAddressbookEntry();
$scope.addAddressbookEntry = !$scope.addAddressbookEntry;
};
$scope.add = function(addressbook) {
$timeout(function() {
addressbookService.add(addressbook, function(err, ab) {
if (err) {
$log.error(err);
return;
}
$scope.init();
$scope.toggleAddAddressbookEntry();
$scope.$digest();
});
}, 100);
};
$scope.remove = function(addr) {
$timeout(function() {
addressbookService.remove(addr, function(err, ab) {
if (err) {
$scope.error = err;
return;
}
$scope.init();
$scope.$digest();
});
}, 100);
};
$scope.$on('$destroy', function() {
$scope.addressbookModal.remove();
});
2016-08-24 18:06:17 -03:00
$scope.$watch('')
2016-08-18 19:25:30 -03:00
2016-08-12 12:44:16 -03:00
});