aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-11 22:09:48 -0800
committerAdam Soltys <[email protected]>2023-01-12 15:44:13 -0800
commitc3565838644123e77881ff4f8536792f074047b9 (patch)
tree96da43fd19940d2f0836e43a511f0a1fb365044d /lib
parentded2c69ce89438326523a512b570be6eee4b23f0 (diff)
downloadcoinos-server-c3565838644123e77881ff4f8536792f074047b9.tar.gz
coinos-server-c3565838644123e77881ff4f8536792f074047b9.zip
migrating to redis
Diffstat (limited to 'lib')
-rw-r--r--lib/binance.js3
-rw-r--r--lib/countries.js249
-rw-r--r--lib/ln.js2
-rw-r--r--lib/nostr.js1
-rw-r--r--lib/passport.js7
-rw-r--r--lib/rates.js34
-rw-r--r--lib/redis.js6
-rw-r--r--lib/register.js33
-rw-r--r--lib/sockets.js12
-rw-r--r--lib/utils.js6
-rw-r--r--lib/webhooks.js13
-rw-r--r--lib/whitelist.js4
12 files changed, 313 insertions, 57 deletions
diff --git a/lib/binance.js b/lib/binance.js
index 9f7f149f..e583d312 100644
--- a/lib/binance.js
+++ b/lib/binance.js
@@ -18,10 +18,7 @@ export default () => {
store.rates[symbol] = msg.c * store.fx[symbol];
});
- // app.set("ask", msg.a);
- // app.set("bid", msg.b);
store.last = msg.c;
- // console.log("last", msg.c, store.fx["CAD"], msg.c * store.fx["CAD"]);
} catch (e) {
err("binance message error", e.message);
}
diff --git a/lib/countries.js b/lib/countries.js
new file mode 100644
index 00000000..4d5a48dc
--- /dev/null
+++ b/lib/countries.js
@@ -0,0 +1,249 @@
+export default {
+ AF: "AFG",
+ AL: "ALB",
+ DZ: "DZA",
+ AD: "AND",
+ AO: "AGO",
+ AI: "AIA",
+ AQ: "ATA",
+ AR: "ARG",
+ AM: "ARM",
+ AW: "ABW",
+ AU: "AUS",
+ AT: "AUT",
+ AZ: "AZE",
+ BS: "BHS",
+ BH: "BHR",
+ BD: "BGD",
+ BB: "BRB",
+ BY: "BLR",
+ BE: "BEL",
+ BZ: "BLZ",
+ BJ: "BEN",
+ BM: "BMU",
+ BT: "BTN",
+ BO: "BOL",
+ BW: "BWA",
+ BV: "NOK",
+ BR: "BRA",
+ IO: "USD",
+ VG: "USD",
+ BN: "BRN",
+ BG: "BGR",
+ BF: "XOF",
+ BI: "BDI",
+ KH: "KHM",
+ CM: "CMR",
+ CA: "CAN",
+ IC: "EUR",
+ CV: "CVE",
+ KY: "KYD",
+ CF: "XAF",
+ TD: "TCD",
+ CL: "CHL",
+ CN: "CHN",
+ CX: "AUD",
+ CC: "AUD",
+ CO: "COL",
+ KM: "COM",
+ CG: "XAF",
+ CD: "CDF",
+ CK: "NZD",
+ CR: "CRC",
+ CI: "XOF",
+ HR: "HRV",
+ CU: "CUB",
+ CW: "ANG",
+ CY: "CYP",
+ CZ: "CZK",
+ DK: "DNK",
+ DJ: "DJI",
+ DM: "DMA",
+ DO: "DOP",
+ TL: "IDS",
+ EC: "ECU",
+ EG: "EGY",
+ SV: "SVC",
+ GQ: "XAF",
+ ER: "ERI",
+ EE: "EST",
+ ET: "ETH",
+ FK: "FKP",
+ FO: "DKK",
+ FJ: "FJI",
+ FI: "FIN",
+ FR: "FRA",
+ GF: "EUR",
+ PF: "XPF",
+ TF: "EUR",
+ GA: "GAB",
+ GM: "GMB",
+ GE: "GEO",
+ DE: "DEU",
+ GH: "GHA",
+ GI: "GIB",
+ GR: "GRC",
+ GL: "GRL",
+ GD: "GRD",
+ GP: "GLP",
+ GU: "GUM",
+ GT: "GTM",
+ GG: "GGY",
+ GN: "GIN",
+ GW: "XOF",
+ GY: "GUY",
+ HT: "HTI",
+ HM: "AUD",
+ HN: "HND",
+ HK: "HKD",
+ HU: "HUN",
+ IS: "ISL",
+ IN: "IND",
+ ID: "IDN",
+ IR: "IRN",
+ IQ: "IRQ",
+ IE: "IRL",
+ Of: "Man",
+ IL: "ISR",
+ IT: "ITA",
+ JM: "JAM",
+ JP: "JPN",
+ JE: "JEY",
+ JO: "JOR",
+ KZ: "KAZ",
+ KE: "KEN",
+ KI: "KIR",
+ XK: "XKX",
+ KW: "KWT",
+ KG: "KGZ",
+ LA: "LAO",
+ LV: "LVA",
+ LB: "LBN",
+ LS: "LSO",
+ LR: "LBR",
+ LY: "LBY",
+ LI: "LIE",
+ LT: "LTU",
+ LU: "LUX",
+ MO: "MAC",
+ MK: "MKD",
+ MG: "MDG",
+ MW: "MWI",
+ MY: "MYS",
+ MV: "MDV",
+ ML: "MLI",
+ MT: "MLT",
+ MH: "USD",
+ MQ: "MTQ",
+ MR: "MRT",
+ MU: "MUS",
+ YT: "MYT",
+ MX: "MEX",
+ FM: "FSM",
+ MD: "MDA",
+ MC: "MCO",
+ MN: "MNG",
+ ME: "MNE",
+ MS: "MSR",
+ MA: "MAR",
+ MZ: "MOZ",
+ MM: "MMR",
+ NA: "NAM",
+ NR: "NRU",
+ NP: "NPL",
+ NL: "NLD",
+ AN: "ANG",
+ NC: "XPF",
+ NZ: "NZD",
+ NI: "NIC",
+ NE: "NER",
+ NG: "NGA",
+ NU: "NIU",
+ NF: "AUD",
+ KP: "KPW",
+ MP: "USD",
+ NO: "NOR",
+ OM: "OMN",
+ PK: "PAK",
+ PW: "PLW",
+ PS: "PSE",
+ PA: "PAN",
+ PG: "PGK",
+ PY: "PRY",
+ PE: "PER",
+ PH: "PHL",
+ PN: "PCN",
+ PL: "POL",
+ PT: "PRT",
+ PR: "USD",
+ QA: "QAT",
+ RE: "REU",
+ RO: "ROU",
+ RU: "RUS",
+ RW: "RWA",
+ BL: "EUR",
+ SH: "GPB",
+ KN: "XCD",
+ LC: "XCD",
+ MF: "EUR",
+ PM: "EUR",
+ VC: "XCD",
+ WS: "WSM",
+ SM: "EUR",
+ ST: "STN",
+ SA: "SAR",
+ SN: "SEN",
+ RS: "SRB",
+ CS: "RSD",
+ SC: "SYC",
+ SL: "SLL",
+ SG: "SGP",
+ SX: "ANG",
+ SK: "SVK",
+ SI: "SVN",
+ SB: "SBD",
+ SO: "SOM",
+ ZA: "ZAR",
+ GS: "GBP",
+ KR: "KRW",
+ SS: "SSP",
+ ES: "ESP",
+ LK: "LKR",
+ SD: "SDN",
+ SR: "SUR",
+ SJ: "NOK",
+ SZ: "SWZ",
+ SE: "SWE",
+ CH: "CHE",
+ SY: "SYR",
+ TW: "TWN",
+ TJ: "TJK",
+ TZ: "TZA",
+ TH: "THA",
+ TG: "TGO",
+ TK: "TKL",
+ TO: "TON",
+ TT: "TTD",
+ TA: "SHP",
+ TN: "TUN",
+ TR: "TUR",
+ TM: "TKM",
+ TC: "USD",
+ TV: "TUV",
+ UM: "USD",
+ VI: "USD",
+ UG: "UGA",
+ UA: "UKR",
+ AE: "AED",
+ GB: "GBP",
+ US: "USD",
+ UY: "URY",
+ UZ: "UZB",
+ VU: "VUT",
+ VA: "VAT",
+ VE: "VEN",
+ VN: "VNM",
+ YE: "YEM",
+ ZM: "ZMB",
+ ZW: "ZWE"
+};
diff --git a/lib/ln.js b/lib/ln.js
index 13bbd004..c4445b26 100644
--- a/lib/ln.js
+++ b/lib/ln.js
@@ -2,6 +2,6 @@ import config from "$config";
let ln;
let { LightningClient } = (await import("clightning-client")).default;
-ln = new LightningClient(config.lna.dir);
+ln = new LightningClient(config.lightning);
export default ln;
diff --git a/lib/nostr.js b/lib/nostr.js
index bc75cc96..ad089bb5 100644
--- a/lib/nostr.js
+++ b/lib/nostr.js
@@ -4,7 +4,6 @@ import { Relay, RelayPool } from "nostr";
import { broadcast } from "$lib/sockets";
import store from "$lib/store";
import redis from "$lib/redis";
-import proxy from "@fastify/http-proxy";
import { nada, wait } from "$lib/utils";
export let pool;
diff --git a/lib/passport.js b/lib/passport.js
index 6638fe73..b559b2f9 100644
--- a/lib/passport.js
+++ b/lib/passport.js
@@ -6,9 +6,10 @@ import config from "$config";
import fs from "fs";
import path from "path";
import { err } from "$lib/logging";
+import { g } from "$lib/redis";
app.register(fastifySecureSession, {
- key: config.jwt
+ key: Buffer.from(config.jwt, "hex")
});
app.register(fastifyPassport.initialize());
@@ -34,10 +35,10 @@ fastifyPassport.use(
async (payload, next) => {
try {
let { uuid } = payload;
- let user = await redis.get(`user:${uuid}`);
+ let user = await g(`user:${uuid}`);
next(null, user);
} catch (e) {
- console.log("payload", payload)
+ console.log("payload", payload);
if (!e.message.includes("locked")) {
console.log(e);
err("problem logging in", e);
diff --git a/lib/rates.js b/lib/rates.js
index 8970ca64..8064a117 100644
--- a/lib/rates.js
+++ b/lib/rates.js
@@ -1,23 +1,23 @@
import app from "$app";
-import axios from "axios";
+import got from "got";
import config from "$config";
import fs from "fs";
-// import binance from "./binance";
+import binance from "./binance";
import { broadcast } from "./sockets";
import store from "./store";
import { err, warn } from "$lib/logging";
-// let b = binance();
-// setInterval(() => {
-// b.terminate();
-// b = binance();
-// }, 360000);
+let b = binance();
+setInterval(() => {
+ b.terminate();
+ b = binance();
+}, 360000);
let fetchRates = async () => {
let date, fx;
try {
- ({ date, fx } = JSON.parse(fs.readFileSync("fx")));
+ ({ date, fx } = JSON.parse(fs.readFileSync("data/fx")));
} catch (e) {
warn("Failed to read fx file");
}
@@ -28,26 +28,22 @@ let fetchRates = async () => {
if (date !== today) {
try {
- let {
- data: { rates: fx }
- } = await axios.get(
+ let { rates: fx } = await got(
`https://api.exchangerate.host/latest?base=USD&v={(new Date()).toISOString().split('T')[0]}`
- );
- fs.writeFileSync("fx", JSON.stringify({ date, fx }));
+ ).json();
+ fs.writeFileSync("data/fx", JSON.stringify({ date, fx }));
} catch (e) {
try {
- let {
- data: { rates: fx }
- } = await axios.get(
+ let { rates: fx } = await got(
`http://data.fixer.io/api/latest?access_key=${config.fixer}`
- );
+ ).json();
let USD = fx["USD"];
Object.keys(fx).map(k => {
fx[k] = parseFloat((fx[k] / fx["USD"]).toFixed(6));
});
- fs.writeFileSync("fx", JSON.stringify({ date, fx }));
+ fs.writeFileSync("data/fx", JSON.stringify({ date, fx }));
} catch (e) {
err("error fetching rates", e.message);
}
@@ -58,7 +54,7 @@ let fetchRates = async () => {
setTimeout(fetchRates, 7200000);
};
-// fetchRates();
+fetchRates();
app.get("/rate", async (req, res, next) => {
res.send(store.last);
diff --git a/lib/redis.js b/lib/redis.js
index 3aed1bd0..6aec2af7 100644
--- a/lib/redis.js
+++ b/lib/redis.js
@@ -1,7 +1,7 @@
import { createClient } from "redis";
const redis = createClient({
- url: "redis://redis"
+ url: "redis://rd"
});
redis.on("error", err => console.log("redis error", err));
@@ -9,3 +9,7 @@ redis.on("error", err => console.log("redis error", err));
await redis.connect();
export default redis;
+
+export let g = async k => JSON.parse(await redis.get(k));
+export let s = (k, v) => redis.set(k, JSON.stringify(v));
+export let rd = redis;
diff --git a/lib/register.js b/lib/register.js
index fd6f27ae..9f18c15e 100644
--- a/lib/register.js
+++ b/lib/register.js
@@ -4,10 +4,12 @@ import { authenticator } from "otplib";
import bcrypt from "bcrypt";
import { l } from "$lib/logging";
import { v4 } from "uuid";
-import redis from "$lib/redis";
+import { s, g } from "$lib/redis";
+import { got } from "got";
export default async (user, ip, requireChallenge) => {
let { password, username } = user;
+ l("registering", username);
if (!username) throw new Error("Username required");
@@ -15,26 +17,35 @@ export default async (user, ip, requireChallenge) => {
let uuid = v4();
user.uuid = uuid;
- let exists = await redis.get(`user:${username}`);
+ let exists = await g(`user:${username}`);
if (exists) throw new Error(`Username ${username} taken`);
if (password) {
user.password = await bcrypt.hash(password, 1);
}
+ console.log("BOO");
+
if (!config.ipxapi || ip.startsWith("127") || ip.startsWith("192")) {
user.currency = "CAD";
} else {
- let { countryCode } = await got(`https://ipxapi.com/api/ip?ip=${ip}`, {
- headers: { authorization: `Bearer ${config.ipxapi}` }
- }).json();
+ let countryCode = "US";
+ try {
+ let r = await got(`https://ipxapi.com/api/ip?ip=${ip}`, {
+ headers: { authorization: `Bearer ${config.ipxapi}` }
+ }).json();
+ if (r.success) ({ countryCode } = r);
+ } catch (e) {
+ console.log(e);
+ }
- user.currency = countries[countryCode] || "USD";
+ user.currency = countries[countryCode];
}
user.currencies = [...new Set([user.currency, "CAD", "USD"])];
user.fiat = false;
user.otpsecret = authenticator.generateSecret();
+ user.balance = 0;
let d = ip.split(".");
ip = ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3];
@@ -42,8 +53,14 @@ export default async (user, ip, requireChallenge) => {
user.ip = ip;
}
- await redis.set(`user:${user.uuid}`, user);
- await redis.set(`user:${user.username}`, user);
+ console.log("user uuid", user.uuid, user.username);
+
+ try {
+ await s(`user:${user.uuid}`, user);
+ await s(`user:${user.username}`, user);
+ } catch (e) {
+ console.log("HUH", e);
+ }
l("new user", user.username);
return user;
};
diff --git a/lib/sockets.js b/lib/sockets.js
index ecf15dd7..0f343937 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -91,8 +91,10 @@ app.register(async function(app) {
try {
if (!token || token === "null" || !jwt.decode(token))
return ws.close(code, `bad token ${token}`);
+
let { uuid } = jwt.decode(token);
if (!uuid) throw new Error("Invalid JWT token");
+ console.log("UUID", uuid)
let user = await getUser(uuid);
if (!user) throw new Error("User not found");
@@ -109,15 +111,6 @@ app.register(async function(app) {
store.sockets[uuid][id] = ws;
names[id] = uuid;
- let { haspin } = user;
-
- let keys = await user.getKeys();
- user = user.get({ plain: true });
- user.accounts = accounts;
- user.keys = keys;
- user.payments = payments;
- user.haspin = haspin;
-
ws.user = user;
ws.send(
JSON.stringify({ type: "login", data: pick(user, ...whitelist) })
@@ -126,6 +119,7 @@ app.register(async function(app) {
JSON.stringify({ type: "version", data: config.clientVersion })
);
} catch (e) {
+ console.log(e)
err("caught socket error", e.message);
if (e.message.includes("not found"))
ws.send(JSON.stringify({ type: "logout" }));
diff --git a/lib/utils.js b/lib/utils.js
index 1513138b..559f8531 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -1,5 +1,5 @@
import store from "$lib/store";
-import redis from "$lib/redis";
+import { g } from "$lib/redis";
import config from "$config";
import ln from "$lib/ln";
@@ -19,7 +19,9 @@ export const requirePin = async ({ body, user }) => {
};
export const getUser = async (uuid) => {
- let user = await redis.get(`user:${uuid}`);
+ let user = g(`user:${uuid}`);
+ if (!user) throw new Error("user not found");
+
user.haspin = !!user.pin;
return user;
diff --git a/lib/webhooks.js b/lib/webhooks.js
index 4741932a..c071eccb 100644
--- a/lib/webhooks.js
+++ b/lib/webhooks.js
@@ -1,5 +1,5 @@
import config from "$config";
-import wretch from "./wretch";
+import got from "got";
import { l } from "$lib/logging";
export const callWebhook = async (invoice, payment) => {
@@ -14,10 +14,9 @@ export const callWebhook = async (invoice, payment) => {
let secret = config.webhooks[webhook];
l("calling webhook", webhook, amount, hash, address, text);
- let res = await wretch()
- .headers({ "x-webhook": secret })
- .url(webhook)
- .post({
+ let res = await got(webhook, {
+ headers: { "x-webhook": secret },
+ json: {
address,
amount,
asset,
@@ -27,8 +26,8 @@ export const callWebhook = async (invoice, payment) => {
received,
text,
webhook
- })
- .res();
+ }
+ }).res();
return res;
}
} catch (e) {
diff --git a/lib/whitelist.js b/lib/whitelist.js
index 47e9ed4e..523e9d37 100644
--- a/lib/whitelist.js
+++ b/lib/whitelist.js
@@ -1,7 +1,5 @@
export default [
- "account",
- "account_id",
- "accounts",
+ "balance",
"address",
"admin",
"banner",