@konemono/nostr-login 1.7.58 → 1.7.60
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/package.json
CHANGED
|
@@ -52,14 +52,37 @@ export class AmberDirectSigner implements Signer {
|
|
|
52
52
|
sessionStorage.setItem(`amber_pending_${id}`, JSON.stringify({ hash, type }));
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
public static resolvePending(id: string, result: string) {
|
|
56
|
-
|
|
55
|
+
public static resolvePending(id: string, type: string, result: string) {
|
|
56
|
+
console.log('AmberDirectSigner: resolvePending called', { id, type, pendingCount: this.pendingResolves.size });
|
|
57
|
+
|
|
58
|
+
// Try resolving by ID first
|
|
59
|
+
let resolve = this.pendingResolves.get(id);
|
|
60
|
+
|
|
61
|
+
// Fallback: If no ID match, try resolving by type if only one is pending of that type
|
|
62
|
+
if (!resolve) {
|
|
63
|
+
console.log('AmberDirectSigner: No direct ID match for resolve, searching by type', { id, type });
|
|
64
|
+
for (const [pendingId, pendingResolve] of this.pendingResolves.entries()) {
|
|
65
|
+
const pending = sessionStorage.getItem(`amber_pending_${pendingId}`);
|
|
66
|
+
if (pending) {
|
|
67
|
+
const { type: pendingType } = JSON.parse(pending);
|
|
68
|
+
if (pendingType === type) {
|
|
69
|
+
resolve = pendingResolve;
|
|
70
|
+
id = pendingId;
|
|
71
|
+
console.log('AmberDirectSigner: Found match by type', { id, type });
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
57
78
|
if (resolve) {
|
|
58
|
-
console.log('Resolving pending
|
|
79
|
+
console.log('AmberDirectSigner: Resolving pending promise', { id, type });
|
|
59
80
|
resolve(result);
|
|
60
81
|
this.pendingResolves.delete(id);
|
|
61
82
|
return true;
|
|
62
83
|
}
|
|
84
|
+
|
|
85
|
+
console.log('AmberDirectSigner: No pending promise found to resolve', { id, type });
|
|
63
86
|
return false;
|
|
64
87
|
}
|
|
65
88
|
|
|
@@ -142,32 +165,37 @@ export class AmberDirectSigner implements Signer {
|
|
|
142
165
|
|
|
143
166
|
private generateUrl(content: string, type: string, id: string, recipient?: string): string {
|
|
144
167
|
const callbackUrl = new URL(window.location.origin + window.location.pathname);
|
|
168
|
+
|
|
169
|
+
// Amber parameters we want to carry back
|
|
145
170
|
callbackUrl.searchParams.set('amberType', type);
|
|
146
171
|
callbackUrl.searchParams.set('amberId', id);
|
|
147
172
|
|
|
148
|
-
const action = `com.greenart7c3.nostrsigner.${type.toUpperCase()}`;
|
|
149
173
|
const name = document.title || window.location.hostname || 'Nostr Login';
|
|
150
174
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
175
|
+
// NIP-55 standard and some common extensions
|
|
176
|
+
const params: string[] = [];
|
|
177
|
+
params.push(`type=${encodeURIComponent(type)}`);
|
|
178
|
+
params.push(`id=${encodeURIComponent(id)}`);
|
|
179
|
+
params.push(`callbackUrl=${encodeURIComponent(callbackUrl.toString())}`);
|
|
180
|
+
|
|
181
|
+
// Multi-alias for app name to fix the "null" issue
|
|
182
|
+
params.push(`name=${encodeURIComponent(name)}`);
|
|
183
|
+
params.push(`app_name=${encodeURIComponent(name)}`);
|
|
184
|
+
params.push(`title=${encodeURIComponent(name)}`);
|
|
157
185
|
|
|
158
186
|
if (recipient) {
|
|
159
|
-
params.push(`
|
|
187
|
+
params.push(`pubkey=${encodeURIComponent(recipient)}`);
|
|
188
|
+
params.push(`pubKey=${encodeURIComponent(recipient)}`);
|
|
189
|
+
params.push(`current_user=${encodeURIComponent(recipient)}`);
|
|
160
190
|
} else if (this._pubkey) {
|
|
161
|
-
params.push(`
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (type === 'sign_event') {
|
|
165
|
-
params.push(`S.event=${encodeURIComponent(content)}`);
|
|
166
|
-
} else {
|
|
167
|
-
params.push(`S.content=${encodeURIComponent(content)}`);
|
|
191
|
+
params.push(`pubkey=${encodeURIComponent(this._pubkey)}`);
|
|
192
|
+
params.push(`pubKey=${encodeURIComponent(this._pubkey)}`);
|
|
193
|
+
params.push(`current_user=${encodeURIComponent(this._pubkey)}`);
|
|
168
194
|
}
|
|
169
195
|
|
|
170
|
-
|
|
196
|
+
const dataPart = content ? encodeURIComponent(content) : '';
|
|
197
|
+
// Format: nostrsigner:<content>?<params>
|
|
198
|
+
return `nostrsigner:${dataPart}?${params.join('&')}`;
|
|
171
199
|
}
|
|
172
200
|
|
|
173
201
|
public static parseResponse(): { type: string; id: string; result: string } | null {
|
|
@@ -175,19 +203,34 @@ export class AmberDirectSigner implements Signer {
|
|
|
175
203
|
const params = new URLSearchParams(url.search);
|
|
176
204
|
const hashParams = new URLSearchParams(url.hash.substring(1));
|
|
177
205
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
206
|
+
if (url.search || url.hash) {
|
|
207
|
+
console.log('AmberDirectSigner: Checking URL for response', {
|
|
208
|
+
search: url.search,
|
|
209
|
+
hash: url.hash
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const getParam = (name: string) => params.get(name) || hashParams.get(name);
|
|
214
|
+
|
|
215
|
+
// Support both our custom names and original intent names
|
|
216
|
+
const type = getParam('amberType') || getParam('type');
|
|
217
|
+
const id = getParam('amberId') || getParam('id');
|
|
218
|
+
const result = getParam('signature') || getParam('result') || getParam('pubKey') || getParam('pubkey');
|
|
219
|
+
|
|
220
|
+
if (type && result) {
|
|
221
|
+
console.log('AmberDirectSigner: Response params detected', { type, id, resultLength: result.length });
|
|
222
|
+
|
|
223
|
+
const finalId = id || '';
|
|
182
224
|
|
|
183
|
-
if (type && id && result) {
|
|
184
225
|
// Clean up URL
|
|
185
226
|
const newUrl = new URL(window.location.href);
|
|
186
|
-
['amberType', 'amberId', 'signature', 'result', 'pubKey', 'pubkey'].forEach(p =>
|
|
227
|
+
['amberType', 'amberId', 'signature', 'result', 'pubKey', 'pubkey', 'type', 'id'].forEach(p => {
|
|
228
|
+
newUrl.searchParams.delete(p);
|
|
229
|
+
});
|
|
187
230
|
newUrl.hash = '';
|
|
188
231
|
window.history.replaceState({}, '', newUrl.toString());
|
|
189
232
|
|
|
190
|
-
return { type, id, result };
|
|
233
|
+
return { type, id: finalId, result };
|
|
191
234
|
}
|
|
192
235
|
return null;
|
|
193
236
|
}
|
|
@@ -88,10 +88,19 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
88
88
|
|
|
89
89
|
setTimeout(() => this.checkAmberResponse(), 100);
|
|
90
90
|
|
|
91
|
-
|
|
92
|
-
console.log('Window focused, checking Amber response');
|
|
91
|
+
const check = () => {
|
|
93
92
|
this.checkAmberResponse();
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
window.addEventListener('focus', check);
|
|
96
|
+
window.addEventListener('visibilitychange', () => {
|
|
97
|
+
if (document.visibilityState === 'visible') check();
|
|
94
98
|
});
|
|
99
|
+
window.addEventListener('popstate', check);
|
|
100
|
+
window.addEventListener('hashchange', check);
|
|
101
|
+
|
|
102
|
+
// Periodic check as a safety net
|
|
103
|
+
setInterval(check, 1000);
|
|
95
104
|
}
|
|
96
105
|
|
|
97
106
|
private checkAmberResponse() {
|
|
@@ -100,7 +109,10 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
100
109
|
console.log('Amber response detected', response);
|
|
101
110
|
|
|
102
111
|
// Resolve pending promises if any (for non-reload cases)
|
|
103
|
-
AmberDirectSigner.resolvePending(response.id, response.result);
|
|
112
|
+
const resolved = AmberDirectSigner.resolvePending(response.id, response.type, response.result);
|
|
113
|
+
if (resolved) {
|
|
114
|
+
console.log('Resolved pending Amber promise via resolvePending');
|
|
115
|
+
}
|
|
104
116
|
|
|
105
117
|
if (response.type === 'get_public_key') {
|
|
106
118
|
const info: Info = {
|
|
@@ -181,6 +193,8 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
181
193
|
if (link && !iframeUrl) {
|
|
182
194
|
if (link === 'amber') {
|
|
183
195
|
const signer = new AmberDirectSigner();
|
|
196
|
+
const url = (signer as any).generateUrl('', 'get_public_key', Math.random().toString(36).substring(7));
|
|
197
|
+
this.emit('onAuthUrl', { url });
|
|
184
198
|
return (signer as any).getPublicKey(); // will redirect
|
|
185
199
|
}
|
|
186
200
|
window.open(link, '_blank', 'width=400,height=700');
|