@konemono/nostr-login 1.7.32 → 1.7.34

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.32",
3
+ "version": "1.7.34",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
@@ -13,6 +13,11 @@ class NostrRpc extends NDKNostrRpc {
13
13
  private sub?: NDKSubscription;
14
14
  protected _useNip44: boolean = false;
15
15
 
16
+ public cleanupTimersAndRequests() {
17
+ this.clearAllTimeouts(); // private/protected なメソッドを呼び出す
18
+ this.requests.clear(); // requests Setをクリア
19
+ }
20
+
16
21
  public constructor(ndk: NDK, signer: PrivateKeySigner, processManager?: ProcessManager) {
17
22
  super(ndk, signer, ndk.debug.extend('nip46:signer:rpc'));
18
23
  this._ndk = ndk;
@@ -34,7 +39,7 @@ class NostrRpc extends NDKNostrRpc {
34
39
  }
35
40
  }
36
41
 
37
- private clearAllTimeouts() {
42
+ protected clearAllTimeouts() {
38
43
  for (const timeout of this.requestTimeouts.values()) {
39
44
  clearTimeout(timeout);
40
45
  }
@@ -155,6 +160,7 @@ class NostrRpc extends NDKNostrRpc {
155
160
  );
156
161
 
157
162
  const connectedRelays = relays.filter(r => r.status === 1);
163
+ console.log('connected relays', connectedRelays);
158
164
 
159
165
  if (connectedRelays.length === 0) {
160
166
  console.log('No connected relays, forcing reconnection...');
@@ -262,6 +268,7 @@ class NostrRpc extends NDKNostrRpc {
262
268
 
263
269
  const timeoutId = setTimeout(() => {
264
270
  if (this.requests.has(id)) {
271
+ clearTimeout(timeoutId);
265
272
  this.requests.delete(id);
266
273
  this.requestTimeouts.delete(id);
267
274
  console.log('NIP46 request timeout for', id);
@@ -303,6 +310,14 @@ class NostrRpc extends NDKNostrRpc {
303
310
  protected async createRequestEvent(id: string, remotePubkey: string, method: string, params: string[] = [], kind = 24133) {
304
311
  this.requests.add(id);
305
312
  const localUser = await this._signer.user();
313
+
314
+ if (!localUser.pubkey) {
315
+ throw new Error('CORRUPTION: Missing local pubkey. Signer state compromised.');
316
+ }
317
+ if (!remotePubkey) {
318
+ throw new Error('CORRUPTION: Missing remote pubkey. Signer state compromised.');
319
+ }
320
+
306
321
  const remoteUser = this._ndk.getUser({ pubkey: remotePubkey });
307
322
  const request = { id, method, params };
308
323
 
@@ -507,6 +522,18 @@ export class Nip46Signer extends NDKNip46Signer {
507
522
  await this.initUserPubkey(sameAsUser ? signerPubkey : '');
508
523
  }
509
524
 
525
+ public forceResetState() {
526
+ console.log('Nip46Signer state reset due to connection failure or timeout.');
527
+
528
+ // 1. ユーザー公開鍵をクリア
529
+ this._userPubkey = '';
530
+
531
+ // 2. リモート公開鍵をクリア
532
+ this.remotePubkey = ''; // 親クラスのプロパティをクリア
533
+
534
+ // 3. RPC側のタイマーとリクエストを、公開メソッド経由でクリア
535
+ (this._rpc as NostrRpc).cleanupTimersAndRequests();
536
+ }
510
537
  public async initUserPubkey(hintPubkey?: string) {
511
538
  if (this._userPubkey) throw new Error('Already called initUserPubkey');
512
539
 
@@ -40,7 +40,7 @@ class ProcessManager extends EventEmitter {
40
40
  if (!this.callTimer) {
41
41
  this.callTimer = setTimeout(() => {
42
42
  console.log('ProcessManager: timeout reached, emitting onCallTimeout');
43
- this.callTimer = undefined; // タイムアウト時にタイマーIDをクリア
43
+ this.callTimer = undefined; // タイムアウト時にタイマーIDをクリア
44
44
  this.emit('onCallTimeout');
45
45
  }, CALL_TIMEOUT);
46
46
  console.log(`Setting up timeout timer for ${CALL_TIMEOUT} ms`);
@@ -52,30 +52,41 @@ class ProcessManager extends EventEmitter {
52
52
 
53
53
  this.callCount++;
54
54
 
55
- let error;
56
- let result;
55
+ let error: any; // 型をanyに変更
56
+ let result: any;
57
57
 
58
+ // 非同期処理の実行
58
59
  try {
59
60
  result = await cb();
60
61
  } catch (e) {
61
62
  error = e;
62
63
  }
63
64
 
65
+ // ★ 修正後のクリーンアップロジック (finallyブロック相当)
66
+ // ----------------------------------------------------
67
+
68
+ // ProcessManagerの呼び出しカウントをデクリメント
64
69
  this.callCount--;
65
70
  this.emit('onCallEnd');
66
71
 
72
+ // タイマーがまだ存在していればクリアし、IDもクリア
73
+ // この処理は、cb()が成功/失敗したどちらの場合でも実行される
67
74
  if (this.callTimer) {
68
75
  clearTimeout(this.callTimer);
69
76
  }
70
77
  this.callTimer = undefined; // ProcessManager のタイマーIDもここでクリア
71
78
 
79
+ // ----------------------------------------------------
80
+
81
+ // エラーがあればスローし、呼び出し元に伝播させる
72
82
  if (error) {
73
83
  throw error;
74
84
  }
75
85
 
76
86
  // @ts-ignore
77
- return result;
87
+ return result as T;
78
88
  }
89
+
79
90
  public pause() {
80
91
  if (this.callTimer) clearTimeout(this.callTimer);
81
92
  this.callTimer = undefined;