Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
bnonni committed May 7, 2022
1 parent ee40f17 commit 6d68023
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 187 deletions.
20 changes: 9 additions & 11 deletions backend/api/account/accountController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ const accountService = require('./accountService');

const getAccountAllowance = async (req, res) => {
try {
const response = await accountService.getAccountAllowance();

const username = req.body.username;
const response = await accountService.getAccountAllowance(username);
debug.info(`Account Allowance Response: ${JSON.stringify(response)}`);

if (!response.success) res.status(500).json(response);
else res.status(200).json(response);

Expand All @@ -18,10 +17,10 @@ const getAccountAllowance = async (req, res) => {

const payInvoice = async (req, res) => {
try {
const response = await accountService.getAccountAllowance();

debug.info(`Account Allowance Response: ${JSON.stringify(response)}`);
const invoice = req.body.invoice;
const response = await accountService.payInvoice(invoice);

debug.info(`Pay Invoice Response: ${JSON.stringify(response)}`);
if (!response.success) res.status(500).json(response);
else res.status(200).json(response);

Expand All @@ -33,13 +32,12 @@ const payInvoice = async (req, res) => {

const createInvoice = async (req, res) => {
try {
const response = await accountService.getAccountAllowance();

debug.info(`Account Allowance Response: ${JSON.stringify(response)}`);

const amountMillisats = req.body.amountMillisats;
const description = req.body.description;
const response = await accountService.createInvoice(amountMillisats, description);
debug.info(`Create Invoice Response: ${JSON.stringify(response)}`);
if (!response.success) res.status(500).json(response);
else res.status(200).json(response);

} catch (error) {
debug.error(error.stack);
res.status(500).json({ message: error.message, error: error.stack });
Expand Down
34 changes: 13 additions & 21 deletions backend/api/account/accountService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,41 @@ const accounts = require('../../db/collection');
const getAccountAllowance = async (username) => {
try {
// get allowance from firestore
return { success: true, message: charge };
return { success: true, message: nodes };
} catch (error) {
debug.error(error.stack, error.status, error.message);
debug.error(error.stack);
throw new Error(error);
}
};

const getAccountBalance = async (username) => {
const getAccountBalance = async () => {
try {

return { success: true, message: charge };
const response = await senseiNodes.getBalance();
return { success: true, message: response.balance_satoshis };
} catch (error) {
debug.error(error.stack, error.status, error.message);
debug.error(error.stack);
throw new Error(error);
}
};

const payInvoice = async (req, res) => {
const payInvoice = async (invoice) => {
try {
const invoice = req.body.invoice;
const response = await senseiNodes.payInvoice(invoice);
debug.info(`Pay Invoice Response: ${JSON.stringify(response)}`);
if (!response.success) res.status(500).json(response);
else res.status(200).json(response);
return { success: true, message: response };
} catch (error) {
debug.error(error.stack);
res.status(500).json({ message: error.message, error: error.stack });
throw new Error(error);
}
};

const createInvoice = async (req, res) => {
const createInvoice = async (amountMillisats, description) => {
try {
const amountMillisats = req.body.amountMillisats;
const description = req.body.description;
const response = await senseiNodes.createInvoice(amountMillisats, description);
debug.info(`Create Invoice Response: ${JSON.stringify(response)}`);
if (!response.success) res.status(500).json(response);
else res.status(200).json(response);

return { success: true, message: response };
} catch (error) {
debug.error(error.stack);
res.status(500).json({ message: error.message, error: error.stack });
throw new Error(error);
}
};

module.exports = { getAccountAllowance, payInvoice, createInvoice };
module.exports = { getAccountAllowance, payInvoice, createInvoice, getAccountBalance };
2 changes: 1 addition & 1 deletion backend/api/admin/adminService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const getAllBalances = async (req, res) => {
const response = await senseiAdmin.listNodes();
let nodes = [];
for (let node of response.nodes) {
node.balance = (await senseiNodes.getBalance(node.username)).balance_satoshis;
node.balance = (await senseiNodes.getBalance()).balance_satoshis;
nodes.push(node);
}
return { success: true, message: nodes };
Expand Down
176 changes: 22 additions & 154 deletions backend/sensei/admin.js
Original file line number Diff line number Diff line change
@@ -1,205 +1,73 @@
const fetch = require('node-fetch');
const BASE_URL = process.env.BASE_URL;
const MACAROON = process.env.MACAROON;
const TOKEN = process.env.TOKEN;
const { apiCall } = require('../utils/apiCall');

const initSensei = async (username, passphrase, alias, electrum_url, start) => {
const res = await fetch(`${BASE_URL}/v1/init`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
username,
passphrase,
alias,
electrum_url,
start,
},
const res = await apiCall('/v1/init', 'POST', {
username,
passphrase,
alias,
electrum_url,
start,
});

console.log(res);
return await res.json();
};

const listNodes = async (page, take, query) => {
const res = await fetch(`${BASE_URL}/v1/nodes?page=${0}&take=${10}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
}
});

console.log(res);
const res = await apiCall('/v1/nodes?page=${0}&take=${10}', 'GET');
return await res.json();
};

const createNode = async (username, passphrase, alias, start) => {
const res = await fetch(`${BASE_URL}/v1/nodes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
username,
passphrase,
alias,
start,
},
const res = await apiCall('/v1/nodes', 'POST', {
username,
passphrase,
alias,
start,
});

console.log(res);
return await res.json();
};

const startNode = async (pubkey, passphrase) => {
const res = await fetch(`${BASE_URL}/v1/nodes/start`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
pubkey,
passphrase,
},
});

console.log(res);
const res = await apiCall('/v1/nodes/start', 'POST', { pubkey, passphrase });
return await res.json();
};

const stopNode = async (pubkey) => {
const res = await fetch(`${BASE_URL}/v1/nodes/stop`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
pubkey,
},
});

console.log(res);
const res = await apiCall('/v1/nodes/stop', 'POST', { pubkey });
return await res.json();
};

const deleteNode = async (pubkey) => {
const res = await fetch(`${BASE_URL}/v1/nodes/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
pubkey,
},
});

console.log(res);
const res = await apiCall('/v1/nodes/delete', 'POST', { pubkey });
return await res.json();
};

const nodeStatus = async () => {
const res = await fetch(`${BASE_URL}/v1/status`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
});

console.log(res);
const res = await apiCall('/v1/status', 'GET');
return await res.json();
};

const startSensi = async (passphrase) => {
const res = await fetch(`${BASE_URL}/v1/start`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
passphrase,
},
});

console.log(res);
const res = await apiCall('/v1/start', 'POST', { passphrase });
return await res.json();
};

const login = async (username, passphrase) => {
const res = await fetch(`${BASE_URL}/v1/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
username,
passphrase,
},
});

console.log(res);
const res = await apiCall('/v1/login', 'POST', { username, passphrase });
return await res.json();
};

const logout = async () => {
const res = await fetch(`${BASE_URL}/v1/logout`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
});

console.log(res);
const res = await apiCall('/v1/logout', 'POST');
return await res.json();
};

const getConfig = async () => {
const res = await fetch(`${BASE_URL}/v1/config`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
});

console.log(res);
const res = await apiCall('/v1/config', 'GET');
return await res.json();
};

const updateConfig = async (electrum_url) => {
const res = await fetch(`${BASE_URL}/v1/config`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
electrum_url,
},
});

console.log(res);
const res = await apiCall('/v1/config', 'POST', { electrum_url });
return await res.json();
};

Expand Down

0 comments on commit 6d68023

Please sign in to comment.