@konemono/nostr-login 1.11.7 → 1.11.8
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 +4 -6
- package/dist/unpkg.js +1 -1
- package/package.json +1 -2
- package/src/modules/AuthNostrService.ts +0 -12
- package/src/modules/Nip46.ts +28 -76
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@konemono/nostr-login",
|
|
3
|
-
"version": "1.11.
|
|
3
|
+
"version": "1.11.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.esm.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
"author": "a-fralou",
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@nostr-dev-kit/ndk": "^2.3.1",
|
|
15
|
-
"events": "^3.3.0",
|
|
16
15
|
"nostr-tools": "^1.17.0",
|
|
17
16
|
"tseep": "^1.2.1"
|
|
18
17
|
},
|
|
@@ -610,18 +610,6 @@ 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
|
-
|
|
614
|
-
// ★ once を使う - 1回だけ実行される ★
|
|
615
|
-
this.signer.once('connectionLost', async () => {
|
|
616
|
-
console.log('Connection lost, attempting to reconnect...');
|
|
617
|
-
this.signer = null;
|
|
618
|
-
|
|
619
|
-
if (this.params.userInfo) {
|
|
620
|
-
await this.initSigner(this.params.userInfo);
|
|
621
|
-
}
|
|
622
|
-
});
|
|
623
|
-
|
|
624
|
-
|
|
625
613
|
// we should notify the banner the same way as
|
|
626
614
|
// the onAuthUrl does
|
|
627
615
|
this.signer.on(`iframeRestart`, async () => {
|
package/src/modules/Nip46.ts
CHANGED
|
@@ -15,8 +15,7 @@ class NostrRpc extends NDKNostrRpc {
|
|
|
15
15
|
protected requests: Set<string> = new Set();
|
|
16
16
|
private sub?: NDKSubscription;
|
|
17
17
|
protected _useNip44: boolean = false;
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
private eventEmitter: EventEmitter = new EventEmitter();
|
|
20
19
|
|
|
21
20
|
public constructor(ndk: NDK, signer: PrivateKeySigner) {
|
|
22
21
|
super(ndk, signer, ndk.debug.extend('nip46:signer:rpc'));
|
|
@@ -150,7 +149,7 @@ class NostrRpc extends NDKNostrRpc {
|
|
|
150
149
|
}
|
|
151
150
|
|
|
152
151
|
// タイムアウト対応のping
|
|
153
|
-
public async pingWithTimeout(remotePubkey: string, timeoutMs: number =
|
|
152
|
+
public async pingWithTimeout(remotePubkey: string, timeoutMs: number = 3000): Promise<void> {
|
|
154
153
|
return withTimeout(this.ping(remotePubkey), timeoutMs, `Ping timeout after ${timeoutMs}ms`);
|
|
155
154
|
}
|
|
156
155
|
|
|
@@ -234,14 +233,6 @@ class NostrRpc extends NDKNostrRpc {
|
|
|
234
233
|
return this;
|
|
235
234
|
}
|
|
236
235
|
|
|
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
|
-
|
|
245
236
|
public override emit = <EventKey extends string | symbol = string>(
|
|
246
237
|
event: EventKey,
|
|
247
238
|
...args: any[]
|
|
@@ -382,13 +373,12 @@ export class ReadyListener {
|
|
|
382
373
|
}
|
|
383
374
|
}
|
|
384
375
|
|
|
385
|
-
|
|
386
376
|
export class Nip46Signer extends NDKNip46Signer {
|
|
387
377
|
private _userPubkey: string = '';
|
|
388
378
|
private _rpc: IframeNostrRpc;
|
|
389
379
|
private lastPingTime: number = 0;
|
|
390
380
|
private pingCacheDuration: number = 30000; // 30秒
|
|
391
|
-
|
|
381
|
+
private _remotePubkey?: string;
|
|
392
382
|
|
|
393
383
|
constructor(ndk: NDK, localSigner: PrivateKeySigner, signerPubkey: string, iframeOrigin?: string) {
|
|
394
384
|
super(ndk, signerPubkey, localSigner);
|
|
@@ -401,18 +391,28 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
401
391
|
});
|
|
402
392
|
|
|
403
393
|
this.rpc = this._rpc;
|
|
404
|
-
|
|
394
|
+
this._remotePubkey = signerPubkey;
|
|
405
395
|
}
|
|
406
396
|
|
|
407
397
|
get userPubkey() {
|
|
408
398
|
return this._userPubkey;
|
|
409
399
|
}
|
|
410
400
|
|
|
401
|
+
// Use a different name to avoid conflict with base class property
|
|
402
|
+
get remotePubkeyAccessor() {
|
|
403
|
+
return this._remotePubkey;
|
|
404
|
+
}
|
|
411
405
|
|
|
406
|
+
set remotePubkeyAccessor(value: string | undefined) {
|
|
407
|
+
this._remotePubkey = value;
|
|
408
|
+
}
|
|
412
409
|
|
|
413
|
-
//
|
|
410
|
+
// Nip46.tsのNip46Signerクラス内
|
|
411
|
+
// 接続確認(必要時のみping リトライ付き 最大10秒)
|
|
412
|
+
// リトライ回数: 2回(計3回試行)
|
|
413
|
+
//合計最大時間: 2秒(ping) × 3回 + 2秒(待機) × 2回 = 10秒
|
|
414
414
|
private async ensureConnection(retries: number = 2): Promise<void> {
|
|
415
|
-
if (!this.
|
|
415
|
+
if (!this._remotePubkey) return;
|
|
416
416
|
|
|
417
417
|
const now = Date.now();
|
|
418
418
|
|
|
@@ -423,48 +423,18 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
423
423
|
|
|
424
424
|
for (let i = 0; i <= retries; i++) {
|
|
425
425
|
try {
|
|
426
|
-
await this._rpc.pingWithTimeout(this.
|
|
426
|
+
await this._rpc.pingWithTimeout(this._remotePubkey, 2000); // 2秒タイムアウト
|
|
427
427
|
this.lastPingTime = now;
|
|
428
428
|
console.log('Connection check OK');
|
|
429
429
|
return;
|
|
430
430
|
} catch (error) {
|
|
431
431
|
if (i === retries) {
|
|
432
|
-
console.error('
|
|
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
|
-
}
|
|
432
|
+
console.error('Connection check failed after retries', error);
|
|
433
|
+
throw new Error('NIP-46 connection lost');
|
|
464
434
|
}
|
|
465
435
|
|
|
466
|
-
const delay =
|
|
467
|
-
console.log(`Ping failed, retrying in ${delay}ms...`);
|
|
436
|
+
const delay = 2000; // 2秒間隔で再送
|
|
437
|
+
console.log(`Ping failed (${i + 1}/${retries + 1}), retrying in ${delay}ms...`);
|
|
468
438
|
await new Promise(resolve => setTimeout(resolve, delay));
|
|
469
439
|
}
|
|
470
440
|
}
|
|
@@ -473,9 +443,8 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
473
443
|
private async setSignerPubkey(signerPubkey: string, sameAsUser: boolean = false) {
|
|
474
444
|
console.log('setSignerPubkey', signerPubkey);
|
|
475
445
|
|
|
476
|
-
|
|
477
446
|
// ensure it's set
|
|
478
|
-
this.
|
|
447
|
+
this._remotePubkey = signerPubkey;
|
|
479
448
|
|
|
480
449
|
// when we're sure it's known
|
|
481
450
|
this._rpc.on(`iframeRestart-${signerPubkey}`, () => {
|
|
@@ -496,10 +465,10 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
496
465
|
|
|
497
466
|
this._userPubkey = await withTimeout(
|
|
498
467
|
new Promise<string>((ok, err) => {
|
|
499
|
-
if (!this.
|
|
468
|
+
if (!this._remotePubkey) throw new Error('Signer pubkey not set');
|
|
500
469
|
|
|
501
|
-
console.log('get_public_key', this.
|
|
502
|
-
this._rpc.sendRequest(this.
|
|
470
|
+
console.log('get_public_key', this._remotePubkey);
|
|
471
|
+
this._rpc.sendRequest(this._remotePubkey, 'get_public_key', [], 24133, (response: NDKRpcResponse) => {
|
|
503
472
|
if (response.error) {
|
|
504
473
|
err(new Error(response.error));
|
|
505
474
|
} else {
|
|
@@ -521,9 +490,9 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
521
490
|
}
|
|
522
491
|
|
|
523
492
|
public async connect(token?: string, perms?: string) {
|
|
524
|
-
if (!this.
|
|
525
|
-
await this._rpc.connectWithTimeout(this.
|
|
526
|
-
await this.setSignerPubkey(this.
|
|
493
|
+
if (!this._remotePubkey) throw new Error('No signer pubkey');
|
|
494
|
+
await this._rpc.connectWithTimeout(this._remotePubkey, token, perms, NIP46_CONNECT_TIMEOUT);
|
|
495
|
+
await this.setSignerPubkey(this._remotePubkey);
|
|
527
496
|
|
|
528
497
|
// ログイン完了後に接続確認
|
|
529
498
|
await this.ensureConnection();
|
|
@@ -574,23 +543,6 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
574
543
|
}
|
|
575
544
|
|
|
576
545
|
// 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
|
-
|
|
594
546
|
public override emit = <EventKey extends string | symbol = string>(
|
|
595
547
|
event: EventKey,
|
|
596
548
|
...args: any[]
|