Skip to content

Commit

Permalink
[WIP] implement part of vasp1.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenlu committed Oct 27, 2023
1 parent 93776c7 commit cc2855f
Show file tree
Hide file tree
Showing 4 changed files with 515 additions and 49 deletions.
6 changes: 5 additions & 1 deletion examples/uma-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ actix-web = "4.4.0"
chrono = "0.4.31"
hex = "0.4.3"
lightspark = { path = "../../lightspark" }
reqwest = "0.11.22"
serde = "1.0.190"
serde_json = "1.0.107"
uma = "0.1.1"
uma = { path = "../../../../uma-rust-sdk"}
url = "2.4.1"
uuid = { version = "1.5.0", features = ["v4"] }
76 changes: 62 additions & 14 deletions examples/uma-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,87 @@
pub mod config;
pub mod vasp;
use std::sync::Arc;
pub mod vasp_request_cache;
use std::sync::{Arc, Mutex};

use actix_web::{get, post, web, App, HttpServer, Responder};
use actix_web::{get, post, web, App, HttpRequest, HttpServer, Responder};
use lightspark::{
client::LightsparkClient, key::Secp256k1SigningKey, request::auth_provider::AccountAuthProvider,
};
use serde::Deserialize;
use uma::public_key_cache::InMemoryPublicKeyCache;

use crate::vasp::{VASPReceiving, VASPSending, VASP};
use crate::vasp::SendingVASP;

#[get("/api/umalookup/{receiver}")]
async fn uma_lookup(vasp: web::Data<Arc<VASP>>, receiver: web::Path<String>) -> impl Responder {
async fn uma_lookup(
vasp: web::Data<Arc<Mutex<SendingVASP<InMemoryPublicKeyCache>>>>,
receiver: web::Path<String>,
) -> impl Responder {
let mut vasp = vasp.lock().unwrap();
vasp.handle_client_uma_lookup(receiver.as_str())
}

#[derive(Deserialize)]
struct PayReqParam {
amount: i64,

#[serde(rename = "currencyCode")]
currency_code: String,
}

#[get("/api/umapayreq/{callback_uuid}")]
async fn client_payreq(
vasp: web::Data<Arc<VASP>>,
vasp: web::Data<Arc<Mutex<SendingVASP<InMemoryPublicKeyCache>>>>,
callback_uuid: web::Path<String>,
params: web::Query<PayReqParam>,
) -> impl Responder {
vasp.handle_client_pay_req(callback_uuid.as_str())
let mut vasp = vasp.lock().unwrap();
vasp.handle_client_pay_req(
callback_uuid.as_str(),
params.amount,
params.currency_code.as_str(),
)
.await
}

#[get("/api/sendpayment/{callback_uuid}")]
async fn send_payment(
vasp: web::Data<Arc<VASP>>,
vasp: web::Data<Arc<Mutex<SendingVASP<InMemoryPublicKeyCache>>>>,
callback_uuid: web::Path<String>,
) -> impl Responder {
let mut vasp = vasp.lock().unwrap();
vasp.handle_client_payment_confirm(callback_uuid.as_str())
}

#[get("/.well-known/lnurlp/{username}")]
async fn well_known_lnurlp(
vasp: web::Data<Arc<VASP>>,
req: HttpRequest,
vasp: web::Data<Arc<SendingVASP<InMemoryPublicKeyCache>>>,
username: web::Path<String>,
) -> impl Responder {
vasp.handle_well_known_lnurlp(username.as_str())
vasp.handle_well_known_lnurlp(&req, username.as_str())
}

#[get("/api/uma/payreq/{uuid}")]
async fn lnurl_payreq(vasp: web::Data<Arc<VASP>>, uuid: web::Path<String>) -> impl Responder {
async fn lnurl_payreq(
vasp: web::Data<Arc<SendingVASP<InMemoryPublicKeyCache>>>,
uuid: web::Path<String>,
) -> impl Responder {
vasp.handle_lnurl_payreq(uuid.as_str())
}

#[post("/api/uma/payreq/{uuid}")]
async fn uma_payreq(vasp: web::Data<Arc<VASP>>, uuid: web::Path<String>) -> impl Responder {
async fn uma_payreq(
vasp: web::Data<Arc<SendingVASP<InMemoryPublicKeyCache>>>,
uuid: web::Path<String>,
) -> impl Responder {
vasp.handle_uma_payreq(uuid.as_str())
}

#[get("/.well-known/lnurlpubkey")]
async fn pubkey_request(vasp: web::Data<Arc<VASP>>) -> impl Responder {
async fn pubkey_request(
vasp: web::Data<Arc<SendingVASP<InMemoryPublicKeyCache>>>,
) -> impl Responder {
vasp.handle_pubkey_request()
}

Expand All @@ -56,9 +91,22 @@ async fn main() -> std::io::Result<()> {
.unwrap()
.parse::<u16>()
.unwrap();
let vasp = Arc::new(VASP {
let pubkey_cache = InMemoryPublicKeyCache::new();
let req_cache = vasp_request_cache::Vasp1PayReqCache::new();
let config = config::Config::new_from_env();
let auth_provider = AccountAuthProvider::new(
config.api_client_id.clone(),
config.api_client_secret.clone(),
);
let lightspark_client = LightsparkClient::<Secp256k1SigningKey>::new(auth_provider).unwrap();

let vasp = Arc::new(Mutex::new(SendingVASP {
config: config::Config::new_from_env(),
});
pubkey_cache,
request_cache: req_cache,
client: lightspark_client,
}));

HttpServer::new(move || {
let vasp = Arc::clone(&vasp);
App::new()
Expand Down
Loading

0 comments on commit cc2855f

Please sign in to comment.