Skip to content

Commit

Permalink
book: add NIP21 JavaScript examples
Browse files Browse the repository at this point in the history
* Updated index.js
* Added nip21.js
* Updated nip21.md

Closes #590

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
RydalWater authored and yukibtc committed Oct 18, 2024
1 parent c09d8de commit d01a2bf
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
2 changes: 2 additions & 0 deletions book/snippets/nostr/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -23,6 +24,7 @@ async function main() {
nip01.run();
await nip05.run();
nip19.run();
nip21.run();
nip44.run();

nip59.run();
Expand Down
64 changes: 64 additions & 0 deletions book/snippets/nostr/js/src/nip21.js
Original file line number Diff line number Diff line change
@@ -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;
35 changes: 33 additions & 2 deletions book/src/nostr/06-nip21.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,38 @@ Coordinate identifier:
<div slot="title">JavaScript</div>
<section>

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

</section>

Expand All @@ -77,4 +108,4 @@ TODO
TODO

</section>
</custom-tabs>
</custom-tabs>

0 comments on commit d01a2bf

Please sign in to comment.