Created
May 29, 2023 01:12
-
-
Save okjodom/23298bb09a796f28f5efa5755e2d903f to your computer and use it in GitHub Desktop.
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
use std::net::SocketAddr; | |
use anyhow::anyhow; | |
use clap::Parser; | |
use fedimint_core::task::TaskGroup; | |
use ln_gateway::gatewaylnrpc::gateway_lightning_server::{ | |
GatewayLightning, GatewayLightningServer, | |
}; | |
use ln_gateway::gatewaylnrpc::{ | |
EmptyRequest, GetNodeInfoResponse, GetRouteHintsResponse, PayInvoiceRequest, | |
PayInvoiceResponse, RouteHtlcRequest, RouteHtlcResponse, | |
}; | |
use serde::{Deserialize, Serialize}; | |
use thiserror::Error; | |
use tokio_stream::wrappers::ReceiverStream; | |
use tonic::transport::Server; | |
use tonic::Status; | |
use tracing::debug; | |
use zebedee_rust::ZebedeeClient; | |
#[derive(Parser)] | |
pub struct ZbdGatewayExtensionOpts { | |
/// ZBD extension service listen address | |
#[arg(long = "listen", env = "FM_ZBD_EXTENSION_LISTEN_ADDRESS")] | |
pub listen: SocketAddr, | |
#[arg(long = "api-key", env = "ZBD_API_KEY")] | |
pub api_key: String, | |
} | |
#[tokio::main] | |
async fn main() -> Result<(), anyhow::Error> { | |
// Read configurations | |
let ZbdGatewayExtensionOpts { listen, api_key } = ZbdGatewayExtensionOpts::parse(); | |
let service = ZbdGatewayExtension::new(api_key) | |
.await | |
.expect("Failed to create zbd rpc service"); | |
debug!( | |
"Starting gateway-zbd-extension with listen address : {}", | |
listen | |
); | |
Server::builder() | |
.add_service(GatewayLightningServer::new(service)) | |
.serve_with_shutdown(listen, async { | |
std::process::exit(0); | |
}) | |
.await | |
.map_err(|e| ZebedeeGatewayError::Error(anyhow!("Failed to start server, {:?}", e)))?; | |
Ok(()) | |
} | |
#[allow(dead_code)] | |
pub struct ZbdGatewayExtension { | |
client: ZebedeeClient, | |
task_group: TaskGroup, | |
api_key: String, | |
} | |
impl ZbdGatewayExtension { | |
pub async fn new(api_key: String) -> Result<Self, ZebedeeGatewayError> { | |
Ok(Self { | |
client: ZebedeeClient::new().apikey(api_key.clone()).build(), | |
task_group: TaskGroup::new(), | |
api_key, | |
}) | |
} | |
} | |
#[derive(Serialize, Deserialize)] | |
struct PayInvoicePayload { | |
description: String, | |
internal_id: String, | |
invoice: String, | |
callback_url: String, | |
amount: u32, | |
} | |
#[derive(Debug, Serialize, Deserialize)] | |
struct ZbdPayInvoiceResponse { | |
success: bool, | |
message: String, | |
data: ZbdData, | |
} | |
#[derive(Debug, Serialize, Deserialize)] | |
struct ZbdData { | |
id: String, | |
fee: String, | |
unit: String, | |
amount: String, | |
invoice: String, | |
preimage: String, | |
internal_id: String, | |
processed_at: String, | |
confirmed_at: String, | |
description: String, | |
status: String, | |
} | |
#[tonic::async_trait] | |
impl GatewayLightning for ZbdGatewayExtension { | |
async fn get_node_info( | |
&self, | |
_request: tonic::Request<EmptyRequest>, | |
) -> Result<tonic::Response<GetNodeInfoResponse>, Status> { | |
unimplemented!() | |
} | |
async fn get_route_hints( | |
&self, | |
_request: tonic::Request<EmptyRequest>, | |
) -> Result<tonic::Response<GetRouteHintsResponse>, Status> { | |
// return empty | |
unimplemented!() | |
} | |
async fn pay_invoice( | |
&self, | |
request: tonic::Request<PayInvoiceRequest>, | |
) -> Result<tonic::Response<PayInvoiceResponse>, tonic::Status> { | |
let reqwest_client = reqwest::Client::new(); | |
let payload = PayInvoicePayload { | |
description: "test".into(), | |
internal_id: "test".into(), | |
invoice: request.into_inner().invoice, | |
callback_url: "testurl".into(), | |
amount: 1000, | |
}; | |
let response = reqwest_client | |
.post("https://api.zebedee.io/v0/payments") | |
.header(reqwest::header::CONTENT_TYPE, "application/json") | |
.header("apikey", self.api_key.clone()) | |
.json(&payload) | |
.send() | |
.await | |
.map_err(|e| Status::internal(e.to_string()))?; | |
let preimage = response | |
.json::<ZbdPayInvoiceResponse>() | |
.await | |
.map_err(|e| Status::internal(e.to_string()))? | |
.data | |
.preimage; | |
Ok(tonic::Response::new(PayInvoiceResponse { | |
preimage: preimage.into_bytes(), | |
})) | |
} | |
type RouteHtlcsStream = ReceiverStream<Result<RouteHtlcResponse, Status>>; | |
async fn route_htlcs( | |
&self, | |
_request: tonic::Request<tonic::Streaming<RouteHtlcRequest>>, | |
) -> Result<tonic::Response<Self::RouteHtlcsStream>, Status> { | |
unimplemented!() | |
} | |
} | |
#[derive(Debug, Error)] | |
pub enum ZebedeeGatewayError { | |
#[error("Zebedee Gateway Extension Error : {0:?}")] | |
Error(#[from] anyhow::Error), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment