Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check the name for a pubkey #13

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 54 additions & 37 deletions Cargo.lock

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

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ chrono = { version = "0.4.26", features = ["serde"] }
diesel = { version = "2.1", features = ["postgres", "postgres_backend", "r2d2", "chrono", "numeric"] }
dotenv = "0.15.0"
async-trait = "0.1.77"
fedimint-tbs = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.2" }
fedimint-core = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.2" }
fedimint-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.2" }
fedimint-wallet-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.2" }
fedimint-mint-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.2" }
fedimint-ln-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.2" }
fedimint-ln-common = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.2" }
fedimint-tbs = "0.3.0"
fedimint-core = "0.3.0"
fedimint-client = "0.3.0"
fedimint-wallet-client = "0.3.0"
fedimint-mint-client = "0.3.0"
fedimint-ln-client = "0.3.0"
fedimint-ln-common = "0.3.0"
futures = "0.3.28"
url = "2.5.0"
itertools = "0.12.0"
Expand All @@ -40,7 +40,7 @@ reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1.12.0", features = ["full"] }
tower-http = { version = "0.4.0", features = ["cors"] }
lazy-regex = "3.1.0"
multimint = { git = "https://github.com/fedimint/fedimint-clientd", rev = "16fe9dd32c745267304a55aacee9501050bb03fa" }
multimint = { git = "https://github.com/fedimint/fedimint-clientd", rev = "b3078124dd65e6b96fe824da2a0c772a6b4bd9cd" }
names = "0.14.0"

[dev-dependencies]
Expand Down
9 changes: 9 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::models::{
#[cfg_attr(test, automock)]
pub(crate) trait DBConnection {
fn check_name_available(&self, name: String) -> anyhow::Result<bool>;
fn check_registered_pubkey(&self, pubkey: String) -> anyhow::Result<Option<String>>;
fn get_user_by_token(&self, msg: String) -> anyhow::Result<Option<AppUser>>;
fn insert_new_user(&self, name: NewAppUser) -> anyhow::Result<AppUser>;
fn get_pending_invoices(&self) -> anyhow::Result<Vec<Invoice>>;
Expand All @@ -37,6 +38,14 @@ impl DBConnection for PostgresConnection {
AppUser::check_available_name(conn, name)
}

fn check_registered_pubkey(&self, pubkey: String) -> anyhow::Result<Option<String>> {
let conn = &mut self.db.get()?;
match AppUser::get_by_pubkey(conn, pubkey)? {
Some(user) => Ok(Some(user.name)),
None => Ok(None),
}
}

fn get_user_by_token(&self, msg: String) -> anyhow::Result<Option<AppUser>> {
let conn = &mut self.db.get()?;
AppUser::get_by_token(conn, msg)
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use crate::{
invoice::handle_pending_invoices,
mint::{setup_multimint, MultiMintWrapperTrait},
routes::{
check_username, health_check, lnurl_callback_route, lnurl_verify_route, register_route,
root, valid_origin, validate_cors, well_known_lnurlp_route, well_known_nip5_route,
check_pubkey, check_username, health_check, lnurl_callback_route, lnurl_verify_route,
register_route, root, valid_origin, validate_cors, well_known_lnurlp_route,
well_known_nip5_route,
},
};

Expand Down Expand Up @@ -166,6 +167,7 @@ async fn main() -> anyhow::Result<()> {
.route("/", get(root))
.route("/health-check", get(health_check))
.route("/v1/check-username/:username", get(check_username))
.route("/v1/check-pubkey/:pubkey", get(check_pubkey))
.route("/v1/register", post(register_route))
.route("/.well-known/nostr.json", get(well_known_nip5_route))
.route(
Expand Down
4 changes: 4 additions & 0 deletions src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ pub fn check_available(state: &State, name: String) -> anyhow::Result<bool> {
state.db.check_name_available(name)
}

pub fn check_registered_pubkey(state: &State, pubkey: String) -> anyhow::Result<Option<String>> {
state.db.check_registered_pubkey(pubkey)
}

pub fn generate_random_name(state: &State) -> anyhow::Result<String> {
loop {
let new_name = Generator::with_naming(names::Name::Numbered)
Expand Down
22 changes: 20 additions & 2 deletions src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use crate::{
lnurlp::{lnurl_callback, verify, well_known_lnurlp},
nostr::well_known_nip5,
register::{check_available, register},
register::{check_available, check_registered_pubkey, register},
State, ALLOWED_LOCALHOST, ALLOWED_ORIGINS, ALLOWED_SUBDOMAIN, API_VERSION,
};
use axum::extract::{Path, Query};
use axum::headers::Origin;
use axum::http::StatusCode;
use axum::response::Redirect;
use axum::Extension;
use axum::{Json, TypedHeader};
use fedimint_core::Amount;
use log::{debug, error};
use nostr::prelude::XOnlyPublicKey;
use serde::{de, Deserialize, Deserializer, Serialize};
use std::{collections::HashMap, fmt::Display, str::FromStr};
use axum::response::Redirect;
use tbs::AggregatePublicKey;
use url::Url;

Expand All @@ -32,6 +32,24 @@ pub async fn check_username(
}
}

pub async fn check_pubkey(
origin: Option<TypedHeader<Origin>>,
Extension(state): Extension<State>,
Path(pubkey): Path<String>,
) -> Result<Json<Option<String>>, (StatusCode, String)> {
debug!("check_pubkey: {}", pubkey);
validate_cors(origin)?;

// check it's a valid pubkey
XOnlyPublicKey::from_str(&pubkey)
.map_err(|_| (StatusCode::BAD_REQUEST, "Nostr Pubkey Invalid".to_string()))?;

match check_registered_pubkey(&state, pubkey) {
Ok(res) => Ok(Json(res)),
Err(e) => Err(handle_anyhow_error("check_pubkey", e)),
}
}

#[derive(Deserialize)]
pub struct RegisterRequest {
pub name: Option<String>,
Expand Down