Skip to content

Commit

Permalink
Implement gift wrap (#354)
Browse files Browse the repository at this point in the history
* Implement gift wrap

* Fixes wrapping and unwrapping of events

* Removes unwrap()

* Refactoring cancel

* Implement nip59 creating new order

* Refactoring with nip59 new logic
  • Loading branch information
grunch authored Sep 13, 2024
1 parent 66fc27f commit 30e1e8b
Show file tree
Hide file tree
Showing 17 changed files with 338 additions and 219 deletions.
224 changes: 109 additions & 115 deletions src/app.rs

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use anyhow::{Error, Result};
use mostro_core::message::{Action, Content, Message};
use mostro_core::order::SmallOrder;
use mostro_core::order::{Kind, Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
Expand All @@ -14,7 +15,7 @@ use tracing::error;

pub async fn add_invoice_action(
msg: Message,
event: &Event,
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
) -> Result<()> {
Expand Down Expand Up @@ -52,8 +53,8 @@ pub async fn add_invoice_action(
}
};
// Only the buyer can add an invoice
if buyer_pubkey != event.pubkey {
send_cant_do_msg(Some(order.id), None, &event.pubkey).await;
if buyer_pubkey != event.sender {
send_cant_do_msg(Some(order.id), None, &event.sender).await;
return Ok(());
}

Expand All @@ -72,7 +73,7 @@ pub async fn add_invoice_action(
{
Ok(_) => payment_request,
Err(_) => {
send_new_order_msg(None, Action::IncorrectInvoiceAmount, None, &event.pubkey)
send_new_order_msg(None, Action::IncorrectInvoiceAmount, None, &event.sender)
.await;
return Ok(());
}
Expand All @@ -98,7 +99,7 @@ pub async fn add_invoice_action(
Some(order.id),
Action::NotAllowedByStatus,
None,
&event.pubkey,
&event.sender,
)
.await;
return Ok(());
Expand Down
9 changes: 5 additions & 4 deletions src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use crate::util::{send_cant_do_msg, send_dm};
use anyhow::Result;
use mostro_core::message::{Action, Content, Message};
use mostro_core::user::User;
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
use tracing::{error, info};

pub async fn admin_add_solver_action(
msg: Message,
event: &Event,
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
) -> Result<()> {
Expand All @@ -29,9 +30,9 @@ pub async fn admin_add_solver_action(
};

// Check if the pubkey is Mostro
if event.pubkey.to_string() != my_keys.public_key().to_string() {
if event.sender.to_string() != my_keys.public_key().to_string() {
// We create a Message
send_cant_do_msg(None, None, &event.pubkey).await;
send_cant_do_msg(None, None, &event.sender).await;
return Ok(());
}
let public_key = PublicKey::from_bech32(npubkey)?.to_hex();
Expand All @@ -45,7 +46,7 @@ pub async fn admin_add_solver_action(
let message = Message::new_dispute(None, None, Action::AdminAddSolver, None);
let message = message.as_json()?;
// Send the message
send_dm(&event.pubkey, message).await?;
send_dm(&event.sender, message).await?;

Ok(())
}
15 changes: 8 additions & 7 deletions src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use anyhow::{Error, Result};
use mostro_core::dispute::Status as DisputeStatus;
use mostro_core::message::{Action, Message, MessageKind};
use mostro_core::order::{Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
use tracing::{error, info};

pub async fn admin_cancel_action(
msg: Message,
event: &Event,
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
Expand All @@ -28,13 +29,13 @@ pub async fn admin_cancel_action(
return Err(Error::msg("No order id"));
};

match is_assigned_solver(pool, &event.pubkey.to_string(), order_id).await {
match is_assigned_solver(pool, &event.sender.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(
Some(order_id),
Action::IsNotYourDispute,
None,
&event.pubkey,
&event.sender,
)
.await;
return Ok(());
Expand All @@ -58,12 +59,12 @@ pub async fn admin_cancel_action(
if order.status == Status::CooperativelyCanceled.to_string() {
let message = MessageKind::new(
Some(order_id),
Some(event.pubkey.to_string()),
Some(event.sender.to_string()),
Action::CooperativeCancelAccepted,
None,
);
if let Ok(message) = message.as_json() {
let _ = send_dm(&event.pubkey, message).await;
let _ = send_dm(&event.sender, message).await;
}
return Ok(());
}
Expand All @@ -73,7 +74,7 @@ pub async fn admin_cancel_action(
Some(order.id),
Action::NotAllowedByStatus,
None,
&event.pubkey,
&event.sender,
)
.await;
return Ok(());
Expand Down Expand Up @@ -124,7 +125,7 @@ pub async fn admin_cancel_action(
let message = Message::new_order(Some(order.id), None, Action::AdminCanceled, None);
let message = message.as_json()?;
// Message to admin
send_dm(&event.pubkey, message.clone()).await?;
send_dm(&event.sender, message.clone()).await?;

let (seller_pubkey, buyer_pubkey) = match (&order.seller_pubkey, &order.buyer_pubkey) {
(Some(seller), Some(buyer)) => (
Expand Down
15 changes: 8 additions & 7 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use anyhow::{Error, Result};
use mostro_core::dispute::Status as DisputeStatus;
use mostro_core::message::{Action, Message, MessageKind};
use mostro_core::order::{Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
Expand All @@ -18,7 +19,7 @@ use super::release::do_payment;

pub async fn admin_settle_action(
msg: Message,
event: &Event,
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
Expand All @@ -29,13 +30,13 @@ pub async fn admin_settle_action(
return Err(Error::msg("No order id"));
};

match is_assigned_solver(pool, &event.pubkey.to_string(), order_id).await {
match is_assigned_solver(pool, &event.sender.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(
Some(order_id),
Action::IsNotYourDispute,
None,
&event.pubkey,
&event.sender,
)
.await;
return Ok(());
Expand All @@ -59,12 +60,12 @@ pub async fn admin_settle_action(
if order.status == Status::CooperativelyCanceled.to_string() {
let message = MessageKind::new(
Some(order_id),
Some(event.pubkey.to_string()),
Some(event.sender.to_string()),
Action::CooperativeCancelAccepted,
None,
);
if let Ok(message) = message.as_json() {
let _ = send_dm(&event.pubkey, message).await;
let _ = send_dm(&event.sender, message).await;
}
return Ok(());
}
Expand All @@ -74,7 +75,7 @@ pub async fn admin_settle_action(
Some(order.id),
Action::NotAllowedByStatus,
None,
&event.pubkey,
&event.sender,
)
.await;
return Ok(());
Expand Down Expand Up @@ -117,7 +118,7 @@ pub async fn admin_settle_action(
let message = Message::new_order(Some(order_updated.id), None, Action::AdminSettled, None);
let message = message.as_json()?;
// Message to admin
send_dm(&event.pubkey, message.clone()).await?;
send_dm(&event.sender, message.clone()).await?;
if let Some(ref seller_pubkey) = order_updated.seller_pubkey {
send_dm(&PublicKey::from_str(seller_pubkey)?, message.clone()).await?;
}
Expand Down
17 changes: 9 additions & 8 deletions src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::{Error, Result};
use mostro_core::dispute::{Dispute, Status};
use mostro_core::message::{Action, Content, Message, Peer};
use mostro_core::order::Order;
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
Expand Down Expand Up @@ -39,7 +40,7 @@ pub async fn pubkey_event_can_solve(

pub async fn admin_take_dispute_action(
msg: Message,
event: &Event,
event: &UnwrappedGift,
pool: &Pool<Sqlite>,
) -> Result<()> {
// Find dipute id in the message
Expand All @@ -54,16 +55,16 @@ pub async fn admin_take_dispute_action(
Some(dispute) => dispute,
None => {
// We create a Message
send_new_order_msg(Some(dispute_id), Action::NotFound, None, &event.pubkey).await;
send_new_order_msg(Some(dispute_id), Action::NotFound, None, &event.sender).await;
return Ok(());
}
};

// Check if the pubkey is a solver or admin
if let Ok(dispute_status) = Status::from_str(&dispute.status) {
if !pubkey_event_can_solve(pool, &event.pubkey, dispute_status).await {
if !pubkey_event_can_solve(pool, &event.sender, dispute_status).await {
// We create a Message
send_cant_do_msg(Some(dispute_id), None, &event.pubkey).await;
send_cant_do_msg(Some(dispute_id), None, &event.sender).await;
return Ok(());
}
} else {
Expand All @@ -85,10 +86,10 @@ pub async fn admin_take_dispute_action(

// Update dispute fields
dispute.status = Status::InProgress.to_string();
dispute.solver_pubkey = Some(event.pubkey.to_string());
dispute.solver_pubkey = Some(event.sender.to_string());
dispute.taken_at = Timestamp::now().as_u64() as i64;

info!("Dispute {} taken by {}", dispute_id, event.pubkey);
info!("Dispute {} taken by {}", dispute_id, event.sender);
// Assign token for admin message
new_order.seller_token = dispute.seller_token;
new_order.buyer_token = dispute.buyer_token;
Expand All @@ -103,10 +104,10 @@ pub async fn admin_take_dispute_action(
Some(Content::Order(new_order)),
);
let message = message.as_json()?;
send_dm(&event.pubkey, message).await?;
send_dm(&event.sender, message).await?;
// Now we create a message to both parties of the order
// to them know who will assist them on the dispute
let solver_pubkey = Peer::new(event.pubkey.to_hex());
let solver_pubkey = Peer::new(event.sender.to_hex());
let msg_to_buyer = Message::new_order(
Some(order.id),
None,
Expand Down
Loading

0 comments on commit 30e1e8b

Please sign in to comment.