MCPcopy
hub / github.com/BlueWallet/BlueWallet / createInvoice

Function createInvoice

screen/lnd/lndCreateInvoice.tsx:237–321  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

235 };
236
237 const createInvoice = async () => {
238 setIsLoading(true);
239 try {
240 let invoiceAmount: string | number = amount ?? 0;
241 switch (unit) {
242 case BitcoinUnit.SATS:
243 invoiceAmount = parseInt(String(invoiceAmount), 10); // basically nop
244 break;
245 case BitcoinUnit.BTC:
246 invoiceAmount = btcToSatoshi(invoiceAmount);
247 break;
248 case BitcoinUnit.LOCAL_CURRENCY:
249 // trying to fetch cached sat equivalent for this fiat amount
250 invoiceAmount = AmountInput.getCachedSatoshis(String(invoiceAmount)) || btcToSatoshi(fiatToBTC(+invoiceAmount));
251 break;
252 }
253
254 if (lnurlParams) {
255 invoiceAmount = +invoiceAmount;
256 const { min, max } = lnurlParams;
257 if (invoiceAmount < min || invoiceAmount > max) {
258 let text;
259 if (invoiceAmount < min) {
260 text =
261 unit === BitcoinUnit.SATS
262 ? loc.formatString(loc.receive.minSats, { min })
263 : loc.formatString(loc.receive.minSatsFull, { min, currency: formatBalance(min, unit) });
264 } else {
265 text =
266 unit === BitcoinUnit.SATS
267 ? loc.formatString(loc.receive.maxSats, { max })
268 : loc.formatString(loc.receive.maxSatsFull, { max, currency: formatBalance(max, unit) });
269 }
270 triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
271 presentAlert({ message: text });
272 setIsLoading(false);
273 return;
274 }
275 }
276
277 assert(wallet.current instanceof LightningArkWallet || wallet.current instanceof LightningCustodianWallet);
278
279 const invoiceRequest = await wallet.current?.addInvoice(+invoiceAmount, description);
280 triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess);
281
282 // lets decode payreq and subscribe groundcontrol so we can receive push notification when our invoice is paid
283 const decoded = await wallet.current?.decodeInvoice(invoiceRequest);
284 tryToObtainPermissions()
285 .then(res => majorTomToGroundControl([], [decoded.payment_hash], []))
286 .catch(err => console.error(err.message));
287
288 // send to lnurl-withdraw callback url if that exists
289 if (lnurlParams) {
290 const { callback, k1 } = lnurlParams;
291 const callbackUrl = callback + (callback.indexOf('?') !== -1 ? '&' : '?') + 'k1=' + k1 + '&pr=' + invoiceRequest;
292
293 const resp = await fetch(callbackUrl, { method: 'GET' });
294 if (resp.status >= 300) {

Callers

nothing calls this directly

Calls 15

btcToSatoshiFunction · 0.90
fiatToBTCFunction · 0.90
formatBalanceFunction · 0.90
tryToObtainPermissionsFunction · 0.90
majorTomToGroundControlFunction · 0.90
triggerHapticFeedbackFunction · 0.85
navigateFunction · 0.85
setIsLoadingFunction · 0.50
fetchFunction · 0.50
addInvoiceMethod · 0.45
decodeInvoiceMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected