diff --git a/app/scripts/app.js b/app/scripts/app.js index 80e4abc..14dbd30 100644 --- a/app/scripts/app.js +++ b/app/scripts/app.js @@ -59,7 +59,7 @@ angular }); }) .config(function (ConfigProvider) { - ConfigProvider.serverHost = 'http://127.0.0.1:9000/api'; + ConfigProvider.serverHost = window.localStorage.serverHost; }) .run(function ($rootScope, SettingsService) { $rootScope.SettingsService = SettingsService; diff --git a/app/scripts/controllers/settings.js b/app/scripts/controllers/settings.js index cdfcd7a..187be74 100644 --- a/app/scripts/controllers/settings.js +++ b/app/scripts/controllers/settings.js @@ -8,14 +8,36 @@ * Controller of the App */ angular.module('App') - .controller('SettingsCtrl', function ($scope, $rootScope, $location, $mdToast, SettingsService) { + .controller('SettingsCtrl', function ($scope, $rootScope, $location, $mdDialog, $mdToast, SettingsService) { $rootScope.$emit('page-title', 'BHB ⚡️ Settings'); - $scope.loading = false; - $scope.currentSettings = SettingsService.getAllSettings(); + $scope.loading = true; + SettingsService.getAllSettings() + .then(function (settings) { + $scope.currentSettings = settings; + }) + .finally(function () { + $scope.loading = false; + }); $scope.validUnits = ['XBT', 'SAT', 'mSAT']; + $scope.disconnectFromServer = function (ev) { + var confirm = $mdDialog.confirm() + .title('Do you really want to disconnect?') + .textContent('You will be asked for a new lightning-ui server address the next time you\'ll come back') + .ariaLabel('Confirm disconnection') + .targetEvent(ev) + .ok('Disconnect') + .cancel('Cancel'); + + $mdDialog.show(confirm) + .then(function () { + window.localStorage.removeItem('serverHost'); + window.location.reload(); + }); + }; + $scope.save = function () { $scope.loading = true; SettingsService.updateSettings($scope.currentSettings) diff --git a/app/scripts/providers/config.js b/app/scripts/providers/config.js index b5fc41b..1a80a09 100644 --- a/app/scripts/providers/config.js +++ b/app/scripts/providers/config.js @@ -11,14 +11,12 @@ angular.module('App') .provider('Config', function () { this.serverHost = null; - var exceptionStringValue = 'Uninitialized config server values'; - this.$get = function () { var obj = this; return { getServerPath: function () { if (!obj.serverHost) { - throw exceptionStringValue; + return false; } return obj.serverHost + '/'; diff --git a/app/scripts/services/lightning.js b/app/scripts/services/lightning.js index d4dd81d..b7ed892 100644 --- a/app/scripts/services/lightning.js +++ b/app/scripts/services/lightning.js @@ -12,100 +12,145 @@ angular.module('App') var _self = this; this.getInfo = function () { - return ResourcesGeneratorService.getResource('lightning/getinfo').get().$promise + return ResourcesGeneratorService.getResource('lightning/getinfo') + .then(function (resource) { + return resource.get().$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.listPeers = function () { - return ResourcesGeneratorService.getResource('lightning/listpeers').get().$promise + return ResourcesGeneratorService.getResource('lightning/listpeers') + .then(function (resource) { + return resource.get().$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.listNodes = function () { - return ResourcesGeneratorService.getResource('lightning/listnodes').get().$promise + return ResourcesGeneratorService.getResource('lightning/listnodes') + .then(function (resource) { + return resource.get().$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.listChannels = function () { - return ResourcesGeneratorService.getResource('lightning/listchannels').get().$promise + return ResourcesGeneratorService.getResource('lightning/listchannels') + .then(function (resource) { + return resource.get().$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.getNewAddress = function () { - return ResourcesGeneratorService.getResource('lightning/getnewaddress').get().$promise + return ResourcesGeneratorService.getResource('lightning/getnewaddress') + .then(function (resource) { + return resource.get().$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.openChannel = function (hostname, port, nodeid, amount) { - return ResourcesGeneratorService.getResource('lightning/openchannel').post({ - ip: hostname, - port: port, - nodeid: nodeid, - amount: amount - }).$promise + return ResourcesGeneratorService.getResource('lightning/openchannel') + .then(function (resource) { + return resource.post({ + ip: hostname, + port: port, + nodeid: nodeid, + amount: amount + }).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.connect = function (hostname, port, nodeid) { - return ResourcesGeneratorService.getResource('lightning/connect').post({ - ip: hostname, - port: port, - nodeid: nodeid - }).$promise + return ResourcesGeneratorService.getResource('lightning/connect') + .then(function (resource) { + return resource.post({ + ip: hostname, + port: port, + nodeid: nodeid + }).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.addFunds = function (rawtx) { - return ResourcesGeneratorService.getResource('lightning/addfunds').post({rawtx: rawtx}).$promise + return ResourcesGeneratorService.getResource('lightning/addfunds') + .then(function (resource) { + return resource.post({rawtx: rawtx}).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.fundChannel = function (nodeid, amount) { - return ResourcesGeneratorService.getResource('lightning/fundchannel').post({ - nodeid: nodeid, - amount: amount - }).$promise + return ResourcesGeneratorService.getResource('lightning/fundchannel') + .then(function (resource) { + return resource.post({ + nodeid: nodeid, + amount: amount + }).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.closeChannel = function (nodeid) { - return ResourcesGeneratorService.getResource('lightning/closechannel').post({nodeid: nodeid}).$promise + return ResourcesGeneratorService.getResource('lightning/closechannel') + .then(function (resource) { + return resource.post({nodeid: nodeid}).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.getRoute = function (nodeid, amount, riskFactor) { - return ResourcesGeneratorService.getResource('lightning/getroute').post({ - nodeid: nodeid, - amount: amount, - riskFactor: riskFactor - }).$promise + return ResourcesGeneratorService.getResource('lightning/getroute') + .then(function (resource) { + return resource.post({ + nodeid: nodeid, + amount: amount, + riskFactor: riskFactor + }).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.createInvoice = function (amount, label) { - return ResourcesGeneratorService.getResource('lightning/createinvoice').post({ - amount: amount, - label: label - }).$promise + return ResourcesGeneratorService.getResource('lightning/createinvoice') + .then(function (resource) { + return resource.post({ + amount: amount, + label: label + }).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.withdraw = function (amount, address) { console.log(amount, address); - return ResourcesGeneratorService.getResource('lightning/withdraw').post({ - amount: amount, - address: address - }).$promise + return ResourcesGeneratorService.getResource('lightning/withdraw') + .then(function (resource) { + return resource.post({ + amount: amount, + address: address + }).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.listInvoices = function () { - return ResourcesGeneratorService.getResource('lightning/listinvoices').get().$promise + return ResourcesGeneratorService.getResource('lightning/listinvoices') + .then(function (resource) { + return resource.get().$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.listFunds = function () { - return ResourcesGeneratorService.getResource('lightning/listfunds').get().$promise + return ResourcesGeneratorService.getResource('lightning/listfunds') + .then(function (resource) { + return resource.get().$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; @@ -123,26 +168,35 @@ angular.module('App') }; this.deleteInvoice = function (label) { - return ResourcesGeneratorService.getResource('lightning/invoice/:label').delete({label: label}).$promise + return ResourcesGeneratorService.getResource('lightning/invoice/:label') + .then(function (resource) { + return resource.delete({label: label}).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.sendPay = function (route, rhash) { - return ResourcesGeneratorService.getResource('lightning/sendpay').post({ - route: route, - hash: rhash - }).$promise + return ResourcesGeneratorService.getResource('lightning/sendpay') + .then(function (resource) { + return resource.post({ + route: route, + hash: rhash + }).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; this.pay = function (payreq, msatoshi, description, riskfactor, maxfeepercent) { - return ResourcesGeneratorService.getResource('lightning/pay').post({ - payreq: payreq, - msatoshi: msatoshi, - description: description, - riskfactor: riskfactor, - maxfeepercent: maxfeepercent - }).$promise + return ResourcesGeneratorService.getResource('lightning/pay') + .then(function (resource) { + return resource.post({ + payreq: payreq, + msatoshi: msatoshi, + description: description, + riskfactor: riskfactor, + maxfeepercent: maxfeepercent + }).$promise; + }) .then(ResourcesGeneratorService.successHandler, ResourcesGeneratorService.failureHandler); }; }); diff --git a/app/scripts/services/resourcesgenerator.js b/app/scripts/services/resourcesgenerator.js index 74c7557..63efcf1 100644 --- a/app/scripts/services/resourcesgenerator.js +++ b/app/scripts/services/resourcesgenerator.js @@ -12,20 +12,73 @@ angular.module('App') var DEFAULT_ERROR_MESSAGE = 'An unexpected error occurred, please try again later'; var lastErrorPopup = $q.resolve(); + var getServerPathPromise = $q.resolve(Config.getServerPath()); + if (!Config.getServerPath()) { + var prompt = $mdDialog.prompt() + .title('Select a lightning-ui server') + .textContent('Don\'t forget the trailing \'/api\'') + .placeholder('https://my.local.node/api') + .ariaLabel('Server URL') + .required(true) + .ok('OK'); + + var tempServerHost = ''; + + var checkForHTTPS = function (result) { + tempServerHost = result; + + var confirm = $q.resolve(); + if (!result.startsWith('https://')) { + var confirmDialog = $mdDialog.confirm() + .title('You are not using https') + .textContent('This setup is not safe - you could loose funds. ' + + 'Do you want to proceed anyways?') + .ariaLabel('Confirm unsecure HTTP') + .ok('Proceed') + .cancel('Cancel'); + + confirm = $mdDialog.show(confirmDialog); + } + + return confirm; + }; + + var saveServerHost = function () { + // save the result in localStorage + window.localStorage.setItem('serverHost', tempServerHost); + return tempServerHost; + }; + + var catchErrors = function () { + return $mdDialog.show(prompt).then(checkForHTTPS).then(saveServerHost) + .catch(catchErrors); + }; + + getServerPathPromise = $mdDialog.show(prompt).then(checkForHTTPS).then(saveServerHost) + .catch(catchErrors); + } + this.getResource = function (path) { - return $resource(Config.getServerPath() + path, {}, { - 'get': {method: 'GET'}, - 'save': {method: 'POST'}, - 'post': {method: 'POST'}, - 'put': { - method: 'PUT', params: { - /* paramName: '@paramName' */ + return getServerPathPromise + .then(function (serverPath) { + if (serverPath[serverPath.length - 1] !== '/') { + serverPath += '/'; // append final slash } - }, - 'query': {method: 'GET', isArray: true}, - 'remove': {method: 'DELETE'}, - 'delete': {method: 'DELETE'} - }); + + return $resource(serverPath + path, {}, { + 'get': {method: 'GET'}, + 'save': {method: 'POST'}, + 'post': {method: 'POST'}, + 'put': { + method: 'PUT', params: { + /* paramName: '@paramName' */ + } + }, + 'query': {method: 'GET', isArray: true}, + 'remove': {method: 'DELETE'}, + 'delete': {method: 'DELETE'} + }); + }); }; this.successHandler = function (response) { @@ -36,7 +89,7 @@ angular.module('App') $rootScope.$emit('loading-stop'); var errorString = null; - if (errorResponse && errorResponse.data && errorResponse.data.error && errorResponse.data.error.message && typeof errorResponse.data.error.message === typeof ('string') ) { + if (errorResponse && errorResponse.data && errorResponse.data.error && errorResponse.data.error.message && typeof errorResponse.data.error.message === typeof ('string')) { errorString = errorResponse.data.error.message; } diff --git a/app/scripts/services/settings.js b/app/scripts/services/settings.js index 7c16daa..1f5c543 100644 --- a/app/scripts/services/settings.js +++ b/app/scripts/services/settings.js @@ -8,9 +8,11 @@ * Service of the App */ angular.module('App') - .service('SettingsService', function (ResourcesGeneratorService) { + .service('SettingsService', function (ResourcesGeneratorService, $q) { var settings = {}; + var getSettingsPromise = $q.reject({}); + this.get = function (key) { if (!(key in settings)) { return null; @@ -20,20 +22,28 @@ angular.module('App') }; this.getAllSettings = function () { - return settings; + return getSettingsPromise; }; this.getRemoteSettings = function () { - return ResourcesGeneratorService.getResource('settings').get().$promise + getSettingsPromise = ResourcesGeneratorService.getResource('settings') + .then(function (resource) { + return resource.get().$promise; + }) .then(function (response) { settings = response; return settings; }, ResourcesGeneratorService.failureHandler); + + return getSettingsPromise; }; this.updateSettings = function (settingsObject) { - return ResourcesGeneratorService.getResource('settings').put(settingsObject).$promise + return ResourcesGeneratorService.getResource('settings') + .then(function (resource) { + return resource.put(settingsObject).$promise; + }) .then(function () { settings = settingsObject; return settings; diff --git a/app/views/settings.html b/app/views/settings.html index 93ceea4..9041e8d 100644 --- a/app/views/settings.html +++ b/app/views/settings.html @@ -22,7 +22,7 @@ - + Save @@ -30,5 +30,9 @@ class="half-size"> + + + Disconnect from this server +