more conversions.
This commit is contained in:
131
nip19/nip19.go
131
nip19/nip19.go
@@ -3,11 +3,10 @@ package nip19
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"fiatjaf.com/nostr"
|
||||
"github.com/btcsuite/btcd/btcutil/bech32"
|
||||
"fiatjaf.com/nostrlib"
|
||||
)
|
||||
|
||||
func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
@@ -22,12 +21,21 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
}
|
||||
|
||||
switch prefix {
|
||||
case "npub", "nsec", "note":
|
||||
case "nsec":
|
||||
if len(data) != 32 {
|
||||
return prefix, nil, fmt.Errorf("data should be 32 bytes (%d)", len(data))
|
||||
return prefix, nil, fmt.Errorf("nsec should be 32 bytes (%d)", len(data))
|
||||
}
|
||||
|
||||
return prefix, hex.EncodeToString(data[0:32]), nil
|
||||
return prefix, [32]byte(data[0:32]), nil
|
||||
case "note":
|
||||
if len(data) != 32 {
|
||||
return prefix, nil, fmt.Errorf("note should be 32 bytes (%d)", len(data))
|
||||
}
|
||||
return prefix, [32]byte(data[0:32]), nil
|
||||
case "npub":
|
||||
if len(data) != 32 {
|
||||
return prefix, nil, fmt.Errorf("npub should be 32 bytes (%d)", len(data))
|
||||
}
|
||||
return prefix, nostr.PubKey(data[0:32]), nil
|
||||
case "nprofile":
|
||||
var result nostr.ProfilePointer
|
||||
curr := 0
|
||||
@@ -35,7 +43,7 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
t, v := readTLVEntry(data[curr:])
|
||||
if v == nil {
|
||||
// end here
|
||||
if result.PublicKey == "" {
|
||||
if result.PublicKey == nostr.ZeroPK {
|
||||
return prefix, result, fmt.Errorf("no pubkey found for nprofile")
|
||||
}
|
||||
|
||||
@@ -47,7 +55,7 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
if len(v) != 32 {
|
||||
return prefix, nil, fmt.Errorf("pubkey should be 32 bytes (%d)", len(v))
|
||||
}
|
||||
result.PublicKey = hex.EncodeToString(v)
|
||||
result.PublicKey = nostr.PubKey(v)
|
||||
case TLVRelay:
|
||||
result.Relays = append(result.Relays, string(v))
|
||||
default:
|
||||
@@ -63,7 +71,7 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
t, v := readTLVEntry(data[curr:])
|
||||
if v == nil {
|
||||
// end here
|
||||
if result.ID == "" {
|
||||
if result.ID == nostr.ZeroID {
|
||||
return prefix, result, fmt.Errorf("no id found for nevent")
|
||||
}
|
||||
|
||||
@@ -75,19 +83,19 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
if len(v) != 32 {
|
||||
return prefix, nil, fmt.Errorf("id should be 32 bytes (%d)", len(v))
|
||||
}
|
||||
result.ID = hex.EncodeToString(v)
|
||||
result.ID = nostr.ID(v)
|
||||
case TLVRelay:
|
||||
result.Relays = append(result.Relays, string(v))
|
||||
case TLVAuthor:
|
||||
if len(v) != 32 {
|
||||
return prefix, nil, fmt.Errorf("author should be 32 bytes (%d)", len(v))
|
||||
}
|
||||
result.Author = hex.EncodeToString(v)
|
||||
result.Author = nostr.PubKey(v)
|
||||
case TLVKind:
|
||||
if len(v) != 4 {
|
||||
return prefix, nil, fmt.Errorf("invalid uint32 value for integer (%v)", v)
|
||||
}
|
||||
result.Kind = int(binary.BigEndian.Uint32(v))
|
||||
result.Kind = uint16(binary.BigEndian.Uint32(v))
|
||||
default:
|
||||
// ignore
|
||||
}
|
||||
@@ -101,7 +109,7 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
t, v := readTLVEntry(data[curr:])
|
||||
if v == nil {
|
||||
// end here
|
||||
if result.Kind == 0 || result.Identifier == "" || result.PublicKey == "" {
|
||||
if result.Kind == 0 || result.Identifier == "" || result.PublicKey == nostr.ZeroPK {
|
||||
return prefix, result, fmt.Errorf("incomplete naddr")
|
||||
}
|
||||
|
||||
@@ -117,9 +125,9 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
if len(v) != 32 {
|
||||
return prefix, nil, fmt.Errorf("author should be 32 bytes (%d)", len(v))
|
||||
}
|
||||
result.PublicKey = hex.EncodeToString(v)
|
||||
result.PublicKey = nostr.PubKey(v)
|
||||
case TLVKind:
|
||||
result.Kind = int(binary.BigEndian.Uint32(v))
|
||||
result.Kind = uint16(binary.BigEndian.Uint32(v))
|
||||
default:
|
||||
// ignore
|
||||
}
|
||||
@@ -131,13 +139,8 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
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 := bech32.ConvertBits(b, 8, 5, true)
|
||||
func EncodeNsec(sk [32]byte) (string, error) {
|
||||
bits5, err := bech32.ConvertBits(sk[:], 8, 5, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -145,79 +148,44 @@ func EncodePrivateKey(privateKeyHex string) (string, error) {
|
||||
return bech32.Encode("nsec", bits5)
|
||||
}
|
||||
|
||||
func EncodePublicKey(publicKeyHex string) (string, error) {
|
||||
b, err := hex.DecodeString(publicKeyHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode public key hex: %w", err)
|
||||
}
|
||||
|
||||
bits5, err := bech32.ConvertBits(b, 8, 5, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return bech32.Encode("npub", bits5)
|
||||
func EncodeNpub(pk nostr.PubKey) string {
|
||||
bits5, _ := bech32.ConvertBits(pk[:], 8, 5, true)
|
||||
npub, _ := bech32.Encode("npub", bits5)
|
||||
return npub
|
||||
}
|
||||
|
||||
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 := bech32.ConvertBits(b, 8, 5, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return bech32.Encode("note", bits5)
|
||||
}
|
||||
|
||||
func EncodeProfile(publicKeyHex string, relays []string) (string, error) {
|
||||
func EncodeNprofile(pk nostr.PubKey, relays []string) string {
|
||||
buf := &bytes.Buffer{}
|
||||
pubkey, err := hex.DecodeString(publicKeyHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid pubkey '%s': %w", publicKeyHex, err)
|
||||
}
|
||||
writeTLVEntry(buf, TLVDefault, pubkey)
|
||||
writeTLVEntry(buf, TLVDefault, pk[:])
|
||||
|
||||
for _, url := range relays {
|
||||
writeTLVEntry(buf, TLVRelay, []byte(url))
|
||||
}
|
||||
|
||||
bits5, err := bech32.ConvertBits(buf.Bytes(), 8, 5, true)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to convert bits: %w", err)
|
||||
}
|
||||
bits5, _ := bech32.ConvertBits(buf.Bytes(), 8, 5, true)
|
||||
|
||||
return bech32.Encode("nprofile", bits5)
|
||||
nprofile, _ := bech32.Encode("nprofile", bits5)
|
||||
return nprofile
|
||||
}
|
||||
|
||||
func EncodeEvent(eventIDHex string, relays []string, author string) (string, error) {
|
||||
func EncodeNevent(id nostr.ID, relays []string, author nostr.PubKey) string {
|
||||
buf := &bytes.Buffer{}
|
||||
id, err := hex.DecodeString(eventIDHex)
|
||||
if err != nil || len(id) != 32 {
|
||||
return "", fmt.Errorf("invalid id '%s': %w", eventIDHex, err)
|
||||
}
|
||||
writeTLVEntry(buf, TLVDefault, id)
|
||||
writeTLVEntry(buf, TLVDefault, id[:])
|
||||
|
||||
for _, url := range relays {
|
||||
writeTLVEntry(buf, TLVRelay, []byte(url))
|
||||
}
|
||||
|
||||
if pubkey, _ := hex.DecodeString(author); len(pubkey) == 32 {
|
||||
writeTLVEntry(buf, TLVAuthor, pubkey)
|
||||
if author != nostr.ZeroPK {
|
||||
writeTLVEntry(buf, TLVAuthor, author[:])
|
||||
}
|
||||
|
||||
bits5, err := bech32.ConvertBits(buf.Bytes(), 8, 5, true)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to convert bits: %w", err)
|
||||
}
|
||||
|
||||
return bech32.Encode("nevent", bits5)
|
||||
bits5, _ := bech32.ConvertBits(buf.Bytes(), 8, 5, true)
|
||||
nevent, _ := bech32.Encode("nevent", bits5)
|
||||
return nevent
|
||||
}
|
||||
|
||||
func EncodeEntity(publicKey string, kind int, identifier string, relays []string) (string, error) {
|
||||
func EncodeNaddr(pk nostr.PubKey, kind uint16, identifier string, relays []string) string {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
writeTLVEntry(buf, TLVDefault, []byte(identifier))
|
||||
@@ -226,20 +194,13 @@ func EncodeEntity(publicKey string, kind int, identifier string, relays []string
|
||||
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)
|
||||
writeTLVEntry(buf, TLVAuthor, pk[:])
|
||||
|
||||
kindBytes := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(kindBytes, uint32(kind))
|
||||
writeTLVEntry(buf, TLVKind, kindBytes)
|
||||
|
||||
bits5, err := bech32.ConvertBits(buf.Bytes(), 8, 5, true)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to convert bits: %w", err)
|
||||
}
|
||||
|
||||
return bech32.Encode("naddr", bits5)
|
||||
bits5, _ := bech32.ConvertBits(buf.Bytes(), 8, 5, true)
|
||||
naddr, _ := bech32.Encode("naddr", bits5)
|
||||
return naddr
|
||||
}
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
package nip19
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"fiatjaf.com/nostrlib"
|
||||
"fiatjaf.com/nostr"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEncodeNpub(t *testing.T) {
|
||||
npub, err := EncodePublicKey("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d")
|
||||
assert.NoError(t, err)
|
||||
npub := EncodeNpub(nostr.MustPubKeyFromHex("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"))
|
||||
assert.Equal(t, "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6", npub, "produced an unexpected npub string")
|
||||
}
|
||||
|
||||
func TestEncodeNsec(t *testing.T) {
|
||||
nsec, err := EncodePrivateKey("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d")
|
||||
skh := "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"
|
||||
var sk [32]byte
|
||||
hex.Decode(sk[:], []byte(skh))
|
||||
nsec, err := EncodeNsec(sk)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "nsec180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsgyumg0", nsec, "produced an unexpected nsec string")
|
||||
}
|
||||
@@ -63,20 +66,22 @@ func TestDecodeNprofile(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEncodeNprofile(t *testing.T) {
|
||||
nprofile, err := EncodeProfile("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", []string{
|
||||
"wss://r.x.com",
|
||||
"wss://djbas.sadkb.com",
|
||||
})
|
||||
nprofile := EncodeNprofile(
|
||||
nostr.MustPubKeyFromHex("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"),
|
||||
[]string{
|
||||
"wss://r.x.com",
|
||||
"wss://djbas.sadkb.com",
|
||||
},
|
||||
)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t,
|
||||
"nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p",
|
||||
nprofile, "produced an unexpected nprofile string: %s", nprofile)
|
||||
}
|
||||
|
||||
func TestEncodeDecodeNaddr(t *testing.T) {
|
||||
naddr, err := EncodeEntity(
|
||||
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
|
||||
naddr := EncodeNaddr(
|
||||
nostr.MustPubKeyFromHex("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"),
|
||||
nostr.KindArticle,
|
||||
"banana",
|
||||
[]string{
|
||||
@@ -84,7 +89,6 @@ func TestEncodeDecodeNaddr(t *testing.T) {
|
||||
"wss://nostr.banana.com",
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t,
|
||||
"naddr1qqrxyctwv9hxzqfwwaehxw309aex2mrp0yhxummnw3ezuetcv9khqmr99ekhjer0d4skjm3wv4uxzmtsd3jjucm0d5q3vamnwvaz7tmwdaehgu3wvfskuctwvyhxxmmdqgsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8grqsqqqa28a3lkds",
|
||||
naddr, "produced an unexpected naddr string: %s", naddr)
|
||||
@@ -116,14 +120,12 @@ func TestDecodeNaddrWithoutRelays(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEncodeDecodeNEvent(t *testing.T) {
|
||||
nevent, err := EncodeEvent(
|
||||
"45326f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194",
|
||||
nevent := EncodeNevent(
|
||||
nostr.MustIDFromHex("45326f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194"),
|
||||
[]string{"wss://banana.com"},
|
||||
"7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751abb88",
|
||||
nostr.MustPubKeyFromHex("7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751abb88"),
|
||||
)
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
expectedNEvent := "nevent1qqsy2vn0t45k92c78n2zfe6ccvqzhpn977cd3h8wnl579zxhw5dvr9qpzpmhxue69uhkyctwv9hxztnrdaksygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2x4m3q04ndyp"
|
||||
assert.Equal(t, expectedNEvent, nevent)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package nip19
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"fiatjaf.com/nostr"
|
||||
)
|
||||
|
||||
func EncodePointer(pointer nostr.Pointer) string {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package nip19
|
||||
|
||||
import (
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"fiatjaf.com/nostr"
|
||||
)
|
||||
|
||||
func NeventFromRelayEvent(ie nostr.RelayEvent) string {
|
||||
v, _ := EncodeEvent(ie.ID, []string{ie.Relay.URL}, ie.PubKey)
|
||||
return v
|
||||
return EncodeNevent(ie.ID, []string{ie.Relay.URL}, ie.PubKey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user