@konemono/nostr-login 1.7.33 → 1.7.35

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.33",
3
+ "version": "1.7.35",
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
  }
@@ -305,6 +310,14 @@ class NostrRpc extends NDKNostrRpc {
305
310
  protected async createRequestEvent(id: string, remotePubkey: string, method: string, params: string[] = [], kind = 24133) {
306
311
  this.requests.add(id);
307
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
+
308
321
  const remoteUser = this._ndk.getUser({ pubkey: remotePubkey });
309
322
  const request = { id, method, params };
310
323
 
@@ -388,6 +401,7 @@ export class IframeNostrRpc extends NostrRpc {
388
401
 
389
402
  if (!this.iframePort) {
390
403
  try {
404
+ console.log(this.processManager);
391
405
  this.processManager?.pause();
392
406
  await this.ensureConnected();
393
407
  } catch (e) {
@@ -509,6 +523,18 @@ export class Nip46Signer extends NDKNip46Signer {
509
523
  await this.initUserPubkey(sameAsUser ? signerPubkey : '');
510
524
  }
511
525
 
526
+ public forceResetState() {
527
+ console.log('Nip46Signer state reset due to connection failure or timeout.');
528
+
529
+ // 1. ユーザー公開鍵をクリア
530
+ this._userPubkey = '';
531
+
532
+ // 2. リモート公開鍵をクリア
533
+ this.remotePubkey = ''; // 親クラスのプロパティをクリア
534
+
535
+ // 3. RPC側のタイマーとリクエストを、公開メソッド経由でクリア
536
+ (this._rpc as NostrRpc).cleanupTimersAndRequests();
537
+ }
512
538
  public async initUserPubkey(hintPubkey?: string) {
513
539
  if (this._userPubkey) throw new Error('Already called initUserPubkey');
514
540
 
@@ -21,13 +21,18 @@ class ProcessManager extends EventEmitter {
21
21
  }
22
22
 
23
23
  private resetTimer() {
24
+ // 既存のタイマーがあればクリア
24
25
  if (this.callTimer) {
25
26
  clearTimeout(this.callTimer);
27
+ this.callTimer = undefined; // IDをクリア
26
28
  console.log('ProcessManager: timer reset');
27
29
  }
28
- if (this.callCount > 0) {
30
+
31
+ // 監視対象が残っていて、かつ一時停止中でなければ、新しいタイマーを設定
32
+ if (this.callCount > 0 && !this.paused) {
29
33
  this.callTimer = setTimeout(() => {
30
34
  console.log('ProcessManager: timeout reached, emitting onCallTimeout');
35
+ this.callTimer = undefined; // タイムアウト時にIDをクリア
31
36
  this.emit('onCallTimeout');
32
37
  }, CALL_TIMEOUT);
33
38
  console.log(`ProcessManager: new timer set for ${CALL_TIMEOUT} ms`);
@@ -37,13 +42,10 @@ class ProcessManager extends EventEmitter {
37
42
  public async wait<T>(cb: () => Promise<T>): Promise<T> {
38
43
  console.log('ProcessManager.wait called, callTimer exists:', !!this.callTimer, 'callCount:', this.callCount);
39
44
 
40
- if (!this.callTimer) {
41
- this.callTimer = setTimeout(() => {
42
- console.log('ProcessManager: timeout reached, emitting onCallTimeout');
43
- this.callTimer = undefined; // ★ タイムアウト時にタイマーIDをクリア
44
- this.emit('onCallTimeout');
45
- }, CALL_TIMEOUT);
46
- console.log(`Setting up timeout timer for ${CALL_TIMEOUT} ms`);
45
+ // 修正点: タイマーの初期設定を resetTimer に任せる
46
+ if (!this.callTimer && this.callCount === 0) {
47
+ // waitの最初の呼び出し時のみ、タイマーを初期設定
48
+ this.resetTimer();
47
49
  }
48
50
 
49
51
  if (!this.callCount) {
@@ -52,7 +54,7 @@ class ProcessManager extends EventEmitter {
52
54
 
53
55
  this.callCount++;
54
56
 
55
- let error: any; // 型をanyに変更
57
+ let error: any;
56
58
  let result: any;
57
59
 
58
60
  // 非同期処理の実行
@@ -62,21 +64,13 @@ class ProcessManager extends EventEmitter {
62
64
  error = e;
63
65
  }
64
66
 
65
- // ★ 修正後のクリーンアップロジック (finallyブロック相当)
66
- // ----------------------------------------------------
67
-
67
+ // ★ 修正点: クリーンアップロジックを resetTimer に置き換え
68
68
  // ProcessManagerの呼び出しカウントをデクリメント
69
69
  this.callCount--;
70
70
  this.emit('onCallEnd');
71
71
 
72
- // タイマーがまだ存在していればクリアし、IDもクリア
73
- // この処理は、cb()が成功/失敗したどちらの場合でも実行される
74
- if (this.callTimer) {
75
- clearTimeout(this.callTimer);
76
- }
77
- this.callTimer = undefined; // ProcessManager のタイマーIDもここでクリア
78
-
79
- // ----------------------------------------------------
72
+ // リクエスト完了後、タイマーをリセットし、callCountに応じて再設定
73
+ this.resetTimer();
80
74
 
81
75
  // エラーがあればスローし、呼び出し元に伝播させる
82
76
  if (error) {
@@ -88,18 +82,24 @@ class ProcessManager extends EventEmitter {
88
82
  }
89
83
 
90
84
  public pause() {
91
- if (this.callTimer) clearTimeout(this.callTimer);
92
- this.callTimer = undefined;
85
+ console.log('ProcessManager: PAUSING timer...');
86
+ if (this.callTimer) {
87
+ clearTimeout(this.callTimer);
88
+ }
89
+ this.callTimer = undefined; // タイマーをクリア
93
90
  this.paused = true;
91
+ this.emit('onCallPause');
94
92
  }
95
93
 
96
94
  public resume() {
95
+ console.log('ProcessManager: RESUMING timer...');
97
96
  this.paused = false;
97
+
98
+ // ★ 修正点: pause解除後、監視対象が残っていればタイマーを再設定 (resetTimerを呼び出す)
98
99
  if (this.callCount > 0 && !this.callTimer) {
99
- this.callTimer = setTimeout(() => {
100
- this.emit('onCallTimeout');
101
- }, CALL_TIMEOUT);
100
+ this.resetTimer();
102
101
  }
102
+ this.emit('onCallResume');
103
103
  }
104
104
  }
105
105