@tomo-inc/embedded-wallet-providers 0.0.20 → 0.0.22
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.cjs +39 -24
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +39 -24
- package/package.json +6 -1
- package/project.json +1 -1
- package/src/__tests__/embedded-wallet.test.ts +600 -0
- package/src/__tests__/hub.test.ts +152 -0
- package/src/__tests__/index.test.ts +26 -0
- package/src/__tests__/relay-route.test.ts +73 -0
- package/src/__tests__/setup.ts +60 -0
- package/src/__tests__/utils.test.ts +66 -0
- package/src/embedded-wallet.ts +57 -27
- package/src/hub.ts +1 -1
- package/vitest.config.ts +14 -0
package/dist/index.cjs
CHANGED
|
@@ -164,12 +164,14 @@ var onResponse = async (requestParams) => {
|
|
|
164
164
|
var EmbeddedWallet = class _EmbeddedWallet {
|
|
165
165
|
constructor() {
|
|
166
166
|
this.themeConfig = null;
|
|
167
|
+
this.walletCloseHandlerRegistered = false;
|
|
167
168
|
this.walletIframe = null;
|
|
168
169
|
this.walletOrigin = "";
|
|
169
170
|
this.maskZIndex = 999;
|
|
170
171
|
this.isAvailable = false;
|
|
171
172
|
this.config = null;
|
|
172
173
|
this.info = null;
|
|
174
|
+
this.oidcToken = "";
|
|
173
175
|
this.connectors = {
|
|
174
176
|
[walletUtils.ChainTypeEnum.BITCOIN]: null,
|
|
175
177
|
[walletUtils.ChainTypeEnum.DOGECOIN]: null,
|
|
@@ -285,7 +287,34 @@ var EmbeddedWallet = class _EmbeddedWallet {
|
|
|
285
287
|
if (!config) {
|
|
286
288
|
throw new Error("config is not initialized");
|
|
287
289
|
}
|
|
290
|
+
this.oidcToken = oidcToken || "";
|
|
288
291
|
return new Promise((resolve, reject) => {
|
|
292
|
+
const receiveResponse = (event) => {
|
|
293
|
+
var _a, _b;
|
|
294
|
+
const { origin, data } = event;
|
|
295
|
+
if ((data == null ? void 0 : data.type) === "wallet-ready" && origin === this.walletOrigin) {
|
|
296
|
+
this.isAvailable = ((_a = data == null ? void 0 : data.data) == null ? void 0 : _a.isAvailable) || false;
|
|
297
|
+
if (this.themeConfig) {
|
|
298
|
+
setTimeout(() => {
|
|
299
|
+
this.request("theme-change", this.themeConfig);
|
|
300
|
+
}, 0);
|
|
301
|
+
}
|
|
302
|
+
const payload = (_b = data == null ? void 0 : data.data) != null ? _b : { isAvailable: false, connectedInfo: null };
|
|
303
|
+
resolve(payload);
|
|
304
|
+
if (this.loginStatusCallback) {
|
|
305
|
+
this == null ? void 0 : this.loginStatusCallback({ type: "login" });
|
|
306
|
+
}
|
|
307
|
+
window.removeEventListener("message", receiveResponse);
|
|
308
|
+
}
|
|
309
|
+
if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
|
|
310
|
+
this.close();
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
window.addEventListener("message", receiveResponse);
|
|
314
|
+
if (!this.walletCloseHandlerRegistered) {
|
|
315
|
+
window.addEventListener("message", this.walletCloseHandler);
|
|
316
|
+
this.walletCloseHandlerRegistered = true;
|
|
317
|
+
}
|
|
289
318
|
try {
|
|
290
319
|
const { walletBaseUrl, clientId, stage, logo, name } = config || {};
|
|
291
320
|
if (!walletBaseUrl || !clientId) {
|
|
@@ -293,6 +322,10 @@ var EmbeddedWallet = class _EmbeddedWallet {
|
|
|
293
322
|
}
|
|
294
323
|
const EMBEDDED_WALLET_ID = clientId;
|
|
295
324
|
let walletIframe = document.getElementById(EMBEDDED_WALLET_ID);
|
|
325
|
+
if (oidcToken && walletIframe) {
|
|
326
|
+
walletIframe.remove();
|
|
327
|
+
walletIframe = null;
|
|
328
|
+
}
|
|
296
329
|
if (!walletIframe) {
|
|
297
330
|
walletIframe = document.createElement("iframe");
|
|
298
331
|
walletIframe.style.cssText = `width: 0; height: 0; background: transparent; background-color: transparent; border: none;`;
|
|
@@ -304,40 +337,20 @@ var EmbeddedWallet = class _EmbeddedWallet {
|
|
|
304
337
|
dappOrigin: window.location.origin,
|
|
305
338
|
tomoStage: stage,
|
|
306
339
|
tomoClientId: clientId,
|
|
307
|
-
oidcToken: oidcToken
|
|
340
|
+
oidcToken: this.oidcToken,
|
|
308
341
|
logo: logo || "",
|
|
309
342
|
name: name || ""
|
|
310
343
|
});
|
|
311
|
-
|
|
344
|
+
const r = Math.random().toString(36).substring(2, 15);
|
|
345
|
+
walletIframe.src = `${walletBaseUrl}?r1=${r}#${searchParams.toString()}`;
|
|
312
346
|
walletIframe.setAttribute("allowTransparency", "true");
|
|
313
347
|
walletIframe.allow = "publickey-credentials-get; publickey-credentials-create";
|
|
314
348
|
this.walletIframe = walletIframe;
|
|
315
349
|
} catch (error) {
|
|
316
350
|
console.error("login error", error);
|
|
351
|
+
window.removeEventListener("message", receiveResponse);
|
|
317
352
|
reject(error);
|
|
318
353
|
}
|
|
319
|
-
const receiveResponse = ({ origin, data }) => {
|
|
320
|
-
var _a;
|
|
321
|
-
if ((data == null ? void 0 : data.type) === "wallet-ready" && origin === this.walletOrigin) {
|
|
322
|
-
console.log("------receiveResponse", data, origin);
|
|
323
|
-
this.isAvailable = ((_a = data == null ? void 0 : data.data) == null ? void 0 : _a.isAvailable) || false;
|
|
324
|
-
if (this.themeConfig) {
|
|
325
|
-
setTimeout(() => {
|
|
326
|
-
this.request("theme-change", this.themeConfig);
|
|
327
|
-
}, 0);
|
|
328
|
-
}
|
|
329
|
-
resolve(data == null ? void 0 : data.data);
|
|
330
|
-
if (this.loginStatusCallback) {
|
|
331
|
-
this == null ? void 0 : this.loginStatusCallback({ type: "login" });
|
|
332
|
-
}
|
|
333
|
-
window.removeEventListener("message", receiveResponse);
|
|
334
|
-
}
|
|
335
|
-
if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
|
|
336
|
-
this.close();
|
|
337
|
-
}
|
|
338
|
-
};
|
|
339
|
-
window.addEventListener("message", receiveResponse);
|
|
340
|
-
window.addEventListener("message", this.walletCloseHandler);
|
|
341
354
|
});
|
|
342
355
|
}
|
|
343
356
|
async themeChange(theme) {
|
|
@@ -367,6 +380,8 @@ var EmbeddedWallet = class _EmbeddedWallet {
|
|
|
367
380
|
async logout() {
|
|
368
381
|
if (this.walletIframe) {
|
|
369
382
|
this.walletIframe.src = `${this.walletOrigin}#logout=true`;
|
|
383
|
+
const config = this.config;
|
|
384
|
+
config && this.init(config);
|
|
370
385
|
}
|
|
371
386
|
window.removeEventListener("message", this.walletCloseHandler);
|
|
372
387
|
window.removeEventListener("message", this.logoutListener);
|
package/dist/index.d.cts
CHANGED
|
@@ -49,12 +49,14 @@ type EmbeddedWalletConnectors = {
|
|
|
49
49
|
declare class EmbeddedWallet {
|
|
50
50
|
private static instance;
|
|
51
51
|
private themeConfig;
|
|
52
|
+
private walletCloseHandlerRegistered;
|
|
52
53
|
walletIframe: HTMLIFrameElement | null;
|
|
53
54
|
walletOrigin: string;
|
|
54
55
|
maskZIndex: number;
|
|
55
56
|
isAvailable: boolean;
|
|
56
57
|
config: EmbeddedWalletConfig | null;
|
|
57
58
|
info: WalletInfo | null;
|
|
59
|
+
private oidcToken;
|
|
58
60
|
connectors: EmbeddedWalletConnectors;
|
|
59
61
|
loginByGoogle: (() => Promise<string>) | null;
|
|
60
62
|
loginByX: (() => Promise<string>) | null;
|
package/dist/index.d.ts
CHANGED
|
@@ -49,12 +49,14 @@ type EmbeddedWalletConnectors = {
|
|
|
49
49
|
declare class EmbeddedWallet {
|
|
50
50
|
private static instance;
|
|
51
51
|
private themeConfig;
|
|
52
|
+
private walletCloseHandlerRegistered;
|
|
52
53
|
walletIframe: HTMLIFrameElement | null;
|
|
53
54
|
walletOrigin: string;
|
|
54
55
|
maskZIndex: number;
|
|
55
56
|
isAvailable: boolean;
|
|
56
57
|
config: EmbeddedWalletConfig | null;
|
|
57
58
|
info: WalletInfo | null;
|
|
59
|
+
private oidcToken;
|
|
58
60
|
connectors: EmbeddedWalletConnectors;
|
|
59
61
|
loginByGoogle: (() => Promise<string>) | null;
|
|
60
62
|
loginByX: (() => Promise<string>) | null;
|
package/dist/index.js
CHANGED
|
@@ -162,12 +162,14 @@ var onResponse = async (requestParams) => {
|
|
|
162
162
|
var EmbeddedWallet = class _EmbeddedWallet {
|
|
163
163
|
constructor() {
|
|
164
164
|
this.themeConfig = null;
|
|
165
|
+
this.walletCloseHandlerRegistered = false;
|
|
165
166
|
this.walletIframe = null;
|
|
166
167
|
this.walletOrigin = "";
|
|
167
168
|
this.maskZIndex = 999;
|
|
168
169
|
this.isAvailable = false;
|
|
169
170
|
this.config = null;
|
|
170
171
|
this.info = null;
|
|
172
|
+
this.oidcToken = "";
|
|
171
173
|
this.connectors = {
|
|
172
174
|
[ChainTypeEnum.BITCOIN]: null,
|
|
173
175
|
[ChainTypeEnum.DOGECOIN]: null,
|
|
@@ -283,7 +285,34 @@ var EmbeddedWallet = class _EmbeddedWallet {
|
|
|
283
285
|
if (!config) {
|
|
284
286
|
throw new Error("config is not initialized");
|
|
285
287
|
}
|
|
288
|
+
this.oidcToken = oidcToken || "";
|
|
286
289
|
return new Promise((resolve, reject) => {
|
|
290
|
+
const receiveResponse = (event) => {
|
|
291
|
+
var _a, _b;
|
|
292
|
+
const { origin, data } = event;
|
|
293
|
+
if ((data == null ? void 0 : data.type) === "wallet-ready" && origin === this.walletOrigin) {
|
|
294
|
+
this.isAvailable = ((_a = data == null ? void 0 : data.data) == null ? void 0 : _a.isAvailable) || false;
|
|
295
|
+
if (this.themeConfig) {
|
|
296
|
+
setTimeout(() => {
|
|
297
|
+
this.request("theme-change", this.themeConfig);
|
|
298
|
+
}, 0);
|
|
299
|
+
}
|
|
300
|
+
const payload = (_b = data == null ? void 0 : data.data) != null ? _b : { isAvailable: false, connectedInfo: null };
|
|
301
|
+
resolve(payload);
|
|
302
|
+
if (this.loginStatusCallback) {
|
|
303
|
+
this == null ? void 0 : this.loginStatusCallback({ type: "login" });
|
|
304
|
+
}
|
|
305
|
+
window.removeEventListener("message", receiveResponse);
|
|
306
|
+
}
|
|
307
|
+
if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
|
|
308
|
+
this.close();
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
window.addEventListener("message", receiveResponse);
|
|
312
|
+
if (!this.walletCloseHandlerRegistered) {
|
|
313
|
+
window.addEventListener("message", this.walletCloseHandler);
|
|
314
|
+
this.walletCloseHandlerRegistered = true;
|
|
315
|
+
}
|
|
287
316
|
try {
|
|
288
317
|
const { walletBaseUrl, clientId, stage, logo, name } = config || {};
|
|
289
318
|
if (!walletBaseUrl || !clientId) {
|
|
@@ -291,6 +320,10 @@ var EmbeddedWallet = class _EmbeddedWallet {
|
|
|
291
320
|
}
|
|
292
321
|
const EMBEDDED_WALLET_ID = clientId;
|
|
293
322
|
let walletIframe = document.getElementById(EMBEDDED_WALLET_ID);
|
|
323
|
+
if (oidcToken && walletIframe) {
|
|
324
|
+
walletIframe.remove();
|
|
325
|
+
walletIframe = null;
|
|
326
|
+
}
|
|
294
327
|
if (!walletIframe) {
|
|
295
328
|
walletIframe = document.createElement("iframe");
|
|
296
329
|
walletIframe.style.cssText = `width: 0; height: 0; background: transparent; background-color: transparent; border: none;`;
|
|
@@ -302,40 +335,20 @@ var EmbeddedWallet = class _EmbeddedWallet {
|
|
|
302
335
|
dappOrigin: window.location.origin,
|
|
303
336
|
tomoStage: stage,
|
|
304
337
|
tomoClientId: clientId,
|
|
305
|
-
oidcToken: oidcToken
|
|
338
|
+
oidcToken: this.oidcToken,
|
|
306
339
|
logo: logo || "",
|
|
307
340
|
name: name || ""
|
|
308
341
|
});
|
|
309
|
-
|
|
342
|
+
const r = Math.random().toString(36).substring(2, 15);
|
|
343
|
+
walletIframe.src = `${walletBaseUrl}?r1=${r}#${searchParams.toString()}`;
|
|
310
344
|
walletIframe.setAttribute("allowTransparency", "true");
|
|
311
345
|
walletIframe.allow = "publickey-credentials-get; publickey-credentials-create";
|
|
312
346
|
this.walletIframe = walletIframe;
|
|
313
347
|
} catch (error) {
|
|
314
348
|
console.error("login error", error);
|
|
349
|
+
window.removeEventListener("message", receiveResponse);
|
|
315
350
|
reject(error);
|
|
316
351
|
}
|
|
317
|
-
const receiveResponse = ({ origin, data }) => {
|
|
318
|
-
var _a;
|
|
319
|
-
if ((data == null ? void 0 : data.type) === "wallet-ready" && origin === this.walletOrigin) {
|
|
320
|
-
console.log("------receiveResponse", data, origin);
|
|
321
|
-
this.isAvailable = ((_a = data == null ? void 0 : data.data) == null ? void 0 : _a.isAvailable) || false;
|
|
322
|
-
if (this.themeConfig) {
|
|
323
|
-
setTimeout(() => {
|
|
324
|
-
this.request("theme-change", this.themeConfig);
|
|
325
|
-
}, 0);
|
|
326
|
-
}
|
|
327
|
-
resolve(data == null ? void 0 : data.data);
|
|
328
|
-
if (this.loginStatusCallback) {
|
|
329
|
-
this == null ? void 0 : this.loginStatusCallback({ type: "login" });
|
|
330
|
-
}
|
|
331
|
-
window.removeEventListener("message", receiveResponse);
|
|
332
|
-
}
|
|
333
|
-
if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
|
|
334
|
-
this.close();
|
|
335
|
-
}
|
|
336
|
-
};
|
|
337
|
-
window.addEventListener("message", receiveResponse);
|
|
338
|
-
window.addEventListener("message", this.walletCloseHandler);
|
|
339
352
|
});
|
|
340
353
|
}
|
|
341
354
|
async themeChange(theme) {
|
|
@@ -365,6 +378,8 @@ var EmbeddedWallet = class _EmbeddedWallet {
|
|
|
365
378
|
async logout() {
|
|
366
379
|
if (this.walletIframe) {
|
|
367
380
|
this.walletIframe.src = `${this.walletOrigin}#logout=true`;
|
|
381
|
+
const config = this.config;
|
|
382
|
+
config && this.init(config);
|
|
368
383
|
}
|
|
369
384
|
window.removeEventListener("message", this.walletCloseHandler);
|
|
370
385
|
window.removeEventListener("message", this.logoutListener);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomo-inc/embedded-wallet-providers",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.22",
|
|
4
4
|
"author": "tomo.inc",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"private": false,
|
|
@@ -28,7 +28,12 @@
|
|
|
28
28
|
"tsx": "^4.19.2",
|
|
29
29
|
"typescript": "^5.0.0",
|
|
30
30
|
"@vitest/browser": "^3.2.4",
|
|
31
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
31
32
|
"playwright": "^1.44.1",
|
|
32
33
|
"vitest": "^3.2.4"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --treeshake",
|
|
37
|
+
"test": "vitest run"
|
|
33
38
|
}
|
|
34
39
|
}
|