aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/payments.js2
-rw-r--r--lib/register.js1
-rw-r--r--lib/utils.js20
-rw-r--r--routes/payments.js32
-rw-r--r--routes/users.js93
5 files changed, 118 insertions, 30 deletions
diff --git a/lib/payments.js b/lib/payments.js
index d0593447..cc8747d7 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -34,7 +34,7 @@ export let debit = async (
if (!amount || amount < 0) fail("Amount must be greater than zero");
- let ourfee = Math.round(amount * config.fee);
+ let ourfee = type === types.internal ? 0 : Math.round(amount * config.fee);
await t(`balance:${uid}`, async (balance, db) => {
await t(`credit:${type}:${uid}`, async (credit, db) => {
let covered = Math.min(credit, ourfee) || 0;
diff --git a/lib/register.js b/lib/register.js
index e8b826e3..a3ebfc86 100644
--- a/lib/register.js
+++ b/lib/register.js
@@ -44,6 +44,7 @@ export default async (user, ip, requireChallenge) => {
user.fiat = false;
user.otpsecret = authenticator.generateSecret();
user.balance = 0;
+ user.migrated = true;
let d = ip.split(".");
ip = ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3];
diff --git a/lib/utils.js b/lib/utils.js
index e58f7d1f..8291d173 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -38,3 +38,23 @@ export const bip21 = (address, { amount, memo, tip }) => {
return url;
};
+
+export const fields = [
+ "cipher",
+ "pubkey",
+ "password",
+ "username",
+ "salt",
+ "currency",
+ "currencies",
+ "fiat",
+ "otpsecret",
+ "balance",
+ "ip",
+ "pin",
+ "display",
+ "haspin",
+ "profile",
+ "prompt",
+ "banner"
+];
diff --git a/routes/payments.js b/routes/payments.js
index ca32e969..26708a44 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -58,21 +58,27 @@ export default {
async list({ user: { id }, query: { start, end, limit, offset } }, res) {
if (limit) limit = parseInt(limit);
- if (offset) offset = parseInt(offset);
-
- // if (start || end) where.createdAt = {};
- // if (start) where.createdAt[Op.gte] = new Date(parseInt(start));
- // if (end) where.createdAt[Op.lte] = new Date(parseInt(end));
+ offset = parseInt(offset) || 0;
let payments = (await db.lRange(`${id}:payments`, 0, -1)) || [];
- payments = await Promise.all(
- payments.map(async id => {
- let p = await g(`payment:${id}`);
- if (p.type === types.internal) p.with = await g(`user:${p.ref}`);
- return p;
- })
- );
- res.send({ payments, total: payments.length });
+ payments = (
+ await Promise.all(
+ payments.map(async id => {
+ let p = await g(`payment:${id}`);
+ if (p.created < start || p.created > end) return;
+ if (p.type === types.internal) p.with = await g(`user:${p.ref}`);
+ return p;
+ })
+ )
+ )
+ .filter(p => p)
+ .sort((a, b) => b.created - a.created);
+
+ let total = payments.length;
+
+ if (limit) payments = payments.slice(offset, offset + limit);
+
+ res.send({ payments, total });
},
async get({ params: { hash } }, res) {
diff --git a/routes/users.js b/routes/users.js
index 49aeebd7..4b5157ce 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -1,7 +1,7 @@
-import { g, s } from "$lib/db";
+import { g, s, db } from "$lib/db";
import config from "$config";
import store from "$lib/store";
-import { nada, pick, uniq, wait } from "$lib/utils";
+import { fields, nada, pick, uniq, wait } from "$lib/utils";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import { authenticator } from "otplib";
@@ -10,6 +10,10 @@ import { l, err, warn } from "$lib/logging";
import { emit } from "$lib/sockets";
import register from "$lib/register";
import { requirePin } from "$lib/utils";
+import { v4 } from "uuid";
+import { parseISO } from "date-fns";
+import { types } from "$lib/payments";
+
import got from "got";
import upload from "$lib/upload";
@@ -172,27 +176,84 @@ export default {
async login(req, res) {
try {
- const { params, sig, key, password, username, token: twofa } = req.body;
+ const { username, password, token: twofa } = req.body;
l("logging in", username);
- if (sig) {
- const { callback } = params;
+ let uid = await g(`user:${username}`);
+ let user = await g(`user:${uid}`);
+ if (!(user && user.migrated)) {
+ let { classic } = config;
try {
- const url = `${callback}&sig=${sig}&key=${key}`;
- const response = await got(url).json();
- res.send(response);
+ let { token } = await got
+ .post(`${classic}/login`, { json: { username, password } })
+ .json();
+ if (!token) fail();
+
+ user = await got(`${classic}/admin/migrate/${username}?zero=true`, {
+ headers: { authorization: `Bearer ${config.admin}` }
+ }).json();
+
+ let { balance } = user;
+ if (!user) fail();
+
+ uid = v4();
+ user = {
+ ...pick(user, fields),
+ id: uid,
+ about: user.address,
+ migrated: true
+ };
+
+ await s(`user:${username}`, uid);
+ await s(`user:${uid}`, user);
+ await s(`balance:${uid}`, balance);
+
+ let payments = await got(`${classic}/payments`, {
+ headers: { authorization: `Bearer ${token}` }
+ }).json();
+
+ for (let p of payments) {
+ let n = pick(p, ["amount", "fee", "confirmed", "rate", "currency"]);
+ n.id = v4();
+ n.created = parseISO(p.createdAt).getTime();
+ n.type = p.network;
+ if (!["bitcoin", "lightning"].includes(n.type))
+ n.type = types.internal;
+
+ if (n.type === types.internal) {
+ if (!p.with) continue;
+
+ let id = await g(`user:${p.with.username}`);
+ let u = await g(`user:${id}`);
+ if (!u) {
+ u = await got(`${classic}/admin/migrate/${p.with.username}`, {
+ headers: { authorization: `Bearer ${config.admin}` }
+ }).json();
+
+ u = { id: v4(), about: u.address, ...pick(u, fields) };
+ delete u.address;
+
+ await s(`user:${p.with.username}`, u.id);
+ await s(`user:${u.id}`, u);
+
+ l("added missing user", u.username);
+ }
+
+ if (!(u && u.id)) continue;
+ n.ref = u.id;
+ }
+
+ await s(`payment:${n.id}`, n);
+ await db.lPush(`${uid}:payments`, n.id);
+ }
+
+ l("migrated user", user.username);
} catch (e) {
- err("problem calling lnurl login", e.message);
- res.code(500).send(e.message);
+ console.log(e);
}
-
- return;
}
- let id = await g(`user:${username}`);
- let user = await g(`user:${id}`);
-
if (
!user ||
(user.password && !(await bcrypt.compare(password, user.password)))
@@ -215,7 +276,7 @@ export default {
req.headers["x-forwarded-for"] || req.socket.remoteAddress
);
- let payload = { username, id: user.id };
+ let payload = { username, id: uid };
let token = jwt.sign(payload, config.jwt);
res.cookie("token", token, { expires: new Date(Date.now() + 432000000) });
user = pick(user, whitelist);