Skip to content

Add support for custom server addresses #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 25 additions & 3 deletions app/scripts/controllers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions app/scripts/providers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '/';
Expand Down
152 changes: 103 additions & 49 deletions app/scripts/services/lightning.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand All @@ -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);
};
});
79 changes: 66 additions & 13 deletions app/scripts/services/resourcesgenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
Loading