diff --git a/backend/api/account/accountController.js b/backend/api/account/accountController.js index 94ba7c6..0557955 100644 --- a/backend/api/account/accountController.js +++ b/backend/api/account/accountController.js @@ -16,4 +16,34 @@ const getAccountAllowance = async (req, res) => { } }; +const payInvoice = async (req, res) => { + try { + const response = await accountService.getAccountAllowance(); + + debug.info(`Account Allowance 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 }); + } +}; + +const createInvoice = async (req, res) => { + try { + const response = await accountService.getAccountAllowance(); + + debug.info(`Account Allowance 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 }); + } +}; + module.exports = { getAccountAllowance }; diff --git a/backend/api/account/accountService.js b/backend/api/account/accountService.js index 82fccac..84561cf 100644 --- a/backend/api/account/accountService.js +++ b/backend/api/account/accountService.js @@ -1,5 +1,6 @@ const debug = require('../../utils/debug'); const senseiAdmin = require('../../sensei/admin'); +const accounts = require('../../db/collection'); const getAccountAllowance = async (username) => { try { @@ -11,4 +12,34 @@ const getAccountAllowance = async (username) => { } }; -module.exports = { getAccountAllowance }; +const payInvoice = async (req, res) => { + try { + const response = await accountService.getAccountAllowance(); + + debug.info(`Account Allowance 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 }); + } +}; + +const createInvoice = async (req, res) => { + try { + const response = await accountService.getAccountAllowance(); + + debug.info(`Account Allowance 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 }); + } +}; + +module.exports = { getAccountAllowance, payInvoice, createInvoice }; diff --git a/backend/api/account/index.js b/backend/api/account/index.js index d468042..c5d57de 100644 --- a/backend/api/account/index.js +++ b/backend/api/account/index.js @@ -1,8 +1,10 @@ const express = require('express'); const router = express(); -const { getAccountAllowance } = require('./accountController'); +const { getAccountAllowance, payInvoice, createInvoice } = require('./accountController'); router.get('/allowance', getAccountAllowance); +router.get('/payment/send', payInvoice); +router.get('/payment/receive', createInvoice); module.exports = router; diff --git a/backend/api/admin/adminController.js b/backend/api/admin/adminController.js index d1045ad..d766031 100644 --- a/backend/api/admin/adminController.js +++ b/backend/api/admin/adminController.js @@ -5,14 +5,8 @@ const senseiNodes = require('../../sensei/nodes'); const getAllBalances = async (req, res) => { try { - const response = await senseiAdmin.listNodes(); - let nodesBalances = [] - for(let node of response.nodes){ - node.balance = await senseiNodes.getBalance(node.username); - nodesBalances.push(node) - } - - debug.info(`Admin All Balances Response: ${nodesBalances}`); + const response = adminService.getAllBalances(); + debug.info(`Admin All Balances Response: ${JSON.stringify(response)}`); if (!response.success) res.status(500).json(response); else res.status(200).json(response); diff --git a/backend/api/admin/adminService.js b/backend/api/admin/adminService.js index 1fc8db7..5152d55 100644 --- a/backend/api/admin/adminService.js +++ b/backend/api/admin/adminService.js @@ -1,4 +1,21 @@ const debug = require('../../utils/debug'); const accounts = require('../../db/collection'); +const senseiAdmin = require('../../sensei/admin'); +const senseiNodes = require('../../sensei/nodes'); -module.exports = { }; +const getAllBalances = async (req, res) => { + try { + const response = await senseiAdmin.listNodes(); + let nodes = []; + for (let node of response.nodes) { + node.balance = await senseiNodes.getBalance(node.username); + nodes.push(node);x + } + return { success: true, message: nodes }; + } catch (error) { + debug.error(error.stack); + throw new Error(error); + } +}; + +module.exports = { getAllBalances }; diff --git a/backend/app.js b/backend/app.js index a291ca3..224ee54 100644 --- a/backend/app.js +++ b/backend/app.js @@ -20,7 +20,7 @@ app.use( ); app.get('/', async (req, res) => { - res.send(`Health check! Server srunning on port ${PORT}!`); + res.send(`Health check! Server running on port ${PORT}!`); }); const account = require('./api/account'); diff --git a/backend/package.json b/backend/package.json index 2022927..5e076e2 100644 --- a/backend/package.json +++ b/backend/package.json @@ -6,10 +6,9 @@ "repository": "git@github.com:atlantabitdevs/familybtc.git", "author": "atlantabitdevs@gmail.com", "license": "MIT", - "private": false, "scripts": { - "start": "NODE_ENV=prod node server.js", - "dev": "NODE_ENV=local nodemon server.js" + "start": "PORT=8080 node server.js", + "dev": "PORT=8081 nodemon server.js" }, "dependencies": { "cors": "^2.8.5", @@ -20,6 +19,7 @@ "http-errors": "^2.0.0", "mailgun-js": "^0.22.0", "morgan": "^1.10.0", + "node-fetch": "2.6.6", "nodemon": "^2.0.15", "opennode": "^1.3.0", "uuid": "^8.3.2", diff --git a/backend/sensei/admin.js b/backend/sensei/admin.js index c68ebe0..24c6e4c 100644 --- a/backend/sensei/admin.js +++ b/backend/sensei/admin.js @@ -1,9 +1,16 @@ const fetch = require('node-fetch'); const BASE_URL = process.env.BASE_URL; +const MACAROON = process.env.MACAROON; +const TOKEN = process.env.TOKEN; 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, @@ -18,8 +25,13 @@ const initSensei = async (username, passphrase, alias, electrum_url, start) => { }; const listNodes = async (page, take, query) => { - const res = await fetch(`${BASE_URL}/v1/nodes?page=${page || 0}&take=${take || 10}`, { + const res = await fetch(`${BASE_URL}/v1/nodes?page=${0}&take=${10}`, { method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` + }, + credentials: 'include', }); console.log(res); @@ -29,6 +41,11 @@ const listNodes = async (page, take, query) => { 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, @@ -44,6 +61,11 @@ const createNode = async (username, passphrase, alias, start) => { 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, @@ -57,6 +79,11 @@ const startNode = async (pubkey, passphrase) => { 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, }, @@ -69,6 +96,11 @@ const stopNode = async (pubkey) => { 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, }, @@ -81,6 +113,11 @@ const deleteNode = async (pubkey) => { 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); @@ -90,6 +127,11 @@ const nodeStatus = async () => { 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, }, @@ -102,6 +144,11 @@ const startSensi = async (passphrase) => { 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, @@ -115,6 +162,11 @@ const login = async (username, passphrase) => { 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); @@ -124,6 +176,11 @@ const logout = async () => { 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); @@ -133,6 +190,11 @@ const getConfig = async () => { 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, }, diff --git a/backend/sensei/nodes.js b/backend/sensei/nodes.js index 3642951..1b18c4e 100644 --- a/backend/sensei/nodes.js +++ b/backend/sensei/nodes.js @@ -1,37 +1,28 @@ -const fetch = require('node-fetch'); const BASE_URL = process.env.BASE_URL; +const apiCall = require('../utils/apiCall'); const getUnusedAddress = async () => { - const res = await fetch(`${BASE_URL}/v1/node/wallet/address`); - console.log(res); - return await res.json(); + const { address } = await apiCall('/v1/node/wallet/address', 'GET') + return await address.json(); } const getBalance = async () => { - const res = await fetch(`${BASE_URL}/v1/node/wallet/balance`); - console.log(res); - return await res.json(); + const { balance } = await apiCall('/v1/node/wallet/balance', 'GET') + return await balance.json(); } const getChannels = async ({ page, searchTerm, take }) => { - const { channels, pagination } = await fetch( - `${BASE_URL}/v1/node/channels?page=${page}&take=${take}&query=${searchTerm}`, { - method: 'GET' - }); - + const { channels, pagination } = await apiCall(`/v1/node/channels?page=${page}&take=${take}&query=${searchTerm}`, 'GET') return await channels.json(); } -const getPayments = async ({ filter = {}, pagination }) => { - const { page, take, } = pagination; - - const res = await fetch(`${BASE_URL}/v1/node/payments?page=${page}&take=${take}`); - - return await res.json(); +const getPayments = async (page, take) => { + const { payments } = await apiCall(`/v1/node/payments?page=${page}&take=${take}`, 'GET') + return await payments.json(); } const getInfo = async () => { - const { node_info } = await fetch(`${BASE_URL}/v1/node/info`); + const { node_info } = await apiCall(`/v1/node/payments?page=${page}&take=${take}`, 'GET') return { version: node_info.version, @@ -45,7 +36,6 @@ const getInfo = async () => { const getPeers = async () => { const { peers } = await fetch(`${BASE_URL}/v1/node/peers`); - return await peers.json(); } @@ -103,6 +93,16 @@ const keySend = async (destPubkey, amtMsat) => { ); } + module.exports = { - getBalance + getUnusedAddress, + getBalance, + getChannels, + getPayments, + getInfo, + getPeers, + stopNode, + createInvoice, + payInvoice, + keySend, } \ No newline at end of file diff --git a/backend/utils/apiCall.js b/backend/utils/apiCall.js new file mode 100644 index 0000000..0dd87bb --- /dev/null +++ b/backend/utils/apiCall.js @@ -0,0 +1,18 @@ +const fetch = require('node-fetch'); +const BASE_URL = process.env.BASE_URL; +const MACAROON = process.env.MACAROON; +const TOKEN = process.env.TOKEN; + +const apiCall = async (path, method, json={}) => { + return await fetch(BASE_URL + path, { + method: method, + headers: { + 'Content-Type': 'application/json', + 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` + }, + credentials: 'include', + body: JSON.stringify(json), + }); +}; + +module.exports = { apiCall } \ No newline at end of file