mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-07-09 23:59:54 +02:00
nip19.
This commit is contained in:
140
nip19/nip19.go
140
nip19/nip19.go
@ -1,17 +1,106 @@
|
||||
package nip19
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type ProfilePointer struct {
|
||||
PublicKey string
|
||||
Relays []string
|
||||
}
|
||||
|
||||
type EventPointer struct {
|
||||
ID string
|
||||
Relays []string
|
||||
}
|
||||
|
||||
func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
prefix, bits5, err := decode(bech32string)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
data, err := convertBits(bits5, 5, 8, false)
|
||||
if err != nil {
|
||||
return prefix, nil, fmt.Errorf("failed translating data into 8 bits: %s", err.Error())
|
||||
}
|
||||
|
||||
switch prefix {
|
||||
case "npub", "nsec", "note":
|
||||
if len(data) < 32 {
|
||||
return prefix, nil, fmt.Errorf("data is less than 32 bytes (%d)", len(data))
|
||||
}
|
||||
|
||||
return prefix, hex.EncodeToString(data[0:32]), nil
|
||||
case "nprofile":
|
||||
var result ProfilePointer
|
||||
curr := 0
|
||||
for {
|
||||
t, v := readTLVEntry(data[curr:])
|
||||
if v == nil {
|
||||
// end here
|
||||
if result.PublicKey == "" {
|
||||
return prefix, result, fmt.Errorf("no pubkey found for nprofile")
|
||||
}
|
||||
|
||||
return prefix, result, nil
|
||||
}
|
||||
|
||||
switch t {
|
||||
case TLVDefault:
|
||||
result.PublicKey = hex.EncodeToString(v)
|
||||
case TLVRelay:
|
||||
result.Relays = append(result.Relays, string(v))
|
||||
default:
|
||||
// ignore
|
||||
}
|
||||
|
||||
curr = curr + 2 + len(v)
|
||||
}
|
||||
case "nevent":
|
||||
var result EventPointer
|
||||
curr := 0
|
||||
for {
|
||||
t, v := readTLVEntry(data[curr:])
|
||||
if v == nil {
|
||||
// end here
|
||||
if result.ID == "" {
|
||||
return prefix, result, fmt.Errorf("no id found for nevent")
|
||||
}
|
||||
|
||||
return prefix, result, nil
|
||||
}
|
||||
|
||||
switch t {
|
||||
case TLVDefault:
|
||||
result.ID = hex.EncodeToString(v)
|
||||
case TLVRelay:
|
||||
result.Relays = append(result.Relays, string(v))
|
||||
default:
|
||||
// ignore
|
||||
}
|
||||
|
||||
curr = curr + 2 + len(v)
|
||||
}
|
||||
}
|
||||
|
||||
return prefix, data, fmt.Errorf("unknown tag %s", prefix)
|
||||
}
|
||||
|
||||
func EncodePrivateKey(privateKeyHex string) (string, error) {
|
||||
b, err := hex.DecodeString(privateKeyHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode private key hex: %w", err)
|
||||
}
|
||||
|
||||
bits5, err := convertBits(b, 8, 5, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return encode("nsec", b)
|
||||
return encode("nsec", bits5)
|
||||
}
|
||||
|
||||
func EncodePublicKey(publicKeyHex string) (string, error) {
|
||||
@ -30,27 +119,54 @@ func EncodePublicKey(publicKeyHex string) (string, error) {
|
||||
|
||||
func EncodeNote(eventIdHex string) (string, error) {
|
||||
b, err := hex.DecodeString(eventIdHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode event id hex: %w", err)
|
||||
}
|
||||
|
||||
bits5, err := convertBits(b, 8, 5, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return encode("note", b)
|
||||
return encode("note", bits5)
|
||||
}
|
||||
|
||||
func Decode(bech32string string) ([]byte, string, error) {
|
||||
prefix, data, err := decode(bech32string)
|
||||
func EncodeProfile(publicKeyHex string, relays []string) (string, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
pubkey, err := hex.DecodeString(publicKeyHex)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
return "", fmt.Errorf("invalid pubkey '%s': %w", publicKeyHex, err)
|
||||
}
|
||||
writeTLVEntry(buf, TLVDefault, pubkey)
|
||||
|
||||
for _, url := range relays {
|
||||
writeTLVEntry(buf, TLVRelay, []byte(url))
|
||||
}
|
||||
|
||||
bits8, err := convertBits(data, 5, 8, false)
|
||||
bits5, err := convertBits(buf.Bytes(), 8, 5, true)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed translating data into 8 bits: %s", err.Error())
|
||||
return "", fmt.Errorf("failed to convert bits: %w", err)
|
||||
}
|
||||
|
||||
if len(data) < 32 {
|
||||
return nil, "", fmt.Errorf("data is less than 32 bytes (%d)", len(data))
|
||||
}
|
||||
|
||||
return bits8[0:32], prefix, nil
|
||||
return encode("nprofile", bits5)
|
||||
}
|
||||
|
||||
func EncodeEvent(eventIdHex string, relays []string) (string, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
pubkey, err := hex.DecodeString(eventIdHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid id '%s': %w", eventIdHex, err)
|
||||
}
|
||||
writeTLVEntry(buf, TLVDefault, pubkey)
|
||||
|
||||
for _, url := range relays {
|
||||
writeTLVEntry(buf, TLVRelay, []byte(url))
|
||||
}
|
||||
|
||||
bits5, err := convertBits(buf.Bytes(), 8, 5, true)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to convert bits: %w", err)
|
||||
}
|
||||
|
||||
return encode("nevent", bits5)
|
||||
}
|
||||
|
Reference in New Issue
Block a user