-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathzaps.rs
386 lines (306 loc) · 10.7 KB
/
zaps.rs
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use enostr::{NoteId, Pubkey};
use nostrdb::NoteBuilder;
use notedeck::ZapError;
use poll_promise::Promise;
use serde::Deserialize;
use url::Url;
fn fetch_pay_req(url: &Url) -> Promise<Result<LNUrlPayRequest, ZapError>> {
let (sender, promise) = Promise::new();
let on_done = move |response: Result<ehttp::Response, String>| {
let handle = response.map_err(ZapError::EndpointError).and_then(|resp| {
if !resp.ok {
return Err(ZapError::EndpointError(format!(
"bad http response: {}",
resp.status_text
)));
}
serde_json::from_slice(&resp.bytes).map_err(|e| ZapError::Serialization(e.to_string()))
});
sender.send(handle);
};
let request = ehttp::Request::get(url);
ehttp::fetch(request, on_done);
promise
}
fn fetch_pay_req_from_lud16(lud16: &str) -> Promise<Result<LNUrlPayRequest, ZapError>> {
let url = match generate_endpoint_url(lud16) {
Ok(url) => url,
Err(e) => return Promise::from_ready(Err(e)),
};
fetch_pay_req(&url)
}
static HRP_LNURL: bech32::Hrp = bech32::Hrp::parse_unchecked("lnurl");
fn lud16_to_lnurl(lud16: &str) -> Result<String, ZapError> {
let endpoint_url = generate_endpoint_url(lud16)?;
let url_str = endpoint_url.to_string();
let data = url_str.as_bytes();
bech32::encode::<bech32::Bech32>(HRP_LNURL, data).map_err(|e| ZapError::Bech(e.to_string()))
}
fn make_kind_9734<'a>(
lnurl: &str,
msats: u64,
sender_nsec: &[u8; 32],
relays: Vec<String>,
recipient: &Pubkey,
event_id: Option<&NoteId>,
) -> nostrdb::Note<'a> {
let mut builder = NoteBuilder::new().kind(9734);
builder = builder.start_tag().tag_str("relays");
for relay in relays {
builder = builder.tag_str(&relay)
}
builder = builder
.start_tag()
.tag_str("amount")
.tag_str(&msats.to_string());
builder = builder.start_tag().tag_str("lnurl").tag_str(lnurl);
builder = builder.start_tag().tag_str("p").tag_str(&recipient.hex());
if let Some(id) = event_id {
builder = builder.start_tag().tag_str("e").tag_str(&id.hex());
}
builder.sign(sender_nsec).build().expect("note")
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct LNUrlPayRequest {
#[serde(rename = "allowsNostr")]
allow_nostr: bool,
#[serde(rename = "nostrPubkey")]
nostr_pubkey: String,
#[serde(rename = "callback")]
callback_url: String,
#[serde(rename = "minSendable")]
min_sendable: u64,
#[serde(rename = "maxSendable")]
max_sendable: u64,
}
#[derive(Debug, Deserialize)]
struct LNInvoice {
#[serde(rename = "pr")]
invoice: String,
}
fn endpoint_query_for_invoice<'a>(
endpoint_base_url: &'a mut Url,
msats: u64,
lnurl: &str,
note: nostrdb::Note,
) -> Result<&'a Url, ZapError> {
let nostr = note
.json()
.map_err(|e| ZapError::Serialization(format!("failed note to json: {e}")))?;
Ok(endpoint_base_url
.query_pairs_mut()
.append_pair("amount", &msats.to_string())
.append_pair("lnurl", lnurl)
.append_pair("nostr", &nostr)
.finish())
}
#[allow(dead_code)]
pub fn fetch_invoice_lud16(
lud16: String,
msats: u64,
sender_nsec: [u8; 32],
event_id: Option<NoteId>,
relays: Vec<String>,
) -> Promise<Result<String, ZapError>> {
let (invoice_sender, invoice_promise) = Promise::new();
std::thread::spawn(move || {
let invoice =
fetch_invoice_lud16_blocking(&lud16, msats, &sender_nsec, event_id.as_ref(), relays);
invoice_sender.send(invoice);
});
invoice_promise
}
#[allow(dead_code)]
pub fn fetch_invoice_lnurl(
lnurl: String,
msats: u64,
sender_nsec: [u8; 32],
event_id: Option<NoteId>,
relays: Vec<String>,
) -> Promise<Result<String, ZapError>> {
let (sender, promise) = Promise::new();
std::thread::spawn(move || {
let pay_req = match fetch_pay_req_from_lnurl(&lnurl).block_and_take() {
Ok(req) => req,
Err(e) => {
sender.send(Err(e));
return;
}
};
let invoice = fetch_invoice_lnurl_blocking(
&lnurl,
&pay_req,
msats,
&sender_nsec,
event_id.as_ref(),
relays,
);
sender.send(invoice);
});
promise
}
fn convert_lnurl_to_endpoint_url(lnurl: &str) -> Result<Url, ZapError> {
let (_, data) = bech32::decode(lnurl).map_err(|e| ZapError::Bech(e.to_string()))?;
let url_str =
String::from_utf8(data).map_err(|e| ZapError::Bech(format!("string conversion: {e}")))?;
Url::parse(&url_str)
.map_err(|e| ZapError::EndpointError(format!("endpoint url from lnurl is invalid: {e}")))
}
fn fetch_pay_req_from_lnurl(lnurl: &str) -> Promise<Result<LNUrlPayRequest, ZapError>> {
let url = match convert_lnurl_to_endpoint_url(lnurl) {
Ok(u) => u,
Err(e) => return Promise::from_ready(Err(e)),
};
fetch_pay_req(&url)
}
fn fetch_invoice_lnurl_blocking(
lnurl: &str,
pay_req: &LNUrlPayRequest,
msats: u64,
sender_nsec: &[u8; 32],
event_id: Option<&NoteId>,
relays: Vec<String>,
) -> Result<String, ZapError> {
let recipient = Pubkey::from_hex(&pay_req.nostr_pubkey)
.map_err(|e| ZapError::EndpointError(format!("invalid pubkey hex from endpoint: {e}")))?;
let note = make_kind_9734(lnurl, msats, sender_nsec, relays, &recipient, event_id);
let mut base_url = Url::parse(&pay_req.callback_url)
.map_err(|e| ZapError::EndpointError(format!("invalid callback url from endpoint: {e}")))?;
let query = endpoint_query_for_invoice(&mut base_url, msats, lnurl, note)?;
fetch_invoice(query).block_and_take().map(|i| i.invoice)
}
fn fetch_invoice_lud16_blocking(
lud16: &str,
msats: u64,
sender_nsec: &[u8; 32],
event_id: Option<&NoteId>,
relays: Vec<String>,
) -> Result<String, ZapError> {
let promise = fetch_pay_req_from_lud16(lud16);
let pay_req = promise.block_and_take()?;
let lnurl = lud16_to_lnurl(lud16)?;
fetch_invoice_lnurl_blocking(&lnurl, &pay_req, msats, sender_nsec, event_id, relays)
}
fn fetch_invoice(req: &Url) -> Promise<Result<LNInvoice, ZapError>> {
let request = ehttp::Request::get(req);
let (sender, promise) = Promise::new();
let on_done = move |response: Result<ehttp::Response, String>| {
let handle = response.map_err(ZapError::EndpointError).and_then(|resp| {
if !resp.ok {
return Err(ZapError::EndpointError(format!(
"invalid http response: {}",
resp.status_text
)));
}
serde_json::from_slice(&resp.bytes).map_err(|e| ZapError::Serialization(e.to_string()))
});
sender.send(handle);
};
ehttp::fetch(request, on_done);
promise
}
fn generate_endpoint_url(lud16: &str) -> Result<Url, ZapError> {
let (user, domain, use_http) = {
let mut split = lud16.split('@');
let user = split
.next()
.ok_or_else(|| ZapError::InvalidLud16("lud16 did not have username".to_owned()))?;
let domain = split
.next()
.ok_or_else(|| ZapError::InvalidLud16("lud16 did not have domain".to_owned()))?;
let mut domain_split = domain.split('.');
let _ = domain_split
.next()
.ok_or_else(|| ZapError::InvalidLud16("lud16 domain is invalid".to_owned()))?;
let tld = domain_split.next().ok_or_else(|| {
ZapError::InvalidLud16("lud16 domain does not include tld".to_owned())
})?;
let use_http = tld == "onion";
(user, domain, use_http)
};
let url_str = format!(
"http{}://{domain}/.well-known/lnurlp/{user}",
if use_http { "" } else { "s" }
);
Url::parse(&url_str).map_err(|e| ZapError::EndpointError(e.to_string()))
}
#[cfg(test)]
mod tests {
use enostr::FullKeypair;
use crate::zaps::convert_lnurl_to_endpoint_url;
use super::{
fetch_invoice_lnurl, fetch_invoice_lud16, fetch_pay_req_from_lud16, lud16_to_lnurl,
};
#[ignore] // don't run this test automatically since it sends real http
#[test]
fn test_get_pay_req() {
let promise = fetch_pay_req_from_lud16(lud16);
let maybe_res = promise.block_and_take();
assert!(maybe_res.is_ok());
let res = maybe_res.unwrap();
assert!(res.allow_nostr);
assert_eq!(
res.nostr_pubkey,
"9630f464cca6a5147aa8a35f0bcdd3ce485324e732fd39e09233b1d848238f31"
);
assert_eq!(res.callback_url, "https://sendsats.lol/@jb55");
assert_eq!(res.min_sendable, 1);
assert_eq!(res.max_sendable, 10000000000);
}
#[test]
fn test_lnurl() {
let maybe_lnurl = lud16_to_lnurl(lud16);
assert!(maybe_lnurl.is_ok());
let lnurl = maybe_lnurl.unwrap();
assert_eq!(
lnurl,
"lnurl1dp68gurn8ghj7um9dej8xct5wvhxcmmv9uh8wetvdskkkmn0wahz7mrww4excup0df3r2dg3mj444"
);
}
#[ignore] // don't run test automatically since it sends real http
#[tokio::test]
async fn test_generate_invoice() {
let kp = FullKeypair::generate();
let relay = "wss://relay.damus.io";
let maybe_invoice = fetch_invoice_lud16(
lud16.to_owned(),
1000,
kp.secret_key.to_secret_bytes(),
None,
[relay.to_owned()].to_vec(),
)
.block_and_take();
assert!(maybe_invoice.is_ok());
assert!(maybe_invoice.unwrap().starts_with("lnbc"));
}
#[test]
fn test_convert_lnurl() {
let lnurl =
"lnurl1dp68gurn8ghj7um9dej8xct5wvhxcmmv9uh8wetvdskkkmn0wahz7mrww4excup0df3r2dg3mj444";
let maybe_url = convert_lnurl_to_endpoint_url(lnurl);
println!("{:?}", maybe_url);
assert!(maybe_url.is_ok());
}
#[ignore]
#[test]
fn test_generate_lnurl_invoice() {
let lnurl =
"lnurl1dp68gurn8ghj7um9dej8xct5wvhxcmmv9uh8wetvdskkkmn0wahz7mrww4excup0df3r2dg3mj444";
let kp = FullKeypair::generate();
let relay = "wss://relay.damus.io";
let maybe_invoice = fetch_invoice_lnurl(
lnurl.to_owned(),
1000,
kp.secret_key.to_secret_bytes(),
None,
[relay.to_owned()].to_vec(),
)
.block_and_take();
assert!(maybe_invoice.is_ok());
assert!(maybe_invoice.unwrap().starts_with("lnbc"));
}
}