Skip to content

Nostr Client Tutorial

TL;DR What Is Nostr?

Nostr is a decentralized P2P social media protocol that used uses cryptography as a primitive rather than DNS. Though it can be used with DNS and DID's for persistent identities across key rotations.

Also see Nostr Relay Tutorial

Requirements

  • Basic knowledge of Javascript
  • Knowledge of how to use a Command Line
  • Git
  • Nodejs + NPM
  • Code Editor

Source Code

Getting Started with Nostr

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

Once you have tried out raw Nostr check out a my 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 using a Mnemonic
  • Import an Existing Nostr Account
  • Publish a note (event) to a Nostr Relay
  • Search and steam events from Nostr Relays
  • Reply to a nostr event
  • React to a nostr event
  • Repost a nostr event
  • 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

Sources