diff --git a/book/snippets/nostr/js/index.js b/book/snippets/nostr/js/index.js index 45107bd1d..bd7065850 100644 --- a/book/snippets/nostr/js/index.js +++ b/book/snippets/nostr/js/index.js @@ -5,6 +5,7 @@ const relayMessages = require("./src/messages/relay"); const nip01 = require("./src/nip01"); const nip05 = require("./src/nip05"); const nip19 = require("./src/nip19"); +const nip21 = require("./src/nip21"); const nip44 = require("./src/nip44"); const nip59 = require("./src/nip59"); const nip65 = require("./src/nip65"); @@ -23,6 +24,7 @@ async function main() { nip01.run(); await nip05.run(); nip19.run(); + nip21.run(); nip44.run(); nip59.run(); diff --git a/book/snippets/nostr/js/src/nip21.js b/book/snippets/nostr/js/src/nip21.js new file mode 100644 index 000000000..80cedfe5e --- /dev/null +++ b/book/snippets/nostr/js/src/nip21.js @@ -0,0 +1,64 @@ +const { loadWasmSync, Keys, EventBuilder, Nip19Profile, Nip19Event, Coordinate} = require("@rust-nostr/nostr"); + +function run(){ + // Load WASM + loadWasmSync(); + + let keys = Keys.generate(); + + console.log(); + console.log("Nostr URIs:"); + + // ANCHOR: npub + // UNCOMMENT_ON_RELEASE: let pk_uri = keys.publicKey.toNostrUri(); + // UNCOMMENT_ON_RELEASE: console.log(` Public key (URI): ${pk_uri}`); + // ANCHOR_END: npub + + console.log(); + // ANCHOR: note + let event = EventBuilder.textNote("Hello from rust-nostr JS bindings!", []).toEvent(keys); + // UNCOMMENT_ON_RELEASE: let note_uri = event.id.toNostrUri() + // UNCOMMENT_ON_RELEASE: console.log(` Event (URI): ${note_uri}`); + // ANCHOR_END: note + + console.log(); + // ANCHOR: nprofile + let relays = ["wss://relay.damus.io"]; + let nprofile = new Nip19Profile(keys.publicKey, relays); + + // URI nprofile + let nprofile_uri = nprofile.toNostrUri(); + console.log(` Profile (URI): ${nprofile_uri}`); + + // bech32 nprofile + let nprofile_bech32 = Nip19Profile.fromNostrUri(nprofile_uri).toBech32(); + console.log(` Profile (bech32): ${nprofile_bech32}`); + // ANCHOR_END: nprofile + + console.log(); + // ANCHOR: nevent + let nevent = new Nip19Event(event.id, keys.publicKey, null, relays); + + // URI nevent + let nevent_uri = nevent.toNostrUri(); + console.log(` Event (URI): ${nevent_uri}`); + + // bech32 nevent + let nevent_bech32 = Nip19Event.fromNostrUri(nevent_uri).toBech32(); + console.log(` Event (bech32): ${nevent_bech32}`); + // ANCHOR_END: nevent + + console.log(); + // ANCHOR: naddr + // URI naddr + let coord_uri = new Coordinate(event.kind, keys.publicKey).toNostrUri(); + console.log(` Coordinate (URI): ${coord_uri}`); + + // bech32 naddr + let coord_bech32 = new Coordinate(event.kind, keys.publicKey).toBech32(); + console.log(` Coordinate (bech32): ${coord_bech32}`); + // ANCHOR_END: naddr + +} + +module.exports.run = run; diff --git a/book/src/nostr/06-nip21.md b/book/src/nostr/06-nip21.md index a5ab53382..bc93766c3 100644 --- a/book/src/nostr/06-nip21.md +++ b/book/src/nostr/06-nip21.md @@ -60,7 +60,38 @@ Coordinate identifier:
JavaScript
-TODO +Generally speaking the simplest way for handling NIP-21 objects is by the `toNostrUri()` and `fromNostrUri()` methods for encoding or decoding data, respectively. + + +Public key: + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip21.js:npub}} +``` + +Note: + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip21.js:note}} +``` + +Profile identifier: + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip21.js:nprofile}} +``` + +Event identifier: + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip21.js:nevent}} +``` + +Coordinate identifier: + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip21.js:naddr}} +```
@@ -77,4 +108,4 @@ TODO TODO - \ No newline at end of file +