Skip to content

Commit

Permalink
Add well known url routes
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio committed Feb 23, 2024
1 parent 037647f commit 5e2f88b
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DATABASE_URL=postgres://localhost/hermes
FM_DB_PATH=./.fedimint-test-dir
NSEC=
DOMAIN_URL=
#HERMES_PORT=8080
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target/
.direnv
.env
.fedimint-test-dir
.test

# These are backup files generated by rustfmt
**/*.rs.bk
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fedimint-wallet-client = "0.2.2"
fedimint-mint-client = "0.2.2"
fedimint-ln-client = "0.2.2"
futures = "0.3.28"
url = "2.5.0"
itertools = "0.12.0"
lightning-invoice = "0.27.0"
hex = "0.4.3"
Expand Down
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
sed -i 's|DATABASE_URL=postgres://localhost/hermes|DATABASE_URL=postgres://hermes_user:password@localhost:5432/hermes|g' .env
# random nsec for CI only
sed -i 's|NSEC=|NSEC=nsec1lmtupx60q0pg6lk3kcl0c56mp7xukulmcc2rxu3gd6sage8xzxhs3slpac|g' .env
# localhost domain
sed -i 's|DOMAIN_URL=|DOMAIN_URL=http://127.0.0.1:8080|g' .env
fi
'';

Expand Down
6 changes: 6 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) trait DBConnection {
fn insert_new_user(&self, name: NewAppUser) -> anyhow::Result<AppUser>;
fn get_pending_invoices(&self) -> anyhow::Result<Vec<Invoice>>;
fn set_invoice_state(&self, invoice: Invoice, s: i32) -> anyhow::Result<()>;
fn get_user_by_name(&self, name: String) -> anyhow::Result<Option<AppUser>>;
fn get_user_by_id(&self, id: i32) -> anyhow::Result<Option<AppUser>>;
fn get_zap_by_id(&self, id: i32) -> anyhow::Result<Option<Zap>>;
fn set_zap_event_id(&self, zap: Zap, event_id: String) -> anyhow::Result<()>;
Expand All @@ -41,6 +42,11 @@ impl DBConnection for PostgresConnection {
Invoice::get_by_state(conn, 0)
}

fn get_user_by_name(&self, name: String) -> anyhow::Result<Option<AppUser>> {
let conn = &mut self.db.get()?;
AppUser::get_by_name(conn, name)
}

fn get_user_by_id(&self, id: i32) -> anyhow::Result<Option<AppUser>> {
let conn = &mut self.db.get()?;
AppUser::get_by_id(conn, id)
Expand Down
88 changes: 88 additions & 0 deletions src/lnurlp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::State;
use anyhow::anyhow;
use fedimint_core::Amount;

use crate::routes::{LnurlStatus, LnurlType, LnurlWellKnownResponse};

pub async fn well_known_lnurlp(
state: &State,
name: String,
) -> anyhow::Result<LnurlWellKnownResponse> {
let user = state.db.get_user_by_name(name.clone())?;
if user.is_none() {
return Err(anyhow!("NotFound"));
}

let res = LnurlWellKnownResponse {
callback: format!("{}/lnurlp/{}/callback", state.domain, name).parse()?,
max_sendable: Amount { msats: 100000 },
min_sendable: Amount { msats: 1000 },
metadata: "test metadata".to_string(), // TODO what should this be?
comment_allowed: None,
tag: LnurlType::PayRequest,
status: LnurlStatus::Ok,
nostr_pubkey: Some(state.nostr.keys().await.public_key()),
allows_nostr: true,
};

Ok(res)
}

#[cfg(all(test, feature = "integration-tests"))]
mod tests_integration {
use nostr::{key::FromSkStr, Keys};
use secp256k1::Secp256k1;
use std::sync::Arc;

use crate::{
db::setup_db, lnurlp::well_known_lnurlp, mint::MockMultiMintWrapperTrait,
models::app_user::NewAppUser, State,
};

#[tokio::test]
pub async fn well_known_nip5_lookup_test() {
dotenv::dotenv().ok();
let pg_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let db = setup_db(pg_url);

// swap out fm with a mock here since that's not what is being tested
let mock_mm = MockMultiMintWrapperTrait::new();

// nostr
let nostr_nsec_str = std::env::var("NSEC").expect("FM_DB_PATH must be set");
let nostr_sk = Keys::from_sk_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

let mock_mm = Arc::new(mock_mm);
let state = State {
db: db.clone(),
mm: mock_mm,
secp: Secp256k1::new(),
nostr,
domain: "http://hello.com".to_string(),
};

let username = "wellknownuser".to_string();
let user = NewAppUser {
pubkey: "e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443".to_string(),
name: username.clone(),
federation_id: "".to_string(),
federation_invite_code: "".to_string(),
};

// don't care about error if already exists
let _ = state.db.insert_new_user(user);

match well_known_lnurlp(&state, username.clone()).await {
Ok(result) => {
assert_eq!(
result.callback,
"http://hello.com/lnurlp/wellknownuser/callback"
.parse()
.unwrap()
);
}
Err(e) => panic!("shouldn't error: {e}"),
}
}
}
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use axum::routing::get;
use axum::{extract::DefaultBodyLimit, routing::post};
use axum::{http, Extension, Router, TypedHeader};
use log::{error, info};
use nostr::{key::FromSkStr, Keys};
use nostr_sdk::nostr::{key::FromSkStr, Keys};
use secp256k1::{All, Secp256k1};
use std::{path::PathBuf, str::FromStr, sync::Arc};
use tokio::signal::unix::{signal, SignalKind};
Expand All @@ -15,13 +15,18 @@ use crate::{
db::{setup_db, DBConnection},
invoice::handle_pending_invoices,
mint::{setup_multimint, MultiMintWrapperTrait},
routes::{check_username, health_check, register_route, valid_origin, validate_cors},
routes::{
check_username, health_check, register_route, valid_origin, validate_cors,
well_known_lnurlp_route, well_known_nip5_route,
},
};

mod db;
mod invoice;
mod lnurlp;
mod mint;
mod models;
mod nostr;
mod register;
mod routes;

Expand Down Expand Up @@ -51,6 +56,7 @@ pub struct State {
mm: Arc<dyn MultiMintWrapperTrait + Send + Sync>,
pub secp: Secp256k1<All>,
pub nostr: nostr_sdk::Client,
pub domain: String,
}

#[tokio::main]
Expand Down Expand Up @@ -83,13 +89,19 @@ async fn main() -> anyhow::Result<()> {
nostr.add_relay("wss://relay.damus.io").await?;
nostr.connect().await;

// domain
let domain = std::env::var("DOMAIN_URL")
.expect("DATABASE_URL must be set")
.to_string();

let db = setup_db(pg_url);
let secp = Secp256k1::new();
let state = State {
db,
mm,
secp,
nostr,
domain,
};

// spawn a task to check for previous pending invoices
Expand Down Expand Up @@ -120,6 +132,11 @@ async fn main() -> anyhow::Result<()> {
.route("/health-check", get(health_check))
.route("/check-username/:username", get(check_username))
.route("/register", post(register_route))
.route("/.well-known/nostr.json", get(well_known_nip5_route))
.route(
"/.well-known/lnurlp/:username",
get(well_known_lnurlp_route),
)
.fallback(fallback)
.layer(
CorsLayer::new()
Expand Down
77 changes: 77 additions & 0 deletions src/nostr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use nostr::prelude::XOnlyPublicKey;
use std::{collections::HashMap, str::FromStr};

use crate::State;

pub fn well_known_nip5(
state: &State,
name: String,
) -> anyhow::Result<HashMap<String, XOnlyPublicKey>> {
let user = state.db.get_user_by_name(name)?;

let mut names = HashMap::new();
if let Some(user) = user {
names.insert(user.name, XOnlyPublicKey::from_str(&user.pubkey).unwrap());
}

Ok(names)
}

#[cfg(all(test, feature = "integration-tests"))]
mod tests_integration {
use nostr::{key::FromSkStr, Keys};
use secp256k1::{PublicKey, Secp256k1, XOnlyPublicKey};
use std::{str::FromStr, sync::Arc};

use crate::{
db::setup_db, mint::MockMultiMintWrapperTrait, models::app_user::NewAppUser,
nostr::well_known_nip5, State,
};

#[tokio::test]
pub async fn well_known_nip5_lookup_test() {
dotenv::dotenv().ok();
let pg_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let db = setup_db(pg_url);

// swap out fm with a mock here since that's not what is being tested
let mock_mm = MockMultiMintWrapperTrait::new();

// nostr
let nostr_nsec_str = std::env::var("NSEC").expect("FM_DB_PATH must be set");
let nostr_sk = Keys::from_sk_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

let mock_mm = Arc::new(mock_mm);
let state = State {
db: db.clone(),
mm: mock_mm,
secp: Secp256k1::new(),
nostr,
domain: "http://127.0.0.1:8080".to_string(),
};

let username = "wellknownuser".to_string();
let kpk1 = PublicKey::from_str(
"02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443",
)
.unwrap();
let pk1 = XOnlyPublicKey::from(kpk1);
let user = NewAppUser {
pubkey: "e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443".to_string(),
name: username.clone(),
federation_id: "".to_string(),
federation_invite_code: "".to_string(),
};

// don't care about error if already exists
let _ = state.db.insert_new_user(user);

match well_known_nip5(&state, username.clone()) {
Ok(result) => {
assert_eq!(result.get(&username).unwrap().to_string(), pk1.to_string());
}
Err(e) => panic!("shouldn't error: {e}"),
}
}
}
3 changes: 3 additions & 0 deletions src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ mod tests_integration {
mm: mock_mm,
secp: Secp256k1::new(),
nostr,
domain: "http://127.0.0.1:8080".to_string(),
};

let name = "veryuniquename123".to_string();
Expand Down Expand Up @@ -182,6 +183,7 @@ mod tests_integration {
mm: mock_mm,
secp: Secp256k1::new(),
nostr,
domain: "http://127.0.0.1:8080".to_string(),
};

let connect = InviteCode::new(
Expand Down Expand Up @@ -233,6 +235,7 @@ mod tests_integration {
mm: mock_mm,
secp: Secp256k1::new(),
nostr,
domain: "http://127.0.0.1:8080".to_string(),
};

let connect = InviteCode::new(
Expand Down
Loading

0 comments on commit 5e2f88b

Please sign in to comment.