diff options
| author | Adam Soltys <[email protected]> | 2022-09-30 15:07:30 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-09-30 15:09:55 -0700 |
| commit | c3efae9d1720c6f9eb6f826c4f1c6d5f3e259407 (patch) | |
| tree | 2ac3b89ba3c0febe7d45e25eb4515d42e7a27188 /lib/send.js | |
| parent | 6dee51ad1c2a74bfb0b57417dae9bce6931cd9f1 (diff) | |
| download | coinos-server-c3efae9d1720c6f9eb6f826c4f1c6d5f3e259407.tar.gz coinos-server-c3efae9d1720c6f9eb6f826c4f1c6d5f3e259407.zip | |
derive address/payreq server side
Diffstat (limited to 'lib/send.js')
| -rw-r--r-- | lib/send.js | 32 |
1 files changed, 13 insertions, 19 deletions
diff --git a/lib/send.js b/lib/send.js index f20185e5..e7255219 100644 --- a/lib/send.js +++ b/lib/send.js @@ -66,10 +66,11 @@ export default async (amount, memo, hash, user) => { }); conversionFeeDeduction = Math.min(account.lightning_credits, conversionFee); + total = amount + conversionFee - conversionFeeDeduction; if ( !account || - account.balance < amount + conversionFee - conversionFeeDeduction + account.balance < total ) { warn( "user attempting to send more than they have", @@ -81,12 +82,12 @@ export default async (amount, memo, hash, user) => { } await account.decrement( - { balance: amount, lightning_credits: conversionFeeDeduction }, + { balance: total, lightning_credits: conversionFeeDeduction }, { transaction } ); await account.reload({ transaction }); - l("debited account", account.id); + l("debited account", account.id, total); let tokens; if (!payreq.satoshis) tokens = amount; @@ -100,26 +101,22 @@ export default async (amount, memo, hash, user) => { if (!m) throw new Error(`lightning payment failed ${amount} ${hash}`); if (m.payment_error) throw new Error(m.payment_error); - ({ fee, secret: preimage, tokens: total } = m); + ({ fee, secret, tokens: total } = m); + preimage = secret.toString("hex"); } else { - m = await ln.pay(hash, tokens ? `${tokens}sats` : undefined); - let sent; - ({ - msatoshi: total, - msatoshi_sent: sent, - payment_preimage: preimage - } = m); - - fee = parseInt((sent - total) / 1000); - total = parseInt(total / 1000); + let { msatoshi, msatoshi_sent, payment_preimage } = await ln.pay(hash, tokens ? `${tokens}sats` : undefined); + preimage = payment_preimage; + fee = parseInt((msatoshi_sent - msatoshi) / 1000); + total = parseInt(msatoshi / 1000); } - preimage = preimage.toString("hex"); - if (store.seen.includes(preimage)) { warn("duplicate payment detected", preimage); throw new Error("Duplicate payment detected"); } + + store.seen.push(preimage); + store.seen.push(hash); }); await db.transaction(async transaction => { @@ -226,9 +223,6 @@ export default async (amount, memo, hash, user) => { emit(user.username, "account", payment.account); emit(user.username, "payment", payment); - store.seen.push(preimage); - store.seen.push(hash); - l("sent lightning", user.username, total - fee, fee); }); |
