@konemono/nostr-login 1.7.62 → 1.7.64
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/modules/AmberDirectSigner.d.ts +7 -7
- package/dist/modules/AuthNostrService.d.ts +1 -1
- package/dist/unpkg.js +1 -1
- package/package.json +1 -1
- package/src/index.ts +5 -1
- package/src/modules/AmberDirectSigner.ts +154 -134
- package/src/modules/AuthNostrService.ts +14 -2
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -148,7 +148,11 @@ export class NostrLoginInitializer {
|
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
private openPopup(url: string) {
|
|
151
|
-
|
|
151
|
+
if (url.startsWith('nostrsigner:')) {
|
|
152
|
+
window.location.href = url;
|
|
153
|
+
} else {
|
|
154
|
+
this.popupManager.openPopup(url);
|
|
155
|
+
}
|
|
152
156
|
}
|
|
153
157
|
|
|
154
158
|
private async switchAccount(info: Info, signup = false) {
|
|
@@ -2,141 +2,161 @@ import { AmberResponse } from '../types';
|
|
|
2
2
|
import { Signer } from './Nostr';
|
|
3
3
|
|
|
4
4
|
export class AmberDirectSigner implements Signer {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
constructor(pubkey: string = '') {
|
|
9
|
-
this._pubkey = pubkey;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
get pubkey() {
|
|
13
|
-
return this._pubkey;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
set pubkey(v: string) {
|
|
17
|
-
this._pubkey = v;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
public nip04 = {
|
|
21
|
-
encrypt: (pubkey: string, plaintext: string) => this.encrypt04(pubkey, plaintext),
|
|
22
|
-
decrypt: (pubkey: string, ciphertext: string) => this.decrypt04(pubkey, ciphertext),
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
public nip44 = {
|
|
26
|
-
encrypt: (pubkey: string, plaintext: string) => this.encrypt44(pubkey, plaintext),
|
|
27
|
-
decrypt: (pubkey: string, ciphertext: string) => this.decrypt44(pubkey, ciphertext),
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
public async getPublicKey(): Promise<string> {
|
|
31
|
-
const id = Math.random().toString(36).substring(7);
|
|
32
|
-
const url = this.generateUrl('', 'get_public_key', id);
|
|
33
|
-
window.location.href = url;
|
|
34
|
-
return new Promise((resolve) => {
|
|
35
|
-
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public async signEvent(event: any): Promise<any> {
|
|
40
|
-
const id = Math.random().toString(36).substring(7);
|
|
41
|
-
const url = this.generateUrl(JSON.stringify(event), 'sign_event', id);
|
|
42
|
-
window.location.href = url;
|
|
43
|
-
return new Promise((resolve) => {
|
|
44
|
-
AmberDirectSigner.pendingResolves.set(id, (result: string) => {
|
|
45
|
-
resolve(JSON.parse(result));
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public async encrypt04(pubkey: string, plaintext: string): Promise<string> {
|
|
51
|
-
const id = Math.random().toString(36).substring(7);
|
|
52
|
-
const url = this.generateUrl(plaintext, 'nip04_encrypt', id, pubkey);
|
|
53
|
-
window.location.href = url;
|
|
54
|
-
return new Promise((resolve) => {
|
|
55
|
-
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public async decrypt04(pubkey: string, ciphertext: string): Promise<string> {
|
|
60
|
-
const id = Math.random().toString(36).substring(7);
|
|
61
|
-
const url = this.generateUrl(ciphertext, 'nip04_decrypt', id, pubkey);
|
|
62
|
-
window.location.href = url;
|
|
63
|
-
return new Promise((resolve) => {
|
|
64
|
-
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public async encrypt44(pubkey: string, plaintext: string): Promise<string> {
|
|
69
|
-
const id = Math.random().toString(36).substring(7);
|
|
70
|
-
const url = this.generateUrl(plaintext, 'nip44_encrypt', id, pubkey);
|
|
71
|
-
window.location.href = url;
|
|
72
|
-
return new Promise((resolve) => {
|
|
73
|
-
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public async decrypt44(pubkey: string, ciphertext: string): Promise<string> {
|
|
78
|
-
const id = Math.random().toString(36).substring(7);
|
|
79
|
-
const url = this.generateUrl(ciphertext, 'nip44_decrypt', id, pubkey);
|
|
80
|
-
window.location.href = url;
|
|
81
|
-
return new Promise((resolve) => {
|
|
82
|
-
AmberDirectSigner.pendingResolves.set(id, resolve);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
private generateUrl(content: string, type: string, id: string, pubkey?: string): string {
|
|
88
|
-
const callbackUrl = window.location.href.split('#')[0].split('?')[0];
|
|
89
|
-
const encodedContent = encodeURIComponent(content || '');
|
|
90
|
-
const encodedCallback = encodeURIComponent(callbackUrl);
|
|
91
|
-
|
|
92
|
-
// ★ 追加: sessionStorageに保存(これが無い)
|
|
93
|
-
sessionStorage.setItem('amber_last_type', type);
|
|
94
|
-
sessionStorage.setItem('amber_last_id', id);
|
|
95
|
-
sessionStorage.setItem('amber_last_timestamp', Date.now().toString());
|
|
96
|
-
|
|
97
|
-
// NIP-55準拠: nostrsigner: スキーム
|
|
98
|
-
let url = `nostrsigner:${encodedContent}`;
|
|
99
|
-
url += `?compressionType=none`;
|
|
100
|
-
url += `&returnType=signature`;
|
|
101
|
-
url += `&type=${type}`;
|
|
102
|
-
url += `&callbackUrl=${encodedCallback}`;
|
|
103
|
-
if (pubkey) url += `&pubkey=${pubkey}`;
|
|
104
|
-
if (this._pubkey) url += `¤t_user=${this._pubkey}`; // ★ 追加
|
|
105
|
-
|
|
106
|
-
return url;
|
|
107
|
-
}
|
|
5
|
+
private _pubkey: string;
|
|
6
|
+
private static pendingResolves: Map<string, (result: string) => void> = new Map();
|
|
108
7
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
8
|
+
constructor(pubkey: string = '') {
|
|
9
|
+
this._pubkey = pubkey;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get pubkey() {
|
|
13
|
+
return this._pubkey;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
set pubkey(v: string) {
|
|
17
|
+
this._pubkey = v;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public nip04 = {
|
|
21
|
+
encrypt: (pubkey: string, plaintext: string) => this.encrypt04(pubkey, plaintext),
|
|
22
|
+
decrypt: (pubkey: string, ciphertext: string) => this.decrypt04(pubkey, ciphertext),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
public nip44 = {
|
|
26
|
+
encrypt: (pubkey: string, plaintext: string) => this.encrypt44(pubkey, plaintext),
|
|
27
|
+
decrypt: (pubkey: string, ciphertext: string) => this.decrypt44(pubkey, ciphertext),
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
public async getPublicKey(id?: string): Promise<string> {
|
|
31
|
+
id = id || Math.random().toString(36).substring(7);
|
|
32
|
+
const url = this.generateUrl('', 'get_public_key', id);
|
|
33
|
+
window.location.href = url;
|
|
34
|
+
return new Promise((resolve) => {
|
|
35
|
+
AmberDirectSigner.pendingResolves.set(id!, resolve);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public async signEvent(event: any, id?: string): Promise<any> {
|
|
40
|
+
id = id || Math.random().toString(36).substring(7);
|
|
41
|
+
const url = this.generateUrl(JSON.stringify(event), 'sign_event', id);
|
|
42
|
+
window.location.href = url;
|
|
43
|
+
return new Promise((resolve) => {
|
|
44
|
+
AmberDirectSigner.pendingResolves.set(id!, (result: string) => {
|
|
45
|
+
resolve(JSON.parse(result));
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async encrypt04(pubkey: string, plaintext: string, id?: string): Promise<string> {
|
|
51
|
+
id = id || Math.random().toString(36).substring(7);
|
|
52
|
+
const url = this.generateUrl(plaintext, 'nip04_encrypt', id, pubkey);
|
|
53
|
+
window.location.href = url;
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
AmberDirectSigner.pendingResolves.set(id!, resolve);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public async decrypt04(pubkey: string, ciphertext: string, id?: string): Promise<string> {
|
|
60
|
+
id = id || Math.random().toString(36).substring(7);
|
|
61
|
+
const url = this.generateUrl(ciphertext, 'nip04_decrypt', id, pubkey);
|
|
62
|
+
window.location.href = url;
|
|
63
|
+
return new Promise((resolve) => {
|
|
64
|
+
AmberDirectSigner.pendingResolves.set(id!, resolve);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public async encrypt44(pubkey: string, plaintext: string, id?: string): Promise<string> {
|
|
69
|
+
id = id || Math.random().toString(36).substring(7);
|
|
70
|
+
const url = this.generateUrl(plaintext, 'nip44_encrypt', id, pubkey);
|
|
71
|
+
window.location.href = url;
|
|
72
|
+
return new Promise((resolve) => {
|
|
73
|
+
AmberDirectSigner.pendingResolves.set(id!, resolve);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public async decrypt44(pubkey: string, ciphertext: string, id?: string): Promise<string> {
|
|
78
|
+
id = id || Math.random().toString(36).substring(7);
|
|
79
|
+
const url = this.generateUrl(ciphertext, 'nip44_decrypt', id, pubkey);
|
|
80
|
+
window.location.href = url;
|
|
81
|
+
return new Promise((resolve) => {
|
|
82
|
+
AmberDirectSigner.pendingResolves.set(id!, resolve);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
public generateUrl(content: string, type: string, id: string, pubkey?: string): string {
|
|
88
|
+
const callbackUrl = window.location.href.split('#')[0].split('?')[0];
|
|
89
|
+
const encodedContent = encodeURIComponent(content || '');
|
|
90
|
+
const encodedCallback = encodeURIComponent(callbackUrl);
|
|
91
|
+
|
|
92
|
+
// ★ 追加: sessionStorageに保存(これが無い)
|
|
93
|
+
sessionStorage.setItem('amber_last_type', type);
|
|
94
|
+
sessionStorage.setItem('amber_last_id', id);
|
|
95
|
+
sessionStorage.setItem('amber_last_timestamp', Date.now().toString());
|
|
96
|
+
|
|
97
|
+
// NIP-55準拠: nostrsigner: スキーム
|
|
98
|
+
let url = `nostrsigner:${encodedContent}`;
|
|
99
|
+
url += `?compressionType=none`;
|
|
100
|
+
url += `&returnType=signature`;
|
|
101
|
+
url += `&type=${type}`;
|
|
102
|
+
url += `&callbackUrl=${encodedCallback}`;
|
|
103
|
+
if (pubkey) url += `&pubkey=${pubkey}`;
|
|
104
|
+
if (this._pubkey) url += `¤t_user=${this._pubkey}`; // ★ 追加
|
|
105
|
+
|
|
106
|
+
return url;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// AmberDirectSigner.ts の parseResponse を拡張
|
|
110
|
+
|
|
111
|
+
static parseResponse(): AmberResponse | null {
|
|
112
|
+
const url = new URL(window.location.href);
|
|
113
|
+
|
|
114
|
+
// パターン1: NIP-55標準 (?event=...)
|
|
115
|
+
let result = url.searchParams.get('event');
|
|
116
|
+
|
|
117
|
+
// パターン2: パス末尾にhexId (/<hexId>)
|
|
118
|
+
if (!result) {
|
|
119
|
+
const pathParts = url.pathname.split('/').filter(Boolean);
|
|
120
|
+
if (pathParts.length > 0) {
|
|
121
|
+
const lastPart = pathParts[pathParts.length - 1];
|
|
122
|
+
// hexIdっぽい(64文字の16進数)
|
|
123
|
+
if (/^[0-9a-f]{64}$/i.test(lastPart)) {
|
|
124
|
+
result = lastPart;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
console.log('Amber response detection:', {
|
|
130
|
+
href: window.location.href,
|
|
131
|
+
eventParam: url.searchParams.get('event'),
|
|
132
|
+
pathResult: result,
|
|
133
|
+
sessionId: sessionStorage.getItem('amber_last_id'),
|
|
134
|
+
sessionType: sessionStorage.getItem('amber_last_type')
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
if (!result) return null;
|
|
138
|
+
|
|
139
|
+
const id = sessionStorage.getItem('amber_last_id');
|
|
140
|
+
const type = sessionStorage.getItem('amber_last_type');
|
|
141
|
+
|
|
142
|
+
sessionStorage.removeItem('amber_last_id');
|
|
143
|
+
sessionStorage.removeItem('amber_last_type');
|
|
144
|
+
sessionStorage.removeItem('amber_last_timestamp');
|
|
145
|
+
|
|
146
|
+
if (id && type) {
|
|
147
|
+
return { id, type, result };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
132
152
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
153
|
+
static resolvePending(id: string, type: string, result: string): boolean {
|
|
154
|
+
const resolve = this.pendingResolves.get(id);
|
|
155
|
+
if (resolve) {
|
|
156
|
+
this.pendingResolves.delete(id);
|
|
157
|
+
resolve(result);
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
139
161
|
}
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
162
|
}
|
|
@@ -193,9 +193,21 @@ class AuthNostrService extends EventEmitter implements Signer {
|
|
|
193
193
|
if (link && !iframeUrl) {
|
|
194
194
|
if (link === 'amber') {
|
|
195
195
|
const signer = new AmberDirectSigner();
|
|
196
|
-
|
|
196
|
+
this.amberSigner = signer;
|
|
197
|
+
|
|
198
|
+
const id = Math.random().toString(36).substring(7);
|
|
199
|
+
const url = signer.generateUrl('', 'get_public_key', id);
|
|
197
200
|
this.emit('onAuthUrl', { url });
|
|
198
|
-
|
|
201
|
+
|
|
202
|
+
const pubkey = await signer.getPublicKey(id);
|
|
203
|
+
|
|
204
|
+
const info: Info = {
|
|
205
|
+
pubkey,
|
|
206
|
+
authMethod: 'amber' as any,
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
this.onAuth('login', info);
|
|
210
|
+
return info;
|
|
199
211
|
}
|
|
200
212
|
window.open(link, '_blank', 'width=400,height=700');
|
|
201
213
|
}
|