-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathQrCode.tsx
More file actions
289 lines (262 loc) · 11.2 KB
/
QrCode.tsx
File metadata and controls
289 lines (262 loc) · 11.2 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { useCallback, useContext, useEffect, useState } from 'react'
import Button from '../../../components/Button'
import Padded from '../../../components/Padded'
import QrCode from '../../../components/QrCode'
import ButtonsOnBottom from '../../../components/ButtonsOnBottom'
import { FlowContext } from '../../../providers/flow'
import { NavigationContext, Pages } from '../../../providers/navigation'
import { WalletContext } from '../../../providers/wallet'
import { NotificationsContext } from '../../../providers/notifications'
import Header from '../../../components/Header'
import Content from '../../../components/Content'
import { consoleError } from '../../../lib/logs'
import { canBrowserShareData, shareData } from '../../../lib/share'
import ExpandAddresses from '../../../components/ExpandAddresses'
import FlexCol from '../../../components/FlexCol'
import { LimitsContext } from '../../../providers/limits'
import { Asset, Coin, ExtendedVirtualCoin } from '@arkade-os/sdk'
import LoadingLogo from '../../../components/LoadingLogo'
import { SwapsContext } from '../../../providers/swaps'
import { encodeBip21, encodeBip21Asset } from '../../../lib/bip21'
import { PendingChainSwap, PendingReverseSwap } from '@arkade-os/boltz-swap'
import { enableChainSwapsReceive, lnurlServerUrl } from '../../../lib/constants'
import { centsToUnits } from '../../../lib/assets'
import WarningBox from '../../../components/Warning'
import ErrorMessage from '../../../components/Error'
import { useLnurlSession } from '../../../hooks/useLnurlSession'
export default function ReceiveQRCode() {
const { navigate } = useContext(NavigationContext)
const { recvInfo, setRecvInfo } = useContext(FlowContext)
const { notifyPaymentReceived } = useContext(NotificationsContext)
const { arkadeSwaps, swapsInitError, connected, createBtcToArkSwap, createReverseSwap } = useContext(SwapsContext)
const { assetMetadataCache, svcWallet } = useContext(WalletContext)
const { validBtcToArk, validLnSwap, validUtxoTx, validVtxoTx, utxoTxsAllowed, vtxoTxsAllowed } =
useContext(LimitsContext)
const [sharing, setSharing] = useState(false)
// manage all possible receive methods
const { boardingAddr, offchainAddr, satoshis, assetId, addressError } = recvInfo
const assetMeta = assetId ? assetMetadataCache.get(assetId) : undefined
const isAssetReceive = assetId && assetId !== ''
const hasError = Boolean(addressError)
const [noPaymentMethods, setNoPaymentMethods] = useState(false)
const [arkAddress, setArkAddress] = useState(offchainAddr)
const [btcAddress, setBtcAddress] = useState(boardingAddr)
const [showQrCode, setShowQrCode] = useState(!satoshis)
const [swapsTimedOut, setSwapsTimedOut] = useState(false)
const [swapAddress, setSwapAddress] = useState('')
const [qrCodeValue, setQrCodeValue] = useState('')
const [bip21Uri, setBip21Uri] = useState('')
const [invoice, setInvoice] = useState('')
// LNURL session for amountless Lightning receives
const isAmountlessLnurl =
!satoshis && !isAssetReceive && !!lnurlServerUrl && connected && !!arkadeSwaps && !swapsInitError
const handleInvoiceRequest = useCallback(
async (req: { amountMsat: number }) => {
const sats = Math.floor(req.amountMsat / 1000)
const pendingSwap = await createReverseSwap(sats)
if (!pendingSwap) throw new Error('Failed to create reverse swap')
// Auto-claim in background
if (arkadeSwaps) {
arkadeSwaps
.waitAndClaim(pendingSwap)
.then(() => {
setRecvInfo({ ...recvInfo, satoshis: pendingSwap.response.onchainAmount })
notifyPaymentReceived(pendingSwap.response.onchainAmount)
navigate(Pages.ReceiveSuccess)
})
.catch((err) => consoleError(err, 'Error claiming LNURL reverse swap'))
}
return pendingSwap.response.invoice
},
[arkadeSwaps, createReverseSwap, setRecvInfo, recvInfo, navigate, notifyPaymentReceived],
)
const lnurlSession = useLnurlSession(isAmountlessLnurl, handleInvoiceRequest)
const createBtcAddress = () => {
return new Promise((resolve, reject) => {
if (!enableChainSwapsReceive) return reject()
if (!validBtcToArk(satoshis)) return reject()
createBtcToArkSwap(satoshis)
.then((result) => {
if (!result) throw new Error('Failed to create chain swap')
resolve(result.pendingSwap)
})
.catch((error) => {
consoleError(error, 'Error creating chain swap')
reject(error)
})
})
}
const createLightningInvoice = () => {
return new Promise((resolve, reject) => {
if (invoice) return reject() // invoice already exists, no need to create another
if (!validLnSwap(satoshis)) return reject()
createReverseSwap(satoshis)
.then((pendingSwap) => {
if (!pendingSwap) throw new Error('Failed to create reverse swap')
resolve(pendingSwap)
})
.catch((error) => {
consoleError(error, 'Error creating reverse swap:')
reject(error)
})
})
}
useEffect(() => {
if (isAssetReceive) return setShowQrCode(true)
if (!satoshis || !svcWallet) return
// LN is only expected when Boltz is enabled and this isn't an asset receive
const lnExpected = connected && !isAssetReceive
if (!arkadeSwaps) {
if (!lnExpected || swapsInitError) {
// LN not expected or already failed — show QR immediately
if (lnExpected && swapsInitError) {
consoleError(swapsInitError, 'Swaps unavailable, showing receive without swap options')
setSwapsTimedOut(true)
}
setShowQrCode(true)
return
}
// LN expected but swaps still initializing — wait up to 5s
const timeout = setTimeout(() => {
setSwapsTimedOut(true)
setShowQrCode(true)
}, 5_000)
return () => clearTimeout(timeout)
}
// arkadeSwaps is ready, generate swaps before showing QR to avoid QR code changing
setSwapsTimedOut(false)
Promise.allSettled([createBtcAddress(), createLightningInvoice()]).then(([btc, lightning]) => {
if (btc.status === 'fulfilled') {
const pendingSwap = btc.value as PendingChainSwap
const btcAddr = pendingSwap.response.lockupDetails.lockupAddress
setSwapAddress(btcAddr)
arkadeSwaps
.waitAndClaimArk(pendingSwap)
.then(() => {
setRecvInfo({ ...recvInfo, satoshis: pendingSwap.response.claimDetails.amount })
navigate(Pages.ReceiveSuccess)
})
.catch((error) => {
consoleError(error, 'Error claiming chain swap:')
})
}
if (lightning.status === 'fulfilled') {
const pendingSwap = lightning.value as PendingReverseSwap
const invoice = pendingSwap.response.invoice
setInvoice(invoice)
arkadeSwaps
.waitAndClaim(pendingSwap)
.then(() => {
setRecvInfo({ ...recvInfo, satoshis: pendingSwap.response.onchainAmount })
navigate(Pages.ReceiveSuccess)
})
.catch((error) => {
consoleError(error, 'Error claiming reverse swap:')
})
}
setShowQrCode(true)
})
}, [satoshis, svcWallet, arkadeSwaps, swapsInitError])
//
useEffect(() => {
if (!showQrCode) return
const arkAddress = validVtxoTx(satoshis) && vtxoTxsAllowed() ? offchainAddr : ''
const btcAddress = validUtxoTx(satoshis) && utxoTxsAllowed() ? swapAddress || boardingAddr : ''
const bip21uri = isAssetReceive
? encodeBip21Asset(arkAddress, assetId, centsToUnits(satoshis, assetMeta?.metadata?.decimals))
: encodeBip21(btcAddress, arkAddress, invoice, satoshis, lnurlSession.lnurl)
const hasLnurl = isAmountlessLnurl && lnurlSession.active
setNoPaymentMethods(!arkAddress && !btcAddress && !invoice && !hasLnurl && !isAssetReceive)
setArkAddress(arkAddress)
setBtcAddress(btcAddress)
setQrCodeValue(bip21uri)
setBip21Uri(bip21uri)
}, [showQrCode, swapAddress, invoice, lnurlSession.lnurl, lnurlSession.active, isAmountlessLnurl])
useEffect(() => {
if (!svcWallet) return
const listenForPayments = (event: MessageEvent) => {
let satoshis = 0
let receivedAssets: Asset[] = []
if (event.data && event.data.type === 'VTXO_UPDATE') {
const newVtxos = event.data.payload?.newVtxos
if (Array.isArray(newVtxos)) {
satoshis = (newVtxos as ExtendedVirtualCoin[]).reduce((acc, v) => acc + v.value, 0)
for (const v of newVtxos as ExtendedVirtualCoin[]) {
receivedAssets.push(...(v.assets ?? []))
}
} else {
consoleError('VTXO_UPDATE message has unexpected payload shape:', event.data.payload)
}
}
// reduce received assets to unique asset ids (sum amounts if same asset id)
receivedAssets = receivedAssets.reduce((acc, v) => {
const existing = acc.find((a) => a.assetId === v.assetId)
if (existing) {
existing.amount += v.amount
} else {
acc.push(v)
}
return acc
}, [] as Asset[])
if (event.data && event.data.type === 'UTXO_UPDATE') {
const coins = event.data.payload?.coins
if (Array.isArray(coins)) {
satoshis = (coins as Coin[]).reduce((acc, v) => acc + v.value, 0)
} else {
consoleError('UTXO_UPDATE message has unexpected payload shape:', event.data.payload)
}
}
if (satoshis || receivedAssets.length > 0) {
setRecvInfo({ ...recvInfo, satoshis, receivedAssets })
if (!isAssetReceive) notifyPaymentReceived(satoshis)
navigate(Pages.ReceiveSuccess)
}
}
navigator.serviceWorker.addEventListener('message', listenForPayments)
return () => {
navigator.serviceWorker.removeEventListener('message', listenForPayments)
}
}, [svcWallet])
const handleShare = () => {
setSharing(true)
shareData(data)
.catch(consoleError)
.finally(() => setSharing(false))
}
const data = { title: 'Receive', text: qrCodeValue }
const disabled = !canBrowserShareData(data) || sharing || hasError || noPaymentMethods
return (
<>
<Header text='Receive' back />
<Content>
<Padded>
{hasError ? (
<ErrorMessage error text={`Failed to get address: ${addressError}`} />
) : noPaymentMethods ? (
<div>No valid payment methods available for this amount</div>
) : qrCodeValue ? (
<FlexCol centered>
<QrCode value={qrCodeValue} />
<ExpandAddresses
bip21uri={bip21Uri}
boardingAddr={btcAddress}
offchainAddr={arkAddress}
invoice={invoice || ''}
lnurl={lnurlSession.lnurl}
onClick={setQrCodeValue}
/>
{swapsTimedOut && !invoice && !isAssetReceive ? (
<WarningBox text='Lightning is temporarily unavailable. This QR code only supports Arkade and on-chain payments.' />
) : null}
</FlexCol>
) : (
<LoadingLogo text='Generating QR code...' />
)}
</Padded>
</Content>
<ButtonsOnBottom>
<Button onClick={handleShare} label='Share' disabled={disabled} />
</ButtonsOnBottom>
</>
)
}