Skip to content

Commit

Permalink
Bumps to nostr-sdk 0.37
Browse files Browse the repository at this point in the history
Fixes related to nostr sdk upgrade and payload
  • Loading branch information
grunch committed Dec 10, 2024
1 parent 693cb9c commit cf4140a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 149 deletions.
134 changes: 10 additions & 124 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ anyhow = "1.0.89"
chrono = "0.4.35"
easy-hasher = "2.2.1"
lightning-invoice = { version = "0.32.0", features = ["std"] }
nostr-sdk = "0.36.0"
nostr-sdk = { version = "0.37.0", features = ["nip59"] }
serde = { version = "1.0.210" }
serde_json = "1.0.128"
sqlx = { version = "0.6.2", features = [
Expand All @@ -38,7 +38,7 @@ uuid = { version = "1.8.0", features = [
"serde",
] }
reqwest = { version = "0.12.1", features = ["json"] }
mostro-core = { version = "0.6.13", features = ["sqlx"] }
mostro-core = { version = "0.6.14", features = ["sqlx"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
Expand Down
4 changes: 3 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ async fn process_message(pool: &Pool<Sqlite>, event: &UnwrappedGift, msg: Messag
match is_user_present(&pool, event.sender.to_string()).await {
Ok(mut user) => {
if let (true, index) = message_kind.has_trade_index() {
let (_, sig): (String, nostr_sdk::secp256k1::schnorr::Signature) =
serde_json::from_str(&event.rumor.content).unwrap();
if index > user.trade_index
&& msg
.get_inner_message_kind()
.verify_signature(event.sender, event.rumor.content.)
.verify_signature(event.sender, sig)
{
user.trade_index = index;
if let Ok(_) = user.update(&pool).await {
Expand Down
23 changes: 18 additions & 5 deletions src/nip59.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use base64::engine::{general_purpose, Engine};
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::*;
Expand All @@ -18,10 +19,17 @@ use nostr_sdk::prelude::*;
pub fn gift_wrap(
sender_keys: &Keys,
receiver: PublicKey,
content: String,
payload: String,
expiration: Option<Timestamp>,
) -> Result<Event, BuilderError> {
let rumor: UnsignedEvent = EventBuilder::text_note(content, []).build(sender_keys.public_key());
// We convert back the string to a message
let message = Message::from_json(&payload).unwrap();
// We sign the message
let sig = message.get_inner_message_kind().sign(sender_keys);
// We compose the content
let content = (message, sig);
let content = serde_json::to_string(&content).unwrap();
let rumor: UnsignedEvent = EventBuilder::text_note(content).build(sender_keys.public_key());
let seal: Event = seal(sender_keys, &receiver, rumor)?.sign_with_keys(sender_keys)?;

gift_wrap_from_seal(&receiver, &seal, expiration)
Expand All @@ -39,7 +47,7 @@ pub fn seal(
// Encode with base64
let b64decoded_content = general_purpose::STANDARD.encode(encrypted_content);
// Compose builder
Ok(EventBuilder::new(Kind::Seal, b64decoded_content, [])
Ok(EventBuilder::new(Kind::Seal, b64decoded_content)
.custom_created_at(Timestamp::tweaked(nip59::RANGE_RANDOM_TIMESTAMP_TWEAK)))
}

Expand All @@ -60,11 +68,16 @@ pub fn gift_wrap_from_seal(
if let Some(timestamp) = expiration {
tags.push(Tag::expiration(timestamp));
}
let tags = Tags::new(tags);
// Encode with base64
let b64decoded_content = general_purpose::STANDARD.encode(encrypted_content);
EventBuilder::new(Kind::GiftWrap, b64decoded_content, tags)
let event = EventBuilder::new(Kind::GiftWrap, b64decoded_content)
.tags(tags)
.custom_created_at(Timestamp::tweaked(nip59::RANGE_RANDOM_TIMESTAMP_TWEAK))
.sign_with_keys(&ephemeral_keys)
.build(ephemeral_keys.public_key())
.sign_with_keys(&ephemeral_keys)?;

Ok(event)
}

pub fn unwrap_gift_wrap(keys: &Keys, gift_wrap: &Event) -> Result<UnwrappedGift, BuilderError> {
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ async fn job_relay_list() {
}
}

if let Ok(relay_ev) = EventBuilder::new(NostrKind::RelayList, "", relay_tags)
.sign_with_keys(&mostro_keys)
if let Ok(relay_ev) =
EventBuilder::new(NostrKind::RelayList, "").sign_with_keys(&mostro_keys)
{
if let Ok(client) = get_nostr_client() {
let _ = client.send_event(relay_ev).await;
Expand Down
Loading

0 comments on commit cf4140a

Please sign in to comment.