Skip to content

Nostr Tutorial

TL;DR What Is Nostr?

Nostr is a decentralized P2P social media protocol that used uses cryptography as a primitive rather than DNS.

Also see Nostr Relay Tutorial

Source Code

Getting Started with Nostr

Before developing on Nostr try out one of their clients from my Nostr Client Reviews document.

Once you have tried out raw Nostr check out a my Notable Nostr Apps to see what can be built with Nostr.

Help I don't know how to Code

Goals of This Tutorial

  • Generate a new Nostr Account
  • Import an Existing Nostr Account
  • Publish a note on Nostr
  • Search a Nostr relay for notes from a specific nip
  • Send a encrypted message via Nostr

Results of This Tutorial

  • Scripts and a CLI that can speak basic Nostr

Requirements

Setup


npm init -y
echo "$(jq '. += {"type": "module"}' package.json)" > package.json
npm install nostr-tools

Generate a key from a Mnemonic

// mnemonic.js
import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools'
import { generateSeedWords, validateWords, privateKeyFromSeedWords } from 'nostr-tools/nip06'



const mnemonic = "curve foster stay broccoli equal icon bamboo champion casino impact will damp";
// const mnemonic = generateSeedWords()
let mnemonic_validation = validateWords(mnemonic)
let secret_key = privateKeyFromSeedWords(mnemonic, "", 0)
// let secret_key = generateSecretKey()
let public_key = getPublicKey(secret_key)
let uint8_secret_key = new Buffer.from(secret_key, "hex")
let nsec = nip19.nsecEncode(uint8_secret_key)
let npub = nip19.npubEncode(public_key)

console.log("mnemonic")
console.log(mnemonic)
console.log("\nmnemonic validation")
console.log(mnemonic_validation)
console.log("\nsecret_key")
console.log(secret_key)
console.log("\npublic_key")
console.log(public_key)
console.log("\nnsec")
console.log(nsec)
console.log("\nnpub")
console.log(npub)
console.log("\n\nsecret_key")
console.log(secret_key)
console.log("\n\nuint8_secret_key")
console.log(uint8_secret_key)

Create a Nostr Note "event" from an account