Skip to content

Commit

Permalink
nostr: add support to ephemeral gift wrap
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jun 5, 2024
1 parent 9fb6e68 commit 2303f59
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* nostr: add `JsonUtil::try_as_json` method ([Yuki Kishimoto])
* nostr: add `public_key` field to `TagStandard::Event` ([Yuki Kishimoto])
* nostr: add support to `nrelay` NIP-19 entity ([Yuki Kishimoto])
* nostr: add support to ephemeral gift wrap ([Yuki Kishimoto])
* pool: allow to set event limits per kind ([Yuki Kishimoto])
* pool: log warn when high latency ([Yuki Kishimoto])
* sdk: add support to automatic authentication to relays (NIP-42) ([Yuki Kishimoto])
Expand Down
14 changes: 10 additions & 4 deletions bindings/nostr-ffi/src/nips/nip59.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ pub fn gift_wrap(
sender_keys: &Keys,
receiver_pubkey: &PublicKey,
rumor: &UnsignedEvent,
ephemeral: bool,
expiration: Option<Arc<Timestamp>>,
) -> Result<Event> {
Ok(EventBuilder::gift_wrap(
sender_keys.deref(),
receiver_pubkey.deref(),
rumor.deref().clone(),
ephemeral,
expiration.map(|t| **t),
)?
.into())
Expand All @@ -37,12 +39,16 @@ pub fn gift_wrap(
pub fn gift_wrap_from_seal(
receiver: &PublicKey,
seal: &Event,
ephemeral: bool,
expiration: Option<Arc<Timestamp>>,
) -> Result<Event> {
Ok(
EventBuilder::gift_wrap_from_seal(receiver.deref(), seal.deref(), expiration.map(|t| **t))?
.into(),
)
Ok(EventBuilder::gift_wrap_from_seal(
receiver.deref(),
seal.deref(),
ephemeral,
expiration.map(|t| **t),
)?
.into())
}

/// Unwrapped Gift Wrap
Expand Down
4 changes: 4 additions & 0 deletions bindings/nostr-js/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,13 @@ impl JsEventBuilder {
pub fn gift_wrap_from_seal(
receiver: &JsPublicKey,
seal: &JsEvent,
ephemeral: bool,
expiration: Option<JsTimestamp>,
) -> Result<JsEvent> {
Ok(EventBuilder::gift_wrap_from_seal(
receiver.deref(),
seal.deref(),
ephemeral,
expiration.map(|t| *t),
)
.map_err(into_err)?
Expand All @@ -580,12 +582,14 @@ impl JsEventBuilder {
sender_keys: &JsKeys,
receiver: &JsPublicKey,
rumor: &JsUnsignedEvent,
ephemeral: bool,
expiration: Option<JsTimestamp>,
) -> Result<JsEvent> {
Ok(EventBuilder::gift_wrap(
sender_keys.deref(),
receiver.deref(),
rumor.deref().clone(),
ephemeral,
expiration.map(|t| *t),
)
.map_err(into_err)?
Expand Down
1 change: 1 addition & 0 deletions bindings/nostr-sdk-js/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl JsNostrDatabase {
pub async fn save_event(&self, event: &JsEvent) -> Result<bool> {
self.inner.save_event(event).await.map_err(into_err)
}

/// Get list of relays that have seen the [`EventId`]
#[wasm_bindgen(js_name = eventSeenOnRelays)]
pub async fn event_seen_on_relays(
Expand Down
6 changes: 4 additions & 2 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ impl Client {
S: Into<String>,
{
let rumor: EventBuilder = EventBuilder::private_msg_rumor(receiver, message, reply_to);
self.gift_wrap(receiver, rumor, None).await
self.gift_wrap(receiver, rumor, false, None).await
}

/// Repost
Expand Down Expand Up @@ -1475,6 +1475,7 @@ impl Client {
&self,
receiver: PublicKey,
rumor: EventBuilder,
ephemeral: bool,
expiration: Option<Timestamp>,
) -> Result<(), Error> {
// Compose rumor
Expand All @@ -1490,7 +1491,8 @@ impl Client {
let seal: Event = self.sign_event_builder(seal).await?;

// Compose gift wrap
let gift_wrap: Event = EventBuilder::gift_wrap_from_seal(&receiver, &seal, expiration)?;
let gift_wrap: Event =
EventBuilder::gift_wrap_from_seal(&receiver, &seal, ephemeral, expiration)?;

// Send event
self.send_event(gift_wrap).await?;
Expand Down
16 changes: 12 additions & 4 deletions crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,8 @@ impl EventBuilder {
pub fn gift_wrap_from_seal(
receiver: &PublicKey,
seal: &Event,
expiration: Option<Timestamp>,
ephemeral: bool,
expiration: Option<Timestamp>, // TODO: remove this and add `EventBuilder::expiration`?
) -> Result<Event, Error> {
if seal.kind != Kind::Seal {
return Err(Error::WrongKind {
Expand All @@ -1343,7 +1344,13 @@ impl EventBuilder {
tags.push(Tag::expiration(timestamp));
}

Self::new(Kind::GiftWrap, content, tags)
let kind: Kind = if ephemeral {
Kind::EphemeralGiftWrap
} else {
Kind::GiftWrap
};

Self::new(kind, content, tags)
.custom_created_at(Timestamp::tweaked(nip59::RANGE_RANDOM_TIMESTAMP_TWEAK))
.to_event(&keys)
}
Expand All @@ -1357,10 +1364,11 @@ impl EventBuilder {
sender_keys: &Keys,
receiver: &PublicKey,
rumor: UnsignedEvent,
expiration: Option<Timestamp>,
ephemeral: bool,
expiration: Option<Timestamp>, // TODO: remove this and add `EventBuilder::expiration`?
) -> Result<Event, Error> {
let seal: Event = Self::seal(sender_keys, receiver, rumor)?.to_event(sender_keys)?;
Self::gift_wrap_from_seal(receiver, &seal, expiration)
Self::gift_wrap_from_seal(receiver, &seal, ephemeral, expiration)
}

/// GiftWrapped Sealed Direct message
Expand Down
1 change: 1 addition & 0 deletions crates/nostr/src/event/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ kind_variants! {
BadgeDefinition => 30009, "Badge Definition (NIP58)",
Seal => 13, "Seal <https://github.com/nostr-protocol/nips/blob/master/59.md>",
GiftWrap => 1059, "Gift Wrap <https://github.com/nostr-protocol/nips/blob/master/59.md>",
EphemeralGiftWrap => 21059, "Ephemeral Gift Wrap",
PrivateDirectMessage => 14, "Private Direct message <https://github.com/nostr-protocol/nips/blob/master/17.md>",
SetStall => 30017, "Set stall (NIP15)",
SetProduct => 30018, "Set product (NIP15)",
Expand Down
3 changes: 2 additions & 1 deletion crates/nostr/src/nips/nip59.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl UnwrappedGift {
C: Verification,
{
// Check event kind
if gift_wrap.kind != Kind::GiftWrap {
if gift_wrap.kind != Kind::GiftWrap && gift_wrap.kind != Kind::EphemeralGiftWrap {
return Err(Error::NotGiftWrap);
}

Expand Down Expand Up @@ -161,6 +161,7 @@ mod tests {
&sender_keys,
&receiver_keys.public_key(),
rumor.clone(),
false,
None,
)
.unwrap();
Expand Down

0 comments on commit 2303f59

Please sign in to comment.