-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathbolt12.js
132 lines (119 loc) · 4.66 KB
/
bolt12.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-disable camelcase */
import { payViaBolt12PaymentRequest, decodeBolt12Invoice } from '@/api/lib/lndk'
import { isBolt12Invoice, isBolt12Offer, isBolt12 } from '@/lib/bolt/bolt12-info'
import { toPositiveNumber } from '@/lib/format'
import { estimateRouteFee } from '@/api/lnd'
export { isBolt12Invoice, isBolt12Offer, isBolt12 }
export async function payBolt12 ({ lndk, request: invoice, max_fee, max_fee_mtokens }) {
if (!lndk) throw new Error('lndk required') // check if forgot to pass lndk
if (!isBolt12Invoice(invoice)) throw new Error('not a bolt12 invoice')
return await payViaBolt12PaymentRequest({ lndk, request: invoice, max_fee, max_fee_mtokens })
}
export async function parseBolt12 ({ lndk, request: invoice }) {
if (!lndk) throw new Error('lndk required') // check if forgot to pass lndk
if (!isBolt12Invoice(invoice)) throw new Error('not a bolt12 request')
const decodedInvoice = await decodeBolt12Invoice({ lndk, request: invoice })
return convertBolt12RequestToLNRequest(decodedInvoice)
}
export async function estimateBolt12RouteFee ({ lnd, lndk, destination, tokens, mtokens, request, timeout }) {
if (!lndk) throw new Error('lndk required') // check if forgot to pass lndk
if (!lnd) throw new Error('lnd required') // check if forgot to pass lnd
if (request && !isBolt12Invoice(request)) throw new Error('not a bolt12 request')
const { amount_msats, node_id } = request ? await decodeBolt12Invoice({ lndk, request }) : {}
// extract mtokens and destination from invoice if they are not provided
if (!tokens && !mtokens) mtokens = toPositiveNumber(amount_msats)
destination ??= Buffer.from(node_id.key).toString('hex')
if (!destination) throw new Error('no destination provided')
if (!tokens && !mtokens) throw new Error('no tokens amount provided')
return await estimateRouteFee({ lnd, destination, tokens, mtokens, timeout })
}
const featureBitTypes = {
0: 'DATALOSS_PROTECT_REQ',
1: 'DATALOSS_PROTECT_OPT',
3: 'INITIAL_ROUTING_SYNC',
4: 'UPFRONT_SHUTDOWN_SCRIPT_REQ',
5: 'UPFRONT_SHUTDOWN_SCRIPT_OPT',
6: 'GOSSIP_QUERIES_REQ',
7: 'GOSSIP_QUERIES_OPT',
8: 'TLV_ONION_REQ',
9: 'TLV_ONION_OPT',
10: 'EXT_GOSSIP_QUERIES_REQ',
11: 'EXT_GOSSIP_QUERIES_OPT',
12: 'STATIC_REMOTE_KEY_REQ',
13: 'STATIC_REMOTE_KEY_OPT',
14: 'PAYMENT_ADDR_REQ',
15: 'PAYMENT_ADDR_OPT',
16: 'MPP_REQ',
17: 'MPP_OPT',
18: 'WUMBO_CHANNELS_REQ',
19: 'WUMBO_CHANNELS_OPT',
20: 'ANCHORS_REQ',
21: 'ANCHORS_OPT',
22: 'ANCHORS_ZERO_FEE_HTLC_REQ',
23: 'ANCHORS_ZERO_FEE_HTLC_OPT',
24: 'ROUTE_BLINDING_REQUIRED',
25: 'ROUTE_BLINDING_OPTIONAL',
30: 'AMP_REQ',
31: 'AMP_OPT'
}
const chainsMap = {
'06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f': 'regtest',
'43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000': 'testnet',
'6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000': 'mainnet'
}
async function convertBolt12RequestToLNRequest (decodedInvoice) {
const {
amount_msats,
description,
node_id,
chain,
payment_hash,
created_at,
relative_expiry,
features,
payer_note,
payment_paths,
invoice_hex_str
} = decodedInvoice
// convert from lndk response to ln-service parsePaymentRequest output layout
let minCltvDelta
for (const path of payment_paths) {
const info = path.blinded_pay_info
if (minCltvDelta === undefined || info.cltv_expiry_delta < minCltvDelta) {
minCltvDelta = info.cltv_expiry_delta
}
}
const out = {
created_at: new Date(created_at * 1000).toISOString(),
// [chain_addresses]
cltv_delta: minCltvDelta,
description: payer_note || description,
// [description_hash]
destination: Buffer.from(node_id.key).toString('hex'),
expires_at: new Date((created_at + relative_expiry) * 1000).toISOString(),
features: features.map(bit => ({
bit,
is_required: (bit % 2) === 0,
type: featureBitTypes[bit]
})),
id: Buffer.from(payment_hash.hash).toString('hex'),
is_expired: new Date().getTime() / 1000 > created_at + relative_expiry,
// [metadata]
mtokens: String(amount_msats),
network: chainsMap[chain],
payment: invoice_hex_str,
routes: payment_paths.map((path) => {
const info = path.blinded_pay_info
const { introduction_node } = path.blinded_path
return {
base_fee_mtokens: String(info.fee_base_msat),
cltv_delta: info.cltv_expiry_delta,
public_key: Buffer.from(introduction_node.node_id.key).toString('hex')
}
}),
safe_tokens: Math.round(toPositiveNumber(BigInt(amount_msats)) / 1000),
tokens: Math.floor(toPositiveNumber(BigInt(amount_msats)) / 1000),
bolt12: decodedInvoice
}
return out
}