-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpay.svelte
342 lines (292 loc) · 9.77 KB
/
pay.svelte
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<script lang="ts">
import { API_URL } from '$lib/constants'
import Button from '$lib/components/Button.svelte'
import { translate } from '$lib/i18n/translations'
import { mainDomain, nowSeconds } from '$lib/utils'
import { satsToMsats } from '$lib/conversion.js'
import decode from '$lib/bolt11.js'
import { combineLatest, map } from 'rxjs'
import { connections$, wallets$ } from '$lib/streams.js'
import type { Wallet } from '$lib/@types/wallets.js'
import WalletSelector from '$lib/components/WalletSelector.svelte'
import { slide } from 'svelte/transition'
import Msg from '$lib/components/Msg.svelte'
import TextInput from '$lib/components/TextInput.svelte'
import { createRandomHex } from '$lib/crypto.js'
import type { Connection } from '$lib/wallets/interfaces.js'
import { db } from '$lib/db/index.js'
import AES from 'crypto-js/aes'
import encBase64 from 'crypto-js/enc-base64'
import encHex from 'crypto-js/enc-hex'
import encUtf8 from 'crypto-js/enc-utf8'
import link from '$lib/icons/link.js'
import CopyValue from '$lib/components/CopyValue.svelte'
import type { AppError } from '$lib/@types/errors.js'
import caret from '$lib/icons/caret.js'
import ErrorDetail from '$lib/components/ErrorDetail.svelte'
import type { InvoicePayment } from '$lib/@types/payments.js'
export let url: URL
export let callback: string // The URL from LN SERVICE which will accept the pay request parameters
export let maxSendable: number // Max sats amount LN SERVICE is willing to receive
export let minSendable: number // Min sats amount LN SERVICE is willing to receive, can not be less than 1 or more than `maxSendable`
export let metadata: string // Metadata json which must be presented as raw string here, this is required to pass signature verification at a later step
export let commentAllowed: number // indicates the length of comment (description) allowed
export let lightningAddress: string | null
const serviceName = lightningAddress || mainDomain(url.hostname)
let meta: string[][]
let shortDescription: string
let longDescription: string | undefined
let image: string | undefined
let mime: string | undefined
let address: string | undefined
let amount: number
let amountError = ''
let comment = ''
let selectedWalletId: Wallet['id']
const availableWallets$ = combineLatest([wallets$, connections$]).pipe(
map(([wallets, connections]) =>
wallets.filter(({ id }) => {
const connection = connections.find(({ walletId }) => walletId === id)
return !!connection?.invoices?.pay
})
)
)
type FormattedMetadata = {
/**required short description of pay request*/
shortDescription: string
/**additional long description*/
longDescription?: string
/**base64 image string*/
image?: string
/**the image data type*/
mime?: string
/**the lightning address*/
address?: string
}
function formatMetadata(meta: string[][]) {
return meta.reduce((acc, [mime, data], index) => {
if (index === 0) {
acc.shortDescription = data
}
if (mime.includes('long-desc')) {
acc.longDescription = data
}
if (mime.includes('image')) {
acc.image = data
acc.mime = mime
}
if (mime.includes('email') || mime.includes('identifier')) {
acc.address = data
}
return acc
}, {} as FormattedMetadata)
}
try {
meta = JSON.parse(metadata)
const formattedMetadata = formatMetadata(meta)
shortDescription = formattedMetadata.shortDescription
longDescription = formattedMetadata.longDescription
image = formattedMetadata.image
mime = formattedMetadata.mime
address = formattedMetadata.address
} catch (error) {
// just don't show invalid metadata
}
$: if (typeof amount !== 'undefined') {
validateAmount()
}
function validateAmount() {
if (amount < minSendable) {
amountError = $translate('app.errors.min_sendable')
return
}
if (amount > maxSendable) {
amountError = $translate('app.errors.max_sendable')
return
}
amountError = ''
}
let requesting = false
let requestError: AppError | null = null
type SuccessMessage = { tag: 'message'; message: string }
type SuccessUrl = { tag: 'url'; description: string; url: string }
type SuccessAES = { tag: 'aes'; description: string; ciphertext: string; iv: string }
type SuccessAction = SuccessMessage | SuccessUrl | SuccessAES
let paidInvoice: InvoicePayment
let success: SuccessAction
let decryptedAes: string
async function initiatePay() {
try {
requestError = null
requesting = true
const amountMsats = satsToMsats(amount)
const url = new URL(callback)
const kind = url.searchParams.get('kind')
if (kind) {
url.searchParams.set('kind', 'payRequest')
}
url.searchParams.set('amount', amountMsats)
if (comment) {
url.searchParams.set('comment', comment)
}
const result = await fetch(url.toString()).then(res => res.json())
if (result.status === 'ERROR') {
throw {
key: 'lnurl_pay_error',
detail: {
timestamp: nowSeconds(),
message: result.reason,
context: 'LNURL Pay request invoice'
}
}
}
const { pr: paymentRequest, successAction } = result
success = successAction
const decoded = decode(paymentRequest)
if (!decoded) {
throw {
key: 'invalid_bolt11',
detail: {
timestamp: nowSeconds(),
message: 'Invoice returned from server is not a valid BOLT11',
context: 'LNURL Pay request invoice'
}
}
}
const { amount: paymentRequestAmount } = decoded
if (paymentRequestAmount !== amount) {
throw {
key: 'lnurl_pay_amount',
detail: {
timestamp: nowSeconds(),
message: 'Invoice returned from server does not have the correct amount',
context: 'LNURL Pay request invoice'
}
}
}
const connection = connections$.value.find(
({ walletId }) => walletId === selectedWalletId
) as Connection
const id = createRandomHex()
paidInvoice = await connection.invoices!.pay!({
id,
request: paymentRequest,
description: metadata
})
await db.payments.add(paidInvoice)
if (successAction?.tag === 'aes') {
decryptedAes = AES.decrypt(
successAction.ciphertext,
encHex.parse(paidInvoice.data.preimage!),
{
iv: encBase64.parse(successAction.iv)
}
).toString(encUtf8)
}
} catch (error) {
requestError = error as AppError
} finally {
requesting = false
}
}
</script>
<div class="flex flex-col gap-y-4">
<h2 class="uppercase text-xl font-semibold mt-2">{serviceName}</h2>
<div class="text-sm font-semibold">
{#if minSendable}
<div>{$translate('app.labels.min_sendable')}: {minSendable}</div>
{/if}
{#if maxSendable}
<div>{$translate('app.labels.max_sendable')}: {maxSendable}</div>
{/if}
</div>
{#if !paidInvoice}
<div out:slide={{ axis: 'y' }}>
{#if $availableWallets$}
<div class="mb-4">
<WalletSelector
label={$translate('app.labels.from_wallet')}
bind:selectedWalletId
wallets={$availableWallets$}
/>
</div>
{/if}
<div class="mb-4">
<TextInput
label={$translate('app.labels.amount')}
bind:value={amount}
name="amount"
type="number"
invalid={amountError}
hint={$translate('app.labels.sats')}
/>
</div>
{#if commentAllowed}
<div class="mb-4">
<TextInput
bind:value={comment}
label={$translate('app.labels.comment')}
name="comment"
type="text"
maxlength={commentAllowed}
hint={$translate('app.labels.optional')}
/>
</div>
{/if}
<div class="w-full flex justify-end">
<div class="w-min">
<Button
{requesting}
disabled={!amount}
text={$translate('app.labels.pay')}
on:click={initiatePay}
/>
</div>
</div>
</div>
{:else}
<div class="flex flex-col gap-y-4" in:slide={{ axis: 'y' }}>
<div>
<Msg type="info" message={$translate('app.labels.lnurl_pay_success')} />
</div>
<a
href={`/payments/${paidInvoice.id}?wallet=${paidInvoice.walletId}`}
class="flex items-center"
>{$translate('app.labels.goto_payment')}
<div class="w-4 -rotate-90">{@html caret}</div>
</a>
{#if success}
<p class="max-w-full">
{#if success.tag === 'message'}
{success.message}
{:else}
{success.description}
{/if}
</p>
{#if success.tag === 'url'}
<div class="w-full flex items-center flex-wrap mb-6 gap-4">
<p class="max-w-full text-neutral-400 italic break-words">
{success.url}
</p>
<a href={success.url} target="_blank" rel="noopener noreferrer">
<Button text={$translate('app.buttons.open')}>
<div class="w-5 -ml-2 mr-1" slot="iconLeft">{@html link}</div>
</Button>
</a>
</div>
{/if}
{#if success.tag === 'aes'}
<div class="w-full">
<p>{success.description}</p>
<CopyValue value={decryptedAes} />
</div>
{/if}
{/if}
</div>
{/if}
{#if requestError}
<div class="mt-4" transition:slide={{ axis: 'y' }}>
<ErrorDetail error={requestError} />
</div>
{/if}
</div>