@konemono/nostr-login 1.11.4 → 1.11.6
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/index.esm.js.map +1 -1
- package/dist/modules/Nip46.d.ts +6 -1
- package/dist/unpkg.js +1 -1
- package/package.json +1 -1
- package/src/modules/AuthNostrService.ts +11 -0
- package/src/modules/Nip46.ts +60 -3
package/package.json
CHANGED
|
@@ -610,6 +610,17 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
610
610
|
const localSigner = new PrivateKeySigner(info.sk!);
|
|
611
611
|
this.signer = new Nip46Signer(this.ndk, localSigner, info.signerPubkey!, info.iframeUrl ? new URL(info.iframeUrl!).origin : undefined);
|
|
612
612
|
|
|
613
|
+
// ★ once を使う - 1回だけ実行される ★
|
|
614
|
+
this.signer.once('connectionLost', async () => {
|
|
615
|
+
console.log('Connection lost, attempting to reconnect...');
|
|
616
|
+
this.signer = null;
|
|
617
|
+
|
|
618
|
+
if (this.params.userInfo) {
|
|
619
|
+
await this.initSigner(this.params.userInfo);
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
|
|
613
624
|
// we should notify the banner the same way as
|
|
614
625
|
// the onAuthUrl does
|
|
615
626
|
this.signer.on(`iframeRestart`, async () => {
|
package/src/modules/Nip46.ts
CHANGED
|
@@ -15,7 +15,8 @@ class NostrRpc extends NDKNostrRpc {
|
|
|
15
15
|
protected requests: Set<string> = new Set();
|
|
16
16
|
private sub?: NDKSubscription;
|
|
17
17
|
protected _useNip44: boolean = false;
|
|
18
|
-
|
|
18
|
+
protected eventEmitter: EventEmitter = new EventEmitter();
|
|
19
|
+
|
|
19
20
|
|
|
20
21
|
public constructor(ndk: NDK, signer: PrivateKeySigner) {
|
|
21
22
|
super(ndk, signer, ndk.debug.extend('nip46:signer:rpc'));
|
|
@@ -233,6 +234,14 @@ class NostrRpc extends NDKNostrRpc {
|
|
|
233
234
|
return this;
|
|
234
235
|
}
|
|
235
236
|
|
|
237
|
+
public override once = <EventKey extends string | symbol = string>(
|
|
238
|
+
event: EventKey,
|
|
239
|
+
listener: (...args: any[]) => void
|
|
240
|
+
): this => {
|
|
241
|
+
this.eventEmitter.once(event as string, listener);
|
|
242
|
+
return this;
|
|
243
|
+
}
|
|
244
|
+
|
|
236
245
|
public override emit = <EventKey extends string | symbol = string>(
|
|
237
246
|
event: EventKey,
|
|
238
247
|
...args: any[]
|
|
@@ -373,6 +382,7 @@ export class ReadyListener {
|
|
|
373
382
|
}
|
|
374
383
|
}
|
|
375
384
|
|
|
385
|
+
|
|
376
386
|
export class Nip46Signer extends NDKNip46Signer {
|
|
377
387
|
private _userPubkey: string = '';
|
|
378
388
|
private _rpc: IframeNostrRpc;
|
|
@@ -426,8 +436,38 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
426
436
|
return;
|
|
427
437
|
} catch (error) {
|
|
428
438
|
if (i === retries) {
|
|
429
|
-
console.error('
|
|
430
|
-
|
|
439
|
+
console.error('Ping failed, attempting reconnection');
|
|
440
|
+
|
|
441
|
+
// 再接続を試みる
|
|
442
|
+
try {
|
|
443
|
+
this.emit('connectionLost');
|
|
444
|
+
|
|
445
|
+
// 再接続完了を待つ(最大10秒)
|
|
446
|
+
await new Promise<void>((resolve, reject) => {
|
|
447
|
+
const timeout = setTimeout(() => reject(new Error('Reconnection timeout')), 10000);
|
|
448
|
+
|
|
449
|
+
const checkReconnection = async () => {
|
|
450
|
+
try {
|
|
451
|
+
await this._rpc.pingWithTimeout(this._remotePubkey!, 10000);
|
|
452
|
+
clearTimeout(timeout);
|
|
453
|
+
this.lastPingTime = Date.now();
|
|
454
|
+
console.log('Reconnection successful');
|
|
455
|
+
resolve();
|
|
456
|
+
} catch (e) {
|
|
457
|
+
// まだ再接続中、1秒後に再確認
|
|
458
|
+
setTimeout(checkReconnection, 1000);
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
// 少し待ってから確認開始
|
|
463
|
+
setTimeout(checkReconnection, 2000);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
return;
|
|
467
|
+
} catch (reconnectError) {
|
|
468
|
+
console.error('Reconnection failed:', reconnectError);
|
|
469
|
+
throw new Error('NIP-46 connection lost and reconnection failed');
|
|
470
|
+
}
|
|
431
471
|
}
|
|
432
472
|
|
|
433
473
|
const delay = Math.min(1000 * Math.pow(2, i), 5000);
|
|
@@ -540,6 +580,23 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
540
580
|
}
|
|
541
581
|
|
|
542
582
|
// EventEmitter互換メソッド
|
|
583
|
+
// ★ ここに once を追加 ★
|
|
584
|
+
public override on = <EventKey extends string | symbol = string>(
|
|
585
|
+
event: EventKey,
|
|
586
|
+
listener: (...args: any[]) => void
|
|
587
|
+
): this => {
|
|
588
|
+
this._rpc.on(event as string, listener);
|
|
589
|
+
return this;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
public override once = <EventKey extends string | symbol = string>(
|
|
593
|
+
event: EventKey,
|
|
594
|
+
listener: (...args: any[]) => void
|
|
595
|
+
): this => {
|
|
596
|
+
this._rpc.once(event as string, listener);
|
|
597
|
+
return this;
|
|
598
|
+
}
|
|
599
|
+
|
|
543
600
|
public override emit = <EventKey extends string | symbol = string>(
|
|
544
601
|
event: EventKey,
|
|
545
602
|
...args: any[]
|