@konemono/nostr-login 1.9.4 → 1.9.5
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 +33 -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,19 @@ 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
|
-
this.onAuth('login', info);
|
|
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.onAuth('login', info);
|
|
158
146
|
}
|
|
159
147
|
|
|
160
148
|
// URLクリーンアップをより徹底的に
|
|
@@ -174,7 +162,7 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
174
162
|
}
|
|
175
163
|
|
|
176
164
|
if (changed) {
|
|
177
|
-
console.log('
|
|
165
|
+
console.log('Cleaning up Amber response URL', url.toString());
|
|
178
166
|
window.history.replaceState({}, '', url.toString());
|
|
179
167
|
}
|
|
180
168
|
}
|
|
@@ -431,16 +419,8 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
431
419
|
}
|
|
432
420
|
|
|
433
421
|
public async setAmber(info: Info) {
|
|
434
|
-
console.log('setAmber', info);
|
|
435
|
-
|
|
436
|
-
// クリア
|
|
437
422
|
this.releaseSigner();
|
|
438
423
|
this.amberSigner = new AmberDirectSigner(info.pubkey);
|
|
439
|
-
|
|
440
|
-
// Amberは署名リクエストをウォレットに投げるだけなので、特別な初期化は不要
|
|
441
|
-
// signEventなどが直接amberSignerを使用する
|
|
442
|
-
|
|
443
|
-
// 認証完了
|
|
444
424
|
this.onAuth('login', info);
|
|
445
425
|
}
|
|
446
426
|
|
|
@@ -676,7 +656,7 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
676
656
|
}
|
|
677
657
|
|
|
678
658
|
private async listen(info: Info) {
|
|
679
|
-
if (!info.iframeUrl) return this.signer!.listen(this.nostrConnectSecret
|
|
659
|
+
if (!info.iframeUrl) return this.signer!.listen(this.nostrConnectSecret);
|
|
680
660
|
const r = await this.starterReady!.wait();
|
|
681
661
|
if (r[0] === 'starterError') throw new Error(r[1]);
|
|
682
662
|
return this.signer!.setListenReply(r[1], this.nostrConnectSecret);
|
|
@@ -737,11 +717,6 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
737
717
|
remotePubkey: info.signerPubkey!,
|
|
738
718
|
relays: info.relays || [],
|
|
739
719
|
timeoutMs: 30000,
|
|
740
|
-
useNip44: true,
|
|
741
|
-
retryConfig: {
|
|
742
|
-
maxRetries: 3,
|
|
743
|
-
retryDelayMs: 1000,
|
|
744
|
-
},
|
|
745
720
|
});
|
|
746
721
|
|
|
747
722
|
const adapter = new Nip46Adapter(client, localSigner);
|
|
@@ -798,44 +773,24 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
798
773
|
if (domain) info.domain = domain;
|
|
799
774
|
if (iframeUrl) info.iframeUrl = iframeUrl;
|
|
800
775
|
|
|
801
|
-
console.log('
|
|
802
|
-
|
|
776
|
+
// console.log('nostr login auth info', info);
|
|
803
777
|
if (!info.signerPubkey || !info.sk || !info.relays || info.relays.length === 0) {
|
|
804
|
-
throw new Error(`
|
|
778
|
+
throw new Error(`Bad bunker url ${bunkerUrl}`);
|
|
805
779
|
}
|
|
806
780
|
|
|
807
781
|
const eventToAddAccount = Boolean(this.params.userInfo);
|
|
808
|
-
|
|
809
|
-
// 接続モードに応じた処理
|
|
810
|
-
const connectMode = type === 'login' && !info.token ? 'listen' : 'connect';
|
|
811
|
-
console.log('authNip46 connection mode:', connectMode);
|
|
782
|
+
console.log('authNip46', type, info);
|
|
812
783
|
|
|
813
784
|
// updates the info
|
|
814
|
-
await this.initSigner(info, {
|
|
815
|
-
listen: connectMode === 'listen',
|
|
816
|
-
connect: connectMode === 'connect',
|
|
817
|
-
eventToAddAccount,
|
|
818
|
-
});
|
|
785
|
+
await this.initSigner(info, { connect: true, eventToAddAccount });
|
|
819
786
|
|
|
820
787
|
// callback
|
|
821
788
|
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
|
-
}
|
|
789
|
+
} catch (e) {
|
|
790
|
+
console.log('nostr login auth failed', e);
|
|
791
|
+
// make ure it's closed
|
|
792
|
+
// this.popupManager.closePopup();
|
|
793
|
+
throw e;
|
|
839
794
|
}
|
|
840
795
|
}
|
|
841
796
|
|