Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nostr: add NEG-OPEN client message
Browse files Browse the repository at this point in the history
yukibtc committed Aug 25, 2023
1 parent 57c3550 commit 5e3e048
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions crates/nostr/src/message/client.rs
Original file line number Diff line number Diff line change
@@ -36,6 +36,17 @@ pub enum ClientMessage {
Close(SubscriptionId),
/// Auth
Auth(Box<Event>),
/// Negentropy Open
NegOpen {
/// Subscription ID
subscription_id: SubscriptionId,
/// Filter
filter: Box<Filter>,
/// ID size (MUST be between 8 and 32, inclusive)
id_size: u8,
/// Initial message
initial_message: String,
},
}

impl Serialize for ClientMessage {
@@ -141,6 +152,20 @@ impl ClientMessage {
}
Self::Close(subscription_id) => json!(["CLOSE", subscription_id]),
Self::Auth(event) => json!(["AUTH", event]),
Self::NegOpen {
subscription_id,
filter,
id_size,
initial_message,
} => {
json!([
"NEG-OPEN",
subscription_id,
filter,
id_size,
initial_message
])
}
}
}

@@ -222,6 +247,26 @@ impl ClientMessage {
return Ok(Self::new_auth(event));
}

// Negentropy Open
// ["NEG-OPEN", <subscription ID string>, <filter>, <idSize>, <initialMessage, lowercase hex-encoded>]
if v[0] == "NEG-OPEN" {
if v_len != 5 {
return Err(MessageHandleError::InvalidMessageFormat);
}
let subscription_id = SubscriptionId::new(v[1].to_string());
let filter = Filter::from_json(v[2].to_string())?;
let id_size = v[3]
.as_u64()
.ok_or(MessageHandleError::InvalidMessageFormat)? as u8;
let initial_message = v[4].to_string();
return Ok(Self::NegOpen {
subscription_id,
filter: Box::new(filter),
id_size,
initial_message,
});
}

Err(MessageHandleError::InvalidMessageFormat)
}

0 comments on commit 5e3e048

Please sign in to comment.