@konemono/nostr-login 1.11.8 → 1.11.10
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 +2 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/modules/Nip46.d.ts +6 -4
- package/dist/unpkg.js +2 -2
- package/package.json +2 -1
- package/src/modules/AuthNostrService.ts +10 -0
- package/src/modules/Nip46.ts +36 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@konemono/nostr-login",
|
|
3
|
-
"version": "1.11.
|
|
3
|
+
"version": "1.11.10",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.esm.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"author": "a-fralou",
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@nostr-dev-kit/ndk": "^2.3.1",
|
|
15
|
+
"events": "^3.3.0",
|
|
15
16
|
"nostr-tools": "^1.17.0",
|
|
16
17
|
"tseep": "^1.2.1"
|
|
17
18
|
},
|
|
@@ -610,6 +610,16 @@ 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
|
+
|
|
613
623
|
// we should notify the banner the same way as
|
|
614
624
|
// the onAuthUrl does
|
|
615
625
|
this.signer.on(`iframeRestart`, async () => {
|
package/src/modules/Nip46.ts
CHANGED
|
@@ -15,7 +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
|
-
|
|
18
|
+
protected eventEmitter: EventEmitter = new EventEmitter();
|
|
19
19
|
|
|
20
20
|
public constructor(ndk: NDK, signer: PrivateKeySigner) {
|
|
21
21
|
super(ndk, signer, ndk.debug.extend('nip46:signer:rpc'));
|
|
@@ -157,6 +157,14 @@ class NostrRpc extends NDKNostrRpc {
|
|
|
157
157
|
return Math.random().toString(36).substring(7);
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
+
public override once = <EventKey extends string | symbol = string>(
|
|
161
|
+
event: EventKey,
|
|
162
|
+
listener: (...args: any[]) => void
|
|
163
|
+
): this => {
|
|
164
|
+
this.eventEmitter.once(event as string, listener);
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
|
|
160
168
|
public async sendRequest(remotePubkey: string, method: string, params: string[] = [], kind = 24133, cb?: (res: NDKRpcResponse) => void): Promise<NDKRpcResponse> {
|
|
161
169
|
const id = this.getId();
|
|
162
170
|
|
|
@@ -378,7 +386,7 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
378
386
|
private _rpc: IframeNostrRpc;
|
|
379
387
|
private lastPingTime: number = 0;
|
|
380
388
|
private pingCacheDuration: number = 30000; // 30秒
|
|
381
|
-
|
|
389
|
+
|
|
382
390
|
|
|
383
391
|
constructor(ndk: NDK, localSigner: PrivateKeySigner, signerPubkey: string, iframeOrigin?: string) {
|
|
384
392
|
super(ndk, signerPubkey, localSigner);
|
|
@@ -391,28 +399,20 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
391
399
|
});
|
|
392
400
|
|
|
393
401
|
this.rpc = this._rpc;
|
|
394
|
-
this._remotePubkey = signerPubkey;
|
|
395
402
|
}
|
|
396
403
|
|
|
397
404
|
get userPubkey() {
|
|
398
405
|
return this._userPubkey;
|
|
399
406
|
}
|
|
400
407
|
|
|
401
|
-
// Use a different name to avoid conflict with base class property
|
|
402
|
-
get remotePubkeyAccessor() {
|
|
403
|
-
return this._remotePubkey;
|
|
404
|
-
}
|
|
405
408
|
|
|
406
|
-
set remotePubkeyAccessor(value: string | undefined) {
|
|
407
|
-
this._remotePubkey = value;
|
|
408
|
-
}
|
|
409
409
|
|
|
410
410
|
// Nip46.tsのNip46Signerクラス内
|
|
411
411
|
// 接続確認(必要時のみping リトライ付き 最大10秒)
|
|
412
412
|
// リトライ回数: 2回(計3回試行)
|
|
413
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,7 +423,7 @@ 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;
|
|
@@ -444,7 +444,7 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
444
444
|
console.log('setSignerPubkey', signerPubkey);
|
|
445
445
|
|
|
446
446
|
// ensure it's set
|
|
447
|
-
this.
|
|
447
|
+
this.remotePubkey = signerPubkey;
|
|
448
448
|
|
|
449
449
|
// when we're sure it's known
|
|
450
450
|
this._rpc.on(`iframeRestart-${signerPubkey}`, () => {
|
|
@@ -465,10 +465,10 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
465
465
|
|
|
466
466
|
this._userPubkey = await withTimeout(
|
|
467
467
|
new Promise<string>((ok, err) => {
|
|
468
|
-
if (!this.
|
|
468
|
+
if (!this.remotePubkey) throw new Error('Signer pubkey not set');
|
|
469
469
|
|
|
470
|
-
console.log('get_public_key', this.
|
|
471
|
-
this._rpc.sendRequest(this.
|
|
470
|
+
console.log('get_public_key', this.remotePubkey);
|
|
471
|
+
this._rpc.sendRequest(this.remotePubkey, 'get_public_key', [], 24133, (response: NDKRpcResponse) => {
|
|
472
472
|
if (response.error) {
|
|
473
473
|
err(new Error(response.error));
|
|
474
474
|
} else {
|
|
@@ -490,9 +490,9 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
490
490
|
}
|
|
491
491
|
|
|
492
492
|
public async connect(token?: string, perms?: string) {
|
|
493
|
-
if (!this.
|
|
494
|
-
await this._rpc.connectWithTimeout(this.
|
|
495
|
-
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);
|
|
496
496
|
|
|
497
497
|
// ログイン完了後に接続確認
|
|
498
498
|
await this.ensureConnection();
|
|
@@ -543,6 +543,23 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
543
543
|
}
|
|
544
544
|
|
|
545
545
|
// EventEmitter互換メソッド
|
|
546
|
+
// ★ ここに once を追加 ★
|
|
547
|
+
public override on = <EventKey extends string | symbol = string>(
|
|
548
|
+
event: EventKey,
|
|
549
|
+
listener: (...args: any[]) => void
|
|
550
|
+
): this => {
|
|
551
|
+
this._rpc.on(event as string, listener);
|
|
552
|
+
return this;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
public override once = <EventKey extends string | symbol = string>(
|
|
556
|
+
event: EventKey,
|
|
557
|
+
listener: (...args: any[]) => void
|
|
558
|
+
): this => {
|
|
559
|
+
this._rpc.once(event as string, listener);
|
|
560
|
+
return this;
|
|
561
|
+
}
|
|
562
|
+
|
|
546
563
|
public override emit = <EventKey extends string | symbol = string>(
|
|
547
564
|
event: EventKey,
|
|
548
565
|
...args: any[]
|