Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce WASM size #259

Merged
merged 7 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ wasm-bindgen = { version = "0.2", default-features = false }
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", default-features = false }

# Pathces needed to reduce bindings size
[patch.crates-io]
bip39 = { git = "https://github.com/rust-bitcoin/rust-bip39", rev = "256a00e7c23c5b2ecc04df90894f60248c8d56d5" }
url = { git = "https://github.com/yukibtc/rust-url", rev = "233f5ceffcd053b93b7f98f4ec86cd9d68dcde2d" }
url-fork = { git = "https://github.com/yukibtc/rust-url", rev = "233f5ceffcd053b93b7f98f4ec86cd9d68dcde2d" }

[profile.release]
lto = true
Expand Down
7 changes: 6 additions & 1 deletion bindings/nostr-js/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ lto = true
codegen-units = 1
panic = "abort"
strip = true
debug = false
debug = false

[unstable]
unstable-options = true
build-std = ["panic_abort", "std"]
build-std-features = ["panic_immediate_abort"]
7 changes: 6 additions & 1 deletion bindings/nostr-js/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ publish: pack

pack:
rm -rf ./pkg
npm run package
npm run package

twiggy:
cargo install twiggy --version 0.7.0
npm run build:dev
twiggy top pkg/nostr_js_bg.wasm
3 changes: 3 additions & 0 deletions bindings/nostr-js/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "nightly-2024-01-11"
targets = ["wasm32-unknown-unknown"]
20 changes: 10 additions & 10 deletions bindings/nostr-js/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::ops::Deref;
use core::ops::Deref;

