-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 #569 Signed-off-by: Yuki Kishimoto <[email protected]>
- Loading branch information
1 parent
b4fbc00
commit ca76872
Showing
5 changed files
with
190 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters