summaryrefslogtreecommitdiffstats
path: root/README.md
blob: 8d42710290ac9167e02274ee2fe09b6e3a865fd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# nostr

A minimal Go library for the [Nostr protocol](https://github.com/nostr-protocol/nostr).

```bash
go get code.northwest.io/nostr
```

## Why This Library?

**Zero dependencies.**

Other Nostr libraries pull in 30+ dependencies. This one has none. The secp256k1 cryptography is implemented in pure Go, embedded in the library.

```
require (nothing)
```

No external crypto libraries, no WebSocket libraries, no logging frameworks, no kitchen sink. Just the core protocol.

## What's Included

- **Keys** — Generate, parse, sign, verify (hex and bech32/npub/nsec)
- **Events** — Create, serialize, sign NIP-01 events  
- **Filters** — Build and match subscription filters
- **Relay** — WebSocket pub/sub (stdlib `net/http` only)
- **Tags** — Parse and build event tags
- **Envelopes** — Protocol message parsing

## What's Not Included

This is a minimal core library. It implements NIP-01 and the basics. It doesn't implement every NIP, handle connection pooling, or manage relay discovery. Build that yourself, or don't.

## Cryptography

This library uses an internal pure-Go implementation of secp256k1 and BIP-340 Schnorr signatures.

**Tradeoffs:**

- ✅ Zero dependencies, fully auditable
- ✅ Passes all BIP-340 test vectors
- ✅ Interoperable with btcec/bitcoin implementations
- ⚠️ **Not constant-time** — uses Go's `math/big`, which has variable-time operations
- ⚠️ ~10x slower than btcec (still fast enough for typical Nostr usage)

For applications signing thousands of events per second or with strict timing-attack threat models, consider a library backed by btcec or libsecp256k1.

## Usage

```go
package main

import (
    "context"
    "fmt"
    "code.northwest.io/nostr"
)

func main() {
    // Generate a key pair
    key, _ := nostr.GenerateKey()
    fmt.Println("npub:", key.Npub())

    // Create and sign an event
    event := &nostr.Event{
        Kind:    nostr.KindTextNote,
        Content: "Hello Nostr!",
    }
    key.Sign(event)

    // Connect and publish
    ctx := context.Background()
    relay, _ := nostr.Connect(ctx, "wss://relay.damus.io")
    defer relay.Close()
    
    relay.Publish(ctx, event)
}
```

## Examples

See [examples/basic](examples/basic) for a runnable example.

## License

MIT