nip05: use typed pubkeys.

This commit is contained in:
fiatjaf
2025-12-14 09:18:18 -03:00
parent 658a40e16c
commit 1176d12b0a
3 changed files with 323 additions and 14 deletions
+31 -4
View File
@@ -2,10 +2,12 @@ package nip05
import (
"context"
"encoding/json"
"testing"
"fiatjaf.com/nostr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParse(t *testing.T) {
@@ -38,11 +40,11 @@ func TestParse(t *testing.T) {
func TestQuery(t *testing.T) {
tests := []struct {
input string
expectedKey string
expectedKey nostr.PubKey
expectError bool
}{
{"fiatjaf.com", "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", false},
{"htlc@fiatjaf.com", "f9dd6a762506260b38a2d3e5b464213c2e47fa3877429fe9ee60e071a31a07d7", false},
{"fiatjaf.com", nostr.MustPubKeyFromHex("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"), false},
{"htlc@fiatjaf.com", nostr.MustPubKeyFromHex("f9dd6a762506260b38a2d3e5b464213c2e47fa3877429fe9ee60e071a31a07d7"), false},
}
for _, test := range tests {
@@ -51,7 +53,32 @@ func TestQuery(t *testing.T) {
assert.Error(t, err, "expected error for input: %s", test.input)
} else {
assert.NoError(t, err, "did not expect error for input: %s", test.input)
assert.Equal(t, nostr.MustPubKeyFromHex(test.expectedKey), pp.PublicKey, "for input: %s", test.input)
assert.Equal(t, test.expectedKey, pp.PublicKey, "for input: %s", test.input)
}
}
}
func TestResponse(t *testing.T) {
pk1 := nostr.Generate().Public()
pk2 := nostr.Generate().Public()
resp := WellKnownResponse{
Names: map[string]nostr.PubKey{
"foo": pk1,
"bar": pk2,
},
Relays: map[nostr.PubKey][]string{
pk1: {"wss://a.com"},
pk2: {"wss://a.com", "wss://b.com"},
},
}
respj, err := json.Marshal(resp)
require.NoError(t, err)
require.Equal(t, `{"names":{"foo":"`+pk1.Hex()+`","bar":"`+pk2.Hex()+`"},"relays":{"`+pk1.Hex()+`":["wss://a.com"],"`+pk2.Hex()+`":["wss://a.com","wss://b.com"]}}`, string(respj))
back := WellKnownResponse{}
err = json.Unmarshal(respj, &back)
require.NoError(t, err)
require.Equal(t, resp, back)
}