Skip to content

Commit

Permalink
signer: add NostrSigner::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 Oct 1, 2024
1 parent 263833f commit 26f3aca
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* relay-builder: add min POW difficulty option to `RelayBuilder` ([Yuki Kishimoto])
* pool: add `RelayPool::force_remove_relay` method ([Yuki Kishimoto])
* pool: add `RelayFiltering::overwrite_public_keys` method ([Yuki Kishimoto])
* signer: add `NostrSigner::gift_wrap` ([Yuki Kishimoto])
* zapper: add `WebLNZapper` struct (moved from `nostr-webln` crate) ([Yuki Kishimoto])
* ffi(nostr): add `tag_kind_to_string` func ([Yuki Kishimoto])
* ffi(nostr): add `Tag::kind_str` method ([Yuki Kishimoto])
Expand Down
4 changes: 3 additions & 1 deletion bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,9 @@ impl Client {
.await?)
}

/// Construct Gift Wrap and send to all relays
/// Construct Gift Wrap and send to relays
///
/// Check `send_event` method to know how sending events works.
///
/// <https://github.com/nostr-protocol/nips/blob/master/59.md>
pub async fn gift_wrap(
Expand Down
4 changes: 3 additions & 1 deletion bindings/nostr-sdk-js/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,9 @@ impl JsClient {
.map_err(into_err)
}

/// Construct Gift Wrap and send to all relays
/// Construct Gift Wrap and send to relays
///
/// Check `sendEvent` method to know how sending events works.
///
/// <https://github.com/nostr-protocol/nips/blob/master/59.md>
#[wasm_bindgen(js_name = giftWrap)]
Expand Down
36 changes: 7 additions & 29 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,33 +1650,9 @@ impl Client {
self.internal_zap(to, satoshi, details).await
}

/// Build gift wrap event
#[cfg(feature = "nip59")]
async fn internal_gift_wrap(
&self,
receiver: &PublicKey,
rumor: EventBuilder,
expiration: Option<Timestamp>,
) -> Result<Event, Error> {
// Compose rumor
let signer: NostrSigner = self.signer().await?;
let public_key: PublicKey = signer.public_key().await?;
let rumor = rumor.to_unsigned_event(public_key);

// Compose seal
// TODO: use directly the `EventBuilder::seal` constructor
let content: String = signer.nip44_encrypt(receiver, rumor.as_json()).await?;
let seal: EventBuilder = EventBuilder::new(Kind::Seal, content, [])
.custom_created_at(Timestamp::tweaked(nip59::RANGE_RANDOM_TIMESTAMP_TWEAK));
let seal: Event = self.sign_event_builder(seal).await?;

// Compose gift wrap
Ok(EventBuilder::gift_wrap_from_seal(
receiver, &seal, expiration,
)?)
}

/// Construct Gift Wrap and send to all relays
/// Construct Gift Wrap and send to relays
///
/// Check [`Client::send_event`] to know how sending events works.
///
/// <https://github.com/nostr-protocol/nips/blob/master/59.md>
#[inline]
Expand All @@ -1687,7 +1663,8 @@ impl Client {
rumor: EventBuilder,
expiration: Option<Timestamp>,
) -> Result<Output<EventId>, Error> {
let gift_wrap: Event = self.internal_gift_wrap(receiver, rumor, expiration).await?;
let signer: NostrSigner = self.signer().await?;
let gift_wrap: Event = signer.gift_wrap(receiver, rumor, expiration).await?;
self.send_event(gift_wrap).await
}

Expand All @@ -1708,7 +1685,8 @@ impl Client {
U: TryIntoUrl,
pool::Error: From<<U as TryIntoUrl>::Err>,
{
let gift_wrap: Event = self.internal_gift_wrap(receiver, rumor, expiration).await?;
let signer: NostrSigner = self.signer().await?;
let gift_wrap: Event = signer.gift_wrap(receiver, rumor, expiration).await?;
self.send_event_to(urls, gift_wrap).await
}

Expand Down
29 changes: 29 additions & 0 deletions crates/nostr-signer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub enum Error {
/// Unsigned event error
#[error(transparent)]
Unsigned(#[from] unsigned::Error),
/// Builder event error
#[error(transparent)]
Builder(#[from] builder::Error),
/// NIP04 error
#[cfg(feature = "nip04")]
#[error(transparent)]
Expand Down Expand Up @@ -238,6 +241,32 @@ impl NostrSigner {
}
}

/// Build gift wrap event
// TODO: find a way to merge this with the `Keys` implementation in `nostr` crate
#[cfg(feature = "nip59")]
pub async fn gift_wrap(
&self,
receiver: &PublicKey,
rumor: EventBuilder,
expiration: Option<Timestamp>,
) -> Result<Event, Error> {
// Compose rumor
let public_key: PublicKey = self.public_key().await?;
let rumor = rumor.to_unsigned_event(public_key);

// Compose seal
// TODO: use directly the `EventBuilder::seal` constructor
let content: String = self.nip44_encrypt(receiver, rumor.as_json()).await?;
let seal: EventBuilder = EventBuilder::new(Kind::Seal, content, [])
.custom_created_at(Timestamp::tweaked(nip59::RANGE_RANDOM_TIMESTAMP_TWEAK));
let seal: Event = self.sign_event_builder(seal).await?;

// Compose gift wrap
Ok(EventBuilder::gift_wrap_from_seal(
receiver, &seal, expiration,
)?)
}

/// Unwrap Gift Wrap event
///
/// Internally verify the `seal` event
Expand Down

0 comments on commit 26f3aca

Please sign in to comment.