support author on nevent.

This commit is contained in:
fiatjaf
2023-03-17 09:09:36 -03:00
parent c982ad0ab1
commit 332ae1ff7f
3 changed files with 46 additions and 2 deletions

View File

@ -71,6 +71,8 @@ func Decode(bech32string string) (prefix string, value any, err error) {
result.ID = hex.EncodeToString(v)
case TLVRelay:
result.Relays = append(result.Relays, string(v))
case TLVAuthor:
result.Author = hex.EncodeToString(v)
default:
// ignore
}
@ -173,10 +175,10 @@ func EncodeProfile(publicKeyHex string, relays []string) (string, error) {
return encode("nprofile", bits5)
}
func EncodeEvent(eventIdHex string, relays []string) (string, error) {
func EncodeEvent(eventIdHex string, relays []string, author string) (string, error) {
buf := &bytes.Buffer{}
id, err := hex.DecodeString(eventIdHex)
if err != nil {
if err != nil || len(id) != 32 {
return "", fmt.Errorf("invalid id '%s': %w", eventIdHex, err)
}
writeTLVEntry(buf, TLVDefault, id)
@ -185,6 +187,10 @@ func EncodeEvent(eventIdHex string, relays []string) (string, error) {
writeTLVEntry(buf, TLVRelay, []byte(url))
}
if pubkey, _ := hex.DecodeString(author); len(pubkey) == 32 {
writeTLVEntry(buf, TLVAuthor, pubkey)
}
bits5, err := convertBits(buf.Bytes(), 8, 5, true)
if err != nil {
return "", fmt.Errorf("failed to convert bits: %w", err)