use nostr::prelude::*;
use wasm_bindgen::prelude::*;
Expand All @@ -28,7 +28,7 @@ impl Deref for JsEventBuilder {
#[wasm_bindgen(js_class = EventBuilder)]
impl JsEventBuilder {
#[wasm_bindgen(constructor)]
pub fn new(kind: f64, content: String, tags: Vec<JsTag>) -> Self {
pub fn new(kind: f64, content: &str, tags: Vec<JsTag>) -> Self {
Self {
builder: EventBuilder::new(kind.into(), content, tags.into_iter().map(|t| t.into())),
}
Expand Down Expand Up @@ -78,7 +78,7 @@ impl JsEventBuilder {
}

#[wasm_bindgen(js_name = textNote)]
pub fn text_note(content: String, tags: Vec<JsTag>) -> Self {
pub fn text_note(content: &str, tags: Vec<JsTag>) -> Self {
Self {
builder: EventBuilder::text_note(content, tags.into_iter().map(|t| t.into())),
}
Expand All @@ -96,7 +96,7 @@ impl JsEventBuilder {
pub fn encrypted_direct_msg(
sender_keys: &JsKeys,
receiver_pubkey: &JsPublicKey,
content: String,
content: &str,
reply_to: Option<JsEventId>,
) -> Result<JsEventBuilder> {
Ok(Self {
Expand Down Expand Up @@ -128,7 +128,7 @@ impl JsEventBuilder {
}
}

pub fn reaction(event_id: &JsEventId, public_key: &JsPublicKey, content: String) -> Self {
pub fn reaction(event_id: &JsEventId, public_key: &JsPublicKey, content: &str) -> Self {
Self {
builder: EventBuilder::reaction(event_id.into(), public_key.into(), content),
}
Expand Down Expand Up @@ -158,10 +158,10 @@ impl JsEventBuilder {
#[wasm_bindgen(js_name = channelMsg)]
pub fn channel_msg(
channel_id: &JsEventId,
relay_url: String,
content: String,
relay_url: &str,
content: &str,
) -> Result<JsEventBuilder> {
let relay_url: Url = Url::parse(&relay_url).map_err(into_err)?;
let relay_url: Url = Url::parse(relay_url).map_err(into_err)?;
Ok(Self {
builder: EventBuilder::channel_msg(channel_id.into(), relay_url, content),
})
Expand All @@ -182,8 +182,8 @@ impl JsEventBuilder {
}

#[wasm_bindgen]
pub fn auth(challenge: String, relay: String) -> Result<JsEventBuilder> {
let url = Url::parse(&relay).map_err(into_err)?;
pub fn auth(challenge: &str, relay: &str) -> Result<JsEventBuilder> {
let url = Url::parse(relay).map_err(into_err)?;
Ok(Self {
builder: EventBuilder::auth(challenge, url),
})
Expand Down
14 changes: 7 additions & 7 deletions bindings/nostr-js/src/event/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::ops::Deref;
use core::ops::Deref;

use nostr::prelude::*;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -51,31 +51,31 @@ impl JsEventId {
created_at: &JsTimestamp,
kind: f64,
tags: Vec<JsTag>,
content: String,
content: &str,
) -> Self {
let kind = Kind::from(kind);
let tags: Vec<Tag> = tags.into_iter().map(|t| t.into()).collect();
Self {
inner: EventId::new(&pubkey.into(), **created_at, &kind, &tags, &content),
inner: EventId::new(&pubkey.into(), **created_at, &kind, &tags, content),
}
}

#[wasm_bindgen(js_name = fromSlice)]
pub fn from_slice(bytes: Vec<u8>) -> Result<JsEventId> {
pub fn from_slice(bytes: &[u8]) -> Result<JsEventId> {
Ok(Self {
inner: EventId::from_slice(&bytes).map_err(into_err)?,
inner: EventId::from_slice(bytes).map_err(into_err)?,
})
}

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

#[wasm_bindgen(js_name = fromBech32)]
pub fn from_bech32(id: String) -> Result<JsEventId> {
pub fn from_bech32(id: &str) -> Result<JsEventId> {
Ok(Self {
inner: EventId::from_bech32(id).map_err(into_err)?,
})
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-js/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::ops::Deref;
use core::ops::Deref;

use js_sys::Array;
use nostr::prelude::*;
Expand Down Expand Up @@ -104,7 +104,7 @@ impl JsEvent {
}

#[wasm_bindgen(js_name = fromJson)]
pub fn from_json(json: String) -> Result<JsEvent> {
pub fn from_json(json: &str) -> Result<JsEvent> {
Ok(Self {
inner: Event::from_json(json).map_err(into_err)?,
})
Expand Down
10 changes: 5 additions & 5 deletions bindings/nostr-js/src/event/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::ops::Deref;
use std::str::FromStr;
use core::ops::Deref;
use core::str::FromStr;

use js_sys::Array;
use nostr::bitcoin::secp256k1::schnorr::Signature;
Expand Down Expand Up @@ -75,7 +75,7 @@ impl JsUnsignedEvent {
}

#[wasm_bindgen(js_name = fromJson)]
pub fn from_json(json: String) -> Result<JsUnsignedEvent> {
pub fn from_json(json: &str) -> Result<JsUnsignedEvent> {
Ok(Self {
inner: UnsignedEvent::from_json(json).map_err(into_err)?,
})
Expand All @@ -92,8 +92,8 @@ impl JsUnsignedEvent {
}

/// Add signature
pub fn add_signature(self, sig: String) -> Result<JsEvent> {
let sig: Signature = Signature::from_str(&sig).map_err(into_err)?;
pub fn add_signature(self, sig: &str) -> Result<JsEvent> {
let sig: Signature = Signature::from_str(sig).map_err(into_err)?;
Ok(self.inner.add_signature(sig).map_err(into_err)?.into())
}
}
12 changes: 6 additions & 6 deletions bindings/nostr-js/src/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ impl JsKeys {

/// Init [`Keys`] from `hex` or `bech32` secret key string
#[wasm_bindgen(js_name = fromSkStr)]
pub fn from_sk_str(secret_key: String) -> Result<JsKeys> {
pub fn from_sk_str(secret_key: &str) -> Result<JsKeys> {
Ok(Self {
inner: Keys::from_sk_str(&secret_key).map_err(into_err)?,
inner: Keys::from_sk_str(secret_key).map_err(into_err)?,
})
}

/// Init [`Keys`] from `hex` or `bech32` public key string
#[wasm_bindgen(js_name = fromPkStr)]
pub fn from_pk_str(public_key: String) -> Result<JsKeys> {
pub fn from_pk_str(public_key: &str) -> Result<JsKeys> {
Ok(Self {
inner: Keys::from_pk_str(&public_key).map_err(into_err)?,
inner: Keys::from_pk_str(public_key).map_err(into_err)?,
})
}

Expand All @@ -77,9 +77,9 @@ impl JsKeys {

/// Derive keys from BIP-39 mnemonics (ENGLISH wordlist).
#[wasm_bindgen(js_name = fromMnemonic)]
pub fn from_mnemonic(mnemonic: String, passphrase: Option<String>) -> Result<JsKeys> {
pub fn from_mnemonic(mnemonic: &str, passphrase: Option<String>) -> Result<JsKeys> {
Ok(Self {
inner: Keys::from_mnemonic(mnemonic, passphrase).map_err(into_err)?,
inner: Keys::from_mnemonic(mnemonic, passphrase.as_deref()).map_err(into_err)?,
})
}

Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-js/src/key/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ impl From<&JsPublicKey> for XOnlyPublicKey {
#[wasm_bindgen(js_class = PublicKey)]
impl JsPublicKey {
#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex: String) -> Result<JsPublicKey> {
pub fn from_hex(hex: &str) -> Result<JsPublicKey> {
Ok(Self {
inner: XOnlyPublicKey::from_str(&hex).map_err(into_err)?,
inner: XOnlyPublicKey::from_str(hex).map_err(into_err)?,
})
}

#[wasm_bindgen(js_name = fromBech32)]
pub fn from_bech32(pk: String) -> Result<JsPublicKey> {
pub fn from_bech32(pk: &str) -> Result<JsPublicKey> {
Ok(Self {
inner: XOnlyPublicKey::from_bech32(pk).map_err(into_err)?,
})
Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-js/src/key/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ impl From<&JsSecretKey> for SecretKey {
#[wasm_bindgen(js_class = SecretKey)]
impl JsSecretKey {
#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex: String) -> Result<JsSecretKey> {
pub fn from_hex(hex: &str) -> Result<JsSecretKey> {
Ok(Self {
inner: SecretKey::from_str(&hex).map_err(into_err)?,
inner: SecretKey::from_str(hex).map_err(into_err)?,
})
}

#[wasm_bindgen(js_name = fromBech32)]
pub fn from_bech32(sk: String) -> Result<JsSecretKey> {
pub fn from_bech32(sk: &str) -> Result<JsSecretKey> {
Ok(Self {
inner: SecretKey::from_bech32(sk).map_err(into_err)?,
})
Expand Down
8 changes: 4 additions & 4 deletions bindings/nostr-js/src/message/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl JsClientMessage {
}

/// Create new `REQ` message
pub fn req(subscription_id: String, filters: Vec<JsFilter>) -> Self {
pub fn req(subscription_id: &str, filters: Vec<JsFilter>) -> Self {
Self {
inner: ClientMessage::req(
SubscriptionId::new(subscription_id),
Expand All @@ -50,7 +50,7 @@ impl JsClientMessage {
}

/// Create new `COUNT` message
pub fn count(subscription_id: String, filters: Vec<JsFilter>) -> Self {
pub fn count(subscription_id: &str, filters: Vec<JsFilter>) -> Self {
Self {
inner: ClientMessage::count(
SubscriptionId::new(subscription_id),
Expand All @@ -60,7 +60,7 @@ impl JsClientMessage {
}

/// Create new `CLOSE` message
pub fn close(subscription_id: String) -> Self {
pub fn close(subscription_id: &str) -> Self {
Self {
inner: ClientMessage::close(SubscriptionId::new(subscription_id)),
}
Expand All @@ -77,7 +77,7 @@ impl JsClientMessage {
///
/// **This method NOT verify the event signature!**
#[wasm_bindgen(js_name = fromJson)]
pub fn from_json(json: String) -> Result<JsClientMessage> {
pub fn from_json(json: &str) -> Result<JsClientMessage> {
Ok(Self {
inner: ClientMessage::from_json(json).map_err(into_err)?,
})
Expand Down
Loading