|
10 | 10 |
|
11 | 11 | use std::fmt;
|
12 | 12 |
|
13 |
| -use nostr::key; |
14 | 13 | use nostr::prelude::*;
|
15 | 14 | use thiserror::Error;
|
16 | 15 |
|
@@ -46,6 +45,14 @@ pub enum Error {
|
46 | 45 | #[cfg(feature = "nip46")]
|
47 | 46 | #[error(transparent)]
|
48 | 47 | NIP46(#[from] nip46::Error),
|
| 48 | + /// NIP59 error |
| 49 | + #[cfg(feature = "nip59")] |
| 50 | + #[error(transparent)] |
| 51 | + NIP59(#[from] nip59::Error), |
| 52 | + /// Event error |
| 53 | + #[cfg(feature = "nip59")] |
| 54 | + #[error(transparent)] |
| 55 | + Event(#[from] event::Error), |
49 | 56 | }
|
50 | 57 |
|
51 | 58 | /// Nostr Signer Type
|
@@ -217,6 +224,35 @@ impl NostrSigner {
|
217 | 224 | Self::NIP46(signer) => Ok(signer.nip44_decrypt(public_key, payload).await?),
|
218 | 225 | }
|
219 | 226 | }
|
| 227 | + |
| 228 | + /// Unwrap Gift Wrap event |
| 229 | + /// |
| 230 | + /// Internally verify the `seal` event |
| 231 | + /// |
| 232 | + /// <https://github.com/nostr-protocol/nips/blob/master/59.md> |
| 233 | + // TODO: find a way to merge this with the `Keys` implementation in `nostr` crate |
| 234 | + #[cfg(feature = "nip59")] |
| 235 | + pub async fn unwrap_gift_wrap(&self, gift_wrap: &Event) -> Result<UnwrappedGift, Error> { |
| 236 | + // Check event kind |
| 237 | + if gift_wrap.kind != Kind::GiftWrap { |
| 238 | + return Err(Error::NIP59(nip59::Error::NotGiftWrap)); |
| 239 | + } |
| 240 | + |
| 241 | + // Decrypt and verify seal |
| 242 | + let seal: String = self |
| 243 | + .nip44_decrypt(gift_wrap.author(), gift_wrap.content()) |
| 244 | + .await?; |
| 245 | + let seal: Event = Event::from_json(seal)?; |
| 246 | + seal.verify()?; |
| 247 | + |
| 248 | + // Decrypt rumor |
| 249 | + let rumor: String = self.nip44_decrypt(seal.author(), seal.content()).await?; |
| 250 | + |
| 251 | + Ok(UnwrappedGift { |
| 252 | + sender: seal.author(), |
| 253 | + rumor: UnsignedEvent::from_json(rumor)?, |
| 254 | + }) |
| 255 | + } |
220 | 256 | }
|
221 | 257 |
|
222 | 258 | impl From<Keys> for NostrSigner {
|
|
0 commit comments