@konemono/nostr-login 1.11.5 → 1.11.7
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 -4
- package/dist/unpkg.js +1 -1
- package/package.json +1 -1
- package/src/modules/AuthNostrService.ts +1 -0
- package/src/modules/Nip46.ts +90 -41
package/package.json
CHANGED
|
@@ -610,6 +610,7 @@ 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
|
+
|
|
613
614
|
// ★ once を使う - 1回だけ実行される ★
|
|
614
615
|
this.signer.once('connectionLost', async () => {
|
|
615
616
|
console.log('Connection lost, attempting to reconnect...');
|
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,12 +382,13 @@ 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;
|
|
379
389
|
private lastPingTime: number = 0;
|
|
380
390
|
private pingCacheDuration: number = 30000; // 30秒
|
|
381
|
-
|
|
391
|
+
|
|
382
392
|
|
|
383
393
|
constructor(ndk: NDK, localSigner: PrivateKeySigner, signerPubkey: string, iframeOrigin?: string) {
|
|
384
394
|
super(ndk, signerPubkey, localSigner);
|
|
@@ -391,59 +401,81 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
391
401
|
});
|
|
392
402
|
|
|
393
403
|
this.rpc = this._rpc;
|
|
394
|
-
|
|
404
|
+
|
|
395
405
|
}
|
|
396
406
|
|
|
397
407
|
get userPubkey() {
|
|
398
408
|
return this._userPubkey;
|
|
399
409
|
}
|
|
400
410
|
|
|
401
|
-
// Use a different name to avoid conflict with base class property
|
|
402
|
-
get remotePubkeyAccessor() {
|
|
403
|
-
return this._remotePubkey;
|
|
404
|
-
}
|
|
405
411
|
|
|
406
|
-
set remotePubkeyAccessor(value: string | undefined) {
|
|
407
|
-
this._remotePubkey = value;
|
|
408
|
-
}
|
|
409
412
|
|
|
410
413
|
// 接続確認(必要時のみping)
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
if (!this._remotePubkey) return;
|
|
414
|
-
|
|
415
|
-
const now = Date.now();
|
|
414
|
+
private async ensureConnection(retries: number = 2): Promise<void> {
|
|
415
|
+
if (!this.remotePubkey) return;
|
|
416
416
|
|
|
417
|
-
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
417
|
+
const now = Date.now();
|
|
420
418
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
await this._rpc.pingWithTimeout(this._remotePubkey, 10000);
|
|
424
|
-
this.lastPingTime = now;
|
|
425
|
-
console.log('Connection check OK');
|
|
419
|
+
// 最近ping成功していればスキップ
|
|
420
|
+
if (now - this.lastPingTime < this.pingCacheDuration) {
|
|
426
421
|
return;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
this.
|
|
432
|
-
|
|
433
|
-
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
for (let i = 0; i <= retries; i++) {
|
|
425
|
+
try {
|
|
426
|
+
await this._rpc.pingWithTimeout(this.remotePubkey, 10000);
|
|
427
|
+
this.lastPingTime = now;
|
|
428
|
+
console.log('Connection check OK');
|
|
429
|
+
return;
|
|
430
|
+
} catch (error) {
|
|
431
|
+
if (i === retries) {
|
|
432
|
+
console.error('Ping failed, attempting reconnection');
|
|
433
|
+
|
|
434
|
+
// 再接続を試みる
|
|
435
|
+
try {
|
|
436
|
+
this.emit('connectionLost');
|
|
437
|
+
|
|
438
|
+
// 再接続完了を待つ(最大10秒)
|
|
439
|
+
await new Promise<void>((resolve, reject) => {
|
|
440
|
+
const timeout = setTimeout(() => reject(new Error('Reconnection timeout')), 10000);
|
|
441
|
+
|
|
442
|
+
const checkReconnection = async () => {
|
|
443
|
+
try {
|
|
444
|
+
await this._rpc.pingWithTimeout(this.remotePubkey!, 10000);
|
|
445
|
+
clearTimeout(timeout);
|
|
446
|
+
this.lastPingTime = Date.now();
|
|
447
|
+
console.log('Reconnection successful');
|
|
448
|
+
resolve();
|
|
449
|
+
} catch (e) {
|
|
450
|
+
// まだ再接続中、1秒後に再確認
|
|
451
|
+
setTimeout(checkReconnection, 1000);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
// 少し待ってから確認開始
|
|
456
|
+
setTimeout(checkReconnection, 2000);
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
return;
|
|
460
|
+
} catch (reconnectError) {
|
|
461
|
+
console.error('Reconnection failed:', reconnectError);
|
|
462
|
+
throw new Error('NIP-46 connection lost and reconnection failed');
|
|
463
|
+
}
|
|
464
|
+
}
|
|
434
465
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
466
|
+
const delay = Math.min(1000 * Math.pow(2, i), 5000);
|
|
467
|
+
console.log(`Ping failed, retrying in ${delay}ms...`);
|
|
468
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
469
|
+
}
|
|
438
470
|
}
|
|
439
471
|
}
|
|
440
|
-
}
|
|
441
472
|
|
|
442
473
|
private async setSignerPubkey(signerPubkey: string, sameAsUser: boolean = false) {
|
|
443
474
|
console.log('setSignerPubkey', signerPubkey);
|
|
444
475
|
|
|
476
|
+
|
|
445
477
|
// ensure it's set
|
|
446
|
-
this.
|
|
478
|
+
this.remotePubkey = signerPubkey;
|
|
447
479
|
|
|
448
480
|
// when we're sure it's known
|
|
449
481
|
this._rpc.on(`iframeRestart-${signerPubkey}`, () => {
|
|
@@ -464,10 +496,10 @@ private async ensureConnection(retries: number = 2): Promise<void> {
|
|
|
464
496
|
|
|
465
497
|
this._userPubkey = await withTimeout(
|
|
466
498
|
new Promise<string>((ok, err) => {
|
|
467
|
-
if (!this.
|
|
499
|
+
if (!this.remotePubkey) throw new Error('Signer pubkey not set');
|
|
468
500
|
|
|
469
|
-
console.log('get_public_key', this.
|
|
470
|
-
this._rpc.sendRequest(this.
|
|
501
|
+
console.log('get_public_key', this.remotePubkey);
|
|
502
|
+
this._rpc.sendRequest(this.remotePubkey, 'get_public_key', [], 24133, (response: NDKRpcResponse) => {
|
|
471
503
|
if (response.error) {
|
|
472
504
|
err(new Error(response.error));
|
|
473
505
|
} else {
|
|
@@ -489,9 +521,9 @@ private async ensureConnection(retries: number = 2): Promise<void> {
|
|
|
489
521
|
}
|
|
490
522
|
|
|
491
523
|
public async connect(token?: string, perms?: string) {
|
|
492
|
-
if (!this.
|
|
493
|
-
await this._rpc.connectWithTimeout(this.
|
|
494
|
-
await this.setSignerPubkey(this.
|
|
524
|
+
if (!this.remotePubkey) throw new Error('No signer pubkey');
|
|
525
|
+
await this._rpc.connectWithTimeout(this.remotePubkey, token, perms, NIP46_CONNECT_TIMEOUT);
|
|
526
|
+
await this.setSignerPubkey(this.remotePubkey);
|
|
495
527
|
|
|
496
528
|
// ログイン完了後に接続確認
|
|
497
529
|
await this.ensureConnection();
|
|
@@ -542,6 +574,23 @@ private async ensureConnection(retries: number = 2): Promise<void> {
|
|
|
542
574
|
}
|
|
543
575
|
|
|
544
576
|
// EventEmitter互換メソッド
|
|
577
|
+
// ★ ここに once を追加 ★
|
|
578
|
+
public override on = <EventKey extends string | symbol = string>(
|
|
579
|
+
event: EventKey,
|
|
580
|
+
listener: (...args: any[]) => void
|
|
581
|
+
): this => {
|
|
582
|
+
this._rpc.on(event as string, listener);
|
|
583
|
+
return this;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
public override once = <EventKey extends string | symbol = string>(
|
|
587
|
+
event: EventKey,
|
|
588
|
+
listener: (...args: any[]) => void
|
|
589
|
+
): this => {
|
|
590
|
+
this._rpc.once(event as string, listener);
|
|
591
|
+
return this;
|
|
592
|
+
}
|
|
593
|
+
|
|
545
594
|
public override emit = <EventKey extends string | symbol = string>(
|
|
546
595
|
event: EventKey,
|
|
547
596
|
...args: any[]
|