From 8d8dc0c473113bfeb0dc545f476ff70470fca7f7 Mon Sep 17 00:00:00 2001 From: Rusted Moon Date: Thu, 1 Feb 2024 16:56:14 +0100 Subject: [PATCH] js(nostr): add `EventBuilder::file_metadata` --- bindings/nostr-js/src/event/builder.rs | 8 ++ bindings/nostr-js/src/nips/mod.rs | 1 + bindings/nostr-js/src/nips/nip94.rs | 108 +++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 bindings/nostr-js/src/nips/nip94.rs diff --git a/bindings/nostr-js/src/event/builder.rs b/bindings/nostr-js/src/event/builder.rs index 9dd161bee..7d3242eaa 100644 --- a/bindings/nostr-js/src/event/builder.rs +++ b/bindings/nostr-js/src/event/builder.rs @@ -14,6 +14,7 @@ use crate::key::{JsKeys, JsPublicKey}; use crate::nips::nip57::JsZapRequestData; use crate::nips::nip65::JsRelayListItem; use crate::nips::nip90::JsDataVendingMachineStatus; +use crate::nips::nip94::JsFileMetadata; use crate::types::{JsContact, JsMetadata}; #[wasm_bindgen(js_name = EventBuilder)] @@ -323,4 +324,11 @@ impl JsEventBuilder { ), } } + + #[wasm_bindgen(js_name = fileMetadata)] + pub fn file_metadata(description: String, metadata: JsFileMetadata) -> Self { + Self { + builder: EventBuilder::file_metadata(description, metadata.deref().clone()), + } + } } diff --git a/bindings/nostr-js/src/nips/mod.rs b/bindings/nostr-js/src/nips/mod.rs index 681c61689..e0e8631ff 100644 --- a/bindings/nostr-js/src/nips/mod.rs +++ b/bindings/nostr-js/src/nips/mod.rs @@ -15,3 +15,4 @@ pub mod nip47; pub mod nip57; pub mod nip65; pub mod nip90; +pub mod nip94; diff --git a/bindings/nostr-js/src/nips/nip94.rs b/bindings/nostr-js/src/nips/nip94.rs new file mode 100644 index 000000000..4cd9dc4da --- /dev/null +++ b/bindings/nostr-js/src/nips/nip94.rs @@ -0,0 +1,108 @@ +// Copyright (c) 2023-2024 Rust Nostr Developers +// Distributed under the MIT software license + +use std::{ops::Deref, str::FromStr}; + +use nostr::hashes::sha256::Hash as Sha256Hash; +use nostr::Url; +use nostr::nips::nip94::FileMetadata; +use wasm_bindgen::prelude::*; + +use crate::error::{into_err, Result}; +use crate::event::tag::JsImageDimensions; + +#[wasm_bindgen(js_name = Aes256Gcm)] +pub struct JsAes256Gcm { + key: String, + iv: String, +} + +impl From for (String, String) { + fn from(value: JsAes256Gcm) -> Self { + (value.key, value.iv) + } +} + +impl From<(String, String)> for JsAes256Gcm { + fn from(value: (String, String)) -> Self { + Self { key: value.0, iv: value.1 } + } +} + +impl JsAes256Gcm { + pub fn new(key: String, iv: String) -> Self { + Self { key, iv } + } +} + +#[wasm_bindgen(js_name = FileMetadata)] +pub struct JsFileMetadata { + inner: FileMetadata, +} + +impl From for JsFileMetadata { + fn from(inner: FileMetadata) -> Self { + Self { inner } + } +} + +impl Deref for JsFileMetadata { + type Target = FileMetadata; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +#[wasm_bindgen(js_class = FileMetadata)] +impl JsFileMetadata { + pub fn new(url: &str, mime_type: String, hash: &str) -> Result { + Ok(Self{ + inner: FileMetadata::new( + Url::from_str(&url).map_err(into_err)?, + mime_type, + Sha256Hash::from_str(&hash).map_err(into_err)?, + ), + }) + } + + #[wasm_bindgen(getter)] + pub fn urls(&self) -> String { + self.inner.url.to_string() + } + + #[wasm_bindgen(getter, js_name = mimeType)] + pub fn mime_type(&self) -> String { + self.inner.mime_type.clone() + } + + #[wasm_bindgen(getter)] + pub fn hash(&self) -> String { + self.inner.hash.to_string() + } + + #[wasm_bindgen(getter, js_name = aes256Gcm)] + pub fn aes_256_gcm(&self) -> Option { + self.inner.aes_256_gcm.clone().map(|t| t.into()) + } + + #[wasm_bindgen(getter)] + pub fn size(&self) -> Option { + self.inner.size.map(|n| n as f64) + } + + #[wasm_bindgen(getter)] + pub fn dim(&self) -> Option { + self.inner.dim.map(|i| i.into()) + } + + #[wasm_bindgen(getter)] + pub fn magnet(&self) -> Option { + self.inner.magnet.clone() + } + + #[wasm_bindgen(getter)] + pub fn blurhash(&self) -> Option { + self.inner.blurhash.clone() + } +}