@konemono/nostr-login 1.7.45 → 1.7.47

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.7.45",
3
+ "version": "1.7.47",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
@@ -614,18 +614,11 @@ class AuthNostrService extends EventEmitter implements Signer {
614
614
  } else if (connect) {
615
615
  await this.connect(info, this.params.optionsModal.perms);
616
616
  } else {
617
- if (info.pubkey) {
618
- await this.signer.initUserPubkey(info.pubkey);
619
- }
620
- }
621
-
622
- if (this.signer.userPubkey) {
623
- info.pubkey = this.signer.userPubkey;
617
+ await this.signer.initUserPubkey(info.pubkey);
624
618
  }
625
619
 
626
- if (this.signer.remotePubkey) {
627
- info.signerPubkey = this.signer.remotePubkey;
628
- }
620
+ info.pubkey = this.signer.userPubkey!;
621
+ info.signerPubkey = this.signer.remotePubkey;
629
622
 
630
623
  ok();
631
624
  } catch (e) {
@@ -660,15 +653,8 @@ class AuthNostrService extends EventEmitter implements Signer {
660
653
  const eventToAddAccount = Boolean(this.params.userInfo);
661
654
  console.log('authNip46', type, info);
662
655
 
663
- await this.startAuth();
664
-
665
- await this.initSigner(info, {
666
- connect: true,
667
- listen: true,
668
- eventToAddAccount,
669
- });
670
-
671
- await this.endAuth();
656
+ // updates the info
657
+ await this.initSigner(info, { connect: true, eventToAddAccount });
672
658
 
673
659
  // callback
674
660
  this.onAuth(type, info);
@@ -143,45 +143,48 @@ class NostrRpc extends NDKNostrRpc {
143
143
  public async sendRequest(remotePubkey: string, method: string, params: string[] = [], kind = 24133, cb?: (res: NDKRpcResponse) => void): Promise<NDKRpcResponse> {
144
144
  const id = this.getId();
145
145
 
146
- // response handler will deduplicate auth urls and responses
147
- this.setResponseHandler(id, cb);
146
+ // 1. Promiseを変数に格納
147
+ const responsePromise = this.setResponseHandler(id, cb);
148
148
 
149
- // create and sign request
150
149
  const event = await this.createRequestEvent(id, remotePubkey, method, params, kind);
151
- console.log('sendRequest', { event, method, remotePubkey, params });
152
150
 
153
- // send to relays
151
+ // 2. 送信
154
152
  await event.publish();
155
153
 
156
- // NOTE: ndk returns a promise that never resolves and
157
- // in fact REQUIRES cb to be provided (otherwise no way
158
- // to consume the result), we've already stepped on the bug
159
- // of waiting for this unresolvable result, so now we return
160
- // undefined to make sure waiters fail, not hang.
161
- // @ts-ignore
162
- return undefined as NDKRpcResponse;
154
+ // 3. undefined ではなく、レスポンスを待つ Promise を返す
155
+ return responsePromise;
163
156
  }
164
157
 
165
158
  protected setResponseHandler(id: string, cb?: (res: NDKRpcResponse) => void) {
166
- let authUrlSent = false;
167
159
  const now = Date.now();
168
- return new Promise<NDKRpcResponse>(() => {
160
+ let authUrlSent = false;
161
+
162
+ // 1. 外部から解決できるように Promise を作成
163
+ return new Promise<NDKRpcResponse>(resolve => {
169
164
  const responseHandler = (response: NDKRpcResponse) => {
165
+ console.log(`[NIP46] Received response for ID: ${response.id}`, response);
166
+
170
167
  if (response.result === 'auth_url') {
171
168
  this.once(`response-${id}`, responseHandler);
172
169
  if (!authUrlSent) {
173
170
  authUrlSent = true;
174
171
  this.emit('authUrl', response.error);
175
172
  }
176
- } else if (cb) {
173
+ } else {
174
+ // IDが一致しているか、リクエストリストに存在するか確認
177
175
  if (this.requests.has(id)) {
178
176
  this.requests.delete(id);
179
177
  console.log('nostr-login processed nip46 request in', Date.now() - now, 'ms');
180
- cb(response);
178
+
179
+ if (cb) cb(response); // 既存のコールバックを実行
180
+ resolve(response); // ★Promise を解決する(これで await が終わる)
181
+ } else {
182
+ console.warn(`[NIP46] Received response for unknown ID: ${id}`);
181
183
  }
182
184
  }
183
185
  };
184
186
 
187
+ // イベント登録
185
188
  this.once(`response-${id}`, responseHandler);
186
189
  });
187
190
  }