-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
202 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::fs::{create_dir_all, File}; | ||
use std::io::{BufReader, Write}; | ||
use std::path::Path; | ||
|
||
use anyhow::{Context, Result}; | ||
use nostr_sdk::secp256k1::SecretKey; | ||
use nostr_sdk::Keys; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Deserialize, Serialize)] | ||
pub struct KeyManager { | ||
server_key: SecretKey, | ||
user_key: SecretKey, | ||
#[serde(default)] | ||
pub sent_info: bool, | ||
} | ||
|
||
impl KeyManager { | ||
pub fn new(keys_file: &str) -> Result<Self> { | ||
let path = Path::new(keys_file); | ||
match File::open(path) { | ||
Ok(file) => { | ||
let reader = BufReader::new(file); | ||
serde_json::from_reader(reader).context("Failed to parse JSON") | ||
} | ||
Err(_) => { | ||
let keys = Self::generate()?; | ||
Self::write_keys(&keys, path)?; | ||
Ok(keys) | ||
} | ||
} | ||
} | ||
|
||
fn generate() -> Result<Self> { | ||
let server_keys = Keys::generate(); | ||
let server_key = server_keys.secret_key()?; | ||
let user_keys = Keys::generate(); | ||
let user_key = user_keys.secret_key()?; | ||
Ok(Self { | ||
server_key: **server_key, | ||
user_key: **user_key, | ||
sent_info: false, | ||
}) | ||
} | ||
|
||
fn write_keys(keys: &Self, path: &Path) -> Result<()> { | ||
let json_str = serde_json::to_string(keys).context("Failed to serialize data")?; | ||
if let Some(parent) = path.parent() { | ||
create_dir_all(parent).context("Failed to create directory")?; | ||
} | ||
let mut file = File::create(path).context("Failed to create file")?; | ||
file.write_all(json_str.as_bytes()) | ||
.context("Failed to write to file")?; | ||
Ok(()) | ||
} | ||
|
||
pub fn server_keys(&self) -> Keys { | ||
Keys::new(self.server_key.into()) | ||
} | ||
|
||
pub fn user_keys(&self) -> Keys { | ||
Keys::new(self.user_key.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod key; | ||
|
||
pub use key::KeyManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod multimint; | ||
pub mod nostr; | ||
|
||
pub use multimint::MultiMintService; | ||
pub use nostr::NostrService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::path::PathBuf; | ||
use std::str::FromStr; | ||
|
||
use anyhow::Result; | ||
use multimint::fedimint_core::api::InviteCode; | ||
use multimint::MultiMint; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct MultiMintService { | ||
multimint: MultiMint, | ||
} | ||
|
||
impl MultiMintService { | ||
pub async fn new(db_path: PathBuf) -> Result<Self> { | ||
let clients = MultiMint::new(db_path).await?; | ||
clients.update_gateway_caches().await?; | ||
Ok(Self { multimint: clients }) | ||
} | ||
|
||
pub async fn init_multimint( | ||
&mut self, | ||
invite_code: &str, | ||
manual_secret: Option<String>, | ||
) -> Result<()> { | ||
match InviteCode::from_str(invite_code) { | ||
Ok(invite_code) => { | ||
let federation_id = self | ||
.multimint | ||
.register_new(invite_code, manual_secret) | ||
.await?; | ||
tracing::info!("Created client for federation id: {:?}", federation_id); | ||
Ok(()) | ||
} | ||
Err(e) => { | ||
tracing::error!("Invalid federation invite code: {}", e); | ||
Err(e.into()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use anyhow::Result; | ||
use nostr_sdk::{Client, EventBuilder, EventId, Kind, RelayPoolNotification}; | ||
use tokio::sync::broadcast::Receiver; | ||
|
||
use crate::managers::key::KeyManager; | ||
use crate::nwc::METHODS; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NostrService { | ||
client: Client, | ||
} | ||
|
||
impl NostrService { | ||
pub async fn new(key_manager: &KeyManager, relays: &str) -> Result<Self> { | ||
let client = Client::new(&key_manager.server_keys()); | ||
Self::add_relays(&client, relays).await?; | ||
Ok(Self { client }) | ||
} | ||
|
||
async fn add_relays(client: &Client, relays: &str) -> Result<()> { | ||
let lines = relays.split(',').collect::<Vec<_>>(); | ||
let relays = lines | ||
.iter() | ||
.map(|line| line.trim()) | ||
.filter(|line| !line.is_empty()) | ||
.map(|line| line.to_string()) | ||
.collect::<Vec<_>>(); | ||
for relay in relays { | ||
client.add_relay(relay).await?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub async fn broadcast_info_event(&self, keys: &KeyManager) -> Result<EventId> { | ||
let content = METHODS | ||
.iter() | ||
.map(ToString::to_string) | ||
.collect::<Vec<_>>() | ||
.join(" "); | ||
let info = EventBuilder::new(Kind::WalletConnectInfo, content, []) | ||
.to_event(&keys.server_keys())?; | ||
self.client | ||
.send_event(info) | ||
.await | ||
.map_err(|e| anyhow::anyhow!("Failed to send event: {}", e)) | ||
} | ||
|
||
pub async fn connect(&self) -> () { | ||
self.client.connect().await | ||
} | ||
|
||
pub async fn disconnect(&self) -> Result<()> { | ||
self.client | ||
.disconnect() | ||
.await | ||
.map_err(|e| anyhow::anyhow!("Failed to disconnect: {}", e)) | ||
} | ||
|
||
pub fn notifications(&self) -> Receiver<RelayPoolNotification> { | ||
self.client.notifications() | ||
} | ||
} |
Oops, something went wrong.