@konemono/nostr-login 1.9.9 → 1.9.10

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konemono/nostr-login",
3
- "version": "1.9.9",
3
+ "version": "1.9.10",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
@@ -73,6 +73,9 @@ class AuthNostrService extends EventEmitter implements Signer {
73
73
  encrypt: this.encrypt44.bind(this),
74
74
  decrypt: this.decrypt44.bind(this),
75
75
  };
76
+
77
+ // ページロード時にAmberフローを復元
78
+ this.checkAndResumeAmberFlow();
76
79
  }
77
80
 
78
81
  public isIframe() {
@@ -96,6 +99,58 @@ class AuthNostrService extends EventEmitter implements Signer {
96
99
  public cancelNostrConnect() {
97
100
  this.releaseSigner();
98
101
  this.resetAuth();
102
+ // Amber pending状態もクリア
103
+ localStorage.removeItem('nostr-login-amber-pending');
104
+ }
105
+
106
+ /**
107
+ * Amberから戻ってきた際に保存された状態を確認して復元する
108
+ */
109
+ private async checkAndResumeAmberFlow() {
110
+ try {
111
+ const pendingData = localStorage.getItem('nostr-login-amber-pending');
112
+ if (!pendingData) return;
113
+
114
+ const { nostrConnectKey, nostrConnectSecret, relays, domain, timestamp } = JSON.parse(pendingData);
115
+
116
+ // タイムアウトチェック(10分以内なら有効)
117
+ if (Date.now() - timestamp > 10 * 60 * 1000) {
118
+ console.log('[Amber] Pending flow expired, clearing');
119
+ localStorage.removeItem('nostr-login-amber-pending');
120
+ return;
121
+ }
122
+
123
+ console.log('[Amber] Resuming pending flow from localStorage');
124
+
125
+ // 状態を復元
126
+ this.nostrConnectKey = nostrConnectKey;
127
+ this.nostrConnectSecret = nostrConnectSecret;
128
+
129
+ const info: Info = {
130
+ authMethod: 'connect',
131
+ pubkey: '',
132
+ signerPubkey: '',
133
+ sk: this.nostrConnectKey,
134
+ domain: domain,
135
+ relays: relays,
136
+ };
137
+
138
+ // リスナーを再開してAmberからの接続を待つ
139
+ await this.startAuth();
140
+ await this.initSigner(info, { listen: true });
141
+
142
+ // 接続が成功したらlocalStorageをクリア
143
+ if (info.pubkey && info.signerPubkey) {
144
+ info.bunkerUrl = `bunker://${info.signerPubkey}?${relays.map((r: string, i: number) => `${i !== 0 ? '&' : ''}relay=${r}`).join('')}`;
145
+ this.onAuth('login', info);
146
+ await this.endAuth();
147
+ localStorage.removeItem('nostr-login-amber-pending');
148
+ console.log('[Amber] Flow resumed successfully');
149
+ }
150
+ } catch (error) {
151
+ console.error('[Amber] Failed to resume flow:', error);
152
+ localStorage.removeItem('nostr-login-amber-pending');
153
+ }
99
154
  }
100
155
 
101
156
  public async nostrConnect(
@@ -132,7 +187,32 @@ class AuthNostrService extends EventEmitter implements Signer {
132
187
  // Amber に nostrconnect:// URL を渡す
133
188
  const nostrconnectUrl = await this.createNostrConnect(relays);
134
189
  console.log('Amber flow via NIP-46, opening:', nostrconnectUrl);
135
- window.location.href = nostrconnectUrl;
190
+
191
+ // 状態を保存してAmberから戻ってきたときに復元できるようにする
192
+ localStorage.setItem(
193
+ 'nostr-login-amber-pending',
194
+ JSON.stringify({
195
+ nostrConnectKey: this.nostrConnectKey,
196
+ nostrConnectSecret: this.nostrConnectSecret,
197
+ relays: relays,
198
+ domain: domain,
199
+ timestamp: Date.now(),
200
+ }),
201
+ );
202
+
203
+ // AndroidではIntent URLを使用してAmberを開く(ページコンテキストを保持しようと試みる)
204
+ try {
205
+ const intentUrl = `intent://${nostrconnectUrl.replace('nostrconnect://', '')}#Intent;scheme=nostrconnect;package=com.greenart7c3.nostrsigner;end;`;
206
+ const opened = window.open(intentUrl, '_system');
207
+
208
+ // window.openが失敗した場合は従来の方法にフォールバック
209
+ if (!opened) {
210
+ window.location.href = nostrconnectUrl;
211
+ }
212
+ } catch (e) {
213
+ // エラー時はdirect schemeにフォールバック
214
+ window.location.href = nostrconnectUrl;
215
+ }
136
216
  } else {
137
217
  window.open(link, '_blank', 'width=400,height=700');
138
218
  }