-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathidentity.service.ts
More file actions
242 lines (194 loc) · 5.87 KB
/
identity.service.ts
File metadata and controls
242 lines (194 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { Injectable } from "@angular/core";
import { Observable, Subject } from "rxjs";
import { v4 as uuid } from "uuid";
import { HttpParams } from "@angular/common/http";
@Injectable({
providedIn: "root",
})
export class IdentityService {
// Requests that were sent before the iframe initialized
private pendingRequests = [];
// All outbound request promises we still need to resolve
private outboundRequests = {};
// The currently active identity window
private identityWindow;
private identityWindowSubject;
// The URL of the identity service
identityServiceURL: string;
sanitizedIdentityServiceURL;
// Importing identities
importingIdentities: any[];
// User data
identityServiceUsers;
identityServicePublicKeyAdded: string;
private initialized = false;
private iframe = null;
// Wait for storageGranted broadcast
storageGranted = new Subject();
// Using testnet or mainnet
isTestnet = false;
constructor() {
window.addEventListener("message", (event) => this.handleMessage(event));
}
// Launch a new identity window
launch(path?: string, params?: { publicKey?: string; tx?: string }): Observable<any> {
let url = this.identityServiceURL as string;
if (path) {
url += path;
}
let httpParams = new HttpParams();
if (this.isTestnet) {
httpParams = httpParams.append("testnet", "true");
}
if (params?.publicKey) {
httpParams = httpParams.append("publicKey", params.publicKey);
}
if (params?.tx) {
httpParams = httpParams.append("tx", params.tx);
}
const paramsStr = httpParams.toString();
if (paramsStr) {
url += `?${paramsStr}`;
}
// center the window
const h = 1000;
const w = 800;
const y = window.outerHeight / 2 + window.screenY - h / 2;
const x = window.outerWidth / 2 + window.screenX - w / 2;
this.identityWindow = window.open(url, null, `toolbar=no, width=${w}, height=${h}, top=${y}, left=${x}`);
this.identityWindowSubject = new Subject();
return this.identityWindowSubject;
}
// Outgoing messages
burn(payload: {
accessLevel: number;
accessLevelHmac: string;
encryptedSeedHex: string;
unsignedHashes: string[];
}): Observable<any> {
return this.send("burn", payload);
}
sign(payload: {
accessLevel: number;
accessLevelHmac: string;
encryptedSeedHex: string;
transactionHex: string;
}): Observable<any> {
return this.send("sign", payload);
}
decrypt(payload: {
accessLevel: number;
accessLevelHmac: string;
encryptedSeedHex: string;
encryptedHexes: string[];
}): Observable<any> {
return this.send("decrypt", payload);
}
jwt(payload: { accessLevel: number; accessLevelHmac: string; encryptedSeedHex: string }): Observable<any> {
return this.send("jwt", payload);
}
info(): Observable<any> {
return this.send("info", {});
}
// Helpers
identityServiceParamsForKey(publicKey: string) {
const { encryptedSeedHex, accessLevel, accessLevelHmac } = this.identityServiceUsers[publicKey];
return { encryptedSeedHex, accessLevel, accessLevelHmac };
}
// Incoming messages
private handleInitialize(event: MessageEvent) {
if (!this.initialized) {
this.initialized = true;
this.iframe = document.getElementById("identity");
for (const request of this.pendingRequests) {
this.postMessage(request);
}
this.pendingRequests = [];
}
// acknowledge, provides hostname data
this.respond(event.source as Window, event.data.id, {});
}
private handleStorageGranted() {
this.storageGranted.next(true);
this.storageGranted.complete();
}
private handleLogin(payload: any) {
this.identityWindow.close();
this.identityWindow = null;
this.identityWindowSubject.next(payload);
this.identityWindowSubject.complete();
this.identityWindowSubject = null;
}
private handleImport(id: string) {
this.respond(this.identityWindow, id, { identities: this.importingIdentities });
}
private handleInfo(id: string) {
this.respond(this.identityWindow, id, {});
}
// Message handling
private handleMessage(event: MessageEvent) {
const { data } = event;
const { service, method } = data;
if (service !== "identity") {
return;
}
// Methods are present on incoming requests but not responses
if (method) {
this.handleRequest(event);
} else {
this.handleResponse(event);
}
}
private handleRequest(event: MessageEvent) {
const {
data: { id, method, payload },
} = event;
if (method === "initialize") {
this.handleInitialize(event);
} else if (method === "storageGranted") {
this.handleStorageGranted();
} else if (method === "login") {
this.handleLogin(payload);
} else if (method === "import") {
this.handleImport(id);
} else if (method === "info") {
this.handleInfo(id);
} else {
console.error("Unhandled identity request");
console.error(event);
}
}
private handleResponse(event: MessageEvent) {
const {
data: { id, payload },
} = event;
const req = this.outboundRequests[id];
req.next(payload);
req.complete();
delete this.outboundRequests[id];
}
// Send a new message and expect a response
private send(method: string, payload: any) {
const req = {
id: uuid(),
method,
payload,
service: "identity",
};
const subject = new Subject();
this.postMessage(req);
this.outboundRequests[req.id] = subject;
return subject;
}
private postMessage(req: any) {
if (this.initialized) {
this.iframe.contentWindow.postMessage(req, "*");
} else {
this.pendingRequests.push(req);
}
}
// Respond to a received message
private respond(window: Window, id: string, payload: any): void {
window.postMessage({ id, service: "identity", payload }, "*");
}
}