@konemono/nostr-login 1.7.70 → 1.9.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.
@@ -1,6 +1,5 @@
1
1
  import { Info, RecentType } from 'nostr-login-components/dist/types/types';
2
- import NDK, { NDKEvent, NDKRelaySet, NDKSigner, NDKUser } from '@nostr-dev-kit/ndk';
3
- import { generatePrivateKey } from 'nostr-tools';
2
+ import { generatePrivateKey, SimplePool, getEventHash } from 'nostr-tools';
4
3
  import { NostrLoginOptions } from '../types';
5
4
 
6
5
  const LOCAL_STORE_KEY = '__nostrlogin_nip46';
@@ -19,7 +18,7 @@ export const localStorageGetItem = (key: string) => {
19
18
  if (value) {
20
19
  try {
21
20
  return JSON.parse(value);
22
- } catch { }
21
+ } catch {}
23
22
  }
24
23
 
25
24
  return null;
@@ -29,12 +28,53 @@ export const localStorageRemoveItem = (key: string) => {
29
28
  localStorage.removeItem(key);
30
29
  };
31
30
 
32
- export const fetchProfile = async (info: Info, profileNdk: NDK) => {
33
- const user = new NDKUser({ pubkey: info.pubkey });
34
-
35
- user.ndk = profileNdk;
31
+ export const fetchProfile = async (info: Info, profileNdkOrRelays?: any) => {
32
+ // support legacy external profile fetcher (NDK compatibility)
33
+ if (profileNdkOrRelays && typeof profileNdkOrRelays.fetchEvents === 'function') {
34
+ try {
35
+ const user = (profileNdkOrRelays as any).getUser({ pubkey: info.pubkey });
36
+ user.ndk = profileNdkOrRelays;
37
+ return await user.fetchProfile();
38
+ } catch (e) {
39
+ // fallthrough to SimplePool-based fetch
40
+ }
41
+ }
36
42
 
37
- return await user.fetchProfile();
43
+ // fallback: use SimplePool to fetch latest kind:0 metadata for the pubkey
44
+ const relays = Array.isArray(profileNdkOrRelays) && profileNdkOrRelays.length ? profileNdkOrRelays : DEFAULT_SIGNUP_RELAYS;
45
+ const pool = new SimplePool();
46
+ const sub = pool.sub(relays, [{ kinds: [0], authors: [info.pubkey], limit: 1 }]);
47
+
48
+ return await new Promise(resolve => {
49
+ const timer = setTimeout(() => {
50
+ try {
51
+ sub.unsub();
52
+ } catch {}
53
+ resolve(null);
54
+ }, 3000);
55
+
56
+ sub.on('event', (event: any) => {
57
+ clearTimeout(timer);
58
+ try {
59
+ sub.unsub();
60
+ } catch {}
61
+ if (!event || !event.content) return resolve(null);
62
+ try {
63
+ const profile = JSON.parse(event.content);
64
+ resolve(profile);
65
+ } catch (e) {
66
+ resolve(null);
67
+ }
68
+ });
69
+
70
+ sub.on('eose', () => {
71
+ clearTimeout(timer);
72
+ try {
73
+ sub.unsub();
74
+ } catch {}
75
+ resolve(null);
76
+ });
77
+ });
38
78
  };
39
79
 
40
80
  export const prepareSignupRelays = (signupRelays?: string) => {
@@ -46,43 +86,46 @@ export const prepareSignupRelays = (signupRelays?: string) => {
46
86
  return relays;
47
87
  };
48
88
 
49
- export const createProfile = async (info: Info, profileNdk: NDK, signer: NDKSigner, signupRelays?: string, outboxRelays?: string[]) => {
89
+ export const createProfile = async (info: Info, signer: any, signupRelays?: string, outboxRelays?: string[]) => {
50
90
  const meta = {
51
91
  name: info.name,
52
92
  };
53
93
 
54
- const profileEvent = new NDKEvent(profileNdk, {
94
+ const profileEvent: any = {
55
95
  kind: 0,
56
96
  created_at: Math.floor(Date.now() / 1000),
57
97
  pubkey: info.pubkey,
58
98
  content: JSON.stringify(meta),
59
99
  tags: [],
60
- });
100
+ };
61
101
  if (window.location.hostname) profileEvent.tags.push(['client', window.location.hostname]);
62
102
 
63
- const relaysEvent = new NDKEvent(profileNdk, {
103
+ const relaysEvent: any = {
64
104
  kind: 10002,
65
105
  created_at: Math.floor(Date.now() / 1000),
66
106
  pubkey: info.pubkey,
67
107
  content: '',
68
108
  tags: [],
69
- });
109
+ };
70
110
 
71
- const relays = prepareSignupRelays(signupRelays)
111
+ const relays = prepareSignupRelays(signupRelays);
72
112
  for (const r of relays) {
73
113
  relaysEvent.tags.push(['r', r]);
74
114
  }
75
115
 
76
- await profileEvent.sign(signer);
116
+ // signer is expected to implement sign(event)
117
+ await signer.sign(profileEvent);
77
118
  console.log('signed profile', profileEvent);
78
- await relaysEvent.sign(signer);
119
+ await signer.sign(relaysEvent);
79
120
  console.log('signed relays', relaysEvent);
80
121
 
81
122
  const outboxRelaysFinal = outboxRelays && outboxRelays.length ? outboxRelays : OUTBOX_RELAYS;
82
123
 
83
- await profileEvent.publish(NDKRelaySet.fromRelayUrls(outboxRelaysFinal, profileNdk));
124
+ // publish using SimplePool
125
+ const pool = new SimplePool();
126
+ await Promise.any(pool.publish(outboxRelaysFinal, profileEvent));
84
127
  console.log('published profile', profileEvent);
85
- await relaysEvent.publish(NDKRelaySet.fromRelayUrls(outboxRelaysFinal, profileNdk));
128
+ await Promise.any(pool.publish(outboxRelaysFinal, relaysEvent));
86
129
  console.log('published relays', relaysEvent);
87
130
  };
88
131
 
@@ -128,9 +171,7 @@ export const getBunkerUrl = async (value: string, optionsModal: NostrLoginOption
128
171
  throw new Error('Bunker relay not provided');
129
172
  }
130
173
 
131
- const relayParams = bunkerRelays
132
- .map(r => `relay=${encodeURIComponent(r)}`)
133
- .join('&');
174
+ const relayParams = bunkerRelays.map(r => `relay=${encodeURIComponent(r)}`).join('&');
134
175
 
135
176
  return `bunker://${userPubkey}?${relayParams}`;
136
177
  }
@@ -167,7 +208,7 @@ export const checkNip05 = async (nip05: string) => {
167
208
  pubkey = d.names[name];
168
209
  return;
169
210
  }
170
- } catch { }
211
+ } catch {}
171
212
 
172
213
  available = true;
173
214
  })();
package/tsconfig.json CHANGED
@@ -11,5 +11,5 @@
11
11
  "outDir": "./dist",
12
12
  },
13
13
  "include": ["src/**/*.ts"],
14
- "exclude": ["node_modules"],
15
- }
14
+ "exclude": ["node_modules", "**/*.test.ts", "**/*.spec.ts"],
15
+ }
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ environment: 'node',
7
+ include: ['src/**/*.test.ts'],
8
+ },
9
+ });