Skip to content

Commit

Permalink
js(nostr): expose from_nostr_uri and to_nostr_uri for PublicKey
Browse files Browse the repository at this point in the history
… and `EventId`

Ref #590

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Oct 18, 2024
1 parent 0455a48 commit 3306e77
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions bindings/nostr-js/src/event/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ impl JsEventId {
})
}

#[wasm_bindgen(js_name = fromNostrUri)]
pub fn from_nostr_uri(uri: &str) -> Result<JsEventId> {
Ok(Self {
inner: EventId::from_nostr_uri(uri).map_err(into_err)?,
})
}

#[wasm_bindgen(js_name = asBytes)]
pub fn as_bytes(&self) -> Vec<u8> {
self.inner.as_bytes().to_vec()
Expand All @@ -102,4 +109,9 @@ impl JsEventId {
pub fn to_bech32(&self) -> Result<String> {
self.inner.to_bech32().map_err(into_err)
}

#[wasm_bindgen(js_name = toNostrUri)]
pub fn to_nostr_uri(&self) -> Result<String> {
self.inner.to_nostr_uri().map_err(into_err)
}
}
12 changes: 12 additions & 0 deletions bindings/nostr-js/src/key/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ impl JsPublicKey {
})
}

#[wasm_bindgen(js_name = fromNostrUri)]
pub fn from_nostr_uri(uri: &str) -> Result<JsPublicKey> {
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 {
Expand All @@ -69,4 +76,9 @@ impl JsPublicKey {
pub fn to_bech32(&self) -> Result<String> {
self.inner.to_bech32().map_err(into_err)
}

#[wasm_bindgen(js_name = toNostrUri)]
pub fn to_nostr_uri(&self) -> Result<String> {
self.inner.to_nostr_uri().map_err(into_err)
}
}

0 comments on commit 3306e77

Please sign in to comment.