-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
f Add
PayjoinScheduler
and PayjoinServer
- Loading branch information
Showing
8 changed files
with
348 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#![allow(dead_code)] | ||
use crate::logger::FilesystemLogger; | ||
use crate::types::{ChannelManager, PeerManager, Wallet}; | ||
use bitcoin::secp256k1::PublicKey; | ||
use http_body_util::{BodyExt, Full}; | ||
use hyper::server::conn::http1; | ||
use hyper::service::service_fn; | ||
use hyper::Request; | ||
use hyper_util::rt::TokioIo; | ||
use lightning::util::persist::KVStore; | ||
use std::sync::{Arc, Mutex}; | ||
use tokio::net::TcpStream; | ||
use tokio::task::JoinError; | ||
|
||
#[derive(Clone)] | ||
pub struct ScheduledChannel { | ||
channel_amount_sats: u64, | ||
push_msat: Option<u64>, | ||
user_channel_id: u128, | ||
announce_channel: bool, | ||
node: PublicKey, | ||
} | ||
|
||
impl ScheduledChannel { | ||
pub fn new( | ||
channel_amount_sats: u64, push_msat: Option<u64>, user_channel_id: u128, | ||
announce_channel: bool, node: PublicKey, | ||
) -> Self { | ||
Self { channel_amount_sats, push_msat, user_channel_id, announce_channel, node } | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct PayjoinExecuter<K: KVStore + Sync + Send + 'static> { | ||
wallet: Arc<Wallet>, | ||
logger: Arc<FilesystemLogger>, | ||
peer_manager: Arc<PeerManager<K>>, | ||
channel_manager: Arc<ChannelManager<K>>, | ||
} | ||
|
||
impl<K: KVStore + Sync + Send + 'static> PayjoinExecuter<K> { | ||
pub fn new( | ||
wallet: Arc<Wallet>, logger: Arc<FilesystemLogger>, peer_manager: Arc<PeerManager<K>>, | ||
channel_manager: Arc<ChannelManager<K>>, | ||
) -> Self { | ||
Self { wallet, logger, peer_manager, channel_manager } | ||
} | ||
|
||
pub fn test(&self) -> String { | ||
let connected_peers = self.peer_manager.get_peer_node_ids(); | ||
for (node_id, con_addr_opt) in connected_peers { | ||
println!("Connected to peer: {:?}", node_id); | ||
println!("Connected to peer: {:?}", con_addr_opt); | ||
} | ||
"test".to_string() | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct PayjoinScheduler<K: KVStore + Sync + Send + 'static> { | ||
channels: Arc<Mutex<Vec<ScheduledChannel>>>, | ||
executer: Arc<PayjoinExecuter<K>>, | ||
} | ||
|
||
impl<K: KVStore + Sync + Send + 'static> PayjoinScheduler<K> { | ||
pub fn new(executer: Arc<PayjoinExecuter<K>>) -> Self { | ||
Self { executer, channels: Arc::new(Mutex::new(Vec::new())) } | ||
} | ||
|
||
pub fn schedule(&self, channel: ScheduledChannel) { | ||
self.channels.lock().unwrap().push(channel); | ||
} | ||
|
||
pub fn list_scheduled_channels(&self) -> Vec<ScheduledChannel> { | ||
self.channels.lock().unwrap().clone() | ||
} | ||
} | ||
|
||
fn make_http_response(s: String) -> Result<hyper::Response<Full<bytes::Bytes>>, hyper::Error> { | ||
Ok(hyper::Response::builder().body(Full::new(bytes::Bytes::from(s))).unwrap()) | ||
} | ||
|
||
async fn payjoin_handler<K: KVStore + Sync + Send + 'static>( | ||
http_request: Request<hyper::body::Incoming>, pj_scheduler: Arc<PayjoinScheduler<K>>, | ||
) -> Result<hyper::Response<Full<bytes::Bytes>>, hyper::Error> { | ||
match (http_request.method(), http_request.uri().path()) { | ||
(&hyper::Method::POST, "/payjoin") => { | ||
let _headers = http_request.headers().clone(); | ||
let body = http_request.into_body().collect().await?; | ||
let body = String::from_utf8(body.to_bytes().to_vec()).unwrap(); | ||
let res = pj_scheduler.executer.test(); | ||
assert_eq!(&res, "test"); | ||
return make_http_response(body); | ||
}, | ||
_ => make_http_response("404".into()), | ||
} | ||
} | ||
|
||
pub struct PayjoinServer<K: KVStore + Sync + Send + 'static> { | ||
scheduler: Arc<PayjoinScheduler<K>>, | ||
} | ||
|
||
impl<K: KVStore + Sync + Send + 'static> PayjoinServer<K> { | ||
pub fn new(scheduler: Arc<PayjoinScheduler<K>>) -> Self { | ||
Self { scheduler } | ||
} | ||
|
||
pub async fn serve(&self, stream: TcpStream) -> Result<(), JoinError> { | ||
let io = TokioIo::new(stream); | ||
let state = self.scheduler.clone(); | ||
tokio::task::spawn(async move { | ||
if let Err(err) = http1::Builder::new() | ||
.serve_connection( | ||
io, | ||
service_fn(move |http_request| payjoin_handler(http_request, state.clone())), | ||
) | ||
.await | ||
{ | ||
println!("Error serving connection: {:?}", err); | ||
} else { | ||
println!("Connection here"); | ||
} | ||
}) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.