Skip to content

Commit

Permalink
deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Dec 20, 2024
1 parent 6c863d8 commit 0e56bc8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
5 changes: 4 additions & 1 deletion lib/bolt11-tags.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { decode } from 'bolt11'
import { bolt11InvoiceSchema } from '@/lib/validate'

export function isBolt11 (request) {
return request.startsWith('lnbc') || request.startsWith('lntb') || request.startsWith('lntbs') || request.startsWith('lnbcrt')
if (!request.startsWith('lnbc') && !request.startsWith('lntb') && !request.startsWith('lntbs') && !request.startsWith('lnbcrt')) return false
bolt11InvoiceSchema.validateSync(request)
return true
}

export function bolt11Tags (bolt11) {
Expand Down
9 changes: 2 additions & 7 deletions lib/bolt11.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/* eslint-disable camelcase */
import { payViaPaymentRequest, parsePaymentRequest } from 'ln-service'
import { bolt11InvoiceSchema } from './validate'

export function isBolt11 (request) {
if (!request.startsWith('lnbc') && !request.startsWith('lntb') && !request.startsWith('lntbs') && !request.startsWith('lnbcrt')) return false
bolt11InvoiceSchema.validateSync(request)
return true
}
import { isBolt11 } from '@/lib/bolt11-tags'
export { isBolt11 }

export async function parseBolt11 ({ request }) {
if (!isBolt11(request)) throw new Error('not a bolt11 invoice')
Expand Down
16 changes: 15 additions & 1 deletion lib/bolt12-info.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { deserializeTLVStream } from './tlv'
import * as bech32b12 from '@/lib/bech32b12'

import { bolt12OfferSchema, bolt12InvoiceSchema } from './validate'

const TYPE_DESCRIPTION = 10n
const TYPE_PAYER_NOTE = 89n
const TYPE_PAYMENT_HASH = 168n

export function isBolt12Offer (invoice) {
if (!invoice.startsWith('lno1')) return false
bolt12OfferSchema.validateSync(invoice)
return true
}

export function isBolt12Invoice (invoice) {
if (!invoice.startsWith('lni1')) return false
bolt12InvoiceSchema.validateSync(invoice)
return true
}

export function isBolt12 (invoice) {
return invoice.startsWith('lni1') || invoice.startsWith('lno1')
return isBolt12Offer(invoice) || isBolt12Invoice(invoice)
}

export function bolt12Info (bolt12) {
Expand Down
19 changes: 2 additions & 17 deletions lib/bolt12.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
/* eslint-disable camelcase */

import { payViaBolt12PaymentRequest, parseBolt12Request } from '@/lib/lndk'
import { bolt12OfferSchema, bolt12InvoiceSchema } from './validate'

export function isBolt12Offer (invoice) {
if (!invoice.startsWith('lno1')) return false
bolt12OfferSchema.validateSync(invoice)
return true
}

export function isBolt12Invoice (invoice) {
if (!invoice.startsWith('lni1')) return false
bolt12InvoiceSchema.validateSync(invoice)
return true
}

export function isBolt12 (invoice) {
return isBolt12Offer(invoice) || isBolt12Invoice(invoice)
}
import { isBolt12Invoice, isBolt12Offer, isBolt12 } from '@/lib/bolt12-info'
export { isBolt12Invoice, isBolt12Offer, isBolt12 }

export async function payBolt12 ({ lnd, request: invoice, max_fee, max_fee_mtokens }) {
if (!isBolt12Invoice(invoice)) throw new Error('not a bolt12 invoice')
Expand Down

0 comments on commit 0e56bc8

Please sign in to comment.