aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-09-23 16:23:19 -0700
committerAdam Soltys <[email protected]>2024-09-23 16:23:19 -0700
commit12ad5c9abb74cbe35c5d07233fdd9a7b7f5c2b70 (patch)
tree8e023771abd75a7b70e407c651aed3bcfe426ea3 /routes
parent01e1686265def675445c0f74a4b92ec4ffd71492 (diff)
downloadcoinos-server-12ad5c9abb74cbe35c5d07233fdd9a7b7f5c2b70.tar.gz
coinos-server-12ad5c9abb74cbe35c5d07233fdd9a7b7f5c2b70.zip
reconcile on-chain balance
Diffstat (limited to 'routes')
-rw-r--r--routes/users.ts29
1 files changed, 23 insertions, 6 deletions
diff --git a/routes/users.ts b/routes/users.ts
index 9746f36a..06e437dd 100644
--- a/routes/users.ts
+++ b/routes/users.ts
@@ -1,7 +1,7 @@
import { appendFile } from "node:fs/promises";
import { g, s, db } from "$lib/db";
import config from "$config";
-import { pick, bail, fail, getUser } from "$lib/utils";
+import { SATS, pick, bail, fail, getUser } from "$lib/utils";
import jwt from "jsonwebtoken";
import { authenticator } from "otplib";
import whitelist from "$lib/whitelist";
@@ -10,7 +10,7 @@ import { emit } from "$lib/sockets";
import register from "$lib/register";
import { requirePin } from "$lib/auth";
import { v4 } from "uuid";
-import { types } from "$lib/payments";
+import { credit, types } from "$lib/payments";
import { bech32 } from "bech32";
import { mail, templates } from "$lib/mail";
import upload from "$lib/upload";
@@ -613,11 +613,11 @@ export default {
async createAccount(req, res) {
try {
- let { fingerprint, pubkey, type, seed } = req.body;
+ let { fingerprint, pubkey, name, seed, type } = req.body;
let { user } = req;
let id = v4();
- let account = { id, seed, type };
+ let account = { id, name, seed, type };
let node = rpc(config[type]);
@@ -644,13 +644,30 @@ export default {
}
await node.importDescriptors(descriptors);
+ let { total_amount } = await node.scanTxOutSet("start", descriptors);
+ let amount = Math.round(total_amount * SATS);
+
+ if (amount) {
+ let hash = v4();
+ let inv = {
+ memo: "On-chain balance reconciliation",
+ type: types.reconcile,
+ hash,
+ amount,
+ uid: user.id,
+ aid: id,
+ };
+ await s(`invoice:${hash}`, inv);
+ let { id: pid } = await credit({ hash, amount, type: types.reconcile });
+ await db.lPush(`${id}:payments`, pid);
+ }
await s(`account:${id}`, account);
- await s(`balance:${id}`, 0);
+ await s(`balance:${id}`, amount);
await s(`pending:${id}`, 0);
await db.lPush(`${user.id}:accounts`, id);
- await appendFile("/bitcoin.conf", `wallet=${id}`);
+ await appendFile("/bitcoin.conf", `wallet=${id}\n`);
res.send(account);
} catch (e) {