Skip to content

Commit

Permalink
refactor: send encrypted response
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed May 26, 2024
1 parent 97cb4d1 commit fc402fc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
20 changes: 4 additions & 16 deletions fedimint-nwc/src/nwc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nostr::nips::nip47::{
ErrorCode, Method, NIP47Error, Request, RequestParams, Response, ResponseResult,
};
use nostr::Tag;
use nostr_sdk::{Event, JsonUtil};
use nostr_sdk::{Event, EventBuilder, JsonUtil, Kind};
use tokio::spawn;
use tracing::{error, info};

Expand Down Expand Up @@ -306,21 +306,9 @@ async fn handle_nwc_params(
}
};

let encrypted = nip04::encrypt(
&keys.server_key.into(),
&keys.user_keys().public_key(),
content.as_json(),
)?;
let p_tag = Tag::public_key(event.pubkey);
let e_tag = Tag::event(event.id);
let tags = match d_tag {
None => vec![p_tag, e_tag],
Some(d_tag) => vec![p_tag, e_tag, d_tag],
};
let response = EventBuilder::new(Kind::WalletConnectResponse, encrypted, tags)
.to_event(&keys.server_keys())?;

client.send_event(response).await?;
nostr
.send_encrypted_response(&event, content, d_tag)
.await?;

Ok(())
}
39 changes: 37 additions & 2 deletions fedimint-nwc/src/services/nostr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use std::fs::{create_dir_all, File};
use std::io::{BufReader, Write};
use std::path::Path;

use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use nostr::nips::nip04;
use nostr::nips::nip47::Response;
use nostr_sdk::secp256k1::SecretKey;
use nostr_sdk::{Client, Event, EventBuilder, JsonUtil, Keys, Kind, RelayPoolNotification};
use nostr_sdk::{
Client, Event, EventBuilder, EventId, JsonUtil, Keys, Kind, RelayPoolNotification, Tag,
};
use serde::{Deserialize, Serialize};
use tokio::sync::broadcast::Receiver;
use tracing::info;
Expand Down Expand Up @@ -95,6 +99,37 @@ impl NostrService {
Ok(())
}

pub async fn send_event(&self, event: Event) -> Result<EventId, anyhow::Error> {
self.client
.send_event(event)
.await
.map_err(|e| anyhow!("Failed to send event: {}", e))
}

pub async fn send_encrypted_response(
&self,
event: &Event,
content: Response,
d_tag: Option<Tag>,
) -> Result<(), anyhow::Error> {
let encrypted = nip04::encrypt(
self.server_keys().secret_key()?,
&self.user_keys().public_key(),
content.as_json(),
)?;
let p_tag = Tag::public_key(event.pubkey);
let e_tag = Tag::event(event.id);
let tags = match d_tag {
None => vec![p_tag, e_tag],
Some(d_tag) => vec![p_tag, e_tag, d_tag],
};
let response = EventBuilder::new(Kind::WalletConnectResponse, encrypted, tags)
.to_event(&self.server_keys())?;

self.send_event(response).await?;
Ok(())
}

pub async fn broadcast_info_event(&self) -> Result<(), anyhow::Error> {
let content = METHODS
.iter()
Expand Down

0 comments on commit fc402fc

Please sign in to comment.