@konemono/nostr-login 1.9.4 → 1.9.6
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/AuthNostrService.ts +34 -78
package/package.json
CHANGED
|
@@ -76,31 +76,26 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
76
76
|
decrypt: this.decrypt44.bind(this),
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
setTimeout(() => this.checkAmberResponse(), 100);
|
|
80
|
+
|
|
80
81
|
const check = () => {
|
|
81
|
-
console.log('[AuthNostrService] Checking Amber response');
|
|
82
82
|
this.checkAmberResponse();
|
|
83
83
|
};
|
|
84
84
|
|
|
85
|
-
// 主要なイベントで検出(ポーリングは廃止)
|
|
86
85
|
window.addEventListener('focus', check);
|
|
87
86
|
window.addEventListener('visibilitychange', () => {
|
|
88
|
-
if (document.visibilityState === 'visible')
|
|
89
|
-
console.log('[AuthNostrService] Page visible, checking Amber response');
|
|
90
|
-
check();
|
|
91
|
-
}
|
|
87
|
+
if (document.visibilityState === 'visible') check();
|
|
92
88
|
});
|
|
93
89
|
window.addEventListener('popstate', check);
|
|
94
90
|
window.addEventListener('hashchange', check);
|
|
95
91
|
|
|
96
|
-
//
|
|
97
|
-
|
|
92
|
+
// Periodic check as a safety net
|
|
93
|
+
setInterval(check, 1000);
|
|
98
94
|
|
|
99
|
-
// ポップアップからのメッセージ受信
|
|
100
95
|
window.addEventListener('message', event => {
|
|
101
96
|
if (event.data && event.data.method === 'amberResponse') {
|
|
102
97
|
const { id, type, result } = event.data;
|
|
103
|
-
console.log('
|
|
98
|
+
console.log('Amber response received via message', { id, type, result });
|
|
104
99
|
this.handleAmberResponse({ id, type, result });
|
|
105
100
|
}
|
|
106
101
|
});
|
|
@@ -109,11 +104,9 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
109
104
|
private checkAmberResponse() {
|
|
110
105
|
const response = AmberDirectSigner.parseResponse();
|
|
111
106
|
if (response) {
|
|
112
|
-
console.log('[AuthNostrService] Amber response detected', response);
|
|
113
|
-
|
|
114
107
|
// If we have an opener and it's not the same window, we are in a popup
|
|
115
108
|
if (window.opener && window.opener !== window) {
|
|
116
|
-
console.log('
|
|
109
|
+
console.log('Amber response in popup, sending back to opener');
|
|
117
110
|
window.opener.postMessage({ method: 'amberResponse', ...response }, window.location.origin);
|
|
118
111
|
window.close();
|
|
119
112
|
return;
|
|
@@ -126,13 +119,10 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
126
119
|
private handledAmberIds: Set<string> = new Set();
|
|
127
120
|
|
|
128
121
|
private handleAmberResponse(response: { id: string; type: string; result: string }) {
|
|
129
|
-
if (this.handledAmberIds.has(response.id))
|
|
130
|
-
console.log('[AuthNostrService] Amber response already handled:', response.id);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
122
|
+
if (this.handledAmberIds.has(response.id)) return;
|
|
133
123
|
this.handledAmberIds.add(response.id);
|
|
134
124
|
|
|
135
|
-
console.log('
|
|
125
|
+
console.log('Handling Amber response', response);
|
|
136
126
|
|
|
137
127
|
// Stop the "Connecting..." spinner
|
|
138
128
|
this.emit('onAuthUrl', { url: '' });
|
|
@@ -140,21 +130,20 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
140
130
|
// Resolve pending promises if any (for non-reload cases)
|
|
141
131
|
const resolved = AmberDirectSigner.resolvePending(response.id, response.type, response.result);
|
|
142
132
|
if (resolved) {
|
|
143
|
-
console.log('
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
133
|
+
console.log('Resolved pending Amber promise via resolvePending');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (response.type === 'get_public_key' || response.type.includes('pub')) {
|
|
137
|
+
const info: Info = {
|
|
138
|
+
pubkey: response.result,
|
|
139
|
+
name: nip19.npubEncode(response.result),
|
|
140
|
+
authMethod: 'amber' as any,
|
|
141
|
+
relays: [],
|
|
142
|
+
signerPubkey: '',
|
|
143
|
+
};
|
|
144
|
+
console.log('Amber login success', info);
|
|
145
|
+
this.emit('onUserInfo', info);
|
|
146
|
+
this.onAuth('login', info);
|
|
158
147
|
}
|
|
159
148
|
|
|
160
149
|
// URLクリーンアップをより徹底的に
|
|
@@ -174,7 +163,7 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
174
163
|
}
|
|
175
164
|
|
|
176
165
|
if (changed) {
|
|
177
|
-
console.log('
|
|
166
|
+
console.log('Cleaning up Amber response URL', url.toString());
|
|
178
167
|
window.history.replaceState({}, '', url.toString());
|
|
179
168
|
}
|
|
180
169
|
}
|
|
@@ -431,16 +420,8 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
431
420
|
}
|
|
432
421
|
|
|
433
422
|
public async setAmber(info: Info) {
|
|
434
|
-
console.log('setAmber', info);
|
|
435
|
-
|
|
436
|
-
// クリア
|
|
437
423
|
this.releaseSigner();
|
|
438
424
|
this.amberSigner = new AmberDirectSigner(info.pubkey);
|
|
439
|
-
|
|
440
|
-
// Amberは署名リクエストをウォレットに投げるだけなので、特別な初期化は不要
|
|
441
|
-
// signEventなどが直接amberSignerを使用する
|
|
442
|
-
|
|
443
|
-
// 認証完了
|
|
444
425
|
this.onAuth('login', info);
|
|
445
426
|
}
|
|
446
427
|
|
|
@@ -676,7 +657,7 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
676
657
|
}
|
|
677
658
|
|
|
678
659
|
private async listen(info: Info) {
|
|
679
|
-
if (!info.iframeUrl) return this.signer!.listen(this.nostrConnectSecret
|
|
660
|
+
if (!info.iframeUrl) return this.signer!.listen(this.nostrConnectSecret);
|
|
680
661
|
const r = await this.starterReady!.wait();
|
|
681
662
|
if (r[0] === 'starterError') throw new Error(r[1]);
|
|
682
663
|
return this.signer!.setListenReply(r[1], this.nostrConnectSecret);
|
|
@@ -737,11 +718,6 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
737
718
|
remotePubkey: info.signerPubkey!,
|
|
738
719
|
relays: info.relays || [],
|
|
739
720
|
timeoutMs: 30000,
|
|
740
|
-
useNip44: true,
|
|
741
|
-
retryConfig: {
|
|
742
|
-
maxRetries: 3,
|
|
743
|
-
retryDelayMs: 1000,
|
|
744
|
-
},
|
|
745
721
|
});
|
|
746
722
|
|
|
747
723
|
const adapter = new Nip46Adapter(client, localSigner);
|
|
@@ -798,44 +774,24 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
798
774
|
if (domain) info.domain = domain;
|
|
799
775
|
if (iframeUrl) info.iframeUrl = iframeUrl;
|
|
800
776
|
|
|
801
|
-
console.log('
|
|
802
|
-
|
|
777
|
+
// console.log('nostr login auth info', info);
|
|
803
778
|
if (!info.signerPubkey || !info.sk || !info.relays || info.relays.length === 0) {
|
|
804
|
-
throw new Error(`
|
|
779
|
+
throw new Error(`Bad bunker url ${bunkerUrl}`);
|
|
805
780
|
}
|
|
806
781
|
|
|
807
782
|
const eventToAddAccount = Boolean(this.params.userInfo);
|
|
808
|
-
|
|
809
|
-
// 接続モードに応じた処理
|
|
810
|
-
const connectMode = type === 'login' && !info.token ? 'listen' : 'connect';
|
|
811
|
-
console.log('authNip46 connection mode:', connectMode);
|
|
783
|
+
console.log('authNip46', type, info);
|
|
812
784
|
|
|
813
785
|
// updates the info
|
|
814
|
-
await this.initSigner(info, {
|
|
815
|
-
listen: connectMode === 'listen',
|
|
816
|
-
connect: connectMode === 'connect',
|
|
817
|
-
eventToAddAccount,
|
|
818
|
-
});
|
|
786
|
+
await this.initSigner(info, { connect: true, eventToAddAccount });
|
|
819
787
|
|
|
820
788
|
// callback
|
|
821
789
|
this.onAuth(type, info);
|
|
822
|
-
} catch (
|
|
823
|
-
console.
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
});
|
|
828
|
-
|
|
829
|
-
// エラーの種類に応じた処理
|
|
830
|
-
if (error.message.includes('timeout') || error.message.includes('timed out')) {
|
|
831
|
-
throw new Error('接続がタイムアウトしました。ネットワークを確認してください。');
|
|
832
|
-
} else if (error.message.includes('Invalid bunker URL')) {
|
|
833
|
-
throw new Error('無効なbunker URL形式です。');
|
|
834
|
-
} else if (error.message.includes('publish')) {
|
|
835
|
-
throw new Error('リレーへの接続に失敗しました。再度お試しください。');
|
|
836
|
-
} else {
|
|
837
|
-
throw new Error(`認証に失敗しました: ${error.message}`);
|
|
838
|
-
}
|
|
790
|
+
} catch (e) {
|
|
791
|
+
console.log('nostr login auth failed', e);
|
|
792
|
+
// make ure it's closed
|
|
793
|
+
// this.popupManager.closePopup();
|
|
794
|
+
throw e;
|
|
839
795
|
}
|
|
840
796
|
}
|
|
841
797
|
|