Skip to content

Commit

Permalink
feat: progress
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlwn123 committed May 27, 2024
1 parent 3cc8c79 commit 537834c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 43 deletions.
8 changes: 4 additions & 4 deletions fedimint-nwc/src/database/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Database {
}

pub fn add_payment(&self, invoice: Bolt11Invoice) -> Result<()> {
let payment_hash_encoded = hex::encode(invoice.payment_hash().to_vec());
let payment_hash_encoded = hex::encode(invoice.payment_hash());
self.write_with(|dbtx| {
let mut payments = dbtx.open_table(PAYMENTS_TABLE)?;
let now = std::time::SystemTime::now()
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Database {
}

pub fn add_invoice(&self, invoice: &Bolt11Invoice) -> Result<()> {
let payment_hash_encoded = hex::encode(invoice.payment_hash().to_vec());
let payment_hash_encoded = hex::encode(invoice.payment_hash());
let invoice = Invoice::from(invoice);
self.write_with(|dbtx| {
let mut invoices = dbtx.open_table(INVOICES_TABLE)?;
Expand All @@ -129,8 +129,8 @@ impl Database {
})
})
} else if let Some(bolt11) = params.invoice {
let invoice = Bolt11Invoice::from_str(&bolt11).map_err(|e| anyhow::Error::new(e))?;
let payment_hash_encoded = hex::encode(invoice.payment_hash().to_vec());
let invoice = Bolt11Invoice::from_str(&bolt11).map_err(anyhow::Error::new)?;
let payment_hash_encoded = hex::encode(invoice.payment_hash());
self.read_with(|dbtx| {
let invoices = dbtx.open_table(INVOICES_TABLE)?;
invoices
Expand Down
2 changes: 1 addition & 1 deletion fedimint-nwc/src/database/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Invoice {
}

pub fn payment_hash(&self) -> String {
hex::encode(self.invoice.payment_hash().to_vec())
hex::encode(self.invoice.payment_hash())
}

pub fn description(&self) -> Option<Bolt11InvoiceDescription> {
Expand Down
37 changes: 16 additions & 21 deletions fedimint-nwc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,23 @@ async fn event_loop(state: AppState) -> Result<()> {
break;
},
notification = notifications.recv() => {
match notification {
Ok(notification) => match notification {
RelayPoolNotification::Event { event, .. } => {
// Only handle nwc events
if event.kind == Kind::WalletConnectRequest
&& event.pubkey == state.nostr_service.user_keys().public_key()
&& event.verify().is_ok() {
info!("Received event: {}", event.as_json());
state.handle_event(*event).await
} else {
error!("Invalid nwc event: {}", event.as_json());
}
},
RelayPoolNotification::Shutdown => {
info!("Relay pool shutdown");
break;
},
_ => {
error!("Unhandled relay pool notification: {notification:?}");
if let Ok(notification) = notification {
if let RelayPoolNotification::Event { event, .. } = notification {
// Only handle nwc events
if event.kind == Kind::WalletConnectRequest
&& event.pubkey == state.nostr_service.user_keys().public_key()
&& event.verify().is_ok() {
info!("Received event: {}", event.as_json());
state.handle_event(*event).await
} else {
error!("Invalid nwc event: {}", event.as_json());
}
},
Err(_) => {},
} else if let RelayPoolNotification::Shutdown = notification {
info!("Relay pool shutdown");
break;
} else {
error!("Unhandled relay pool notification: {notification:?}");
}
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions fedimint-nwc/src/nwc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ async fn handle_multiple_payments<T>(
let event_clone = event.clone();
let mm = state.multimint_service.clone();
let nostr = state.nostr_service.clone();
let mut db = state.db.clone();
spawn(async move {
handle_nwc_params(params, method, &event_clone, &mm, &nostr, &mut db).await
})
let db = state.db.clone();
spawn(
async move { handle_nwc_params(params, method, &event_clone, &mm, &nostr, &db).await },
)
.await??;
}
Ok(())
Expand Down Expand Up @@ -117,15 +117,15 @@ async fn handle_nwc_params(
};

match response_result {
Ok(response) => nostr.send_encrypted_response(&event, response, d_tag).await,
Ok(response) => nostr.send_encrypted_response(event, response, d_tag).await,
Err(e) => {
let error_response = Response {
result_type: method,
error: Some(e),
result: None,
};
nostr
.send_encrypted_response(&event, error_response, d_tag)
.send_encrypted_response(event, error_response, d_tag)
.await
}
}
Expand Down Expand Up @@ -234,16 +234,13 @@ async fn handle_lookup_invoice(
None => (None, None),
};

let preimage = match invoice.clone().preimage {
Some(preimage) => Some(hex::encode(preimage)),
None => None,
};
let preimage = invoice.clone().preimage.map(hex::encode);

let settled_at = invoice.settled_at();
let created_at = invoice.created_at();
let expires_at = invoice.expires_at();
let invoice_str = invoice.invoice.to_string();
let amount = invoice.invoice.amount_milli_satoshis().unwrap_or(0) as u64;
let amount = invoice.invoice.amount_milli_satoshis().unwrap_or(0);

Ok(Response {
result_type: method,
Expand All @@ -255,7 +252,7 @@ async fn handle_lookup_invoice(
description_hash,
preimage,
payment_hash,
amount: amount,
amount,
fees_paid: 0,
created_at,
expires_at,
Expand Down
4 changes: 2 additions & 2 deletions fedimint-nwc/src/services/multimint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl MultiMintService {
}
Err(e) => {
tracing::error!("Invalid federation invite code: {}", e);
Err(e.into())
Err(e)
}
}
}
Expand All @@ -71,7 +71,7 @@ impl MultiMintService {
) -> Result<ClientHandleArc, anyhow::Error> {
let federation_id = match federation_id {
Some(id) => id,
None => match self.default_federation_id.clone() {
None => match self.default_federation_id {
Some(id) => id,
None => return Err(anyhow!("No default federation id set")),
},
Expand Down
2 changes: 1 addition & 1 deletion fedimint-nwc/src/services/nostr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl NostrService {
Ok(())
}

pub async fn connect(&self) -> () {
pub async fn connect(&self) {
self.client.connect().await
}

Expand Down
3 changes: 1 addition & 2 deletions fedimint-nwc/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ impl AppState {
let event_id = event.id;
self.active_requests.lock().await.insert(event_id);

match tokio::time::timeout(Duration::from_secs(60), handle_nwc_request(&self, event)).await
{
match tokio::time::timeout(Duration::from_secs(60), handle_nwc_request(self, event)).await {
Ok(Ok(_)) => {}
Ok(Err(e)) => error!("Error processing request: {e}"),
Err(e) => error!("Timeout error: {e}"),
Expand Down

0 comments on commit 537834c

Please sign in to comment.