nip19_test.go raw
1 package bech32encoding
2
3 import (
4 "reflect"
5 "testing"
6
7 "next.orly.dev/pkg/nostr/encoders/bech32encoding/pointers"
8 "next.orly.dev/pkg/nostr/encoders/hex"
9 "next.orly.dev/pkg/nostr/encoders/kind"
10 "next.orly.dev/pkg/nostr/utils"
11 "next.orly.dev/pkg/nostr/utils/constraints"
12 "next.orly.dev/pkg/lol/chk"
13 "next.orly.dev/pkg/lol/log"
14 )
15
16 func TestEncodeNpub(t *testing.T) {
17 npub, err := HexToNpub([]byte("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"))
18 if err != nil {
19 t.Errorf("shouldn't error: %s", err)
20 }
21 if !utils.FastEqual(
22 npub,
23 []byte("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6"),
24 ) {
25 t.Error("produced an unexpected npub string")
26 }
27 }
28
29 func TestEncodeNsec(t *testing.T) {
30 nsec, err := HexToNsec([]byte("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"))
31 if err != nil {
32 t.Errorf("shouldn't error: %s", err)
33 }
34 if !utils.FastEqual(
35 nsec,
36 []byte("nsec180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsgyumg0"),
37 ) {
38 t.Error("produced an unexpected nsec string")
39 }
40 }
41
42 func TestDecodeNpub(t *testing.T) {
43 prefix, pubkey, err := Decode([]byte("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6"))
44 if err != nil {
45 t.Errorf("shouldn't error: %s", err)
46 }
47 if !utils.FastEqual(prefix, []byte("npub")) {
48 t.Error("returned invalid prefix")
49 }
50 if !utils.FastEqual(
51 pubkey.([]byte),
52 []byte("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"),
53 ) {
54 t.Error("returned wrong pubkey")
55 }
56 }
57
58 func TestFailDecodeBadChecksumNpub(t *testing.T) {
59 _, _, err := Decode([]byte("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w4"))
60 if err == nil {
61 t.Errorf("should have errored: %s", err)
62 }
63 }
64
65 func TestDecodeNprofile(t *testing.T) {
66 prefix, data, err := Decode(
67 []byte(
68 "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p"),
69 )
70 if err != nil {
71 t.Errorf("failed to decode nprofile: %s", err.Error())
72 }
73 if !utils.FastEqual(prefix, []byte("nprofile")) {
74 t.Error("what")
75 }
76 pp, ok := data.(pointers.Profile)
77 if !ok {
78 t.Error("value returned of wrong type")
79 }
80
81 if !utils.FastEqual(
82 pp.PublicKey,
83 []byte("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"),
84 ) {
85 t.Error("decoded invalid public key")
86 }
87
88 if len(pp.Relays) != 2 {
89 t.Error("decoded wrong number of relays")
90 }
91 if !utils.FastEqual(pp.Relays[0], []byte("wss://r.x.com")) ||
92 !utils.FastEqual(pp.Relays[1], []byte("wss://djbas.sadkb.com")) {
93 t.Error("decoded relay URLs wrongly")
94 }
95 }
96
97 func TestDecodeOtherNprofile(t *testing.T) {
98 prefix, data, err := Decode([]byte("nprofile1qqsw3dy8cpumpanud9dwd3xz254y0uu2m739x0x9jf4a9sgzjshaedcpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qyw8wumn8ghj7mn0wd68yttjv4kxz7fww4h8get5dpezumt9qyvhwumn8ghj7un9d3shjetj9enxjct5dfskvtnrdakstl69hg"))
99 if err != nil {
100 t.Error("failed to decode nprofile")
101 }
102 if !utils.FastEqual(prefix, []byte("nprofile")) {
103 t.Error("what")
104 }
105 pp, ok := data.(pointers.Profile)
106 if !ok {
107 t.Error("value returned of wrong type")
108 }
109
110 if !utils.FastEqual(
111 pp.PublicKey,
112 []byte("e8b487c079b0f67c695ae6c4c2552a47f38adfa2533cc5926bd2c102942fdcb7"),
113 ) {
114 t.Error("decoded invalid public key")
115 }
116
117 if len(pp.Relays) != 3 {
118 t.Error("decoded wrong number of relays")
119 }
120 if !utils.FastEqual(
121 pp.Relays[0], []byte("wss://nostr-pub.wellorder.net"),
122 ) ||
123 !utils.FastEqual(pp.Relays[1], []byte("wss://nostr-relay.untethr.me")) {
124
125 t.Error("decoded relay URLs wrongly")
126 }
127 }
128
129 func TestEncodeNprofile(t *testing.T) {
130 nprofile, err := EncodeProfile(
131 []byte("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"),
132 [][]byte{
133 []byte("wss://r.x.com"),
134 []byte("wss://djbas.sadkb.com"),
135 },
136 )
137 if err != nil {
138 t.Errorf("shouldn't error: %s", err)
139 }
140 if !utils.FastEqual(
141 nprofile,
142 []byte("nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p"),
143 ) {
144 t.Error("produced an unexpected nprofile string")
145 }
146 }
147
148 func TestEncodeDecodeNaddr(t *testing.T) {
149 var naddr []byte
150 var err error
151 naddr, err = EncodeEntity(
152 []byte("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"),
153 kind.Article,
154 []byte("banana"),
155 [][]byte{
156 []byte("wss://relay.nostr.example.mydomain.example.com"),
157 []byte("wss://nostr.banana.com"),
158 },
159 )
160 if err != nil {
161 t.Errorf("shouldn't error: %s", err)
162 }
163 if !utils.FastEqual(
164 naddr,
165 []byte("naddr1qqrxyctwv9hxzqfwwaehxw309aex2mrp0yhxummnw3ezuetcv9khqmr99ekhjer0d4skjm3wv4uxzmtsd3jjucm0d5q3vamnwvaz7tmwdaehgu3wvfskuctwvyhxxmmdqgsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8grqsqqqa28a3lkds"),
166 ) {
167 t.Errorf("produced an unexpected naddr string: %s", naddr)
168 }
169 var prefix []byte
170 var data any
171 prefix, data, err = Decode(naddr)
172 // log.D.S(prefix, data, e)
173 if chk.D(err) {
174 t.Errorf("shouldn't error: %s", err)
175 }
176 if !utils.FastEqual(prefix, NentityHRP) {
177 t.Error("returned invalid prefix")
178 }
179 ep, ok := data.(pointers.Entity)
180 if !ok {
181 t.Fatalf("did not decode an entity type, got %v", reflect.TypeOf(data))
182 }
183 if !utils.FastEqual(
184 ep.PublicKey,
185 []byte("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"),
186 ) {
187 t.Error("returned wrong pubkey")
188 }
189 if ep.Kind.ToU16() != kind.Article.ToU16() {
190 t.Error("returned wrong kind")
191 }
192 if !utils.FastEqual(ep.Identifier, []byte("banana")) {
193 t.Error("returned wrong identifier")
194 }
195 if !utils.FastEqual(
196 ep.Relays[0],
197 []byte("wss://relay.nostr.example.mydomain.example.com"),
198 ) ||
199 !utils.FastEqual(ep.Relays[1], []byte("wss://nostr.banana.com")) {
200 t.Error("returned wrong relays")
201 }
202 }
203
204 func TestDecodeNaddrWithoutRelays(t *testing.T) {
205 prefix, data, err := Decode([]byte("naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5"))
206 if err != nil {
207 t.Errorf("shouldn't error: %s", err)
208 }
209 if !utils.FastEqual(prefix, []byte("naddr")) {
210 t.Error("returned invalid prefix")
211 }
212 ep := data.(pointers.Entity)
213 if !utils.FastEqual(
214 ep.PublicKey,
215 []byte("7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194"),
216 ) {
217 t.Error("returned wrong pubkey")
218 }
219 if ep.Kind.ToU16() != kind.Article.ToU16() {
220 t.Error("returned wrong kind")
221 }
222 if !utils.FastEqual(ep.Identifier, []byte("references")) {
223 t.Error("returned wrong identifier")
224 }
225 if len(ep.Relays) != 0 {
226 t.Error("relays should have been an empty array")
227 }
228 }
229
230 func TestEncodeDecodeNEventTestEncodeDecodeNEvent(t *testing.T) {
231 aut := []byte("7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751abb88")
232 eid := []byte("45326f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194")
233 nevent, err := EncodeEvent(
234 MustDecode(eid),
235 [][]byte{[]byte("wss://banana.com")}, aut,
236 )
237 if err != nil {
238 t.Errorf("shouldn't error: %s", err.Error())
239 }
240
241 prefix, res, err := Decode(nevent)
242 if err != nil {
243 t.Errorf("shouldn't error: %s", err)
244 }
245
246 if !utils.FastEqual(prefix, []byte("nevent")) {
247 t.Errorf("should have 'nevent' prefix, not '%s'", prefix)
248 }
249 ep, ok := res.(pointers.Event)
250 if !ok {
251 t.Errorf("'%s' should be an nevent, not %v", nevent, res)
252 }
253
254 if !utils.FastEqual(ep.Author, aut) {
255 t.Errorf("wrong author got\n%s, expect\n%s", ep.Author, aut)
256 }
257 id := MustDecode("45326f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194")
258 if !utils.FastEqual(hex.Enc(ep.ID), id) {
259 log.I.S(ep.ID, id)
260 t.Error("wrong id")
261 }
262
263 if len(ep.Relays) != 1 ||
264 !utils.FastEqual(ep.Relays[0], []byte("wss://banana.com")) {
265 t.Error("wrong relay")
266 }
267 }
268
269 func MustDecode[V constraints.Bytes](s V) (b []byte) {
270 var err error
271 if _, err = hex.Dec(string(s)); chk.E(err) {
272 panic(err)
273 }
274 b = []byte(s)
275 return
276 }
277