Skip to content

Commit

Permalink
book: add NIP05 & NIP19 JavaScript examples
Browse files Browse the repository at this point in the history
* 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 #569

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
RydalWater authored and yukibtc committed Sep 19, 2024
1 parent b4fbc00 commit ca76872
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 12 deletions.
28 changes: 18 additions & 10 deletions book/snippets/nostr/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
nip59.run();
}

main();
37 changes: 37 additions & 0 deletions book/snippets/nostr/js/src/nip05.js
Original file line number Diff line number Diff line change
@@ -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("[email protected]");
// ANCHOR_END: set-metadata

console.log();
// ANCHOR: verify-nip05
console.log("Verify NIP-05:");
let nip05 = "[email protected]";
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 = "[email protected]";
let profile = await getNip05Profile(nip_05);
console.log(` ${nip_05} Public key: ${profile.publicKey().toBech32()}`);
// ANCHOR_END: nip05-profile
}

module.exports.run = run;
69 changes: 69 additions & 0 deletions book/snippets/nostr/js/src/nip19.js
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 19 additions & 1 deletion book/src/nostr/06-nip05.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,25 @@ To get the NIP-05 profile data (ex. user public key and relays) the `get_nip05_p
<div slot="title">JavaScript</div>
<section>

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}}
```

</section>

Expand Down
48 changes: 47 additions & 1 deletion book/src/nostr/06-nip19.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,53 @@ Using the `Coordinate` class to generate the coordinates for a replaceable event
<div slot="title">JavaScript</div>
<section>

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}}
```

</section>

Expand Down

0 comments on commit ca76872

Please sign in to comment.