Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encoded LNURL support #310

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"expo-status-bar": "~1.6.0",
"expo-updates": "~0.18.17",
"i18next": "^23.7.8",
"js-lnurl": "^0.6.0",
"lottie-react-native": "5.1.6",
"nostr-tools": "^1.17.0",
"react": "^18.2.0",
Expand Down Expand Up @@ -152,7 +153,7 @@
"blind-signatures",
"lightning-network"
],
"version": "0.3.0",
"version": "0.3.1",
"license": "AGPL-3.0-only",
"bugs": {
"url": "https://github.com/cashubtc/eNuts/issues"
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Payment/Processing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { addLnPaymentToHistory } from '@store/HistoryStore'
import { addToHistory, updateLatestHistory } from '@store/latestHistoryEntries'
import { getDefaultMint } from '@store/mintStore'
import { globals } from '@styles'
import { decodeLnInvoice, getInvoiceFromLnurl, isErr, isLnurl, isNum, uniqByIContacts } from '@util'
import { decodeLnInvoice, getInvoiceFromLnurl, isErr, isLnurlAddress, isNum, uniqByIContacts } from '@util'
import { autoMintSwap, checkFees, fullAutoMintSwap, getHighestBalMint, payLnInvoice, requestMint, sendToken } from '@wallet'
import { useEffect, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down Expand Up @@ -99,7 +99,7 @@ export default function ProcessingScreen({ navigation, route }: TProcessingPageP
const handleMelting = async () => {
let invoice = ''
// recipient can be a LNURL (address) or a LN invoice
if (recipient?.length && isLnurl(recipient)) {
if (recipient?.length && isLnurlAddress(recipient)) {
try {
invoice = await getInvoiceFromLnurl(recipient, +amount)
if (!invoice?.length) {
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Payment/Send/CoinSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useInitialURL } from '@src/context/Linking'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { globals } from '@styles'
import { formatInt, formatMintUrl, formatSatStr, getSelectedAmount, isLnurl, isNum } from '@util'
import { formatInt, formatMintUrl, formatSatStr, getSelectedAmount, isLnurlAddress, isNum } from '@util'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { ScrollView, View } from 'react-native'
Expand Down Expand Up @@ -60,7 +60,7 @@ export default function CoinSelectionScreen({ navigation, route }: TCoinSelectio

const getRecipient = () => {
if (recipient) {
return !isLnurl(recipient) ? truncateStr(recipient, 16) : recipient
return !isLnurlAddress(recipient) ? truncateStr(recipient, 16) : recipient
}
const npub = npubEncode(nostr?.contact?.hex ?? '')
const receiverName = getNostrUsername(nostr?.contact)
Expand Down
6 changes: 3 additions & 3 deletions src/screens/Payment/Send/Inputfield.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { usePromptContext } from '@src/context/Prompt'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { globals } from '@styles'
import { decodeLnInvoice, getStrFromClipboard, isErr, isLnInvoice, isLnurl, openUrl } from '@util'
import { decodeLnInvoice, getStrFromClipboard, isErr, isLnInvoice, isLnurlAddress, openUrl } from '@util'
import { checkFees } from '@wallet'
import { createRef, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down Expand Up @@ -43,7 +43,7 @@ export default function InputfieldScreen({ navigation, route }: TMeltInputfieldP
if (!clipboard) { return }
setInput(clipboard)
// pasted LNURL address which does not need decoding
if (isLnurl(clipboard)) { return }
if (isLnurlAddress(clipboard)) { return }
// pasted LN invoice
await handleInvoicePaste(clipboard)
}
Expand Down Expand Up @@ -73,7 +73,7 @@ export default function InputfieldScreen({ navigation, route }: TMeltInputfieldP
openPromptAutoClose({ msg: isErr(e) ? e.message : t('deepLinkErr') }))
}
// user pasted a LNURL, we need to get the amount by the user
if (isLnurl(input)) {
if (isLnurlAddress(input)) {
return navigation.navigate('selectAmount', { mint, balance, isMelt: true, lnurl: input })
}
// not enough funds
Expand Down
6 changes: 6 additions & 0 deletions src/screens/QRScan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { NS } from '@src/i18n'
import { getDefaultMint } from '@store/mintStore'
import { globals, mainColors } from '@styles'
import { decodeLnInvoice, extractStrFromURL, hasTrustedMint, isCashuToken, isNull, isStr, isUrl } from '@util'
import { isLnurlEncoded } from '@util/lnurl'
import { getTokenInfo } from '@wallet/proofs'
import { BarCodeScanner, PermissionStatus } from 'expo-barcode-scanner'
import { Camera, FlashMode } from 'expo-camera'
Expand Down Expand Up @@ -113,6 +114,11 @@ export default function QRScanPage({ navigation, route }: TQRScanPageProps) {
if (isUrl(data) && new URL(data).protocol === 'https:') {
return navigation.navigate('mint confirm', { mintUrl: data })
}
// handle LNURL
if (isLnurlEncoded(data)) {
// TODO decode and handle lnurl
return
}
// handle LN invoice
try {
const invoice = extractStrFromURL(data) || data
Expand Down
4 changes: 2 additions & 2 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export function vib(pattern?: number | number[]) {
Vibration.vibrate(pattern)
}

export function isLnurl(addr: string) {
export function isLnurlAddress(addr: string) {
const [user, host] = addr.split('@')
return addr.includes('.')
&& addr.split('@').length === 2
Expand All @@ -182,7 +182,7 @@ export function hasTrustedMint(uMints: ({ mintUrl: string } | string)[], tMints:

export async function getInvoiceFromLnurl(address: string, amount: number) {
try {
if (!isLnurl(address)) { throw new Error('invalid address') }
if (!isLnurlAddress(address)) { throw new Error('invalid address') }
const [user, host] = address.split('@')
amount *= 1000
const resp = await fetch(`https://${host}/.well-known/lnurlp/${user}`)
Expand Down
21 changes: 21 additions & 0 deletions src/util/lnurl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function isLnurlEncoded(str: string) {
str = str?.trim()
const uriPrefixes = [
'lightning:',
'lightning=',
'lightning://',
'lnurlp://',
'lnurlp=',
'lnurlp:',
'lnurl:',
'lnurl=',
'lnurl://',
]
for (const prefix of uriPrefixes) {
if (str?.length && str.startsWith(prefix)) {
str = str.slice(prefix.length).trim()
break
}
}
return str.toLowerCase().startsWith('lnurl1')
}
Loading