ns-auth-sdk 1.2.6 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,1399 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+ const require_utils = require('./utils-BUM1trwg.cjs');
29
+ let react = require("react");
30
+ let react_jsx_runtime = require("react/jsx-runtime");
31
+ let isomorphic_dompurify = require("isomorphic-dompurify");
32
+ isomorphic_dompurify = __toESM(isomorphic_dompurify);
33
+ let react_zxing = require("react-zxing");
34
+ let zustand = require("zustand");
35
+
36
+ //#region src/services/auth.service.ts
37
+ /**
38
+ * Service wrapper around NosskeyManager
39
+ * Handles WebAuthn/Passkey integration with Nostr
40
+ */
41
+ var AuthService = class {
42
+ constructor(config = {}) {
43
+ this.manager = null;
44
+ this.config = {
45
+ rpId: config.rpId || (typeof window !== "undefined" ? window.location.hostname.replace(/^www\./, "") : "localhost"),
46
+ rpName: config.rpName || this.getDefaultRpName(),
47
+ storageKey: config.storageKey || "nsauth_keyinfo",
48
+ cacheTimeoutMs: config.cacheTimeoutMs || 1800 * 1e3,
49
+ cacheOnCreation: config.cacheOnCreation !== void 0 ? config.cacheOnCreation : true
50
+ };
51
+ }
52
+ getDefaultRpName() {
53
+ if (typeof window === "undefined") return "localhost";
54
+ const hostname = window.location.hostname;
55
+ if (hostname.includes("nosskey.app")) return "nosskey.app";
56
+ return hostname.replace(/^www\./, "");
57
+ }
58
+ /**
59
+ * Initialize the NosskeyManager instance
60
+ */
61
+ getManager() {
62
+ if (!this.manager) this.manager = new require_utils.NosskeyManager({
63
+ cacheOptions: {
64
+ enabled: true,
65
+ timeoutMs: this.config.cacheTimeoutMs,
66
+ cacheOnCreation: this.config.cacheOnCreation
67
+ },
68
+ storageOptions: {
69
+ enabled: true,
70
+ storageKey: this.config.storageKey
71
+ }
72
+ });
73
+ return this.manager;
74
+ }
75
+ /**
76
+ * Create a new passkey
77
+ * Uses platform authenticator only (Touch ID, Face ID, Windows Hello)
78
+ */
79
+ async createPasskey(username) {
80
+ const manager = this.getManager();
81
+ const rpId = this.config.rpId === "localhost" ? "localhost" : this.config.rpId;
82
+ const rpName = this.config.rpName;
83
+ const trimmedUsername = username?.trim();
84
+ const uniqueUsername = trimmedUsername ? trimmedUsername : `user-${Date.now()}@example.com`;
85
+ const options = {
86
+ rp: {
87
+ id: rpId,
88
+ name: rpName
89
+ },
90
+ user: {
91
+ name: uniqueUsername,
92
+ displayName: trimmedUsername || "User"
93
+ },
94
+ authenticatorSelection: {
95
+ authenticatorAttachment: "platform",
96
+ residentKey: "preferred",
97
+ userVerification: "preferred"
98
+ },
99
+ extensions: { prf: {} }
100
+ };
101
+ return await manager.createPasskey(options);
102
+ }
103
+ /**
104
+ * Create a new Nostr key from a credential ID
105
+ */
106
+ async createNostrKey(credentialId) {
107
+ return await this.getManager().createNostrKey(credentialId);
108
+ }
109
+ /**
110
+ * Get the current public key
111
+ */
112
+ async getPublicKey() {
113
+ return await this.getManager().getPublicKey();
114
+ }
115
+ /**
116
+ * Sign a Nostr event
117
+ */
118
+ async signEvent(event) {
119
+ return await this.getManager().signEvent(event);
120
+ }
121
+ /**
122
+ * Get current key info
123
+ */
124
+ getCurrentKeyInfo() {
125
+ return this.getManager().getCurrentKeyInfo();
126
+ }
127
+ /**
128
+ * Set current key info
129
+ */
130
+ setCurrentKeyInfo(keyInfo) {
131
+ this.getManager().setCurrentKeyInfo(keyInfo);
132
+ }
133
+ /**
134
+ * Check if key info exists
135
+ */
136
+ hasKeyInfo() {
137
+ return this.getManager().hasKeyInfo();
138
+ }
139
+ /**
140
+ * Clear stored key info
141
+ */
142
+ clearStoredKeyInfo() {
143
+ this.getManager().clearStoredKeyInfo();
144
+ }
145
+ /**
146
+ * Check if PRF is supported
147
+ */
148
+ async isPrfSupported() {
149
+ const { isPrfSupported } = await Promise.resolve().then(() => require("./utils-BV-7ST9t.cjs"));
150
+ return await isPrfSupported();
151
+ }
152
+ };
153
+
154
+ //#endregion
155
+ //#region src/utils/validation.ts
156
+ const MAX_ROLE_LENGTH = 100;
157
+ const ROLE_PATTERN = /^[a-zA-Z0-9\s\-_]+$/;
158
+ const isValidRoleTag = (role) => {
159
+ const trimmed = role.trim();
160
+ return trimmed.length > 0 && trimmed.length <= MAX_ROLE_LENGTH && ROLE_PATTERN.test(trimmed);
161
+ };
162
+ const isValidPubkey = (pubkey) => pubkey.length === 64 && /^[0-9a-fA-F]+$/.test(pubkey);
163
+ const isValidHttpUrl = (url) => {
164
+ try {
165
+ const parsed = new URL(url);
166
+ return ["http:", "https:"].includes(parsed.protocol);
167
+ } catch {
168
+ return false;
169
+ }
170
+ };
171
+ const isValidRelayUrl = (url) => {
172
+ try {
173
+ const parsed = new URL(url);
174
+ return ["ws:", "wss:"].includes(parsed.protocol) && parsed.hostname.length > 0;
175
+ } catch {
176
+ return false;
177
+ }
178
+ };
179
+
180
+ //#endregion
181
+ //#region src/services/relay.service.ts
182
+ /**
183
+ * Service for communicating with Nostr relays using applesauce-core
184
+ */
185
+ var RelayService = class {
186
+ constructor(config = {}) {
187
+ this.eventStore = null;
188
+ this.defaultRelays = ["wss://relay.damus.io"];
189
+ this.maxProfileContentSize = 1e4;
190
+ this.minProfileQueryIntervalMs = 300;
191
+ this.minPublishIntervalMs = 750;
192
+ this.lastActionAt = /* @__PURE__ */ new Map();
193
+ this.relayUrls = this.validateRelayUrls(config.relayUrls ?? this.defaultRelays);
194
+ }
195
+ /**
196
+ * Initialize with applesauce EventStore
197
+ */
198
+ initialize(eventStore) {
199
+ this.eventStore = eventStore;
200
+ if (eventStore && "setRelays" in eventStore && typeof eventStore.setRelays === "function") eventStore.setRelays(this.relayUrls);
201
+ }
202
+ /**
203
+ * Set relay URLs
204
+ */
205
+ setRelays(urls) {
206
+ this.relayUrls = this.validateRelayUrls(urls);
207
+ if (this.eventStore && "setRelays" in this.eventStore && typeof this.eventStore.setRelays === "function") this.eventStore.setRelays(this.relayUrls);
208
+ }
209
+ /**
210
+ * Get current relay URLs
211
+ */
212
+ getRelays() {
213
+ return [...this.relayUrls];
214
+ }
215
+ /**
216
+ * Publish an event to relays
217
+ */
218
+ async publishEvent(event, timeoutMs = 1e3) {
219
+ if (!this.eventStore) throw new Error("RelayService not initialized. Call initialize() with an EventStore instance.");
220
+ await this.enforceRateLimit("publish", this.minPublishIntervalMs);
221
+ return new Promise((resolve, reject) => {
222
+ if (this.relayUrls.length === 0) {
223
+ reject(/* @__PURE__ */ new Error("No relays configured"));
224
+ return;
225
+ }
226
+ const eventStore = this.eventStore;
227
+ if (!eventStore || typeof eventStore.publish !== "function") {
228
+ reject(/* @__PURE__ */ new Error("EventStore does not support publish method"));
229
+ return;
230
+ }
231
+ const subscription = eventStore.publish(event).subscribe({
232
+ next: (response) => {
233
+ if (response?.type === "OK") {
234
+ subscription.unsubscribe();
235
+ resolve(true);
236
+ }
237
+ },
238
+ error: (error) => {
239
+ subscription.unsubscribe();
240
+ reject(error);
241
+ }
242
+ });
243
+ setTimeout(() => {
244
+ subscription.unsubscribe();
245
+ resolve(false);
246
+ }, timeoutMs);
247
+ });
248
+ }
249
+ /**
250
+ * Fetch a profile (Kind 0 event)
251
+ */
252
+ async fetchProfile(pubkey) {
253
+ if (!this.eventStore) throw new Error("RelayService not initialized. Call initialize() with an EventStore instance.");
254
+ await this.enforceRateLimit("fetch-profile", this.minProfileQueryIntervalMs);
255
+ return new Promise((resolve) => {
256
+ const filter = {
257
+ kinds: [0],
258
+ authors: [pubkey],
259
+ limit: 1
260
+ };
261
+ const eventStore = this.eventStore;
262
+ if (!eventStore || typeof eventStore.query !== "function") {
263
+ resolve(null);
264
+ return;
265
+ }
266
+ const subscription = eventStore.query(filter).subscribe({
267
+ next: (packet) => {
268
+ if (packet?.event && packet.event.kind === 0) {
269
+ const metadata = this.parseProfileMetadata(packet.event.content);
270
+ if (metadata) {
271
+ subscription.unsubscribe();
272
+ resolve(metadata);
273
+ return;
274
+ }
275
+ console.error("Failed to parse profile metadata");
276
+ }
277
+ },
278
+ complete: () => {
279
+ subscription.unsubscribe();
280
+ resolve(null);
281
+ },
282
+ error: (error) => {
283
+ console.error("Error fetching profile:", error);
284
+ subscription.unsubscribe();
285
+ resolve(null);
286
+ }
287
+ });
288
+ setTimeout(() => {
289
+ subscription.unsubscribe();
290
+ resolve(null);
291
+ }, 5e3);
292
+ });
293
+ }
294
+ /**
295
+ * Fetch role tag from profile event (Kind 0)
296
+ */
297
+ async fetchProfileRoleTag(pubkey) {
298
+ if (!this.eventStore) throw new Error("RelayService not initialized. Call initialize() with an EventStore instance.");
299
+ await this.enforceRateLimit("fetch-role-tag", this.minProfileQueryIntervalMs);
300
+ return new Promise((resolve) => {
301
+ const filter = {
302
+ kinds: [0],
303
+ authors: [pubkey],
304
+ limit: 1
305
+ };
306
+ const eventStore = this.eventStore;
307
+ if (!eventStore || typeof eventStore.query !== "function") {
308
+ resolve(null);
309
+ return;
310
+ }
311
+ const subscription = eventStore.query(filter).subscribe({
312
+ next: (packet) => {
313
+ if (packet?.event && packet.event.kind === 0) {
314
+ const tags = packet.event.tags || [];
315
+ for (const tag of tags) if (tag[0] === "role" && tag[1]) {
316
+ const candidate = tag[1].trim();
317
+ if (isValidRoleTag(candidate)) {
318
+ subscription.unsubscribe();
319
+ resolve(candidate);
320
+ return;
321
+ }
322
+ }
323
+ subscription.unsubscribe();
324
+ resolve(null);
325
+ }
326
+ },
327
+ complete: () => {
328
+ subscription.unsubscribe();
329
+ resolve(null);
330
+ },
331
+ error: (error) => {
332
+ console.error("Error fetching profile role tag:", error);
333
+ subscription.unsubscribe();
334
+ resolve(null);
335
+ }
336
+ });
337
+ setTimeout(() => {
338
+ subscription.unsubscribe();
339
+ resolve(null);
340
+ }, 5e3);
341
+ });
342
+ }
343
+ /**
344
+ * Fetch a follow list (Kind 3 event)
345
+ */
346
+ async fetchFollowList(pubkey) {
347
+ if (!this.eventStore) throw new Error("RelayService not initialized. Call initialize() with an EventStore instance.");
348
+ await this.enforceRateLimit("fetch-follow-list", this.minProfileQueryIntervalMs);
349
+ return new Promise((resolve) => {
350
+ const filter = {
351
+ kinds: [3],
352
+ authors: [pubkey],
353
+ limit: 1
354
+ };
355
+ const eventStore = this.eventStore;
356
+ if (!eventStore || typeof eventStore.query !== "function") {
357
+ resolve([]);
358
+ return;
359
+ }
360
+ const subscription = eventStore.query(filter).subscribe({
361
+ next: (packet) => {
362
+ if (packet?.event && packet.event.kind === 3) {
363
+ const followList = [];
364
+ const tags = packet.event.tags || [];
365
+ for (const tag of tags) if (tag[0] === "p" && tag[1]) followList.push({
366
+ pubkey: tag[1],
367
+ relay: tag[2] || void 0,
368
+ petname: tag[3] || void 0
369
+ });
370
+ subscription.unsubscribe();
371
+ resolve(followList);
372
+ }
373
+ },
374
+ complete: () => {
375
+ subscription.unsubscribe();
376
+ resolve([]);
377
+ },
378
+ error: (error) => {
379
+ console.error("Error fetching follow list:", error);
380
+ subscription.unsubscribe();
381
+ resolve([]);
382
+ }
383
+ });
384
+ setTimeout(() => {
385
+ subscription.unsubscribe();
386
+ resolve([]);
387
+ }, 1e4);
388
+ });
389
+ }
390
+ /**
391
+ * Fetch multiple profiles in batch
392
+ */
393
+ async fetchMultipleProfiles(pubkeys) {
394
+ if (!this.eventStore) throw new Error("RelayService not initialized. Call initialize() with an EventStore instance.");
395
+ if (pubkeys.length === 0) return /* @__PURE__ */ new Map();
396
+ await this.enforceRateLimit("fetch-multiple-profiles", this.minProfileQueryIntervalMs);
397
+ return new Promise((resolve) => {
398
+ const profiles = /* @__PURE__ */ new Map();
399
+ const filter = {
400
+ kinds: [0],
401
+ authors: pubkeys
402
+ };
403
+ const eventStore = this.eventStore;
404
+ if (!eventStore || typeof eventStore.query !== "function") {
405
+ resolve(/* @__PURE__ */ new Map());
406
+ return;
407
+ }
408
+ const subscription = eventStore.query(filter).subscribe({
409
+ next: (packet) => {
410
+ if (packet?.event && packet.event.kind === 0 && packet.event.pubkey) {
411
+ const metadata = this.parseProfileMetadata(packet.event.content);
412
+ if (metadata) profiles.set(packet.event.pubkey, metadata);
413
+ else console.error("Failed to parse profile metadata");
414
+ }
415
+ },
416
+ complete: () => {
417
+ subscription.unsubscribe();
418
+ resolve(profiles);
419
+ },
420
+ error: (error) => {
421
+ console.error("Error fetching profiles:", error);
422
+ subscription.unsubscribe();
423
+ resolve(profiles);
424
+ }
425
+ });
426
+ setTimeout(() => {
427
+ subscription.unsubscribe();
428
+ resolve(profiles);
429
+ }, 1e3);
430
+ });
431
+ }
432
+ /**
433
+ * Query kind 0 events (profiles) by pubkey
434
+ * If pubkeys array is empty, fetches recent kind 0 events
435
+ */
436
+ async queryProfiles(pubkeys = [], limit = 100) {
437
+ if (!this.eventStore) throw new Error("RelayService not initialized. Call initialize() with an EventStore instance.");
438
+ await this.enforceRateLimit("query-profiles", this.minProfileQueryIntervalMs);
439
+ return new Promise((resolve) => {
440
+ const profiles = /* @__PURE__ */ new Map();
441
+ const filter = {
442
+ kinds: [0],
443
+ limit
444
+ };
445
+ if (pubkeys.length > 0) filter.authors = pubkeys;
446
+ const eventStore = this.eventStore;
447
+ if (!eventStore || typeof eventStore.query !== "function") {
448
+ resolve(/* @__PURE__ */ new Map());
449
+ return;
450
+ }
451
+ const subscription = eventStore.query(filter).subscribe({
452
+ next: (packet) => {
453
+ if (packet?.event && packet.event.kind === 0 && packet.event.pubkey) {
454
+ const metadata = this.parseProfileMetadata(packet.event.content);
455
+ if (metadata) {
456
+ const timestamp = packet.event.created_at || 0;
457
+ const existing = profiles.get(packet.event.pubkey);
458
+ if (!existing || timestamp > existing.timestamp) profiles.set(packet.event.pubkey, {
459
+ metadata,
460
+ timestamp
461
+ });
462
+ } else console.error("Failed to parse profile metadata");
463
+ }
464
+ },
465
+ complete: () => {
466
+ subscription.unsubscribe();
467
+ const result = /* @__PURE__ */ new Map();
468
+ profiles.forEach((value, pubkey) => {
469
+ result.set(pubkey, value.metadata);
470
+ });
471
+ resolve(result);
472
+ },
473
+ error: (error) => {
474
+ console.error("Error querying profiles:", error);
475
+ subscription.unsubscribe();
476
+ const result = /* @__PURE__ */ new Map();
477
+ profiles.forEach((value, pubkey) => {
478
+ result.set(pubkey, value.metadata);
479
+ });
480
+ resolve(result);
481
+ }
482
+ });
483
+ setTimeout(() => {
484
+ subscription.unsubscribe();
485
+ const result = /* @__PURE__ */ new Map();
486
+ profiles.forEach((value, pubkey) => {
487
+ result.set(pubkey, value.metadata);
488
+ });
489
+ resolve(result);
490
+ }, 1e4);
491
+ });
492
+ }
493
+ /**
494
+ * Publish or update a kind 3 event (follow list/contacts)
495
+ */
496
+ async publishFollowList(pubkey, followList, signEvent) {
497
+ const tags = followList.map((entry) => {
498
+ if (!isValidPubkey(entry.pubkey)) throw new Error("Invalid pubkey format for follow list entry.");
499
+ if (entry.relay && !isValidRelayUrl(entry.relay)) throw new Error("Invalid relay URL format for follow list entry.");
500
+ const tag = ["p", entry.pubkey];
501
+ if (entry.relay) tag.push(entry.relay);
502
+ if (entry.petname) tag.push(entry.petname);
503
+ return tag;
504
+ });
505
+ const signedEvent = await signEvent({
506
+ kind: 3,
507
+ content: "",
508
+ created_at: Math.floor(Date.now() / 1e3),
509
+ tags
510
+ });
511
+ return await this.publishEvent(signedEvent);
512
+ }
513
+ validateRelayUrls(urls) {
514
+ if (urls.length === 0) return [];
515
+ const validUrls = urls.filter((url) => isValidRelayUrl(url));
516
+ if (validUrls.length !== urls.length) throw new Error("Invalid relay URL format");
517
+ return validUrls;
518
+ }
519
+ parseProfileMetadata(content) {
520
+ if (content.length > this.maxProfileContentSize) return null;
521
+ try {
522
+ const parsed = JSON.parse(content);
523
+ if (!parsed || typeof parsed !== "object") return null;
524
+ const metadata = {};
525
+ if (typeof parsed.name === "string") metadata.name = parsed.name;
526
+ if (typeof parsed.display_name === "string") metadata.display_name = parsed.display_name;
527
+ if (typeof parsed.about === "string") metadata.about = parsed.about;
528
+ if (typeof parsed.picture === "string") metadata.picture = parsed.picture;
529
+ if (typeof parsed.website === "string") metadata.website = parsed.website;
530
+ return metadata;
531
+ } catch (error) {
532
+ console.error("Failed to parse profile metadata:", error);
533
+ return null;
534
+ }
535
+ }
536
+ async enforceRateLimit(action, minIntervalMs) {
537
+ const lastAt = this.lastActionAt.get(action) ?? 0;
538
+ const waitMs = minIntervalMs - (Date.now() - lastAt);
539
+ if (waitMs > 0) await new Promise((resolve) => setTimeout(resolve, waitMs));
540
+ this.lastActionAt.set(action, Date.now());
541
+ }
542
+ };
543
+
544
+ //#endregion
545
+ //#region src/components/auth/LoginButton.tsx
546
+ function LoginButton({ authService, setAuthenticated, setLoginError, onSuccess }) {
547
+ const [isLoading, setIsLoading] = (0, react.useState)(false);
548
+ const handleLogin = async () => {
549
+ setIsLoading(true);
550
+ setLoginError(null);
551
+ try {
552
+ if (!authService.hasKeyInfo()) throw new Error("No account found. Please register first.");
553
+ const keyInfo = authService.getCurrentKeyInfo();
554
+ if (!keyInfo) throw new Error("Failed to load account information.");
555
+ await authService.getPublicKey();
556
+ setAuthenticated(keyInfo);
557
+ if (onSuccess) onSuccess();
558
+ } catch (err) {
559
+ console.error("Login error:", err);
560
+ setLoginError(err instanceof Error ? err.message : "Failed to login");
561
+ setIsLoading(false);
562
+ }
563
+ };
564
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
565
+ className: "login-button-container",
566
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
567
+ className: "auth-button secondary",
568
+ onClick: handleLogin,
569
+ disabled: isLoading,
570
+ children: isLoading ? "Logging in..." : "Login"
571
+ })
572
+ });
573
+ }
574
+
575
+ //#endregion
576
+ //#region src/components/auth/RegistrationFlow.tsx
577
+ function RegistrationFlow({ authService, setAuthenticated, onSuccess }) {
578
+ const [isLoading, setIsLoading] = (0, react.useState)(false);
579
+ const [error, setError] = (0, react.useState)(null);
580
+ const [step, setStep] = (0, react.useState)("info");
581
+ const [username, setUsername] = (0, react.useState)("");
582
+ const maxUsernameLength = 100;
583
+ const handleRegister = async () => {
584
+ setIsLoading(true);
585
+ setError(null);
586
+ setStep("creating");
587
+ try {
588
+ authService.clearStoredKeyInfo();
589
+ let credentialId;
590
+ const passkeyUsername = username.trim() || void 0;
591
+ try {
592
+ credentialId = await authService.createPasskey(passkeyUsername);
593
+ } catch (passkeyError) {
594
+ console.error("[RegistrationFlow] Passkey creation failed:", passkeyError);
595
+ throw new Error("Unable to create passkey. Please try again.");
596
+ }
597
+ let keyInfo;
598
+ try {
599
+ keyInfo = await authService.createNostrKey(credentialId);
600
+ } catch (nostrKeyError) {
601
+ console.error("[RegistrationFlow] Failed to create Nostr key from passkey:", nostrKeyError);
602
+ throw new Error("Unable to finalize account. Please try again.");
603
+ }
604
+ authService.setCurrentKeyInfo(keyInfo);
605
+ setAuthenticated(keyInfo);
606
+ setStep("success");
607
+ if (onSuccess) setTimeout(() => {
608
+ onSuccess();
609
+ }, 1500);
610
+ } catch (err) {
611
+ console.error("[RegistrationFlow] Registration error:", err);
612
+ setError("Registration failed. Please try again.");
613
+ setStep("info");
614
+ setIsLoading(false);
615
+ }
616
+ };
617
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
618
+ className: "auth-container",
619
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
620
+ className: "auth-card",
621
+ children: [
622
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h1", { children: "Create Account" }),
623
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
624
+ className: "auth-description",
625
+ children: "Create a new decentralized Identity. Your identity will be securely derived from a passkey."
626
+ }),
627
+ step === "info" && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
628
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
629
+ className: "auth-features",
630
+ children: [
631
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
632
+ className: "feature-item",
633
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
634
+ className: "feature-icon",
635
+ children: "🔐"
636
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: "Phishing-resistant authentication" })]
637
+ }),
638
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
639
+ className: "feature-item",
640
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
641
+ className: "feature-icon",
642
+ children: "📱"
643
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: "Biometric authentication support" })]
644
+ }),
645
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
646
+ className: "feature-item",
647
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
648
+ className: "feature-icon",
649
+ children: "🌐"
650
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: "Cross-device synchronization" })]
651
+ })
652
+ ]
653
+ }),
654
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
655
+ className: "username-section",
656
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("label", {
657
+ htmlFor: "username",
658
+ className: "username-label",
659
+ children: "Name (Optional)"
660
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
661
+ id: "username",
662
+ type: "text",
663
+ className: "username-input",
664
+ placeholder: "Enter a name for this passkey",
665
+ value: username,
666
+ onChange: (e) => setUsername(e.target.value),
667
+ disabled: isLoading,
668
+ maxLength: maxUsernameLength
669
+ })]
670
+ }),
671
+ error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
672
+ className: "error-message",
673
+ children: error
674
+ }),
675
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
676
+ className: "auth-button primary",
677
+ onClick: handleRegister,
678
+ disabled: isLoading,
679
+ children: isLoading ? "Creating..." : "Create Account"
680
+ })
681
+ ] }),
682
+ step === "creating" && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
683
+ className: "loading-state",
684
+ children: [
685
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "spinner" }),
686
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "Creating your passkey..." }),
687
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
688
+ className: "loading-hint",
689
+ children: "Please follow your browser's authentication prompt"
690
+ }),
691
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
692
+ className: "loading-hint-small",
693
+ children: "💡 Using your system's native passkey manager (Touch ID, Face ID, Windows Hello, etc.)"
694
+ })
695
+ ]
696
+ }),
697
+ step === "success" && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
698
+ className: "success-state",
699
+ children: [
700
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
701
+ className: "success-icon",
702
+ children: "✓"
703
+ }),
704
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "Account created successfully!" }),
705
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
706
+ className: "success-hint",
707
+ children: "Redirecting to profile setup..."
708
+ })
709
+ ]
710
+ })
711
+ ]
712
+ })
713
+ });
714
+ }
715
+
716
+ //#endregion
717
+ //#region src/utils/sanitize.ts
718
+ const sanitizeOptions = {
719
+ ALLOWED_TAGS: [],
720
+ ALLOWED_ATTR: []
721
+ };
722
+ const sanitizeText = (value) => isomorphic_dompurify.default.sanitize(value, sanitizeOptions);
723
+
724
+ //#endregion
725
+ //#region src/components/membership/BarcodeScanner.tsx
726
+ const BarcodeScanner = ({ onDecode, active = true }) => {
727
+ const [error, setError] = (0, react.useState)(null);
728
+ const { ref } = (0, react_zxing.useZxing)({
729
+ onDecodeResult: (result) => {
730
+ onDecode(result.getText());
731
+ },
732
+ onError: (e) => {
733
+ setError(e instanceof Error ? e.message : "Camera error");
734
+ }
735
+ });
736
+ if (!active) return null;
737
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
738
+ style: {
739
+ position: "relative",
740
+ width: "100%",
741
+ maxWidth: "400px"
742
+ },
743
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("video", {
744
+ ref,
745
+ style: {
746
+ width: "100%",
747
+ borderRadius: "8px"
748
+ },
749
+ playsInline: true,
750
+ muted: true
751
+ }), error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
752
+ style: {
753
+ color: "red",
754
+ marginTop: "0.5rem"
755
+ },
756
+ children: error
757
+ })]
758
+ });
759
+ };
760
+
761
+ //#endregion
762
+ //#region src/components/membership/MembershipPage.tsx
763
+ const TrustInfo = () => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("section", {
764
+ className: "trust-info",
765
+ children: [
766
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", { children: "Building Trust with Verifiable Relationship Credentials" }),
767
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", { children: [
768
+ "Beyond the Personhood Credential, members can create ",
769
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("strong", { children: "Verifiable Relationship Credentials (VRCs)" }),
770
+ ". A VRC is issued directly between two members – for example, by scanning a QR‑code at a meetup – and certifies a first‑hand trust link."
771
+ ] }),
772
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", { children: [
773
+ "Each VRC becomes a node in a decentralized trust graph. When you add a member, the system records a ",
774
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("strong", { children: "role" }),
775
+ " tag that ties the new participant to your existing graph, enabling permissionless access to network‑state resources while keeping the underlying data private."
776
+ ] }),
777
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "The graph grows organically: trusted authorities issue PHCs, and members continuously enrich the network with peer‑generated VRCs, creating a resilient, scalable web of verified participants." })
778
+ ]
779
+ });
780
+ function MembershipPage({ authService, relayService, publicKey, onUnauthenticated }) {
781
+ const [isLoading, setIsLoading] = (0, react.useState)(false);
782
+ const [isSaving, setIsSaving] = (0, react.useState)(false);
783
+ const [saveMessage, setSaveMessage] = (0, react.useState)(null);
784
+ const [searchQuery, setSearchQuery] = (0, react.useState)("");
785
+ const [profiles, setProfiles] = (0, react.useState)([]);
786
+ const [members, setMembers] = (0, react.useState)([]);
787
+ const [memberProfiles, setMemberProfiles] = (0, react.useState)(/* @__PURE__ */ new Map());
788
+ const [showScanner, setShowScanner] = (0, react.useState)(false);
789
+ (0, react.useEffect)(() => {
790
+ if (!publicKey) {
791
+ if (onUnauthenticated) onUnauthenticated();
792
+ return;
793
+ }
794
+ loadFollowList();
795
+ }, [publicKey]);
796
+ (0, react.useEffect)(() => {
797
+ if (members.length > 0) loadMemberProfiles();
798
+ }, [members]);
799
+ const handleDecoded = (decoded) => {
800
+ setSearchQuery(decoded);
801
+ setShowScanner(false);
802
+ handleSearch();
803
+ };
804
+ const loadFollowList = async () => {
805
+ if (!publicKey) return;
806
+ setIsLoading(true);
807
+ try {
808
+ setMembers(await relayService.fetchFollowList(publicKey));
809
+ } catch (error) {
810
+ console.error("Failed to load follow list:", error);
811
+ setSaveMessage("Failed to load membership list");
812
+ } finally {
813
+ setIsLoading(false);
814
+ }
815
+ };
816
+ const loadMemberProfiles = async () => {
817
+ if (members.length === 0) return;
818
+ try {
819
+ const pubkeys = members.map((m) => m.pubkey);
820
+ setMemberProfiles(await relayService.fetchMultipleProfiles(pubkeys));
821
+ } catch (error) {
822
+ console.error("Failed to load member profiles:", error);
823
+ }
824
+ };
825
+ const handleSearch = async () => {
826
+ if (!searchQuery.trim()) {
827
+ setIsLoading(true);
828
+ try {
829
+ const profilesMap = await relayService.queryProfiles([], 50);
830
+ const profilesList = [];
831
+ profilesMap.forEach((profile, pubkey) => {
832
+ profilesList.push({
833
+ ...profile,
834
+ pubkey
835
+ });
836
+ });
837
+ setProfiles(profilesList);
838
+ } catch (error) {
839
+ console.error("Failed to query profiles:", error);
840
+ setSaveMessage("Failed to search profiles");
841
+ } finally {
842
+ setIsLoading(false);
843
+ }
844
+ return;
845
+ }
846
+ const trimmedQuery = searchQuery.trim();
847
+ if (trimmedQuery.length !== 64 || !/^[0-9a-fA-F]+$/.test(trimmedQuery)) {
848
+ setSaveMessage("Invalid pubkey format. Must be 64 hex characters.");
849
+ setTimeout(() => setSaveMessage(null), 3e3);
850
+ return;
851
+ }
852
+ setIsLoading(true);
853
+ try {
854
+ const profilesMap = await relayService.queryProfiles([trimmedQuery], 1);
855
+ const profilesList = [];
856
+ profilesMap.forEach((profile, pubkey) => {
857
+ profilesList.push({
858
+ ...profile,
859
+ pubkey
860
+ });
861
+ });
862
+ setProfiles(profilesList);
863
+ if (profilesList.length === 0) {
864
+ setSaveMessage("No profile found for this pubkey");
865
+ setTimeout(() => setSaveMessage(null), 3e3);
866
+ }
867
+ } catch (error) {
868
+ console.error("Failed to query profile:", error);
869
+ setSaveMessage("Failed to search profile");
870
+ } finally {
871
+ setIsLoading(false);
872
+ }
873
+ };
874
+ const handleAddMember = async (pubkey) => {
875
+ if (!publicKey) return;
876
+ if (members.some((m) => m.pubkey === pubkey)) {
877
+ setSaveMessage("User is already a member");
878
+ setTimeout(() => setSaveMessage(null), 3e3);
879
+ return;
880
+ }
881
+ setIsSaving(true);
882
+ try {
883
+ const newMembers = [...members, { pubkey }];
884
+ await relayService.publishFollowList(publicKey, newMembers, (event) => authService.signEvent(event));
885
+ setMembers(newMembers);
886
+ setSaveMessage("Member added successfully!");
887
+ setTimeout(() => setSaveMessage(null), 3e3);
888
+ } catch (error) {
889
+ console.error("Failed to add member:", error);
890
+ setSaveMessage("Failed to add member. Please try again.");
891
+ } finally {
892
+ setIsSaving(false);
893
+ }
894
+ };
895
+ const handleRemoveMember = async (pubkey) => {
896
+ if (!publicKey) return;
897
+ setIsSaving(true);
898
+ try {
899
+ const newMembers = members.filter((m) => m.pubkey !== pubkey);
900
+ await relayService.publishFollowList(publicKey, newMembers, (event) => authService.signEvent(event));
901
+ setMembers(newMembers);
902
+ setSaveMessage("Member removed successfully!");
903
+ setTimeout(() => setSaveMessage(null), 3e3);
904
+ } catch (error) {
905
+ console.error("Failed to remove member:", error);
906
+ setSaveMessage("Failed to remove member. Please try again.");
907
+ } finally {
908
+ setIsSaving(false);
909
+ }
910
+ };
911
+ const getProfileDisplayName = (profile, pubkey) => {
912
+ return profile.display_name || profile.name || pubkey.slice(0, 16) + "...";
913
+ };
914
+ const formatPubkey = (pubkey) => {
915
+ return `${pubkey.slice(0, 8)}...${pubkey.slice(-8)}`;
916
+ };
917
+ if (isLoading && members.length === 0) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
918
+ className: "membership-container",
919
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
920
+ className: "loading-state",
921
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "spinner" }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "Loading membership list..." })]
922
+ })
923
+ });
924
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
925
+ className: "membership-container",
926
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
927
+ className: "membership-card",
928
+ children: [
929
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h1", { children: "Membership Management" }),
930
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TrustInfo, {}),
931
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
932
+ className: "membership-description",
933
+ children: "Query applicants and manage the membership list."
934
+ }),
935
+ saveMessage && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
936
+ className: `save-message ${saveMessage.includes("Error") || saveMessage.includes("Failed") ? "error" : "success"}`,
937
+ children: saveMessage
938
+ }),
939
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
940
+ className: "search-section",
941
+ children: [
942
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", { children: "Add Member" }),
943
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
944
+ className: "search-form",
945
+ children: [
946
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
947
+ type: "text",
948
+ value: searchQuery,
949
+ onChange: (e) => setSearchQuery(e.target.value),
950
+ onKeyPress: (e) => e.key === "Enter" && handleSearch(),
951
+ placeholder: "Enter pubkey or leave empty for recent profiles",
952
+ className: "search-input",
953
+ disabled: isLoading
954
+ }),
955
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
956
+ onClick: handleSearch,
957
+ className: "search-button",
958
+ disabled: isLoading || isSaving,
959
+ children: isLoading ? "Searching..." : "Search"
960
+ }),
961
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
962
+ onClick: () => setShowScanner((prev) => !prev),
963
+ className: "scanner-toggle",
964
+ disabled: isLoading || isSaving,
965
+ children: showScanner ? "Close Scanner" : "Scan QR"
966
+ })
967
+ ]
968
+ }),
969
+ showScanner && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
970
+ style: { marginTop: "1rem" },
971
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(BarcodeScanner, {
972
+ onDecode: handleDecoded,
973
+ active: true
974
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
975
+ style: {
976
+ fontSize: "0.85rem",
977
+ marginTop: "0.5rem"
978
+ },
979
+ children: "Point at QR‑code containing a pubkey"
980
+ })]
981
+ }),
982
+ profiles.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
983
+ className: "profiles-list",
984
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h3", { children: "Search Results" }), profiles.map((profile) => {
985
+ const isMember = members.some((m) => m.pubkey === profile.pubkey);
986
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
987
+ className: "profile-item",
988
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
989
+ className: "profile-info",
990
+ children: [profile.picture && isValidHttpUrl(profile.picture) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
991
+ src: profile.picture,
992
+ alt: sanitizeText(getProfileDisplayName(profile, profile.pubkey)),
993
+ className: "profile-avatar",
994
+ loading: "lazy",
995
+ referrerPolicy: "no-referrer",
996
+ onError: (event) => {
997
+ event.currentTarget.style.display = "none";
998
+ }
999
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1000
+ className: "profile-details",
1001
+ children: [
1002
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1003
+ className: "profile-name",
1004
+ children: sanitizeText(getProfileDisplayName(profile, profile.pubkey))
1005
+ }),
1006
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1007
+ className: "profile-pubkey",
1008
+ children: formatPubkey(profile.pubkey)
1009
+ }),
1010
+ profile.about && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1011
+ className: "profile-about",
1012
+ children: sanitizeText(profile.about)
1013
+ })
1014
+ ]
1015
+ })]
1016
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
1017
+ onClick: () => isMember ? handleRemoveMember(profile.pubkey) : handleAddMember(profile.pubkey),
1018
+ className: `member-button ${isMember ? "remove" : "add"}`,
1019
+ disabled: isSaving,
1020
+ children: isMember ? "Remove" : "Add Member"
1021
+ })]
1022
+ }, profile.pubkey);
1023
+ })]
1024
+ })
1025
+ ]
1026
+ }),
1027
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1028
+ className: "members-section",
1029
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("h2", { children: [
1030
+ "Current Members (",
1031
+ members.length,
1032
+ ")"
1033
+ ] }), members.length === 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
1034
+ className: "empty-message",
1035
+ children: "No members yet. Add members from search results above."
1036
+ }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1037
+ className: "members-list",
1038
+ children: members.map((member) => {
1039
+ const profile = memberProfiles.get(member.pubkey);
1040
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1041
+ className: "member-item",
1042
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1043
+ className: "profile-info",
1044
+ children: [profile?.picture && isValidHttpUrl(profile.picture) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
1045
+ src: profile.picture,
1046
+ alt: sanitizeText(getProfileDisplayName(profile || {}, member.pubkey)),
1047
+ className: "profile-avatar",
1048
+ loading: "lazy",
1049
+ referrerPolicy: "no-referrer",
1050
+ onError: (event) => {
1051
+ event.currentTarget.style.display = "none";
1052
+ }
1053
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1054
+ className: "profile-details",
1055
+ children: [
1056
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1057
+ className: "profile-name",
1058
+ children: profile ? sanitizeText(getProfileDisplayName(profile, member.pubkey)) : formatPubkey(member.pubkey)
1059
+ }),
1060
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1061
+ className: "profile-pubkey",
1062
+ children: formatPubkey(member.pubkey)
1063
+ }),
1064
+ profile?.about && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1065
+ className: "profile-about",
1066
+ children: sanitizeText(profile.about)
1067
+ }),
1068
+ member.petname && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1069
+ className: "profile-petname",
1070
+ children: ["Name: ", sanitizeText(member.petname)]
1071
+ })
1072
+ ]
1073
+ })]
1074
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
1075
+ onClick: () => handleRemoveMember(member.pubkey),
1076
+ className: "member-button remove",
1077
+ disabled: isSaving,
1078
+ children: "Remove"
1079
+ })]
1080
+ }, member.pubkey);
1081
+ })
1082
+ })]
1083
+ })
1084
+ ]
1085
+ })
1086
+ });
1087
+ }
1088
+
1089
+ //#endregion
1090
+ //#region src/components/profile/ProfilePage.tsx
1091
+ const PersonhoodInfo = () => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("section", {
1092
+ className: "personhood-info",
1093
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", { children: [
1094
+ "Network‑state members receive a ",
1095
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("strong", { children: "Personhood Credential (PHC)" }),
1096
+ " from a trusted authority. The PHC attests that the holder is a unique, real individual. Because the credential lives in your passport, you can present a ",
1097
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("em", { children: "zero‑knowledge proof" }),
1098
+ " that you are verified."
1099
+ ] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "In this form we compare the name you enter with the name disclosed by your verified passport proof. If the two match, the profile will be saved as your business card credential together with a passport tag that references the PHC's unique identifier." })]
1100
+ });
1101
+ const MAX_NAME_LENGTH = 100;
1102
+ const MAX_ABOUT_LENGTH = 1e3;
1103
+ const MAX_URL_LENGTH = 2048;
1104
+ function ProfilePage({ authService, relayService, publicKey, onUnauthenticated, onSuccess, onRoleSuggestion }) {
1105
+ const [isLoading, setIsLoading] = (0, react.useState)(false);
1106
+ const [isSaving, setIsSaving] = (0, react.useState)(false);
1107
+ const [saveMessage, setSaveMessage] = (0, react.useState)(null);
1108
+ const [suggestedRole, setSuggestedRole] = (0, react.useState)(null);
1109
+ const [isGettingRole, setIsGettingRole] = (0, react.useState)(false);
1110
+ const [formData, setFormData] = (0, react.useState)({
1111
+ name: "",
1112
+ display_name: "",
1113
+ about: "",
1114
+ picture: "",
1115
+ website: ""
1116
+ });
1117
+ (0, react.useEffect)(() => {
1118
+ if (!publicKey) {
1119
+ if (onUnauthenticated) onUnauthenticated();
1120
+ return;
1121
+ }
1122
+ loadProfile();
1123
+ }, [publicKey]);
1124
+ const loadProfile = async () => {
1125
+ if (!publicKey) return;
1126
+ setIsLoading(true);
1127
+ try {
1128
+ const [profile, roleTag] = await Promise.all([relayService.fetchProfile(publicKey), relayService.fetchProfileRoleTag(publicKey)]);
1129
+ if (profile) setFormData({
1130
+ name: sanitizeText(profile.name || ""),
1131
+ display_name: sanitizeText(profile.display_name || ""),
1132
+ about: sanitizeText(profile.about || ""),
1133
+ picture: profile.picture || "",
1134
+ website: profile.website || ""
1135
+ });
1136
+ if (roleTag) setSuggestedRole(roleTag);
1137
+ } catch (error) {
1138
+ console.error("Failed to load profile:", error);
1139
+ } finally {
1140
+ setIsLoading(false);
1141
+ }
1142
+ };
1143
+ const handleSubmit = async (e) => {
1144
+ e.preventDefault();
1145
+ if (!publicKey) return;
1146
+ setIsSaving(true);
1147
+ setSaveMessage(null);
1148
+ try {
1149
+ const tags = [];
1150
+ if (!isValidPubkey(publicKey)) throw new Error("Invalid public key format");
1151
+ if (formData.about && formData.about.trim().length > 0 && onRoleSuggestion) {
1152
+ setIsGettingRole(true);
1153
+ try {
1154
+ const roleSuggestion = await onRoleSuggestion(formData.about);
1155
+ const candidate = roleSuggestion ? roleSuggestion.trim() : "";
1156
+ if (candidate && isValidRoleTag(candidate)) {
1157
+ tags.push(["role", candidate]);
1158
+ setSuggestedRole(candidate);
1159
+ } else setSuggestedRole(null);
1160
+ } catch (error) {
1161
+ console.error("Failed to get role suggestion:", error);
1162
+ setSuggestedRole(null);
1163
+ } finally {
1164
+ setIsGettingRole(false);
1165
+ }
1166
+ } else setSuggestedRole(null);
1167
+ const profileEvent = {
1168
+ kind: 0,
1169
+ content: JSON.stringify({
1170
+ ...formData,
1171
+ name: sanitizeText(formData.name || ""),
1172
+ display_name: sanitizeText(formData.display_name || ""),
1173
+ about: sanitizeText(formData.about || "")
1174
+ }),
1175
+ created_at: Math.floor(Date.now() / 1e3),
1176
+ tags
1177
+ };
1178
+ const follows = {
1179
+ kind: 3,
1180
+ content: "",
1181
+ created_at: Math.floor(Date.now() / 1e3),
1182
+ tags: [(() => {
1183
+ const followTag = ["p", publicKey];
1184
+ const relayUrl = relayService.getRelays()[0];
1185
+ if (relayUrl) followTag.push(relayUrl);
1186
+ if (formData.name) followTag.push(formData.name);
1187
+ return followTag;
1188
+ })()]
1189
+ };
1190
+ const signedProfile = await authService.signEvent(profileEvent);
1191
+ const signedFollows = await authService.signEvent(follows);
1192
+ await relayService.publishEvent(signedProfile);
1193
+ await relayService.publishEvent(signedFollows);
1194
+ setSaveMessage("Profile saved successfully!");
1195
+ setTimeout(() => {
1196
+ setSaveMessage(null);
1197
+ }, 3e3);
1198
+ if (onSuccess) onSuccess();
1199
+ } catch (error) {
1200
+ console.error("Failed to save profile:", error);
1201
+ setSaveMessage("Failed to save profile. Please try again.");
1202
+ } finally {
1203
+ setIsSaving(false);
1204
+ }
1205
+ };
1206
+ const handleChange = (e) => {
1207
+ const { name, value } = e.target;
1208
+ setFormData((prev) => ({
1209
+ ...prev,
1210
+ [name]: value
1211
+ }));
1212
+ };
1213
+ if (isLoading) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1214
+ className: "profile-container",
1215
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1216
+ className: "loading-state",
1217
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "spinner" }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "Loading profile..." })]
1218
+ })
1219
+ });
1220
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1221
+ className: "profile-container",
1222
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1223
+ className: "profile-card",
1224
+ children: [
1225
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h1", { children: "Profile Setup" }),
1226
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(PersonhoodInfo, {}),
1227
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("form", {
1228
+ onSubmit: handleSubmit,
1229
+ className: "profile-form",
1230
+ children: [
1231
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1232
+ className: "form-group",
1233
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("label", {
1234
+ htmlFor: "name",
1235
+ children: "First Name"
1236
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
1237
+ type: "text",
1238
+ id: "name",
1239
+ name: "name",
1240
+ value: formData.name || "",
1241
+ onChange: handleChange,
1242
+ placeholder: "Your username",
1243
+ maxLength: MAX_NAME_LENGTH
1244
+ })]
1245
+ }),
1246
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1247
+ className: "form-group",
1248
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("label", {
1249
+ htmlFor: "display_name",
1250
+ children: "Last Name"
1251
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
1252
+ type: "text",
1253
+ id: "display_name",
1254
+ name: "display_name",
1255
+ value: formData.display_name || "",
1256
+ onChange: handleChange,
1257
+ placeholder: "Your Last Name",
1258
+ maxLength: MAX_NAME_LENGTH
1259
+ })]
1260
+ }),
1261
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1262
+ className: "form-group",
1263
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("label", {
1264
+ htmlFor: "about",
1265
+ children: "About"
1266
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("textarea", {
1267
+ id: "about",
1268
+ name: "about",
1269
+ value: formData.about || "",
1270
+ onChange: handleChange,
1271
+ placeholder: "Tell us about yourself",
1272
+ rows: 4,
1273
+ maxLength: MAX_ABOUT_LENGTH
1274
+ })]
1275
+ }),
1276
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1277
+ className: "form-group",
1278
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("label", {
1279
+ htmlFor: "picture",
1280
+ children: "Profile Picture URL"
1281
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
1282
+ type: "url",
1283
+ id: "picture",
1284
+ name: "picture",
1285
+ value: formData.picture || "",
1286
+ onChange: handleChange,
1287
+ placeholder: "https://example.com/avatar.jpg",
1288
+ maxLength: MAX_URL_LENGTH
1289
+ })]
1290
+ }),
1291
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1292
+ className: "form-group",
1293
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("label", {
1294
+ htmlFor: "website",
1295
+ children: "LinkedIn"
1296
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
1297
+ type: "url",
1298
+ id: "website",
1299
+ name: "website",
1300
+ value: formData.website || "",
1301
+ onChange: handleChange,
1302
+ placeholder: "https://linkedin.com/example",
1303
+ maxLength: MAX_URL_LENGTH
1304
+ })]
1305
+ }),
1306
+ saveMessage && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1307
+ className: `save-message ${saveMessage.includes("Error") ? "error" : "success"}`,
1308
+ children: saveMessage
1309
+ }),
1310
+ (isGettingRole || suggestedRole) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1311
+ className: "role-tag-container",
1312
+ children: isGettingRole ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1313
+ className: "role-tag-label",
1314
+ children: "Getting AI suggestion..."
1315
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1316
+ className: "role-tag-loading",
1317
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "role-tag-spinner" })
1318
+ })] }) : suggestedRole ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1319
+ className: "role-tag-label",
1320
+ children: "AI Suggested Role:"
1321
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1322
+ className: "role-tag",
1323
+ children: sanitizeText(suggestedRole)
1324
+ })] }) : null
1325
+ }),
1326
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1327
+ className: "form-actions",
1328
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
1329
+ type: "submit",
1330
+ className: "save-button",
1331
+ disabled: isSaving,
1332
+ children: isSaving ? "Saving..." : "Save Profile"
1333
+ })
1334
+ })
1335
+ ]
1336
+ })
1337
+ ]
1338
+ })
1339
+ });
1340
+ }
1341
+
1342
+ //#endregion
1343
+ //#region src/hooks/useAuth.ts
1344
+ /**
1345
+ * Hook to initialize auth state on app load
1346
+ */
1347
+ function useAuthInit(authService, setAuthenticated) {
1348
+ (0, react.useEffect)(() => {
1349
+ if (authService.hasKeyInfo()) {
1350
+ const keyInfo = authService.getCurrentKeyInfo();
1351
+ if (keyInfo) setAuthenticated(keyInfo);
1352
+ }
1353
+ }, [authService, setAuthenticated]);
1354
+ }
1355
+
1356
+ //#endregion
1357
+ //#region src/store/authStore.ts
1358
+ const createAuthStore = () => {
1359
+ return (0, zustand.create)((set) => ({
1360
+ isAuthenticated: false,
1361
+ publicKey: null,
1362
+ keyInfo: null,
1363
+ loginError: null,
1364
+ setAuthenticated: (keyInfo) => {
1365
+ set({
1366
+ isAuthenticated: !!keyInfo,
1367
+ publicKey: keyInfo?.pubkey || null,
1368
+ keyInfo,
1369
+ loginError: null
1370
+ });
1371
+ },
1372
+ setLoginError: (error) => {
1373
+ set({ loginError: error });
1374
+ },
1375
+ logout: () => {
1376
+ set({
1377
+ isAuthenticated: false,
1378
+ publicKey: null,
1379
+ keyInfo: null,
1380
+ loginError: null
1381
+ });
1382
+ }
1383
+ }));
1384
+ };
1385
+ const useAuthStore = createAuthStore();
1386
+
1387
+ //#endregion
1388
+ exports.AuthService = AuthService;
1389
+ exports.BarcodeScanner = BarcodeScanner;
1390
+ exports.LoginButton = LoginButton;
1391
+ exports.MembershipPage = MembershipPage;
1392
+ exports.ProfilePage = ProfilePage;
1393
+ exports.RegistrationFlow = RegistrationFlow;
1394
+ exports.RelayService = RelayService;
1395
+ exports.__toESM = __toESM;
1396
+ exports.createAuthStore = createAuthStore;
1397
+ exports.useAuthInit = useAuthInit;
1398
+ exports.useAuthStore = useAuthStore;
1399
+ //# sourceMappingURL=index.cjs.map