aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-09-08 20:57:06 -0700
committerAdam Soltys <[email protected]>2024-09-08 20:57:06 -0700
commit5c105c8f4738ab1ead3c5b92f465f899cf0a7446 (patch)
tree9cea2d1b46cdab9869f742e3286c9fb0762f8948 /lib
parent92bd9085a576517e06dc000d1e1ff11d27bc34b4 (diff)
downloadcoinos-server-5c105c8f4738ab1ead3c5b92f465f899cf0a7446.tar.gz
coinos-server-5c105c8f4738ab1ead3c5b92f465f899cf0a7446.zip
refactor rpc helper, accounts endpoints
Diffstat (limited to 'lib')
-rw-r--r--lib/invoices.ts7
-rw-r--r--lib/lightning.ts2
-rw-r--r--lib/payments.ts29
-rw-r--r--lib/rpc.ts43
-rw-r--r--lib/utils.ts1
5 files changed, 33 insertions, 49 deletions
diff --git a/lib/invoices.ts b/lib/invoices.ts
index 6934a2d4..013cb975 100644
--- a/lib/invoices.ts
+++ b/lib/invoices.ts
@@ -5,13 +5,10 @@ import { getUser, bip21, fail, SATS } from "$lib/utils";
import { types } from "$lib/payments";
import { v4 } from "uuid";
import rpc from "$lib/rpc";
-
-import bc_ from "$lib/bitcoin";
-import lq_ from "$lib/liquid";
import ln from "$lib/ln";
-let bc = bc_ as any;
-let lq = lq_ as any;
+let bc = rpc(config.bitcoin);
+let lq = rpc(config.liquid);
export let generate = async ({ invoice, user, sender = undefined }) => {
let {
diff --git a/lib/lightning.ts b/lib/lightning.ts
index 2d44581d..f86b9a2d 100644
--- a/lib/lightning.ts
+++ b/lib/lightning.ts
@@ -44,7 +44,7 @@ export async function listenForLightning() {
if (p) return warn("already processed", bolt11);
- await credit(bolt11, received, invoice.memo, preimage, types.lightning);
+ await credit({ hash:bolt11, amount:received, memo:invoice.memo, ref:preimage, type:types.lightning});
} catch (e) {
console.log(e);
err("problem receiving lightning payment", e.message);
diff --git a/lib/payments.ts b/lib/payments.ts
index 5d578af9..36f18e71 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -133,7 +133,14 @@ export let debit = async ({
return p;
};
-export let credit = async (hash, amount, memo, ref, type = types.internal) => {
+export let credit = async ({
+ hash,
+ amount,
+ memo = undefined,
+ ref = undefined,
+ type = types.internal,
+ account = undefined,
+}) => {
amount = parseInt(amount) || 0;
let inv = await getInvoice(hash);
@@ -169,6 +176,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
let id = v4();
let p = {
+ account,
id,
iid: inv.id,
hash,
@@ -208,8 +216,8 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
m.set(`invoice:${inv.id}`, JSON.stringify(inv))
.set(`payment:${p.id}`, JSON.stringify(p))
- .lPush(`${uid}:payments`, p.id)
- .incrBy(`${balanceKey}:${uid}`, amount)
+ .lPush(`${account || uid}:payments`, p.id)
+ .incrBy(`${balanceKey}:${account || uid}`, amount)
.exec();
emit(uid, "payment", p);
@@ -355,7 +363,7 @@ export let sendOnchain = async (params) => {
if (!hex) ({ hex } = await build(params));
let { tx, type } = await decode(hex);
- let node = getNode(type);
+ let node = rpc(config[type]);
let { txid } = tx;
try {
@@ -582,16 +590,6 @@ export let sendLightning = async ({
}
};
-export let getNode = (type, wallet = undefined) => {
- if (type === types.bitcoin) {
- return bc;
- } else if (type === types.liquid) {
- return lq;
- } else if (wallet) {
- return rpc({ ...config.bitcoin, wallet });
- } else fail("unrecognized transaction type");
-};
-
let getAddressType = async (a) => {
try {
await bc.getAddressInfo(a);
@@ -608,8 +606,7 @@ let getAddressType = async (a) => {
export let build = async ({ amount, address, feeRate, user, subtract }) => {
let type = await getAddressType(address);
-
- let node = getNode(type);
+ let node = rpc(config[type]);
amount = parseInt(amount);
if (amount < 0) fail("invalid amount");
diff --git a/lib/rpc.ts b/lib/rpc.ts
index 0346a3cd..909ec96f 100644
--- a/lib/rpc.ts
+++ b/lib/rpc.ts
@@ -1,28 +1,19 @@
import got from "got";
+import { fail } from "$lib/utils";
+import { err } from "$lib/logging";
-export default ({ host, port, wallet, username, password }) =>
- new Proxy(
- {},
- {
- get:
- (_, prop) =>
- (...params) =>
- ((method, ...params) => {
- let url = `http://${host}:${port}/wallet/${wallet}`;
- return got
- .post(url, {
- json: {
- method,
- params,
- },
- headers: {
- authorization: `Basic ${Buffer.from(
- `${username}:${password}`,
- ).toString("base64")}`,
- },
- })
- .json()
- .then(({ result }) => result);
- })((prop as any).toLowerCase(), ...params),
- },
- );
+export default ({ host, port, wallet, username, password }): any => {
+ let url = `http://${host}:${port}/wallet/${wallet}`;
+ let token = btoa(`${username}:${password}`);
+ let headers = { authorization: `Basic ${token}` };
+ let p = (json) => got.post(url, { json, headers }).json();
+
+ let get =
+ (_, prop: any) =>
+ (...params) =>
+ p({ method: prop.toLowerCase(), params })
+ .then((res: any) => res.result)
+ .catch((e) => (err(e.message), fail(`Failed to call ${prop}`)));
+
+ return new Proxy({}, { get });
+};
diff --git a/lib/utils.ts b/lib/utils.ts
index ada32521..b0875873 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -1,6 +1,5 @@
import { randomBytes, bytesToHex } from "@noble/hashes/utils";
import migrate from "$lib/migrate";
-import store from "$lib/store";
import { g, s } from "$lib/db";
import config from "$config";