@konemono/nostr-login 1.7.59 → 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/dist/index.esm.js +1 -1
- package/dist/unpkg.js +1 -1
- package/package.json +1 -1
- package/src/modules/AmberDirectSigner.ts +37 -23
- package/src/modules/AuthNostrService.ts +17 -3
package/package.json
CHANGED
|
@@ -53,12 +53,14 @@ export class AmberDirectSigner implements Signer {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
public static resolvePending(id: string, type: string, result: string) {
|
|
56
|
+
console.log('AmberDirectSigner: resolvePending called', { id, type, pendingCount: this.pendingResolves.size });
|
|
57
|
+
|
|
56
58
|
// Try resolving by ID first
|
|
57
59
|
let resolve = this.pendingResolves.get(id);
|
|
58
60
|
|
|
59
61
|
// Fallback: If no ID match, try resolving by type if only one is pending of that type
|
|
60
62
|
if (!resolve) {
|
|
61
|
-
console.log('No direct ID match for
|
|
63
|
+
console.log('AmberDirectSigner: No direct ID match for resolve, searching by type', { id, type });
|
|
62
64
|
for (const [pendingId, pendingResolve] of this.pendingResolves.entries()) {
|
|
63
65
|
const pending = sessionStorage.getItem(`amber_pending_${pendingId}`);
|
|
64
66
|
if (pending) {
|
|
@@ -66,6 +68,7 @@ export class AmberDirectSigner implements Signer {
|
|
|
66
68
|
if (pendingType === type) {
|
|
67
69
|
resolve = pendingResolve;
|
|
68
70
|
id = pendingId;
|
|
71
|
+
console.log('AmberDirectSigner: Found match by type', { id, type });
|
|
69
72
|
break;
|
|
70
73
|
}
|
|
71
74
|
}
|
|
@@ -73,11 +76,13 @@ export class AmberDirectSigner implements Signer {
|
|
|
73
76
|
}
|
|
74
77
|
|
|
75
78
|
if (resolve) {
|
|
76
|
-
console.log('Resolving pending
|
|
79
|
+
console.log('AmberDirectSigner: Resolving pending promise', { id, type });
|
|
77
80
|
resolve(result);
|
|
78
81
|
this.pendingResolves.delete(id);
|
|
79
82
|
return true;
|
|
80
83
|
}
|
|
84
|
+
|
|
85
|
+
console.log('AmberDirectSigner: No pending promise found to resolve', { id, type });
|
|
81
86
|
return false;
|
|
82
87
|
}
|
|
83
88
|
|
|
@@ -161,36 +166,36 @@ export class AmberDirectSigner implements Signer {
|
|
|
161
166
|
private generateUrl(content: string, type: string, id: string, recipient?: string): string {
|
|
162
167
|
const callbackUrl = new URL(window.location.origin + window.location.pathname);
|
|
163
168
|
|
|
164
|
-
//
|
|
165
|
-
const currentParams = new URLSearchParams(window.location.search);
|
|
166
|
-
currentParams.forEach((value, key) => {
|
|
167
|
-
if (!['amberType', 'amberId', 'signature', 'result', 'pubKey', 'pubkey'].includes(key)) {
|
|
168
|
-
callbackUrl.searchParams.set(key, value);
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
|
|
169
|
+
// Amber parameters we want to carry back
|
|
172
170
|
callbackUrl.searchParams.set('amberType', type);
|
|
173
171
|
callbackUrl.searchParams.set('amberId', id);
|
|
174
172
|
|
|
175
173
|
const name = document.title || window.location.hostname || 'Nostr Login';
|
|
176
174
|
|
|
177
|
-
|
|
178
|
-
params
|
|
179
|
-
params.
|
|
180
|
-
params.
|
|
181
|
-
params.
|
|
182
|
-
|
|
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)}`);
|
|
183
185
|
|
|
184
186
|
if (recipient) {
|
|
185
|
-
params.
|
|
186
|
-
params.
|
|
187
|
+
params.push(`pubkey=${encodeURIComponent(recipient)}`);
|
|
188
|
+
params.push(`pubKey=${encodeURIComponent(recipient)}`);
|
|
189
|
+
params.push(`current_user=${encodeURIComponent(recipient)}`);
|
|
187
190
|
} else if (this._pubkey) {
|
|
188
|
-
params.
|
|
189
|
-
params.
|
|
191
|
+
params.push(`pubkey=${encodeURIComponent(this._pubkey)}`);
|
|
192
|
+
params.push(`pubKey=${encodeURIComponent(this._pubkey)}`);
|
|
193
|
+
params.push(`current_user=${encodeURIComponent(this._pubkey)}`);
|
|
190
194
|
}
|
|
191
195
|
|
|
192
|
-
const dataPart = content ? encodeURIComponent(content) : '
|
|
193
|
-
|
|
196
|
+
const dataPart = content ? encodeURIComponent(content) : '';
|
|
197
|
+
// Format: nostrsigner:<content>?<params>
|
|
198
|
+
return `nostrsigner:${dataPart}?${params.join('&')}`;
|
|
194
199
|
}
|
|
195
200
|
|
|
196
201
|
public static parseResponse(): { type: string; id: string; result: string } | null {
|
|
@@ -198,14 +203,23 @@ export class AmberDirectSigner implements Signer {
|
|
|
198
203
|
const params = new URLSearchParams(url.search);
|
|
199
204
|
const hashParams = new URLSearchParams(url.hash.substring(1));
|
|
200
205
|
|
|
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
|
+
|
|
201
213
|
const getParam = (name: string) => params.get(name) || hashParams.get(name);
|
|
202
214
|
|
|
215
|
+
// Support both our custom names and original intent names
|
|
203
216
|
const type = getParam('amberType') || getParam('type');
|
|
204
217
|
const id = getParam('amberId') || getParam('id');
|
|
205
218
|
const result = getParam('signature') || getParam('result') || getParam('pubKey') || getParam('pubkey');
|
|
206
219
|
|
|
207
220
|
if (type && result) {
|
|
208
|
-
|
|
221
|
+
console.log('AmberDirectSigner: Response params detected', { type, id, resultLength: result.length });
|
|
222
|
+
|
|
209
223
|
const finalId = id || '';
|
|
210
224
|
|
|
211
225
|
// Clean up URL
|
|
@@ -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.type, 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');
|