From 80e22c59a03160d54813b9012b39b7de74a0becb Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 8 Dec 2014 19:31:15 -0300 Subject: [PATCH] bitcoin: working. paypro todo --- TODO | 2 +- js/controllers/home.js | 2 +- js/controllers/send.js | 120 +++++++++++++++++++++-------------------- views/send.html | 10 ++-- 4 files changed, 68 insertions(+), 66 deletions(-) diff --git a/TODO b/TODO index b47a45bfe..2d468a2e6 100644 --- a/TODO +++ b/TODO @@ -1,2 +1,2 @@ - join. on walletComplete! - +- paypro fetch-> return TIMEOUT diff --git a/js/controllers/home.js b/js/controllers/home.js index 8048343ec..2807a6570 100644 --- a/js/controllers/home.js +++ b/js/controllers/home.js @@ -5,7 +5,7 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc // Global go. This should be in a better place TODO // We dont do a 'go' directive, to use the benefits of ng-touch with ng-click $rootScope.go = function (path) { - go.go(path); + go.path(path); }; var _credentials, _firstpin; diff --git a/js/controllers/send.js b/js/controllers/send.js index b45130788..28badc215 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -30,12 +30,7 @@ angular.module('copayApp.controllers').controller('SendController', $scope.isMobile = isMobile.any(); if ($rootScope.pendingPayment) { - var value; - var pp = $rootScope.pendingPayment; - var amount = (pp.data && pp.data.amount) ? - pp.data.amount * 100000000 * satToUnit : 0; - $scope.setForm(pp.address, amount, pp.data.message) - _onAddressChange(pp); + $scope.setFromUri($rootScope.pendingPayment) } $scope.setInputs(); @@ -95,8 +90,7 @@ angular.module('copayApp.controllers').controller('SendController', return this._address; }, set: function(newValue) { - this._address = newValue; - _onAddressChange(); + this._address = $scope.onAddressChange(newValue); }, enumerable: true, configurable: true @@ -104,10 +98,10 @@ angular.module('copayApp.controllers').controller('SendController', }; $scope.setScanner = function() { - navigator.getUserMedia = navigator.getUserMedia || - navigator.webkitGetUserMedia || navigator.mozGetUserMedia || + navigator.getUserMedia = navigator.getUserMedia || + navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; - window.URL = window.URL || window.webkitURL || + window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL; if (!window.cordova && !navigator.getUserMedia) @@ -343,16 +337,21 @@ angular.module('copayApp.controllers').controller('SendController', $scope.setForm = function(to, amount, comment) { var form = $scope.sendForm; - form.address.$setViewValue(merchantData.domain); + form.address.$setViewValue(to); form.address.$render(); form.address.$isValid = true; + $scope.lockAddress = true; - form.amount.$setViewValue(merchantData.unitTotal); - form.amount.$render(); - form.amount.$isValid = true; + if (amount) { + form.amount.$setViewValue(amount); + form.amount.$render(); + form.amount.$isValid = true; + $scope.lockAmount = true; + } - if (comment) - $scope.commentText = comment; + if (comment) { + form.commentText.$setViewValue(comment); + } }; $scope.cancelSend = function(error) { @@ -388,50 +387,18 @@ angular.module('copayApp.controllers').controller('SendController', }; + $scope.setFromPayPro = function(uri) { + console.log('[send.js.391:uri:]', uri); //TODO - var _onAddressChange = function(pp) { - var value; - value = value || $scope.address || ''; - var uri; - - $scope.error = $scope.success = null; - - if (value.indexOf('bitcoin:') === 0) { - uri = new bitcore.BIP21(value); - } else if (/^https?:\/\//.test(value)) { - uri = { - data: { - merchant: value - } - }; - } - - if (!uri || !uri.data.merchant) { - if (uri && uri.address) { - var amount = (uri.data && uri.data.amount) ? uri.data.amount * 100000000 * satToUnit : 0; - var address = uri.address.data; - if (amount && address) { - $scope.isPayUri = {fixedAmount: true} ; - } - $timeout(function() { - $scope.amount = amount; - }, 1000); - $scope.commentText = uri.data.message; - $scope.address = address; - } - return; - } - - $scope.fetchingURL = uri.data.merchant; + $scope.fetchingURL = uri; $scope.loading = true; - var balance = w.balanceInfo.availableBalance; var available = +(balance * unitToSat).toFixed(0); // Payment Protocol URI (BIP-72) - $scope.wallet.fetchPaymentRequest({ - url: uri.data.merchant + w.fetchPaymentRequest({ + url: uri }, function(err, merchantData) { $scope.loading = false; $scope.fetchingURL = null; @@ -442,18 +409,53 @@ angular.module('copayApp.controllers').controller('SendController', } else { $scope.cancelSend(err.toString()); } - } else if (merchantData && available < +merchantData.total) { - $scope.cancelSend(err.toString('Insufficient funds')); + $scope.cancelSend('Insufficient funds'); } else { $scope.setForm(merchantData.domain, merchantData.unitTotal) } - $timeout(function() { - $scope.$digest(); - }, 1); }); }; + $scope.setFromUri = function(uri) { + var form = $scope.sendForm; + + var parsed = new bitcore.BIP21(uri); + if (!parsed.isValid() || !parsed.address.isValid()) { + $scope.error = 'Invalid bitcoin URL'; + form.address.$isValid = false; + return uri; + }; + + var addr = parsed.address.toString(); + +console.log('[send.js.430:parsed:]',addr,parsed.data); //TODO + + if (parsed.data.merchant) + return $scope.setFromPayPro(parsed.data.merchant); + + var amount = (parsed.data && parsed.data.amount) ? + parsed.data.amount * 100000000 * satToUnit : 0; + + $scope.setForm(addr, amount, parsed.data.message, true); + return addr; + }; + + $scope.onAddressChange = function(value) { + var addr; + console.log('[send.js.391:value:]', value); //TODO + + $scope.error = $scope.success = null; + if (!value) return ''; + + if (value.indexOf('bitcoin:') === 0) { + return $scope.setFromUri(value); + } else if (/^https?:\/\//.test(value)) { + return $scope.setFromPayPro(value); + } + return value; + }; + $scope.openAddressBook = function() { var modalInstance = $modal.open({ templateUrl: 'views/modals/address-book.html', diff --git a/views/send.html b/views/send.html index e5db67f6d..8489b35ee 100644 --- a/views/send.html +++ b/views/send.html @@ -48,7 +48,7 @@
-
@@ -87,7 +87,7 @@
@@ -112,7 +112,7 @@
@@ -173,10 +173,10 @@