aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-09-13 23:18:20 -0700
committerAdam Soltys <[email protected]>2024-09-13 23:18:20 -0700
commitfb9b4cc39df1608aa1a04286408f03e244f8f15f (patch)
tree40fd6c4f4ba0a9b1237f90225be8f1c89cf4e49b
parent183315347b3ce6d4057abe05861c39f5d4b1aa46 (diff)
downloadcoinos-server-fb9b4cc39df1608aa1a04286408f03e244f8f15f.tar.gz
coinos-server-fb9b4cc39df1608aa1a04286408f03e244f8f15f.zip
add default account if missing
-rw-r--r--lib/invoices.ts4
-rw-r--r--lib/migrate.ts19
-rw-r--r--lib/payments.ts26
3 files changed, 37 insertions, 12 deletions
diff --git a/lib/invoices.ts b/lib/invoices.ts
index c18a120b..45c97fe2 100644
--- a/lib/invoices.ts
+++ b/lib/invoices.ts
@@ -53,8 +53,8 @@ export let generate = async ({ invoice, user, sender = undefined }) => {
let hash, text;
if (account) {
- bc = rpc({ ...config.bitcoin, wallet: account });
- hash = await bc.getNewAddress();
+ let node = rpc({ ...config.bitcoin, wallet: account });
+ hash = await node.getNewAddress();
text = bip21(hash, invoice);
} else if (type === types.lightning) {
let r;
diff --git a/lib/migrate.ts b/lib/migrate.ts
index 8896f54b..2d7f83f7 100644
--- a/lib/migrate.ts
+++ b/lib/migrate.ts
@@ -14,13 +14,20 @@ async function migrate(id) {
if (typeof user === "string") user = await g(`user:${user}`);
if (user) {
- let accounts = await db.lLen(`${user.id}:accounts`);
- if (!accounts) {
- await s(`account:${user.id}`, { id: user.id });
- await db.lPush(`${user.id}:accounts`, user.id);
- }
+ let k = `lock:${user.id}:accounts`;
+ const lock = await db.set(k, "1", { NX: true, PX: 5000 });
+ if (!lock) return user;
+ try {
+ let accounts = await db.lLen(`${user.id}:accounts`);
- return user;
+ if (!accounts) {
+ await db.set(`account:${user.id}`, JSON.stringify({ id: user.id }));
+ await db.lPush(`${user.id}:accounts`, user.id);
+ }
+ return user;
+ } finally {
+ await db.del(k);
+ }
}
user = await ga(`user:${id}`);
diff --git a/lib/payments.ts b/lib/payments.ts
index 454c9753..3f5cd152 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -84,6 +84,7 @@ export let debit = async ({
let tip = parseInt(invoice?.tip) || null;
if (tip < 0) fail("Invalid tip");
+ console.log("AMT", amount);
if (!amount || amount < 0) fail("Amount must be greater than zero");
let ourfee: any = [types.bitcoin, types.liquid, types.lightning].includes(
@@ -366,7 +367,10 @@ export let sendOnchain = async (params) => {
if (!hex) ({ hex } = await build(params));
let { tx, type } = await decode(hex);
- let node = rpc({ ...config[type], wallet: account });
+ let node =
+ account === user.id
+ ? rpc(config[type])
+ : rpc({ ...config[type], wallet: account });
let { txid } = tx;
try {
@@ -426,7 +430,9 @@ export let sendOnchain = async (params) => {
if (invoice?.account === account)
fail("Cannot send to internal address");
- if ((await node.getAddressInfo(address)).ismine) change += sats(value);
+ if ((await node.getAddressInfo(address)).ismine) {
+ change += sats(value);
+ }
}
fee = totalIn - total;
@@ -611,9 +617,20 @@ let getAddressType = async (a) => {
}
};
-export let build = async ({ account, amount, address, feeRate, subtract }) => {
+export let build = async ({
+ account,
+ amount,
+ address,
+ feeRate,
+ subtract,
+ user,
+}) => {
let type = await getAddressType(address);
- let node = rpc({ ...config[type], wallet: account });
+ let node =
+ account === user.id
+ ? rpc(config[type])
+ : rpc({ ...config[type], wallet: account });
+
amount = parseInt(amount);
if (amount < 0) fail("invalid amount");
@@ -649,6 +666,7 @@ export let build = async ({ account, amount, address, feeRate, subtract }) => {
fee = sats(tx.fee);
} catch (e) {
if (e.message.startsWith("Insufficient")) subtract = true;
+ else throw e;
}
let balance = await g(`balance:${account}`);