Skip to content

Commit

Permalink
Remove lightning prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Aug 29, 2024
1 parent 7c210b8 commit 04cec05
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion bot/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
} = require('../util');
const ordersActions = require('./ordersActions');
const OrderEvents = require('./modules/events/orders');
const { removeLightningPrefix } = require('../util');

const { resolvLightningAddress } = require('../lnurl/lnurl-pay');
const { logger } = require('../logger');
Expand All @@ -32,7 +33,7 @@ const waitPayment = async (ctx, bot, buyer, seller, order, buyerInvoice) => {
return;
}

order.buyer_invoice = buyerInvoice;
order.buyer_invoice = removeLightningPrefix(buyerInvoice);
// We need the i18n context to send the message with the correct language
const i18nCtx = await getUserI18nContext(seller);
// If the buyer is the creator, at this moment the seller already paid the hold invoice
Expand Down
12 changes: 9 additions & 3 deletions bot/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const { parsePaymentRequest } = require('invoices');
const { ObjectId } = require('mongoose').Types;
const messages = require('./messages');
const { Order, User, Community } = require('../models');
const { isIso4217, isDisputeSolver } = require('../util');
const {
isIso4217,
isDisputeSolver,
removeLightningPrefix,
} = require('../util');
const { existLightningAddress } = require('../lnurl/lnurl-pay');
const { logger } = require('../logger');

Expand Down Expand Up @@ -305,7 +309,8 @@ const validateLightningAddress = async lightningAddress => {

const validateInvoice = async (ctx, lnInvoice) => {
try {
const invoice = parsePaymentRequest({ request: lnInvoice });
const checkedPrefixlnInvoice = removeLightningPrefix(lnInvoice);
const invoice = parsePaymentRequest({ request: checkedPrefixlnInvoice });
const latestDate = new Date(
Date.now() + parseInt(process.env.INVOICE_EXPIRATION_WINDOW)
).toISOString();
Expand Down Expand Up @@ -344,7 +349,8 @@ const validateInvoice = async (ctx, lnInvoice) => {

const isValidInvoice = async (ctx, lnInvoice) => {
try {
const invoice = parsePaymentRequest({ request: lnInvoice });
const checkedPrefixlnInvoice = removeLightningPrefix(lnInvoice);
const invoice = parsePaymentRequest({ request: checkedPrefixlnInvoice });
const latestDate = new Date(
Date.now() + parseInt(process.env.INVOICE_EXPIRATION_WINDOW)
).toISOString();
Expand Down
12 changes: 12 additions & 0 deletions util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,15 @@ exports.getStars = (rate, totalReviews) => {
exports.removeAtSymbol = text => {
return text[0] === '@' ? text.slice(1) : text;
};

exports.removeLightningPrefix = invoice => {
const prefix = 'lightning:';

// Check if the invoice starts with the prefix
if (invoice.startsWith(prefix)) {
return invoice.substring(prefix.length);
}

// Return the invoice as is if no prefix is found
return invoice;
};

0 comments on commit 04cec05

Please sign in to comment.