@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.
- package/dist/index.esm.js +10 -12
- package/dist/index.esm.js.map +1 -1
- package/dist/modules/AmberDirectSigner.d.ts +1 -0
- package/dist/modules/AuthNostrService.d.ts +5 -5
- package/dist/modules/Nip46.d.ts +47 -31
- package/dist/modules/NostrExtensionService.d.ts +5 -0
- package/dist/modules/Signer.d.ts +11 -4
- package/dist/modules/nip46/Nip46Adapter.d.ts +34 -0
- package/dist/modules/nip46/Nip46Client.d.ts +67 -0
- package/dist/modules/nip46/types.d.ts +32 -0
- package/dist/unpkg.js +10 -12
- package/dist/utils/index.d.ts +2 -3
- package/package.json +7 -4
- package/src/index.ts +1 -1
- package/src/modules/AmberDirectSigner.ts +205 -162
- package/src/modules/AuthNostrService.ts +186 -132
- package/src/modules/Nip46.iframe.test.ts +124 -0
- package/src/modules/Nip46.test.ts +31 -0
- package/src/modules/Nip46.ts +212 -261
- package/src/modules/NostrExtensionService.ts +6 -0
- package/src/modules/Signer.ts +39 -8
- package/src/modules/nip46/Nip46Adapter.ts +180 -0
- package/src/modules/nip46/Nip46Client.ts +356 -0
- package/src/modules/nip46/types.ts +34 -0
- package/src/utils/index.ts +63 -22
- package/tsconfig.json +2 -2
- package/vitest.config.ts +9 -0
package/src/utils/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Info, RecentType } from 'nostr-login-components/dist/types/types';
|
|
2
|
-
import
|
|
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,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
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,
|
|
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 =
|
|
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 =
|
|
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
|
-
|
|
116
|
+
// signer is expected to implement sign(event)
|
|
117
|
+
await signer.sign(profileEvent);
|
|
77
118
|
console.log('signed profile', profileEvent);
|
|
78
|
-
await
|
|
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
|
-
|
|
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
|
|
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