From 3306e775d733c0ac9b8864419b66435fe95352d5 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Fri, 18 Oct 2024 13:33:16 +0200 Subject: [PATCH] js(nostr): expose `from_nostr_uri` and `to_nostr_uri` for `PublicKey` and `EventId` Ref https://github.com/rust-nostr/nostr/pull/590 Signed-off-by: Yuki Kishimoto --- CHANGELOG.md | 1 + bindings/nostr-js/src/event/id.rs | 12 ++++++++++++ bindings/nostr-js/src/key/public_key.rs | 12 ++++++++++++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4adb2adbe..9a2bdfcf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,7 @@ * ffi(nostr): add `Tag::kind_str` method ([Yuki Kishimoto]) * ffi(nostr): impl `Display` for `Kind` ([Yuki Kishimoto]) * js(nostr): add `JsKind::_to_string` method ([Yuki Kishimoto]) +* js(nostr): expose `from_nostr_uri` and `to_nostr_uri` for `PublicKey` and `EventId` ([Yuki Kishimoto]) * book: add some examples ([RydalWater]) ### Fixed diff --git a/bindings/nostr-js/src/event/id.rs b/bindings/nostr-js/src/event/id.rs index d332fbddc..51c7b487c 100644 --- a/bindings/nostr-js/src/event/id.rs +++ b/bindings/nostr-js/src/event/id.rs @@ -88,6 +88,13 @@ impl JsEventId { }) } + #[wasm_bindgen(js_name = fromNostrUri)] + pub fn from_nostr_uri(uri: &str) -> Result { + Ok(Self { + inner: EventId::from_nostr_uri(uri).map_err(into_err)?, + }) + } + #[wasm_bindgen(js_name = asBytes)] pub fn as_bytes(&self) -> Vec { self.inner.as_bytes().to_vec() @@ -102,4 +109,9 @@ impl JsEventId { pub fn to_bech32(&self) -> Result { self.inner.to_bech32().map_err(into_err) } + + #[wasm_bindgen(js_name = toNostrUri)] + pub fn to_nostr_uri(&self) -> Result { + self.inner.to_nostr_uri().map_err(into_err) + } } diff --git a/bindings/nostr-js/src/key/public_key.rs b/bindings/nostr-js/src/key/public_key.rs index 045f1ae46..07eab2273 100644 --- a/bindings/nostr-js/src/key/public_key.rs +++ b/bindings/nostr-js/src/key/public_key.rs @@ -58,6 +58,13 @@ impl JsPublicKey { }) } + #[wasm_bindgen(js_name = fromNostrUri)] + pub fn from_nostr_uri(uri: &str) -> Result { + Ok(Self { + inner: PublicKey::from_nostr_uri(uri).map_err(into_err)?, + }) + } + /// Get in hex format #[wasm_bindgen(js_name = toHex)] pub fn to_hex(&self) -> String { @@ -69,4 +76,9 @@ impl JsPublicKey { pub fn to_bech32(&self) -> Result { self.inner.to_bech32().map_err(into_err) } + + #[wasm_bindgen(js_name = toNostrUri)] + pub fn to_nostr_uri(&self) -> Result { + self.inner.to_nostr_uri().map_err(into_err) + } }