mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01:00
support for naddr on nip19.
This commit is contained in:
parent
e7e20f3e00
commit
916a6a6abb
@ -2,6 +2,7 @@ package nip19
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
@ -74,6 +75,35 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
curr = curr + 2 + len(v)
|
||||
}
|
||||
case "naddr":
|
||||
var result nostr.EntityPointer
|
||||
curr := 0
|
||||
for {
|
||||
t, v := readTLVEntry(data[curr:])
|
||||
if v == nil {
|
||||
// end here
|
||||
if result.Kind == 0 || result.Identifier == "" || result.PublicKey == "" {
|
||||
return prefix, result, fmt.Errorf("incomplete naddr")
|
||||
}
|
||||
|
||||
return prefix, result, nil
|
||||
}
|
||||
|
||||
switch t {
|
||||
case TLVDefault:
|
||||
result.Identifier = string(v)
|
||||
case TLVRelay:
|
||||
result.Relays = append(result.Relays, string(v))
|
||||
case TLVAuthor:
|
||||
result.PublicKey = hex.EncodeToString(v)
|
||||
case TLVKind:
|
||||
result.Kind = int(binary.BigEndian.Uint32(v))
|
||||
default:
|
||||
// ignore
|
||||
}
|
||||
|
||||
curr = curr + 2 + len(v)
|
||||
}
|
||||
}
|
||||
@ -145,11 +175,11 @@ func EncodeProfile(publicKeyHex string, relays []string) (string, error) {
|
||||
|
||||
func EncodeEvent(eventIdHex string, relays []string) (string, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
pubkey, err := hex.DecodeString(eventIdHex)
|
||||
id, err := hex.DecodeString(eventIdHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid id '%s': %w", eventIdHex, err)
|
||||
}
|
||||
writeTLVEntry(buf, TLVDefault, pubkey)
|
||||
writeTLVEntry(buf, TLVDefault, id)
|
||||
|
||||
for _, url := range relays {
|
||||
writeTLVEntry(buf, TLVRelay, []byte(url))
|
||||
@ -162,3 +192,30 @@ func EncodeEvent(eventIdHex string, relays []string) (string, error) {
|
||||
|
||||
return encode("nevent", bits5)
|
||||
}
|
||||
|
||||
func EncodeEntity(publicKey string, kind int, identifier string, relays []string) (string, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
writeTLVEntry(buf, TLVDefault, []byte(identifier))
|
||||
|
||||
for _, url := range relays {
|
||||
writeTLVEntry(buf, TLVRelay, []byte(url))
|
||||
}
|
||||
|
||||
pubkey, err := hex.DecodeString(publicKey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid pubkey '%s': %w", pubkey, err)
|
||||
}
|
||||
writeTLVEntry(buf, TLVAuthor, pubkey)
|
||||
|
||||
kindBytes := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(kindBytes, uint32(kind))
|
||||
writeTLVEntry(buf, TLVKind, kindBytes)
|
||||
|
||||
bits5, err := convertBits(buf.Bytes(), 8, 5, true)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to convert bits: %w", err)
|
||||
}
|
||||
|
||||
return encode("naddr", bits5)
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package nip19
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
|
||||
@ -107,3 +108,64 @@ func TestEncodeNprofile(t *testing.T) {
|
||||
t.Error("produced an unexpected nprofile string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeDecodeNaddr(t *testing.T) {
|
||||
naddr, err := EncodeEntity(
|
||||
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
|
||||
30023,
|
||||
"banana",
|
||||
[]string{
|
||||
"wss://relay.nostr.example.mydomain.example.com",
|
||||
"wss://nostr.banana.com",
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("shouldn't error: %s", err)
|
||||
}
|
||||
if naddr != "naddr1qqrxyctwv9hxzqfwwaehxw309aex2mrp0yhxummnw3ezuetcv9khqmr99ekhjer0d4skjm3wv4uxzmtsd3jjucm0d5q3vamnwvaz7tmwdaehgu3wvfskuctwvyhxxmmdqgsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8grqsqqqa28a3lkds" {
|
||||
t.Errorf("produced an unexpected naddr string: %s", naddr)
|
||||
}
|
||||
|
||||
prefix, data, err := Decode(naddr)
|
||||
if err != nil {
|
||||
t.Errorf("shouldn't error: %s", err)
|
||||
}
|
||||
if prefix != "naddr" {
|
||||
t.Error("returned invalid prefix")
|
||||
}
|
||||
ep := data.(nostr.EntityPointer)
|
||||
if ep.PublicKey != "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d" {
|
||||
t.Error("returned wrong pubkey")
|
||||
}
|
||||
if ep.Kind != 30023 {
|
||||
t.Error("returned wrong kind")
|
||||
}
|
||||
if ep.Identifier != "banana" {
|
||||
t.Error("returned wrong identifier")
|
||||
}
|
||||
if ep.Relays[0] != "wss://relay.nostr.example.mydomain.example.com" || ep.Relays[1] != "wss://nostr.banana.com" {
|
||||
t.Error("returned wrong relays")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeNaddrWithoutRelays(t *testing.T) {
|
||||
prefix, data, err := Decode("naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5")
|
||||
if err != nil {
|
||||
t.Errorf("shouldn't error: %s", err)
|
||||
}
|
||||
if prefix != "naddr" {
|
||||
t.Error("returned invalid prefix")
|
||||
}
|
||||
ep := data.(nostr.EntityPointer)
|
||||
if ep.PublicKey != "7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194" {
|
||||
t.Error("returned wrong pubkey")
|
||||
}
|
||||
if ep.Kind != 30023 {
|
||||
t.Error("returned wrong kind")
|
||||
}
|
||||
if ep.Identifier != "references" {
|
||||
t.Error("returned wrong identifier")
|
||||
}
|
||||
if len(ep.Relays) != 0 {
|
||||
t.Error("relays should have been an empty array")
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
const (
|
||||
TLVDefault uint8 = 0
|
||||
TLVRelay uint8 = 1
|
||||
TLVAuthor uint8 = 2
|
||||
TLVKind uint8 = 3
|
||||
)
|
||||
|
||||
func readTLVEntry(data []byte) (typ uint8, value []byte) {
|
||||
|
@ -9,3 +9,10 @@ type EventPointer struct {
|
||||
ID string
|
||||
Relays []string
|
||||
}
|
||||
|
||||
type EntityPointer struct {
|
||||
PublicKey string
|
||||
Kind int
|
||||
Identifier string
|
||||
Relays []string
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user