Skip to content

Commit 7eb5234

Browse files
committed
refactor bolt12/bolt11 client parser lib
1 parent 7ab3099 commit 7eb5234

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

api/resolvers/wallet.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import {
1313
import { amountSchema, validateSchema, withdrawlSchema, lnAddrSchema } from '@/lib/validate'
1414
import assertGofacYourself from './ofac'
1515
import assertApiKeyNotPermitted from './apiKey'
16-
import { bolt11Tags, isBolt11 } from '@/lib/bolt/bolt11-tags'
17-
import { bolt12Info } from '@/lib/bolt/bolt12-info'
16+
import { getInvoiceDescription } from '@/lib/bolt/bolt-info'
1817
import { finalizeHodlInvoice } from '@/worker/wallet'
1918
import walletDefs from '@/wallets/server'
2019
import { generateResolverName, generateTypeDefName } from '@/wallets/graphql'
@@ -380,7 +379,7 @@ const resolvers = {
380379
f = { ...f, ...f.other }
381380

382381
if (f.bolt11) {
383-
f.description = isBolt11(f.bolt11) ? bolt11Tags(f.bolt11).description : bolt12Info(f.bolt11).description
382+
f.description = getInvoiceDescription(f.bolt11)
384383
}
385384

386385
switch (f.type) {

components/bolt11-info.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import AccordianItem from './accordian-item'
22
import { CopyInput } from './form'
3-
import { bolt11Tags, isBolt11 } from '@/lib/bolt/bolt11-tags'
4-
import { bolt12Info } from '@/lib/bolt/bolt12-info'
3+
import { getInvoiceDescription, getInvoicePaymentHash } from '@/lib/bolt/bolt-info'
54

65
export default ({ bolt11, preimage, children }) => {
76
let description, paymentHash
87
if (bolt11) {
9-
({ description, payment_hash: paymentHash } = isBolt11(bolt11) ? bolt11Tags(bolt11) : bolt12Info(bolt11))
8+
description = getInvoiceDescription(bolt11)
9+
paymentHash = getInvoicePaymentHash(bolt11)
1010
}
1111

1212
return (

lib/bolt/bolt-info.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getBolt11Description, getBolt11PaymentHash } from '@/lib/bolt/bolt11-tags'
2+
import { getBolt12Description, getBolt12PaymentHash, isBolt12 } from '@/lib/bolt/bolt12-info'
3+
4+
export function getInvoiceDescription (bolt) {
5+
if (isBolt12(bolt)) return getBolt12Description(bolt)
6+
return getBolt11Description(bolt)
7+
}
8+
9+
export function getInvoicePaymentHash (bolt) {
10+
if (isBolt12(bolt)) return getBolt12PaymentHash(bolt)
11+
return getBolt11PaymentHash(bolt)
12+
}

lib/bolt/bolt11-tags.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
/* eslint-disable camelcase */
12
import { decode } from 'bolt11'
23
import { bolt11InvoiceSchema } from '@/lib/validate'
34

45
export function isBolt11 (request) {
56
return bolt11InvoiceSchema.isValidSync(request)
67
}
78

8-
export function bolt11Tags (bolt11) {
9+
function bolt11Tags (bolt11) {
910
if (!isBolt11(bolt11)) throw new Error('not a bolt11 invoice')
1011
return decode(bolt11).tagsObject
1112
}
13+
14+
export function getBolt11Description (bolt11) {
15+
const { description } = bolt11Tags(bolt11)
16+
return description
17+
}
18+
19+
export function getBolt11PaymentHash (bolt11) {
20+
const { payment_hash } = bolt11Tags(bolt11)
21+
return payment_hash
22+
}

lib/bolt/bolt12-info.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,26 @@ export function isBolt12 (invoice) {
1919
return isBolt12Offer(invoice) || isBolt12Invoice(invoice)
2020
}
2121

22-
export function bolt12Info (bolt12) {
22+
export function getBolt12Description (bolt12) {
2323
if (!isBolt12(bolt12)) throw new Error('not a bolt12 invoice or offer')
2424
const buf = bech32b12.decode(bolt12.substring(4)/* remove lni1 or lno1 prefix */)
2525
const tlv = deserializeTLVStream(buf)
26-
27-
const info = {
28-
description: '',
29-
payment_hash: ''
30-
}
31-
26+
let description = ''
3227
for (const { type, value } of tlv) {
3328
if (type === TYPE_DESCRIPTION) {
34-
info.description = value.toString() || info.description
29+
description = value.toString() || description
3530
} else if (type === TYPE_PAYER_NOTE) {
36-
info.description = value.toString() || info.description
37-
} else if (type === TYPE_PAYMENT_HASH) {
38-
info.payment_hash = value.toString('hex')
31+
description = value.toString() || description
32+
break
3933
}
4034
}
35+
return description
36+
}
4137

42-
return info
38+
export function getBolt12PaymentHash (bolt12) {
39+
if (!isBolt12(bolt12)) throw new Error('not a bolt12 invoice or offer')
40+
const buf = bech32b12.decode(bolt12.substring(4)/* remove lni1 or lno1 prefix */)
41+
const tlv = deserializeTLVStream(buf)
42+
const paymentHash = tlv.find(({ type }) => type === TYPE_PAYMENT_HASH)
43+
return paymentHash?.value?.toString('hex')
4344
}

0 commit comments

Comments
 (0)