Skip to content

Commit

Permalink
Fixes: managed better content of rumor
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Dec 11, 2024
1 parent 617548e commit 7c7fd6e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 41 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
clap = { version = "4.5.19", features = ["derive"] }
lnurl-rs = "0.9.0"
lnurl-rs = "0.8.0"
openssl = { version = "0.10.66", features = ["vendored"] }
once_cell = "1.20.2"
bitcoin = "0.32.5"

[patch.crates-io]
mostro-core = { path = "../mostro-core" }
5 changes: 3 additions & 2 deletions migrations/20221222153301_orders.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ CREATE TABLE IF NOT EXISTS orders (
payment_attempts integer default 0,
failed_payment integer default 0,
expires_at integer not null,
trade_index integer default 0
);
trade_index_seller integer default 0,
trade_index_buyer integer default 0
);
3 changes: 2 additions & 1 deletion migrations/20231005195154_users.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE IF NOT EXISTS users (
pubkey char(64) primary key not null,
id char(36) primary key not null,
pubkey char(64) not null,
is_admin integer not null default 0,
is_solver integer not null default 0,
is_banned integer not null default 0,
Expand Down
61 changes: 31 additions & 30 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ async fn check_trade_index(pool: &Pool<Sqlite>, event: &UnwrappedGift, msg: &Mes
trade_index,
..Default::default()
};
if new_user.create(pool).await.is_ok() {
tracing::info!("Added new user for rate");

match new_user.create(pool).await {
Ok(_) => tracing::info!("Added new user for rate"),
Err(e) => tracing::error!("Error creating user: {:#?}", e),
}
}
}
Expand Down Expand Up @@ -206,35 +208,34 @@ pub async fn run(
if event.rumor.created_at.as_u64() < since_time {
continue;
}
// Parse and process the message
let message = Message::from_json(&event.rumor.content);

match message {
Ok(msg) => {
let message_kind = msg.get_inner_message_kind();
// Check if message is message with trade index
check_trade_index(&pool, &event, &msg).await;

if message_kind.verify() {
if let Some(action) = msg.inner_action() {
if let Err(e) = handle_message_action(
&action,
msg,
&event,
&my_keys,
&pool,
ln_client,
rate_list.clone(),
)
.await
{
warning_msg(&action, e)
}
}
}

let msg = match Message::from_json(&event.rumor.content) {
Ok(msg) => msg.0,
Err(_) => {
tracing::warn!("Failed to parse event message from JSON");
continue;
}
Err(e) => {
tracing::warn!("Failed to parse event message from JSON: {:?}", e)
};

let message_kind = msg.get_inner_message_kind();
// Check if message is message with trade index
check_trade_index(&pool, &event, &msg).await;

if message_kind.verify() {
if let Some(action) = msg.inner_action() {
if let Err(e) = handle_message_action(
&action,
msg,
&event,
&my_keys,
&pool,
ln_client,
rate_list.clone(),
)
.await
{
warning_msg(&action, e)
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod tests {
#[test]
fn test_message_deserialize_serialize() {
let sample_message = r#"{"order":{"version":1,"request_id":1,"trade_index":null,"id":"7dd204d2-d06c-4406-a3d9-4415f4a8b9c9","action":"fiat-sent","payload":null}}"#;
let message = Message::from_json(sample_message).unwrap();
let message: Message = serde_json::from_str(sample_message).unwrap();
assert!(message.verify());
let json_message = message.as_json().unwrap();
assert_eq!(sample_message, json_message);
Expand All @@ -120,7 +120,7 @@ mod tests {
#[test]
fn test_wrong_message_should_fail() {
let sample_message = r#"{"order":{"version":1,"request_id":1,"action":"take-sell","payload":{"order":{"kind":"sell","status":"pending","amount":100,"fiat_code":"XXX","fiat_amount":10,"payment_method":"SEPA","premium":1,"payment_request":null,"created_at":1640839235}}}}"#;
let message = Message::from_json(sample_message).unwrap();
let message: Message = serde_json::from_str(sample_message).unwrap();
assert!(!message.verify());
}

Expand Down
4 changes: 3 additions & 1 deletion src/nip59.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use mostro_core::message::Message;
use nostr_sdk::event::builder::Error as BuilderError;
use nostr_sdk::nostr::nips::nip44::v2::{decrypt_to_bytes, encrypt_to_bytes, ConversationKey};
use nostr_sdk::prelude::*;
use tracing::info;

/// Creates a new nip59 event
///
Expand All @@ -23,7 +24,8 @@ pub fn gift_wrap(
expiration: Option<Timestamp>,
) -> Result<Event, BuilderError> {
// We convert back the string to a message
let message = Message::from_json(&payload).unwrap();
info!("payload: {}", payload);
let message: Message = serde_json::from_str(&payload).unwrap();
// We sign the message
let sig = message.get_inner_message_kind().sign(sender_keys);
// We compose the content
Expand Down

0 comments on commit 7c7fd6e

Please sign in to comment.