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

Use more relays & update rust-nostr #26

Merged
merged 2 commits into from
Apr 10, 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
316 changes: 221 additions & 95 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ url = "2.5.0"
itertools = "0.12.0"
hex = "0.4.3"
jwt-compact = { version = "0.8.0", features = ["es256k"] }
nostr = "0.26.0"
nostr-sdk = "0.26.0"
nostr = "0.29.1"
nostr-sdk = "0.29.0"
log = "0.4.20"
pretty_env_logger = "0.5"
secp256k1 = { version = "0.28.2", default-features = false, features = ["hashes", "alloc"] }
Expand Down
11 changes: 5 additions & 6 deletions src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use itertools::Itertools;
use log::{error, info};
use nostr::prelude::rand::rngs::OsRng;
use nostr::prelude::rand::RngCore;
use nostr::secp256k1::XOnlyPublicKey;
use nostr::Keys;
use nostr::{Event, EventBuilder, JsonUtil};
use nostr_sdk::Client;
Expand Down Expand Up @@ -146,7 +145,7 @@ async fn notify_user(

let dm = nostr
.send_direct_msg(
XOnlyPublicKey::from_str(&user.pubkey)?,
::nostr::PublicKey::from_str(&user.pubkey)?,
json!({
"federation_id": invoice.federation_id,
"tweak_index": invoice.user_invoice_index,
Expand All @@ -163,7 +162,7 @@ async fn notify_user(
// Send zap if needed
if let Some(zap) = zap {
let request = Event::from_json(&zap.request)?;
let event = create_zap_event(request, invoice.amount as u64, nostr.keys().await)?;
let event = create_zap_event(request, invoice.amount as u64, &state.nostr_sk)?;

let event_id = nostr.send_event(event).await?;
info!("Broadcasted zap {event_id}!");
Expand All @@ -176,7 +175,7 @@ async fn notify_user(
}

/// Creates a nostr zap event with a fake invoice
fn create_zap_event(request: Event, amt_msats: u64, nsec: Keys) -> Result<Event> {
fn create_zap_event(request: Event, amt_msats: u64, nsec: &Keys) -> Result<Event> {
let preimage = &mut [0u8; 32];
OsRng.fill_bytes(preimage);
let invoice_hash = Sha256::hash(preimage);
Expand All @@ -199,12 +198,12 @@ fn create_zap_event(request: Event, amt_msats: u64, nsec: Keys) -> Result<Event>
.min_final_cltv_expiry_delta(144)
.build_signed(|hash| Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))?;

let event = EventBuilder::new_zap_receipt(
let event = EventBuilder::zap_receipt(
fake_invoice.to_string(),
Some(hex::encode(preimage)),
request,
)
.to_event(&nsec)?;
.to_event(nsec)?;

Ok(event)
}
18 changes: 11 additions & 7 deletions src/lnurlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub async fn well_known_lnurlp(
comment_allowed: None,
tag: LnurlType::PayRequest,
status: LnurlStatus::Ok,
nostr_pubkey: Some(state.nostr.keys().await.public_key()),
nostr_pubkey: Some(state.nostr_sk.public_key()),
allows_nostr: true,
};

Expand Down Expand Up @@ -201,7 +201,7 @@ pub async fn verify(
mod tests_integration {
use fedimint_core::api::InviteCode;
use nostr::prelude::{rand, ZapRequestData};
use nostr::{key::FromSkStr, EventBuilder, Keys};
use nostr::{EventBuilder, Keys};
use secp256k1::Secp256k1;
use std::path::PathBuf;
use std::sync::Arc;
Expand All @@ -226,7 +226,7 @@ mod tests_integration {

// 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_sk = Keys::from_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
Expand All @@ -242,6 +242,7 @@ mod tests_integration {
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://hello.com".to_string(),
nostr_sk,
};

let username = "wellknownuser".to_string();
Expand Down Expand Up @@ -277,7 +278,7 @@ mod tests_integration {

// 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_sk = Keys::from_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
Expand All @@ -295,6 +296,7 @@ mod tests_integration {
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://hello.com".to_string(),
nostr_sk,
};

let invite_code = InviteCode::from_str(INVITE_CODE).unwrap();
Expand Down Expand Up @@ -340,7 +342,7 @@ mod tests_integration {

// 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_sk = Keys::from_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
Expand All @@ -362,6 +364,7 @@ mod tests_integration {
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://hello.com".to_string(),
nostr_sk,
};

let invite_code = InviteCode::from_str(INVITE_CODE).unwrap();
Expand Down Expand Up @@ -405,7 +408,7 @@ mod tests_integration {

// 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_sk = Keys::from_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
Expand All @@ -427,6 +430,7 @@ mod tests_integration {
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://hello.com".to_string(),
nostr_sk,
};

let invite_code = InviteCode::from_str(INVITE_CODE).unwrap();
Expand All @@ -447,7 +451,7 @@ mod tests_integration {

let zap_request = {
let data = ZapRequestData::new(pk, vec![]);
EventBuilder::new_zap_request(data)
EventBuilder::public_zap_request(data)
.to_event(&Keys::generate())
.unwrap()
};
Expand Down
28 changes: 21 additions & 7 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_sdk::nostr::{key::FromSkStr, Keys};
use nostr_sdk::nostr::Keys;
use secp256k1::{All, Secp256k1};
use std::{path::PathBuf, str::FromStr, sync::Arc};
use tbs::{AggregatePublicKey, PubKeyPoint};
Expand All @@ -18,8 +18,7 @@ use crate::{
mint::{setup_multimint, MultiMintWrapperTrait},
routes::{
check_pubkey, check_username, health_check, lnurl_callback_route, lnurl_verify_route,
register_route, root, validate_cors, well_known_lnurlp_route,
well_known_nip5_route,
register_route, root, validate_cors, well_known_lnurlp_route, well_known_nip5_route,
},
};

Expand All @@ -46,6 +45,18 @@ const ALLOWED_LOCALHOST: &str = "http://127.0.0.1:";

const API_VERSION: &str = "v1";

const RELAYS: [&str; 9] = [
"wss://nostr.mutinywallet.com",
"wss://relay.mutinywallet.com",
"wss://relay.snort.social",
"wss://nos.lol",
"wss://relay.damus.io",
"wss://relay.primal.net",
"wss://nostr.wine",
"wss://nostr.zbd.gg",
"wss://relay.nos.social",
];

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct SignerIdentity {
pub service_id: i32,
Expand All @@ -58,6 +69,7 @@ pub struct State {
mm: Arc<dyn MultiMintWrapperTrait + Send + Sync>,
pub secp: Secp256k1<All>,
pub nostr: nostr_sdk::Client,
pub nostr_sk: Keys,
pub domain: String,
pub free_pk: AggregatePublicKey,
pub paid_pk: AggregatePublicKey,
Expand Down Expand Up @@ -114,11 +126,12 @@ async fn main() -> anyhow::Result<()> {

// nostr
let nostr_nsec_str = std::env::var("NSEC").expect("NSEC must be set");
let nostr_sk = Keys::from_sk_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr_sk = Keys::from_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);
nostr.add_relay("wss://nostr.mutinywallet.com").await?;
nostr.add_relay("wss://relay.mutinywallet.com").await?;
nostr.add_relay("wss://relay.damus.io").await?;
nostr
.add_relays(RELAYS)
.await
.expect("Failed to add relays");
nostr.connect().await;

// domain
Expand All @@ -133,6 +146,7 @@ async fn main() -> anyhow::Result<()> {
mm,
secp,
nostr,
nostr_sk,
domain,
free_pk,
paid_pk,
Expand Down
11 changes: 6 additions & 5 deletions src/nostr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axum::http::StatusCode;
use axum::Json;
use nostr::prelude::XOnlyPublicKey;
use nostr::PublicKey;
use serde_json::{json, Value};
use std::{collections::HashMap, str::FromStr};

Expand All @@ -9,7 +9,7 @@ use crate::State;
pub fn well_known_nip5(
state: &State,
name: String,
) -> Result<HashMap<String, XOnlyPublicKey>, (StatusCode, Json<Value>)> {
) -> Result<HashMap<String, PublicKey>, (StatusCode, Json<Value>)> {
let user = state.db.get_user_by_name(name).map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Expand All @@ -21,7 +21,7 @@ pub fn well_known_nip5(
if let Some(user) = user {
names.insert(
user.name,
XOnlyPublicKey::from_str(&user.pubkey).expect("valid npub"),
PublicKey::from_str(&user.pubkey).expect("valid npub"),
);
} else {
return Err((
Expand All @@ -35,7 +35,7 @@ pub fn well_known_nip5(

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

Expand All @@ -55,7 +55,7 @@ mod tests_integration {

// 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_sk = Keys::parse(nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
Expand All @@ -71,6 +71,7 @@ mod tests_integration {
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://127.0.0.1:8080".to_string(),
nostr_sk,
};

let username = "wellknownuser".to_string();
Expand Down
18 changes: 11 additions & 7 deletions src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use fedimint_core::api::InviteCode;
use lazy_regex::*;
use log::error;
use names::Generator;
use nostr::prelude::XOnlyPublicKey;
use nostr::PublicKey;
use reqwest::StatusCode;

pub static ALPHANUMERIC_REGEX: Lazy<Regex> = lazy_regex!("^[a-z0-9-_.]+$");
Expand Down Expand Up @@ -57,7 +57,7 @@ pub async fn register(
if requested_paid && !is_valid_name(&req.name.clone().unwrap()) {
return Err((StatusCode::BAD_REQUEST, "Unavailable".to_string()));
}
XOnlyPublicKey::from_str(&req.pubkey)
PublicKey::from_str(&req.pubkey)
.map_err(|_| (StatusCode::BAD_REQUEST, "Nostr Pubkey Invalid".to_string()))?;

// a different signer based on paid vs free
Expand Down Expand Up @@ -220,7 +220,7 @@ mod tests_integration {
use std::{str::FromStr, sync::Arc};

use fedimint_core::{api::InviteCode, config::FederationId, PeerId};
use nostr::{key::FromSkStr, Keys};
use nostr::Keys;
use secp256k1::Secp256k1;
use tbs::{blind_message, unblind_signature, BlindingKey};

Expand All @@ -243,7 +243,7 @@ mod tests_integration {
let mock_mm = Arc::new(MockMultiMintWrapperTrait::new());

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_sk = Keys::from_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
Expand All @@ -258,6 +258,7 @@ mod tests_integration {
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://127.0.0.1:8080".to_string(),
nostr_sk,
};

let name = "veryuniquename123".to_string();
Expand Down Expand Up @@ -295,7 +296,7 @@ mod tests_integration {

// 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_sk = Keys::from_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
Expand All @@ -311,6 +312,7 @@ mod tests_integration {
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://127.0.0.1:8080".to_string(),
nostr_sk,
};

// generate valid blinded message
Expand Down Expand Up @@ -361,7 +363,7 @@ mod tests_integration {

// 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_sk = Keys::from_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
Expand All @@ -377,6 +379,7 @@ mod tests_integration {
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://127.0.0.1:8080".to_string(),
nostr_sk,
};

// generate valid blinded message
Expand Down Expand Up @@ -423,7 +426,7 @@ mod tests_integration {

// 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_sk = Keys::from_str(&nostr_nsec_str).expect("Invalid NOSTR_SK");
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
Expand All @@ -439,6 +442,7 @@ mod tests_integration {
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://127.0.0.1:8080".to_string(),
nostr_sk,
};

// generate valid blinded message
Expand Down
Loading