Keys and Identity in Nostr
Learning Objectives
After this lesson, you'll understand:
- How digital identity works in the Nostr protocol
- The difference between private and public keys
- Different key formats and their purposes
- Best practices for key security and management
- Why Nostr uses cryptographic identity instead of usernames
Cryptographic Identity
Nostr uses a fundamentally different approach to identity compared to traditional social media platforms. Instead of usernames and passwords managed by a central authority, Nostr employs cryptographic key pairs to establish and verify identity.
This approach provides users with complete ownership and control over their digital identity, eliminating dependence on any central platform or service provider.
Understanding Key Pairs
Cryptographic Fundamentals
Cryptographic keys work as mathematically related pairs that enable secure digital communication:
- One-way mathematical relationship: Public keys are derived from private keys, but the reverse is computationally infeasible
- Digital signatures: Private keys create signatures that public keys can verify
- Identity verification: Signatures prove ownership without revealing the private key
graph LR
A[Private Key<br/>Secret Component] -->|generates| B[Public Key<br/>Identity Component]
A -->|signs| C[Messages/Events]
B -->|verifies| C
B -->|becomes| D[Nostr Identity]
Private Key Management
Definition and Purpose
The private key serves as the master secret that controls your entire Nostr identity. It is a cryptographically secure random number that:
- Generates your public identity: Mathematically derives your public key
- Creates digital signatures: Proves authorship of your events
- Controls all interactions: Required for posting, following, and other actions
- Cannot be recovered: Loss results in permanent identity loss
Security Requirements
Private key security is critical for maintaining control of your Nostr identity:
Critical Security Practices
Never Share Your Private Key - Do not transmit via email, messaging, or any digital communication - Avoid storing in plain text files or unencrypted formats - Never enter into untrusted applications or websites
Secure Storage Methods - Use hardware wallets for maximum security - Employ encrypted password managers - Create offline, encrypted backups - Store in multiple secure locations
Key Format Examples
Public Key Distribution
Purpose and Function
Your public key serves as your permanent Nostr address and identity. It enables:
- Global identification: Unique address in the Nostr network
- Signature verification: Others can verify your message authenticity
- Social connections: Followers use this to find and follow you
- Cross-client compatibility: Works across all Nostr applications
Sharing Guidelines
Unlike private keys, public keys are designed for open distribution:
✅ Safe to share publicly
✅ Include in social media profiles
✅ Display on websites and business cards
✅ Send via any communication method
✅ Embed in QR codes
Public Key Formats
Mathematical Relationship
The relationship between private and public keys relies on elliptic curve cryptography:
Key Generation Process
graph TB
A[Cryptographically Secure<br/>Random Number] -->|secp256k1| B[Private Key]
B -->|Elliptic Curve<br/>Point Multiplication| C[Public Key]
B -->|ECDSA| D[Digital Signatures]
C -->|verifies| D
Security Properties
- One-way function: Computing public key from private key is fast
- Computational security: Deriving private key from public key is infeasible
- Signature uniqueness: Each message produces a unique signature
- Non-repudiation: Signatures cannot be forged without the private key
Your Private Key in Detail
Let's dive deeper into your most important digital possession.
What Makes It Special
- Completely random: Generated using cryptographically secure randomness
- Astronomically unique: The chances of two people getting the same key are basically zero
- Mathematically powerful: Can create unlimited verified signatures
- Irreplaceable: There's no customer service to call if you lose it
What It Looks Like
Creating Your First Key Pair
Your Public Key in Detail
Now let's explore your public identity - the part of you that the Nostr world gets to see.
What Makes It Perfect for Sharing
- Derived from private: Mathematically created from your private key
- Always the same: Your private key always generates the same public key
- Safe to broadcast: No security risk in sharing it widely
- Verifiable: Others can use it to confirm your signatures are real
Key Formats and Encoding
Nostr uses different formats for displaying keys to users:
Hex Format
- Raw format: 64 character hexadecimal string
- Use case: Internal processing, APIs
- Example:
d63b64d9c2c4f8c7b8e9f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2
Bech32 Format (NIP-19)
- User-friendly: Includes checksums and prefixes
- Private keys: Start with
nsec1
- Public keys: Start with
npub1
- Benefits: Error detection, easier to copy/paste
Format Conversion
Most Nostr libraries provide functions to convert between formats:
Digital Signatures
Your private key is used to create digital signatures that prove authenticity:
How Signatures Work
- Create Event: You compose a message or event
- Hash Content: The event content is hashed
- Sign Hash: Your private key signs the hash
- Attach Signature: The signature is included with the event
- Verify: Others use your public key to verify the signature
Example: Signing an Event
Identity Without Usernames
Traditional vs Nostr Identity
Traditional Social Media | Nostr |
---|---|
Username + Password | Cryptographic Keys |
Platform controls identity | You control identity |
Can be banned/suspended | Censorship resistant |
Tied to one platform | Portable across clients |
Password can be reset | Keys cannot be recovered |
Benefits of Cryptographic Identity
Advantages
- True Ownership: You own your identity, not a platform
- Portability: Use the same identity across all Nostr clients
- Censorship Resistance: No central authority can ban you
- Verification: Cryptographic proof of authenticity
- Privacy: No personal information required
Responsibilities
- Key Security: You must protect your private key
- No Recovery: Lost keys mean lost identity
- Backup Important: Always backup your keys securely
Key Management Best Practices
Secure Storage
Never Do This
- Store private keys in plain text files
- Share private keys via email or messaging
- Use the same key across multiple devices without encryption
- Store keys in browser localStorage without encryption
Best Practices
- Use hardware wallets when possible
- Encrypt private keys with strong passwords
- Use secure key management software
- Create offline backups in multiple locations
- Consider using key derivation for multiple identities
Key Backup Strategies
Multiple Identities
You can have multiple Nostr identities for different purposes:
Use Cases
- Personal: Your main social identity
- Professional: Work-related content
- Anonymous: Privacy-focused interactions
- Bot: Automated accounts
- Testing: Development and experimentation
Managing Multiple Keys
class NostrIdentityManager {
constructor() {
this.identities = new Map()
}
createIdentity(name) {
const privateKey = generatePrivateKey()
const publicKey = getPublicKey(privateKey)
this.identities.set(name, {
privateKey,
publicKey,
npub: nip19.npubEncode(publicKey)
})
return this.identities.get(name)
}
getIdentity(name) {
return this.identities.get(name)
}
listIdentities() {
return Array.from(this.identities.keys())
}
}
Interactive Exercise
Let's practice working with keys:
Try This
- Generate a new key pair
- Convert between hex and bech32 formats
- Create and sign a simple event
- Verify the signature
Use the code examples above to experiment with key generation and signing.
Common Pitfalls
Avoid These Mistakes
Key Confusion - Mixing up private and public keys - Using wrong key formats - Sharing private keys accidentally
Security Issues - Storing keys insecurely - Not backing up keys - Using weak random number generation
Format Errors - Invalid hex characters - Incorrect bech32 encoding - Wrong key length
Next Steps
Now that you understand keys and identity, let's explore how events and messages work in Nostr.
Quick Quiz
Test Your Understanding
- What's the difference between a private key and public key?
- Which key format starts with "npub1"?
- What happens if you lose your private key?
- Why doesn't Nostr use usernames and passwords?
- What is the purpose of digital signatures?
Answers
- Private key is secret and used for signing; public key is shared and used for verification
- Public keys in bech32 format start with "npub1"
- You lose access to that identity forever - there's no recovery
- Cryptographic keys provide better security, ownership, and censorship resistance
- Digital signatures prove authenticity and prevent tampering