aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-05-14 20:45:18 -0700
committerAdam Soltys <[email protected]>2020-05-14 20:45:18 -0700
commitb45deffeda2f616453b8284dcfb78d65a48b858e (patch)
tree96a6f250eb9404082891737f4925b8c4d1775fb7
parenta9a061656668965fbf627ae73165d361d8ca9183 (diff)
downloadcoinos-server-b45deffeda2f616453b8284dcfb78d65a48b858e.tar.gz
coinos-server-b45deffeda2f616453b8284dcfb78d65a48b858e.zip
support opt-in to the different networks
-rw-r--r--index.js4
-rw-r--r--lib/sockets.js1
-rw-r--r--routes/balances.js46
-rw-r--r--routes/payments.js54
-rw-r--r--routes/users.js37
5 files changed, 87 insertions, 55 deletions
diff --git a/index.js b/index.js
index 1c1bddd2..37e1b237 100644
--- a/index.js
+++ b/index.js
@@ -6,6 +6,10 @@ const express = require("express");
l = require("pino")();
config = require("./config");
+networks = [];
+if (config.bitcoin) networks.push('bitcoin');
+if (config.liquid) networks.push('liquid');
+if (config.lna) networks.push('lightning');
SATS = 100000000;
toSats = (n) => parseInt((n * SATS).toFixed());
diff --git a/lib/sockets.js b/lib/sockets.js
index 16f42214..d35627b3 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -45,6 +45,7 @@ wss.on("connection", function (ws) {
sockets[username][id] = ws;
names[id] = username;
+ ws.send(JSON.stringify({ type: "networks", data: networks }));
ws.send(JSON.stringify({ type: "rates", data: app.get("rates") }));
ws.send(JSON.stringify({ type: "login", data: await getUser(username) }));
l.info("socket open", username);
diff --git a/routes/balances.js b/routes/balances.js
index 78567c44..ba834306 100644
--- a/routes/balances.js
+++ b/routes/balances.js
@@ -2,7 +2,6 @@ const sequelize = require("sequelize");
const { Op } = sequelize;
app.get("/info", async (req, res) => {
- const assets = await lq.getBalance();
const accounts = await db.Account.findAll({
attributes: [
"asset",
@@ -15,37 +14,48 @@ app.get("/info", async (req, res) => {
let lnwallet;
let lninfo;
- if (config.lna.clightning) {
- lninfo = await lna.getinfo();
- const funds = await lna.listfunds();
- lnchannel = parseInt(funds.channels.reduce((a, b) => a + b.channel_sat, 0));
- lnwallet = parseInt(funds.outputs.reduce((a, b) => a + b.value, 0));
- } else {
- lninfo = await lna.getInfo({});
- lnchannel = parseInt((await lna.channelBalance({})).balance);
- lnwallet = parseInt((await lna.walletBalance({})).total_balance);
+ if (config.lna) {
+ if (config.lna.clightning) {
+ const funds = await lna.listfunds();
+ lninfo = await lna.getinfo();
+ lnchannel = parseInt(funds.channels.reduce((a, b) => a + b.channel_sat, 0));
+ lnwallet = parseInt(funds.outputs.reduce((a, b) => a + b.value, 0));
+ } else {
+ lninfo = await lna.getInfo({});
+ lnchannel = parseInt((await lna.channelBalance({})).balance);
+ lnwallet = parseInt((await lna.walletBalance({})).total_balance);
+ }
+ }
+
+ let bitcoin, bitcoind;
+ if (config.bitcoin) {
+ bitcoin = parseInt((await bc.getBalance()) * SATS);
+ bitcoind = await bc.getNetworkInfo();
+ }
+
+ let assets, liquid, elementsd;
+ if (config.liquid) {
+ assets = await lq.getBalance();
+ liquid = parseInt(assets.bitcoin * SATS);
+ elementsd = await lq.getNetworkInfo();
}
const info = {
- bitcoind: await bc.getNetworkInfo(),
- elementsd: await lq.getNetworkInfo(),
+ bitcoind,
+ elementsd,
accounts,
assets,
- bitcoin: parseInt((await bc.getBalance()) * SATS),
- liquid: parseInt(assets.bitcoin * SATS),
+ bitcoin,
+ liquid,
lnchannel,
lnwallet,
};
- const { bitcoin, liquid, user } = info;
-
info.total =
parseInt(bitcoin) +
parseInt(liquid) +
parseInt(lnchannel) +
parseInt(lnwallet);
- info.ratio = (info.total / parseInt(user)).toFixed(2);
-
res.send(info);
});
diff --git a/routes/payments.js b/routes/payments.js
index 02e0c88f..81d7540b 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -4,25 +4,13 @@ const { Op } = require("sequelize");
const { join } = require("path");
(async () => {
- bc = new BitcoinCore(config.bitcoin);
- lq = new BitcoinCore(config.liquid);
-
- if (config.lna.clightning) {
- const lnapath = join(require('os').homedir(), '.lightningreg/regtest')
- const lnbpath = join(require('os').homedir(), '.lightningregb/regtest')
- lna = require('clightning-client')(lnapath);
- lnb = require('clightning-client')(lnbpath);
- } else {
- lna = lnd(config.lna);
- lnb = lnd(config.lnb);
- }
-
seen = [];
addresses = {};
+
await db.User.findAll({
attributes: ["username", "address", "liquid"]
}).map(u => {
- addresses[u.address] = u.username;
+ if (u.address) addresses[u.address] = u.username;
if (u.liquid) addresses[u.liquid] = u.username;
});
@@ -32,18 +20,36 @@ const { join } = require("path");
app.post("/send", auth, require("./send"));
- app.post("/lightning/invoice", auth, require("./lightning/invoice"));
- app.post("/lightning/query", auth, require("./lightning/query"));
- app.post("/lightning/send", auth, require("./lightning/send"));
- require("./lightning/receive");
+ if (config.lna) {
+ if (config.lna.clightning) {
+ const lnapath = join(require('os').homedir(), '.lightningreg/regtest')
+ const lnbpath = join(require('os').homedir(), '.lightningregb/regtest')
+ lna = require('clightning-client')(lnapath);
+ lnb = require('clightning-client')(lnbpath);
+ } else {
+ lna = lnd(config.lna);
+ lnb = lnd(config.lnb);
+ }
- app.post("/bitcoin/fee", auth, require("./bitcoin/fee"));
- app.post("/bitcoin/send", auth, require("./bitcoin/send"));
- require("./bitcoin/receive");
+ app.post("/lightning/invoice", auth, require("./lightning/invoice"));
+ app.post("/lightning/query", auth, require("./lightning/query"));
+ app.post("/lightning/send", auth, require("./lightning/send"));
+ require("./lightning/receive");
+ }
+
+ if (config.bitcoin) {
+ bc = new BitcoinCore(config.bitcoin);
+ app.post("/bitcoin/fee", auth, require("./bitcoin/fee"));
+ app.post("/bitcoin/send", auth, require("./bitcoin/send"));
+ require("./bitcoin/receive");
+ }
- app.post("/liquid/fee", auth, require("./liquid/fee"));
- app.post("/liquid/send", auth, require("./liquid/send"));
- require("./liquid/receive");
+ if (config.liquid) {
+ lq = new BitcoinCore(config.liquid);
+ app.post("/liquid/fee", auth, require("./liquid/fee"));
+ app.post("/liquid/send", auth, require("./liquid/send"));
+ require("./liquid/receive");
+ }
app.get("/payments", auth, async (req, res) => {
const payments = await db.Payment.findAll({
diff --git a/routes/users.js b/routes/users.js
index 72c8049c..38fdf850 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -55,16 +55,22 @@ app.post("/register", async (req, res) => {
let exists = await db.User.count({ where: { username: user.username } });
if (exists) return err("Username taken");
- if (config.bitcoin.walletpass)
- await bc.walletPassphrase(config.bitcoin.walletpass, 300);
+ if (config.bitcoin) {
+ if (config.bitcoin.walletpass)
+ await bc.walletPassphrase(config.bitcoin.walletpass, 300);
- user.address = await bc.getNewAddress("", "bech32");
+ user.address = await bc.getNewAddress("", "bech32");
+ addresses[user.address] = user.username;
+ }
- if (config.liquid.walletpass)
- await lq.walletPassphrase(config.liquid.walletpass, 300);
+ if (config.liquid) {
+ if (config.liquid.walletpass)
+ await lq.walletPassphrase(config.liquid.walletpass, 300);
- user.confidential = await lq.getNewAddress();
- user.liquid = (await lq.getAddressInfo(user.confidential)).unconfidential;
+ user.confidential = await lq.getNewAddress();
+ user.liquid = (await lq.getAddressInfo(user.confidential)).unconfidential;
+ addresses[user.liquid] = user.username;
+ }
let ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
@@ -89,8 +95,6 @@ app.post("/register", async (req, res) => {
user.currencies = [...new Set([user.currency, "CAD", "USD", "JPY"])];
user.otpsecret = authenticator.generateSecret();
- addresses[user.address] = user.username;
- addresses[user.liquid] = user.username;
user = await db.User.create(user);
let account = await db.Account.create({
@@ -105,8 +109,8 @@ app.post("/register", async (req, res) => {
user.accounts = [account];
- let d = ip.split(".");
- let numericIp = ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3];
+ const d = ip.split(".");
+ const numericIp = ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3];
user.ip = numericIp;
const ipExists = await db.User.findOne({ where: { ip: numericIp } });
if (!ipExists) await gift(user);
@@ -180,8 +184,13 @@ app.post("/user", auth, async (req, res) => {
} else {
sockets[username] = sockets[user.username];
user.username = username;
- addresses[user.address] = username;
- addresses[user.liquid] = user.username;
+
+ if (user.address)
+ addresses[user.address] = username;
+
+ if (user.liquid)
+ addresses[user.liquid] = user.username;
+
token = jwt.sign({ username }, config.jwt);
res.cookie("token", token, {
expires: new Date(Date.now() + 432000000),
@@ -248,6 +257,8 @@ app.post("/login", async (req, res) => {
});
app.post("/address", auth, async (req, res) => {
+ if (!config.bitcoin) return res.status(500).send("Bitcoin not configured");
+
let { user } = req;
delete addresses[user.address];