Skip to content

Commit

Permalink
limit nostr
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Nov 21, 2024
1 parent b5d0188 commit 4abfb0e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ async fn onchain_handler(
return Err(AppError::new("Too many payments"));
}

if state.payments.get_total_payments(&payload.address).await > MAX_SEND_AMOUNT {
return Err(AppError::new("Too many payments"));
}

let res = pay_onchain(state, x_forwarded_for, payload).await?;

Ok(Json(res))
Expand Down
33 changes: 31 additions & 2 deletions src/nostr_dms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async fn pay_invoice(invoice: Bolt11Invoice, state: &AppState) -> anyhow::Result
.amount_milli_satoshis()
.is_some_and(|amt| amt / 1_000 < MAX_SEND_AMOUNT)
{
info!("Paying invoice: {invoice}");
info!("Paying invoice: {invoice} from nostr dm");
let mut lightning_client = state.lightning_client.clone();

let response = lightning_client
Expand Down Expand Up @@ -173,9 +173,27 @@ async fn handle_event(event: Event, state: AppState) -> anyhow::Result<()> {
return Err(anyhow::anyhow!("Amount exceeds max send amount"));
}

if state
.payments
.get_total_payments(&event.pubkey.to_string())
.await
> MAX_SEND_AMOUNT * 10
{
return Err(anyhow::anyhow!("Too many payments"));
}

if state
.payments
.get_total_payments(&address.to_string())
.await
> MAX_SEND_AMOUNT
{
return Err(anyhow::anyhow!("Too many payments"));
}

let resp = {
let mut wallet_client = state.lightning_client.clone();
info!("Sending {amount} to {address}");
info!("Sending {amount} to {address} from nostr dm");
let req = lnrpc::SendCoinsRequest {
addr: address.to_string(),
amount: amount.to_sat() as i64,
Expand All @@ -186,6 +204,17 @@ async fn handle_event(event: Event, state: AppState) -> anyhow::Result<()> {
wallet_client.send_coins(req).await?.into_inner()
};

state
.payments
.add_payment(&event.pubkey.to_string(), amount.to_sat())
.await;

// track for address too
state
.payments
.add_payment(&address.to_string(), amount.to_sat())
.await;

let txid = resp.txid;

info!("Sent onchain tx: {txid}");
Expand Down
10 changes: 8 additions & 2 deletions src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::str::FromStr;

#[derive(Clone, Deserialize)]
pub struct OnchainRequest {
sats: Option<u64>,
address: String,
pub sats: Option<u64>,
pub address: String,
}

#[derive(Clone, Serialize)]
Expand Down Expand Up @@ -69,6 +69,12 @@ pub async fn pay_onchain(
.add_payment(x_forwarded_for, amount.to_sat())
.await;

// track for address too
state
.payments
.add_payment(&address.to_string(), amount.to_sat())
.await;

OnchainResponse {
txid: resp.txid,
address: address.to_string(),
Expand Down

0 comments on commit 4abfb0e

Please sign in to comment.