|
| 1 | +use bitcoin::address::NetworkChecked; |
| 2 | +use hyper::HeaderMap; |
| 3 | +use payjoin::bitcoin::{self, Amount}; |
| 4 | +use payjoin::Uri; |
| 5 | +use std::str::FromStr; |
| 6 | + |
| 7 | +struct Headers(HeaderMap); |
| 8 | + |
| 9 | +impl payjoin::receive::Headers for Headers { |
| 10 | + fn get_header(&self, key: &str) -> Option<&str> { |
| 11 | + self.0.get(key).and_then(|v| v.to_str().ok()) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +pub struct Receiver; |
| 16 | + |
| 17 | +impl Receiver { |
| 18 | + pub async fn start() -> Result<(), Box<dyn std::error::Error>> { |
| 19 | + http_server::start().await.unwrap(); |
| 20 | + Ok(()) |
| 21 | + } |
| 22 | + |
| 23 | + fn _build_pj_uri( |
| 24 | + address: bitcoin::Address, amount: Amount, pj: &'static str, |
| 25 | + ) -> Result<Uri<'static, NetworkChecked>, Box<dyn std::error::Error>> { |
| 26 | + let pj_uri_string = format!("{}?amount={}&pj={}", address.to_qr_uri(), amount.to_btc(), pj); |
| 27 | + let pj_uri = Uri::from_str(&pj_uri_string).map_err(|e| e.to_string())?; |
| 28 | + Ok(pj_uri.assume_checked()) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +mod http_server { |
| 33 | + use bytes::Bytes; |
| 34 | + use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full}; |
| 35 | + use hyper::server::conn::http1; |
| 36 | + use hyper::service::service_fn; |
| 37 | + use hyper::{Method, Request, Response, StatusCode}; |
| 38 | + use hyper_util::rt::TokioIo; |
| 39 | + use std::net::SocketAddr; |
| 40 | + use tokio::net::TcpListener; |
| 41 | + |
| 42 | + pub async fn start() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 43 | + let addr = SocketAddr::from(([127, 0, 0, 1], 3227)); |
| 44 | + let listener = TcpListener::bind(addr).await?; |
| 45 | + println!("Listening on http://{}", addr); |
| 46 | + loop { |
| 47 | + let (stream, _) = listener.accept().await?; |
| 48 | + let io = TokioIo::new(stream); |
| 49 | + |
| 50 | + tokio::task::spawn(async move { |
| 51 | + if let Err(err) = |
| 52 | + http1::Builder::new().serve_connection(io, service_fn(request_handler)).await |
| 53 | + { |
| 54 | + println!("Error serving connection: {:?}", err); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + async fn request_handler( |
| 61 | + req: Request<hyper::body::Incoming>, |
| 62 | + ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> { |
| 63 | + match (req.method(), req.uri().path()) { |
| 64 | + // Serve some instructions at / |
| 65 | + (&Method::GET, "/") => Ok(Response::new(full( |
| 66 | + "Try POSTing data to /request_handler such as: `curl localhost:3000/request_handler -XPOST -d \"PAYJOIN ME\"`", |
| 67 | + ))), |
| 68 | + |
| 69 | + // Simply echo the body back to the client. |
| 70 | + (&Method::POST, "/payjoin") => Ok(Response::new(req.into_body().boxed())), |
| 71 | + |
| 72 | + // Return the 404 Not Found for other routes. |
| 73 | + _ => { |
| 74 | + let mut not_found = Response::new( |
| 75 | + Empty::<Bytes>::new() |
| 76 | + .map_err(|never| match never {}) |
| 77 | + .boxed() |
| 78 | + ); |
| 79 | + *not_found.status_mut() = StatusCode::NOT_FOUND; |
| 80 | + Ok(not_found) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> { |
| 86 | + Full::new(chunk.into()).map_err(|never| match never {}).boxed() |
| 87 | + } |
| 88 | +} |
0 commit comments