Skip to content

Commit 9bb60c4

Browse files
committed
js: gossip
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent f8cc743 commit 9bb60c4

File tree

7 files changed

+75
-9
lines changed

7 files changed

+75
-9
lines changed

bindings/nostr-sdk-js/.cargo/config.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ opt-level = 'z' # Optimize for size.
33
lto = true # Enable Link Time Optimization
44
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
55
panic = "abort" # Abort on panic
6-
strip = true # Strip symbols from binary
6+
strip = "debuginfo" # Strip debug symbols from binary. Full strip will cause issues with SQLite
77

88
[build]
99
target = "wasm32-unknown-unknown"
10-
rustflags = ["-C", "panic=abort"]
10+
rustflags = [
11+
"-Cpanic=abort",
12+
"-Ctarget-feature=+bulk-memory" # Required for SQLite WASM
13+
]
1114

1215
[unstable]
1316
unstable-options = true

bindings/nostr-sdk-js/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ crate-type = ["cdylib"]
1111
console_error_panic_hook = "0.1"
1212
js-sys.workspace = true
1313
nostr-connect.workspace = true
14-
nostr-sdk = { workspace = true, default-features = false, features = ["all-nips", "indexeddb"] }
14+
nostr-sdk = { workspace = true, default-features = false, features = ["all-nips", "indexeddb", "gossip"] }
1515
nwc.workspace = true
1616
tracing.workspace = true
1717
tracing-subscriber.workspace = true
1818
wasm-bindgen = { workspace = true, features = ["std"] }
1919
wasm-bindgen-futures.workspace = true
2020

2121
[package.metadata.wasm-pack.profile.profiling]
22-
wasm-opt = true
22+
wasm-opt = false # Optimizations causes issues to SQLite gossip storage
2323

2424
[lints.rust]
2525
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const { Keys, Client, NostrSigner, PublicKey, EventBuilder, loadWasmAsync, initLogger, Gossip, LogLevel, Tag } = require("../");
2+
3+
async function main() {
4+
await loadWasmAsync();
5+
6+
initLogger(LogLevel.info());
7+
8+
let keys = Keys.parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85");
9+
let signer = NostrSigner.keys(keys);
10+
11+
let gossip = Gossip.inMemory()
12+
13+
let client = Client.builder().signer(signer).gossip(gossip).build();
14+
15+
await client.addDiscoveryRelay("wss://relay.damus.io");
16+
await client.addDiscoveryRelay("wss://purplepag.es");
17+
18+
await client.connect();
19+
20+
let pk = PublicKey.parse("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet");
21+
22+
let builder = EventBuilder.textNote(
23+
"Hello world nostr:npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet",
24+
).tags([Tag.publicKey(pk)]);
25+
let output = await client.sendEventBuilder(builder);
26+
console.log("Event ID", output.id.toBech32());
27+
console.log("Successfully sent to:", output.success);
28+
console.log("Failed to sent to:", output.failed);
29+
}
30+
31+
main();

bindings/nostr-sdk-js/src/client/builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use wasm_bindgen::prelude::*;
1010
use super::options::JsOptions;
1111
use super::{JsClient, JsNostrSigner};
1212
use crate::database::JsNostrDatabase;
13+
use crate::gossip::JsGossip;
1314
use crate::policy::{FFI2RustAdmitPolicy, JsAdmitPolicy};
1415

1516
#[wasm_bindgen(js_name = ClientBuilder)]
@@ -41,6 +42,10 @@ impl JsClientBuilder {
4142
self.inner.database(database.deref().clone()).into()
4243
}
4344

45+
pub fn gossip(self, gossip: &JsGossip) -> Self {
46+
self.inner.gossip(gossip.deref().clone()).into()
47+
}
48+
4449
#[wasm_bindgen(js_name = admitPolicy)]
4550
pub fn admit_policy(self, policy: JsAdmitPolicy) -> Self {
4651
self.inner

bindings/nostr-sdk-js/src/client/options.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ impl JsOptions {
5252
self.inner.automatic_authentication(enabled).into()
5353
}
5454

55-
/// Enable gossip model (default: false)
56-
pub fn gossip(self, enable: bool) -> Self {
57-
self.inner.gossip(enable).into()
58-
}
59-
6055
/// Set custom relay limits
6156
#[wasm_bindgen(js_name = relayLimits)]
6257
pub fn relay_limits(self, limits: &JsRelayLimits) -> Self {

bindings/nostr-sdk-js/src/gossip.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2022-2023 Yuki Kishimoto
2+
// Copyright (c) 2023-2025 Rust Nostr Developers
3+
// Distributed under the MIT software license
4+
5+
use std::ops::Deref;
6+
7+
use nostr_sdk::prelude::*;
8+
use wasm_bindgen::prelude::*;
9+
10+
#[wasm_bindgen(js_name = Gossip)]
11+
pub struct JsGossip {
12+
inner: Gossip,
13+
}
14+
15+
impl Deref for JsGossip {
16+
type Target = Gossip;
17+
18+
fn deref(&self) -> &Self::Target {
19+
&self.inner
20+
}
21+
}
22+
23+
#[wasm_bindgen(js_class = Gossip)]
24+
impl JsGossip {
25+
#[wasm_bindgen(js_name = inMemory)]
26+
pub fn in_memory() -> Self {
27+
Self {
28+
inner: Gossip::in_memory(),
29+
}
30+
}
31+
}

bindings/nostr-sdk-js/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mod connect;
1919
pub mod database;
2020
pub mod duration;
2121
pub mod error;
22+
pub mod gossip;
2223
pub mod logger;
2324
pub mod nwc;
2425
pub mod policy;

0 commit comments

Comments
 (0)