From ca7687201a9ccad599da0a8291756f0c65b67294 Mon Sep 17 00:00:00 2001 From: Mynima <62206239+rydalwater@users.noreply.github.com> Date: Tue, 10 Sep 2024 17:36:18 +0100 Subject: [PATCH] book: add NIP05 & NIP19 JavaScript examples * Updated index.js to include nip05.js and nip19.js * Added js examples to snippets for nip05 and nip19 * Updated book src to embed code snippets into existing text Closes https://github.com/rust-nostr/nostr/pull/569 Signed-off-by: Yuki Kishimoto --- book/snippets/nostr/js/index.js | 28 +++++++----- book/snippets/nostr/js/src/nip05.js | 37 ++++++++++++++++ book/snippets/nostr/js/src/nip19.js | 69 +++++++++++++++++++++++++++++ book/src/nostr/06-nip05.md | 20 ++++++++- book/src/nostr/06-nip19.md | 48 +++++++++++++++++++- 5 files changed, 190 insertions(+), 12 deletions(-) create mode 100644 book/snippets/nostr/js/src/nip05.js create mode 100644 book/snippets/nostr/js/src/nip19.js diff --git a/book/snippets/nostr/js/index.js b/book/snippets/nostr/js/index.js index c726b22a4..564223536 100644 --- a/book/snippets/nostr/js/index.js +++ b/book/snippets/nostr/js/index.js @@ -3,20 +3,28 @@ const eventJson = require("./src/event/json"); const eventBuilder = require("./src/event/builder"); const relayMessageJson = require("./src/messages/relay"); const nip01 = require("./src/nip01"); +const nip05 = require("./src/nip05"); +const nip19 = require("./src/nip19"); const nip44 = require("./src/nip44"); const nip59 = require("./src/nip59"); -// Keys -keys.generate(); -keys.restore(); -keys.vanity(); +async function main() { + // Keys + keys.generate(); + keys.restore(); + keys.vanity(); -eventJson.eventJson(); -eventBuilder.eventBuilder(); + eventJson.eventJson(); + eventBuilder.eventBuilder(); -relayMessageJson.relayMessageJson(); + relayMessageJson.relayMessageJson(); -nip01.run(); -nip44.run(); + nip01.run(); + await nip05.run(); + nip19.run(); + nip44.run(); -nip59.run(); \ No newline at end of file + nip59.run(); +} + +main(); diff --git a/book/snippets/nostr/js/src/nip05.js b/book/snippets/nostr/js/src/nip05.js new file mode 100644 index 000000000..d51184ba2 --- /dev/null +++ b/book/snippets/nostr/js/src/nip05.js @@ -0,0 +1,37 @@ +const { loadWasmAsync, PublicKey, Metadata, verifyNip05, getNip05Profile } = require("@rust-nostr/nostr"); + +async function run() { + // Load WASM + await loadWasmAsync(); + + console.log(); + // ANCHOR: set-metadata + // Create metadata object with name and NIP05 + let metadata = new Metadata() + .name("TestName") + .nip05("TestName@rustNostr.com"); + // ANCHOR_END: set-metadata + + console.log(); + // ANCHOR: verify-nip05 + console.log("Verify NIP-05:"); + let nip05 = "Rydal@gitlurker.info"; + let publicKey = PublicKey.parse("npub1zwnx29tj2lnem8wvjcx7avm8l4unswlz6zatk0vxzeu62uqagcash7fhrf"); + if (await verifyNip05(publicKey, nip05)) { + console.log(` '${nip05}' verified, for ${publicKey.toBech32()}`); + } else { + console.log(` Unable to verify NIP-05, for ${publicKey.toBech32()}`); + }; + // ANCHOR_END: verify-nip05 + + console.log(); + + // ANCHOR: nip05-profile + console.log("Get NIP-05 profile:"); + let nip_05 = "yuki@yukikishimoto.com"; + let profile = await getNip05Profile(nip_05); + console.log(` ${nip_05} Public key: ${profile.publicKey().toBech32()}`); + // ANCHOR_END: nip05-profile +} + +module.exports.run = run; diff --git a/book/snippets/nostr/js/src/nip19.js b/book/snippets/nostr/js/src/nip19.js new file mode 100644 index 000000000..477ecefce --- /dev/null +++ b/book/snippets/nostr/js/src/nip19.js @@ -0,0 +1,69 @@ +const { loadWasmSync, Keys, EventBuilder, Nip19Profile, Nip19Event, Coordinate, Kind } = require("@rust-nostr/nostr"); + +function run() { + // Load WASM + loadWasmSync(); + + // Generate random keys + let keys = Keys.generate(); + + console.log(); + console.log("Bare keys and ids (bech32):"); + // ANCHOR: nip19-npub + console.log(` Public key: ${keys.publicKey.toBech32()}`); + // ANCHOR_END: nip19-npub + + // ANCHOR: nip19-nsec + console.log(` Secret key: ${keys.secretKey.toBech32()}`); + // ANCHOR_END: nip19-nsec + + // ANCHOR: nip19-note + let event = EventBuilder.textNote("Hello from Rust Nostr JS Bindings!", []).toEvent(keys); + console.log(` Event : ${event.id.toBech32()}`); + // ANCHOR_END: nip19-note + + console.log(); + console.log("Shareable identifiers with extra metadata (bech32):"); + // ANCHOR: nip19-nprofile-encode + // Create NIP-19 profile including relays data + let relays = ["wss://relay.damus.io"]; + let nprofile = new Nip19Profile(keys.publicKey, relays); + console.log(` Profile (encoded): ${nprofile.toBech32()}`); + // ANCHOR_END: nip19-nprofile-encode + + // ANCHOR: nip19-nprofile-decode + // Decode NIP-19 profile + let decode_nprofile = Nip19Profile.fromBech32(nprofile.toBech32()); + console.log(` Profile (decoded): ${decode_nprofile.publicKey().toBech32()}`); + // ANCHOR_END: nip19-nprofile-decode + + console.log(); + // ANCHOR: nip19-nevent-encode + // Create NIP-19 event including author and relays data + let nevent = new Nip19Event(event.id, keys.publicKey, null, relays); + console.log(` Event (encoded): ${nevent.toBech32()}`); + // ANCHOR_END: nip19-nevent-encode + + // ANCHOR: nip19-nevent-decode + // Decode NIP-19 event + let decode_nevent = Nip19Event.fromBech32(nevent.toBech32()); + console.log(` Event (decoded): ${decode_nevent.eventId().toBech32()}`); + // ANCHOR_END: nip19-nevent-decode + + console.log(); + // ANCHOR: nip19-naddr-encode + // Create NIP-19 coordinate + let kind = new Kind(0); + let coord = new Coordinate(kind, keys.publicKey); + console.log(` Coordinate (encoded): ${coord.toBech32()}`); + // ANCHOR_END: nip19-naddr-encode + + // ANCHOR: nip19-naddr-decode + // Decode NIP-19 coordinate + let decode_coord = Coordinate.parse(coord.toBech32()); + console.log(` Coordinate (decoded): ${decode_coord}`); + // ANCHOR_END: nip19-naddr-decode + +} + +module.exports.run = run; diff --git a/book/src/nostr/06-nip05.md b/book/src/nostr/06-nip05.md index 5275b6d10..c6184f12c 100644 --- a/book/src/nostr/06-nip05.md +++ b/book/src/nostr/06-nip05.md @@ -42,7 +42,25 @@ To get the NIP-05 profile data (ex. user public key and relays) the `get_nip05_p
JavaScript
-TODO +Using the `Metadata` class to build the metadata object and incorporate the NIP-05 identifier with the `nip05()` method. + +For more details on metadata (or general) events please refer back to the [examples](06-nip01.md) provided for NIP-01. + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip05.js:set-metadata}} +``` + +For verification of NIP-05 identifiers associated with a given `PublicKey` object we can the `verifyNip05()` function as follows: + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip05.js:verify-nip05}} +``` + +To get the NIP-05 profile data (ex. user public key and relays) the `getNip05Profile()` function can be called: + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip05.js:nip05-profile}} +```
diff --git a/book/src/nostr/06-nip19.md b/book/src/nostr/06-nip19.md index 7ed477ad6..cb92a8528 100644 --- a/book/src/nostr/06-nip19.md +++ b/book/src/nostr/06-nip19.md @@ -78,7 +78,53 @@ Using the `Coordinate` class to generate the coordinates for a replaceable event
JavaScript
-TODO +For most of these examples you will see that the `toBech32()` and `fromBech32()` methods generally facilitate encoding or decoding objects per the NIP-19 standard. + +Public and Private (or secret) keys in `npub` and `nsec` formats. + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip19.js:nip19-npub}} +``` + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip19.js:nip19-nsec}} +``` + +Simple note presented in NIP-19 format. + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip19.js:nip19-note}} +``` + +Using the `Nip19Profile` class to create a shareable `nprofile` that includes relay data to help other applications to locate the profile data. This is followed by decoding the event object. + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip19.js:nip19-nprofile-encode}} +``` + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip19.js:nip19-nprofile-decode}} +``` + +Using the `Nip19Event` class to create a shareable `nevent` that includes author and relay data. This is followed by decoding the event object. + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip19.js:nip19-nevent-encode}} +``` + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip19.js:nip19-nevent-decode}} +``` + +Using the `Coordinate` class to generate the coordinates for a replaceable event (in this case Metadata). This is followed by decoding the object which uses the `parse` method. + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip19.js:nip19-naddr-encode}} +``` + +```javascript,ignore +{{#include ../../snippets/nostr/js/src/nip19.js:nip19-naddr-decode}} +```