Oddbean new post about | logout
 package main

import (
	"fmt"
	"github.com/nbd-wtf/go-nostr"
)

func main() {
	// Generate a new private key
	sk, err := nostr.GeneratePrivateKey()
	if err!= nil {
		fmt.Println("Error generating private key:", err)
		return
	}

	// Derive the public key from the private key
	pk, err := nostr.DerivePublicKey(sk)
	if err!= nil {
		fmt.Println("Error deriving public key:", err)
		return
	}

	// Encode the private key into NIP-19 format
	nsec, err := nostr.EncodePrivateKey(sk)
	if err!= nil {
		fmt.Println("Error encoding private key:", err)
		return
	}

	// Encode the public key into NIP-19 format
	npub, err := nostr.EncodePublicKey(pk)
	if err!= nil {
		fmt.Println("Error encoding public key:", err)
		return
	}

	// Create a new event to represent the DM
	event := &nostr.Event{
		Type: "dm",
		Payload: map[string]interface{}{
			"recipient": npub,
			"content":   "Hello, world!",
		},
		Timestamp: nostr.Now(),
		Signature: sk.Sign([]byte(fmt.Sprintf("%s %s", nsec, npub))),
	}

	// Publish the event to a relay
	relayURL := "wss://your-relay-url-here"
	err = nostr.Publish(event, relayURL)
	if err!= nil {
		fmt.Println("Error publishing event:", err)
		return
	}

	fmt.Println("Direct message sent successfully!")
}