Skip to content

Commit

Permalink
[Draft] outline payjoin service
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Jan 7, 2024
1 parent 02d22f6 commit 86cdb68
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/api/server/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl From<PayoutQueue> for proto::PayoutQueue {
proto::payout_queue_config::Trigger::IntervalSecs(seconds.as_secs() as u32)
}
PayoutQueueTrigger::Manual => proto::payout_queue_config::Trigger::Manual(true),
PayoutQueueTrigger::Payjoin => proto::payout_queue_config::Trigger::Payjoin(true)
PayoutQueueTrigger::Payjoin => proto::payout_queue_config::Trigger::Payjoin(true),
};
let tx_priority: proto::TxPriority = payout_queue.config.tx_priority.into();
let config = Some(proto::PayoutQueueConfig {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod fees;
mod job;
pub mod ledger;
mod outbox;
mod payjoin;
pub mod payout;
pub mod payout_queue;
pub mod primitives;
Expand Down
32 changes: 32 additions & 0 deletions src/payjoin/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use tracing::instrument;

use super::error::*;
use crate::{account::*, dev_constants, ledger::Ledger, primitives::bitcoin, profile::*};

const BOOTSTRAP_KEY_NAME: &str = "payjoin_bootstrap_key";

pub struct PayjoinApp {
accounts: Accounts,
profiles: Profiles,
ledger: Ledger,
pool: sqlx::PgPool,
network: bitcoin::Network,
}

impl PayjoinApp {
pub fn new(pool: sqlx::PgPool, network: bitcoin::Network) -> Self {
Self {
accounts: Accounts::new(&pool),
profiles: Profiles::new(&pool),
ledger: Ledger::new(&pool),
pool,
network,
}
}
}

impl PayjoinApp {
pub fn run() {
println!("run PayjoinApp");
}
}
18 changes: 18 additions & 0 deletions src/payjoin/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PayjoinConfig {
#[serde(default = "default_port")]
pub listen_port: u16,
}
impl Default for PayjoinConfig {
fn default() -> Self {
Self {
listen_port: default_port(),
}
}
}

fn default_port() -> u16 {
8088
}
8 changes: 8 additions & 0 deletions src/payjoin/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use thiserror::Error;

#[allow(clippy::large_enum_variant)]
#[derive(Error, Debug)]
pub enum PayjoinApiError {
#[error("PayjoinApiError - Error")]
Error,
}
68 changes: 68 additions & 0 deletions src/payjoin/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
mod app;
mod config;
pub mod error;
mod server;

pub use app::*;
pub use config::*;
pub use error::*;
pub use server::*;

// pub async fn run_dev(
// pool: sqlx::PgPool,
// config: ApiConfig,
// app_cfg: AppConfig,
// xpub: Option<(String, String)>,
// derivation_path: Option<String>,
// ) -> Result<(), ApplicationError> {
// use crate::{
// dev_constants,
// payout_queue::*,
// profile::Profiles,
// xpub::{BitcoindSignerConfig, SignerConfig},
// };

// let app = App::run(pool.clone(), app_cfg).await?;
// if let Some((xpub, signer_endpoint)) = xpub {
// println!("Creating dev entities");
// let profile = Profiles::new(&pool)
// .find_by_key(dev_constants::BRIA_DEV_KEY)
// .await?;
// let (_, xpubs) = app
// .create_wpkh_wallet(
// &profile,
// dev_constants::DEV_WALLET_NAME.to_string(),
// xpub,
// derivation_path,
// )
// .await?;
// app.set_signer_config(
// &profile,
// xpubs[0].to_string(),
// SignerConfig::Bitcoind(BitcoindSignerConfig {
// endpoint: signer_endpoint,
// rpc_user: dev_constants::DEFAULT_BITCOIND_RPC_USER.to_string(),
// rpc_password: dev_constants::DEFAULT_BITCOIND_RPC_PASSWORD.to_string(),
// }),
// )
// .await?;
// app.create_payout_queue(
// &profile,
// dev_constants::DEV_QUEUE_NAME.to_string(),
// None,
// Some(PayoutQueueConfig {
// trigger: PayoutQueueTrigger::Payjoin,
// ..PayoutQueueConfig::default()
// }),
// )
// .await?;
// }
// server::start(config, app).await?;
// Ok(())
// }

pub async fn run(pool: sqlx::PgPool, config: PayjoinConfig) -> Result<(), PayjoinApiError> {
let app = PayjoinApp::run().await?;
server::start(config, app).await?;
Ok(())
}
7 changes: 7 additions & 0 deletions src/payjoin/server/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::payjoin::PayjoinApiError;

impl From<PayjoinApiError> for tonic::Status {
fn from(err: PayjoinApiError) -> Self {
tonic::Status::new(tonic::Code::Unknown, format!("{err}"))
}
}
41 changes: 41 additions & 0 deletions src/payjoin/server/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
mod convert;

use futures::StreamExt;
use opentelemetry::propagation::{Extractor, TextMapPropagator};
use opentelemetry_sdk::propagation::TraceContextPropagator;
use tonic::{transport::Server, Request, Response, Status};
use tracing::instrument;
use tracing_opentelemetry::OpenTelemetrySpanExt;

use super::{config::*, PayjoinApp};
use crate::{
app::{error::ApplicationError, *},
payout_queue,
primitives::*,
profile,
};

pub const PROFILE_API_KEY_HEADER: &str = "x-bria-payjoin-api-key";

pub struct Payjoin {
app: PayjoinApp,
}

#[tonic::async_trait]
impl PayjoinService for Payjoin {}

pub(crate) async fn start(
server_config: PayjoinConfig,
app: PayjoinApp,
) -> Result<(), PayjoinApiError> {
let payjoin = Payjoin { app };
println!(
"Starting payjoin server on port {}",
server_config.listen_port
);

Server::builder()
.serve(([0, 0, 0, 0], server_config.listen_port).into())
.await?;
Ok(())
}

0 comments on commit 86cdb68

Please sign in to comment.