From 2ff53e6939d658635829fd4034255769b3dce60d Mon Sep 17 00:00:00 2001 From: Tony Giorgio Date: Wed, 3 Apr 2024 15:43:23 -0500 Subject: [PATCH] Check the name for a pubkey --- Cargo.lock | 91 +++++++++++++++++++++++++++++-------------------- Cargo.toml | 16 ++++----- src/db.rs | 9 +++++ src/main.rs | 6 ++-- src/register.rs | 4 +++ src/routes.rs | 22 ++++++++++-- 6 files changed, 99 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 928f338..b23c8c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -363,7 +363,7 @@ dependencies = [ "bitflags 2.4.2", "cexpr", "clang-sys", - "itertools 0.10.5", + "itertools 0.12.1", "lazy_static", "lazycell", "proc-macro2", @@ -1089,8 +1089,9 @@ checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "fedimint-aead" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aa38d22aa2ec4a8182f781c9bdd91b899cf9c5f4f5cf4d8470ce72df4499fd" dependencies = [ "anyhow", "argon2", @@ -1101,8 +1102,9 @@ dependencies = [ [[package]] name = "fedimint-bitcoind" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59bd38e6c7ca4c9294660037cbb4c3eaa09c2e4566f90feedcebd56b4a396a27" dependencies = [ "anyhow", "async-trait", @@ -1122,16 +1124,18 @@ dependencies = [ [[package]] name = "fedimint-build" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a87d1526d927524a13663e844b6a65a6bf91d619ccdf1ffa122f7ab197d32ec6" dependencies = [ "serde_json", ] [[package]] name = "fedimint-client" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c291c1f8a74d52181ec996bc1954df91242af134fb690aa0c9b560462b6ffa7" dependencies = [ "anyhow", "aquamarine", @@ -1161,8 +1165,9 @@ dependencies = [ [[package]] name = "fedimint-core" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5664f67aee6ea8b28dc91363d602ee17c6c05f1f36f2f1d0064785e1cf1c3d0e" dependencies = [ "anyhow", "async-lock", @@ -1214,8 +1219,9 @@ dependencies = [ [[package]] name = "fedimint-derive" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3212a1f0b4f3d7a8b93bc458c9dbc591eabf8342ab44496a26d0201834140a" dependencies = [ "itertools 0.12.1", "proc-macro2", @@ -1225,8 +1231,9 @@ dependencies = [ [[package]] name = "fedimint-derive-secret" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eecd4ff491a3eae9de9c8a4844febad2af2e414b0100d31fc14b6f6c3c1e0f9" dependencies = [ "anyhow", "fedimint-core", @@ -1238,16 +1245,18 @@ dependencies = [ [[package]] name = "fedimint-hkdf" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2303eedda2fc433b1f6a9969ec3aba0fd40f41e403fd60b7e079cab9a02232a5" dependencies = [ "bitcoin_hashes 0.11.0", ] [[package]] name = "fedimint-ln-client" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11e2f9a7125f76b26edc719e87b5550b4cec41dcd4f46080f19b3e0b9cb959a1" dependencies = [ "anyhow", "aquamarine", @@ -1280,8 +1289,9 @@ dependencies = [ [[package]] name = "fedimint-ln-common" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd0590df1190cfb2a53c39d2c7eb152c45f77760c2653a2e823b4c10a57c2612" dependencies = [ "anyhow", "aquamarine", @@ -1310,8 +1320,9 @@ dependencies = [ [[package]] name = "fedimint-logging" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26a4aec2b4da537dad90e76eae070a066e211f11bd1d16c5d84fa16f61cf568" dependencies = [ "anyhow", "tracing-subscriber", @@ -1319,8 +1330,9 @@ dependencies = [ [[package]] name = "fedimint-mint-client" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bc49a3963e108a9ee34e097d7929496cae14cf3b4e97cc3fb4b0da92e039037" dependencies = [ "anyhow", "aquamarine", @@ -1354,8 +1366,9 @@ dependencies = [ [[package]] name = "fedimint-mint-common" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c40186a21482c8162ddef517692b3547fddc6ab584fb4f9e90a09adf8a331e16" dependencies = [ "anyhow", "async-trait", @@ -1378,8 +1391,9 @@ dependencies = [ [[package]] name = "fedimint-rocksdb" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6855695fa429c4cdcdff1756ac217369f3585e3a68a2c52bcff173e656bb0325" dependencies = [ "anyhow", "async-trait", @@ -1392,8 +1406,9 @@ dependencies = [ [[package]] name = "fedimint-tbs" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad90977bc2ca480371aeb395efe35c57ddabad103b10ffe418dd8c068092dd93" dependencies = [ "bitcoin_hashes 0.11.0", "bls12_381", @@ -1429,8 +1444,9 @@ dependencies = [ [[package]] name = "fedimint-wallet-client" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9edc4fcd7f3284fe9d89d7e94fb03d765753a66b46b53e2256de70b65f52c4f3" dependencies = [ "anyhow", "aquamarine", @@ -1460,8 +1476,9 @@ dependencies = [ [[package]] name = "fedimint-wallet-common" -version = "0.3.0-rc.2" -source = "git+https://github.com/fedimint/fedimint?tag=v0.3.0-rc.2#44f129b216df39a4d896f96146eb89efe264d838" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4d8f6767ab717bbc47ffbb2dc3bb7d68ea5d83c48f25fbfe100b6b879406b48" dependencies = [ "anyhow", "async-trait", @@ -2612,8 +2629,8 @@ dependencies = [ [[package]] name = "multimint" -version = "0.1.12" -source = "git+https://github.com/fedimint/fedimint-clientd?rev=16fe9dd32c745267304a55aacee9501050bb03fa#16fe9dd32c745267304a55aacee9501050bb03fa" +version = "0.1.13" +source = "git+https://github.com/fedimint/fedimint-clientd?rev=b3078124dd65e6b96fe824da2a0c772a6b4bd9cd#b3078124dd65e6b96fe824da2a0c772a6b4bd9cd" dependencies = [ "anyhow", "fedimint-client", diff --git a/Cargo.toml b/Cargo.toml index 4b4db97..5d19270 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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] diff --git a/src/db.rs b/src/db.rs index 4ab7f7e..6c55c02 100644 --- a/src/db.rs +++ b/src/db.rs @@ -13,6 +13,7 @@ use crate::models::{ #[cfg_attr(test, automock)] pub(crate) trait DBConnection { fn check_name_available(&self, name: String) -> anyhow::Result; + fn check_registered_pubkey(&self, pubkey: String) -> anyhow::Result>; fn get_user_by_token(&self, msg: String) -> anyhow::Result>; fn insert_new_user(&self, name: NewAppUser) -> anyhow::Result; fn get_pending_invoices(&self) -> anyhow::Result>; @@ -37,6 +38,14 @@ impl DBConnection for PostgresConnection { AppUser::check_available_name(conn, name) } + fn check_registered_pubkey(&self, pubkey: String) -> anyhow::Result> { + 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> { let conn = &mut self.db.get()?; AppUser::get_by_token(conn, msg) diff --git a/src/main.rs b/src/main.rs index 142273c..d60712f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, }, }; @@ -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( diff --git a/src/register.rs b/src/register.rs index 78f604d..6ff4df0 100644 --- a/src/register.rs +++ b/src/register.rs @@ -31,6 +31,10 @@ pub fn check_available(state: &State, name: String) -> anyhow::Result { state.db.check_name_available(name) } +pub fn check_registered_pubkey(state: &State, pubkey: String) -> anyhow::Result> { + state.db.check_registered_pubkey(pubkey) +} + pub fn generate_random_name(state: &State) -> anyhow::Result { loop { let new_name = Generator::with_naming(names::Name::Numbered) diff --git a/src/routes.rs b/src/routes.rs index 811d6ea..921ed80 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,12 +1,13 @@ 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; @@ -14,7 +15,6 @@ 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; @@ -32,6 +32,24 @@ pub async fn check_username( } } +pub async fn check_pubkey( + origin: Option>, + Extension(state): Extension, + Path(pubkey): Path, +) -> Result>, (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,