Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to HTTP over nostr #458

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions bindings/nostr-ffi/src/event/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ pub enum KindEnum {
Seal,
/// Gift Wrap (NIP59)
GiftWrap,
/// Ephemeral Gift Wrap
EphemeralGiftWrap,
/// Private Direct message
///
/// <https://github.com/nostr-protocol/nips/blob/master/17.md>
Expand Down Expand Up @@ -307,6 +309,7 @@ impl From<nostr::Kind> for KindEnum {
nostr::Kind::BadgeDefinition => Self::BadgeDefinition,
nostr::Kind::Seal => Self::Seal,
nostr::Kind::GiftWrap => Self::GiftWrap,
nostr::Kind::EphemeralGiftWrap => Self::EphemeralGiftWrap,
nostr::Kind::PrivateDirectMessage => Self::PrivateDirectMessage,
nostr::Kind::LongFormTextNote => Self::LongFormTextNote,
nostr::Kind::ApplicationSpecificData => Self::ApplicationSpecificData,
Expand Down Expand Up @@ -385,6 +388,7 @@ impl From<KindEnum> for nostr::Kind {
KindEnum::BadgeDefinition => Self::BadgeDefinition,
KindEnum::Seal => Self::Seal,
KindEnum::GiftWrap => Self::GiftWrap,
KindEnum::EphemeralGiftWrap => Self::EphemeralGiftWrap,
KindEnum::PrivateDirectMessage => Self::PrivateDirectMessage,
KindEnum::LongFormTextNote => Self::LongFormTextNote,
KindEnum::ApplicationSpecificData => Self::ApplicationSpecificData,
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
3 changes: 3 additions & 0 deletions bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,17 +566,20 @@ impl Client {
/// Gift Wrap
///
/// <https://github.com/nostr-protocol/nips/blob/master/59.md>
#[uniffi::method(default(expiration = None))]
pub async fn gift_wrap(
&self,
receiver: &PublicKey,
rumor: Arc<EventBuilder>,
ephemeral: bool,
expiration: Option<Arc<Timestamp>>,
) -> Result<()> {
Ok(self
.inner
.gift_wrap(
**receiver,
rumor.as_ref().deref().clone(),
ephemeral,
expiration.map(|t| **t),
)
.await?)
Expand Down
8 changes: 7 additions & 1 deletion bindings/nostr-sdk-js/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,16 @@ impl JsClient {
&self,
receiver: &JsPublicKey,
rumor: &JsEventBuilder,
ephemeral: bool,
expiration: Option<JsTimestamp>,
) -> Result<()> {
self.inner
.gift_wrap(**receiver, rumor.deref().clone(), expiration.map(|t| *t))
.gift_wrap(
**receiver,
rumor.deref().clone(),
ephemeral,
expiration.map(|t| *t),
)
.await
.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
Loading