Skip to content

Commit

Permalink
First draft on message version 1 (#153)
Browse files Browse the repository at this point in the history
* First draft on message version 1

* Initial phase of refactor of mostro code to update protcol message (#154)

* Refactoring for mostro-core last changes

* Mostro alignment to new protocol 1 enrichment - still not compiling

* cargo fmt executed

* Now it compiles with latest commit of mostro-core branch: message-version-1

* Cargo clippy and cargo fmt

* Refactoring using SmallOrder

* Update mostro core version

* Fix tests

---------

Co-authored-by: arkanoider <[email protected]>
Co-authored-by: arkanoider <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2023
1 parent 373ecaa commit d0165ab
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 244 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ uuid = { version = "1.3.0", features = [
"serde",
] }
reqwest = { version = "0.11", features = ["json"] }
mostro-core = "0.3.13"
mostro-core = "0.4.0"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
config = "0.13.3"
Expand Down
147 changes: 76 additions & 71 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::app::take_sell::take_sell_action;
use crate::lightning::LndConnector;

use anyhow::Result;
use mostro_core::{Action, Message};
use mostro_core::message::{Action, Message};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use std::sync::Arc;
Expand Down Expand Up @@ -57,85 +57,90 @@ pub async fn run(
if let Ok(m) = message {
let message = Message::from_json(&m);
if let Ok(msg) = message {
if msg.verify() {
match msg.action {
Action::Order => {
order_action(msg, &event, &my_keys, &client, &pool).await?;
}
Action::TakeSell => {
take_sell_action(msg, &event, &my_keys, &client, &pool)
if msg.get_inner_message_kind().verify() {
if let Some(action) = msg.inner_action() {
match action {
Action::NewOrder => {
order_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::TakeSell => {
take_sell_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::TakeBuy => {
take_buy_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::FiatSent => {
fiat_sent_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::Release => {
release_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::TakeBuy => {
take_buy_action(msg, &event, &my_keys, &client, &pool)
}
Action::Cancel => {
cancel_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::FiatSent => {
fiat_sent_action(msg, &event, &my_keys, &client, &pool)
}
Action::AddInvoice => {
add_invoice_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
}
Action::Release => {
release_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::Cancel => {
cancel_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::AddInvoice => {
add_invoice_action(msg, &event, &my_keys, &client, &pool)
}
Action::PayInvoice => todo!(),
Action::RateUser => {
update_user_reputation_action(
msg,
&event,
&my_keys,
&client,
&pool,
rate_list.clone(),
)
.await?;
}
Action::PayInvoice => todo!(),
Action::RateUser => {
update_user_reputation_action(
msg,
&event,
&my_keys,
&client,
&pool,
rate_list.clone(),
)
.await?;
}
Action::Dispute => {
dispute_action(msg, &event, &my_keys, &client, &pool)
}
Action::Dispute => {
dispute_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::AdminCancel => {
admin_cancel_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::AdminSettle => {
admin_settle_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::AdminAddSolver => {
admin_add_solver_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
}
Action::AdminTakeDispute => {
admin_take_dispute_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
}
_ => todo!(),
}
Action::AdminCancel => {
admin_cancel_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::AdminSettle => {
admin_settle_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::AdminAddSolver => {
admin_add_solver_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
}
Action::AdminTakeDispute => {
admin_take_dispute_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
}
_ => todo!(),
}
}
}
}
};
};
}
}
}
}
Expand Down
35 changes: 17 additions & 18 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::util::{send_dm, show_hold_invoice};

use anyhow::Result;
use log::error;
use mostro_core::message::{Action, Content, Message};
use mostro_core::order::SmallOrder;
use mostro_core::order::{Order, Status};
use mostro_core::{order::SmallOrder, Action, Content, Message};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
Expand All @@ -19,19 +20,19 @@ pub async fn add_invoice_action(
client: &Client,
pool: &Pool<Sqlite>,
) -> Result<()> {
let order_msg = msg.get_inner_message_kind();
// Safe unwrap as we verified the message
let order_id = msg.order_id.unwrap();
let order_id = order_msg.id.unwrap();
let order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
error!("Order Id {order_id} not found!");
return Ok(());
}
};

let pr: String;
// If a buyer sent me a lightning invoice we get it
if let Some(payment_request) = msg.get_payment_request() {
if let Some(payment_request) = order_msg.get_payment_request() {
// Verify if invoice is valid
match is_valid_invoice(
&payment_request,
Expand All @@ -46,11 +47,9 @@ pub async fn add_invoice_action(
| MostroError::WrongAmountError
| MostroError::MinAmountError => {
// We create a Message
let message = Message::new(
0,
let message = Message::cant_do(
Some(order.id),
None,
Action::CantDo,
Some(Content::TextMessage(e.to_string())),
);
let message = message.as_json()?;
Expand Down Expand Up @@ -84,7 +83,7 @@ pub async fn add_invoice_action(
// Only the buyer can add an invoice
if buyer_pubkey != event.pubkey {
// We create a Message
let message = Message::new(0, Some(order.id), None, Action::CantDo, None);
let message = Message::cant_do(Some(order.id), None, None);
let message = message.as_json().unwrap();
send_dm(client, my_keys, &event.pubkey, message).await?;

Expand All @@ -95,11 +94,9 @@ pub async fn add_invoice_action(
Status::WaitingBuyerInvoice => {}
_ => {
// We create a Message
let message = Message::new(
0,
let message = Message::cant_do(
Some(order.id),
None,
Action::CantDo,
Some(Content::TextMessage(format!(
"Order Id {order_id} status must be WaitingBuyerInvoice!"
))),
Expand All @@ -116,33 +113,35 @@ pub async fn add_invoice_action(
if order.preimage.is_some() {
// We send this data related to the order to the parties
let order_data = SmallOrder::new(
order.id,
Some(order.id),
None,
None,
order.amount,
order.fiat_code.clone(),
order.fiat_amount,
pr.clone(),
order.premium,
order.buyer_pubkey.as_ref().cloned(),
order.seller_pubkey.as_ref().cloned(),
None,
None,
);
// We send a confirmation message to seller
let message = Message::new(
0,
let message = Message::new_order(
Some(order.id),
None,
Action::BuyerTookOrder,
Some(Content::SmallOrder(order_data.clone())),
Some(Content::Order(order_data.clone())),
);
let message = message.as_json().unwrap();

send_dm(client, my_keys, &seller_pubkey, message).await?;
// We send a message to buyer saying seller paid
let message = Message::new(
0,
let message = Message::new_order(
Some(order.id),
None,
Action::HoldInvoicePaymentAccepted,
Some(Content::SmallOrder(order_data)),
Some(Content::Order(order_data)),
);
let message = message.as_json().unwrap();
send_dm(client, my_keys, &buyer_pubkey, message)
Expand Down
11 changes: 6 additions & 5 deletions src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::util::send_dm;

use anyhow::Result;
use log::error;
use mostro_core::message::{Action, Content, Message};
use mostro_core::user::User;
use mostro_core::{Action, Content, Message};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};

Expand All @@ -15,7 +15,8 @@ pub async fn admin_add_solver_action(
client: &Client,
pool: &Pool<Sqlite>,
) -> Result<()> {
let content = if let Some(content) = msg.content {
let inner_message = msg.get_inner_message_kind();
let content = if let Some(content) = &inner_message.content {
content
} else {
error!("No pubkey found!");
Expand All @@ -32,17 +33,17 @@ pub async fn admin_add_solver_action(
// Check if the pubkey is Mostro
if event.pubkey.to_bech32()? != mostro_pubkey {
// We create a Message
let message = Message::new(0, None, None, Action::CantDo, None);
let message = Message::cant_do(None, None, None);
let message = message.as_json()?;
send_dm(client, my_keys, &event.pubkey, message).await?;

return Ok(());
}
let user = User::new(npubkey, 0, 1, 0, 0);
let user = User::new(npubkey.to_string(), 0, 1, 0, 0);
add_user(&user, pool).await?;

// We create a Message for admin
let message = Message::new(0, None, None, Action::AdminAddSolver, None);
let message = Message::new_dispute(None, None, Action::AdminAddSolver, None);
let message = message.as_json()?;
// Send the message
send_dm(client, my_keys, &event.pubkey, message.clone()).await?;
Expand Down
8 changes: 4 additions & 4 deletions src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::util::{send_dm, update_order_event};
use anyhow::Result;
use log::{error, info};
use mostro_core::dispute::Status as DisputeStatus;
use mostro_core::message::{Action, Message};
use mostro_core::order::{Order, Status};
use mostro_core::{Action, Message};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
Expand All @@ -19,7 +19,7 @@ pub async fn admin_cancel_action(
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
) -> Result<()> {
let order_id = msg.order_id.unwrap();
let order_id = msg.get_inner_message_kind().id.unwrap();
let order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
Expand All @@ -31,7 +31,7 @@ pub async fn admin_cancel_action(
// Check if the pubkey is Mostro
if event.pubkey.to_bech32()? != mostro_pubkey {
// We create a Message
let message = Message::new(0, Some(order.id), None, Action::CantDo, None);
let message = Message::cant_do(Some(order.id), None, None);
let message = message.as_json()?;
send_dm(client, my_keys, &event.pubkey, message).await?;

Expand All @@ -58,7 +58,7 @@ pub async fn admin_cancel_action(
// and update on local database the status and new event id
update_order_event(pool, client, my_keys, Status::CanceledByAdmin, &order, None).await?;
// We create a Message
let message = Message::new(0, Some(order.id), None, Action::AdminCancel, None);
let message = Message::new_dispute(Some(order.id), None, Action::AdminCancel, None);
let message = message.as_json()?;
// Message to admin
send_dm(client, my_keys, &event.pubkey, message.clone()).await?;
Expand Down
6 changes: 3 additions & 3 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::util::{send_dm, settle_seller_hold_invoice};
use anyhow::Result;
use log::error;
use mostro_core::dispute::Status as DisputeStatus;
use mostro_core::message::{Action, Message};
use mostro_core::order::{Order, Status};
use mostro_core::{Action, Message};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
Expand All @@ -19,7 +19,7 @@ pub async fn admin_settle_action(
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
) -> Result<()> {
let order_id = msg.order_id.unwrap();
let order_id = msg.get_inner_message_kind().id.unwrap();
let order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
Expand All @@ -43,7 +43,7 @@ pub async fn admin_settle_action(
d.update(pool).await?;
}
// We create a Message
let message = Message::new(0, Some(order.id), None, Action::AdminSettle, None);
let message = Message::new_dispute(Some(order.id), None, Action::AdminSettle, None);
let message = message.as_json()?;
// Message to admin
send_dm(client, my_keys, &event.pubkey, message.clone()).await?;
Expand Down
Loading

0 comments on commit d0165ab

Please sign in to comment.