@tomo-inc/embedded-wallet-providers 0.0.20 → 0.0.21

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 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,42 @@ var EmbeddedWallet = class _EmbeddedWallet {
285
287
  if (!config) {
286
288
  throw new Error("config is not initialized");
287
289
  }
290
+ const cacheKey = config.clientId + "-cube-oidc-token";
291
+ if (oidcToken) {
292
+ walletUtils.cache.set(cacheKey, oidcToken, false);
293
+ }
294
+ this.oidcToken = oidcToken || walletUtils.cache.get(cacheKey) || "";
295
+ console.log("embedded wallet login with oidcToken:", config, this.oidcToken);
296
+ if (!this.oidcToken) {
297
+ console.warn("oidcToken is not found");
298
+ }
288
299
  return new Promise((resolve, reject) => {
300
+ const receiveResponse = (event) => {
301
+ var _a, _b;
302
+ const { origin, data } = event;
303
+ if ((data == null ? void 0 : data.type) === "wallet-ready" && origin === this.walletOrigin) {
304
+ this.isAvailable = ((_a = data == null ? void 0 : data.data) == null ? void 0 : _a.isAvailable) || false;
305
+ if (this.themeConfig) {
306
+ setTimeout(() => {
307
+ this.request("theme-change", this.themeConfig);
308
+ }, 0);
309
+ }
310
+ const payload = (_b = data == null ? void 0 : data.data) != null ? _b : { isAvailable: false, connectedInfo: null };
311
+ resolve(payload);
312
+ if (this.loginStatusCallback) {
313
+ this == null ? void 0 : this.loginStatusCallback({ type: "login" });
314
+ }
315
+ window.removeEventListener("message", receiveResponse);
316
+ }
317
+ if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
318
+ this.close();
319
+ }
320
+ };
321
+ window.addEventListener("message", receiveResponse);
322
+ if (!this.walletCloseHandlerRegistered) {
323
+ window.addEventListener("message", this.walletCloseHandler);
324
+ this.walletCloseHandlerRegistered = true;
325
+ }
289
326
  try {
290
327
  const { walletBaseUrl, clientId, stage, logo, name } = config || {};
291
328
  if (!walletBaseUrl || !clientId) {
@@ -293,6 +330,10 @@ var EmbeddedWallet = class _EmbeddedWallet {
293
330
  }
294
331
  const EMBEDDED_WALLET_ID = clientId;
295
332
  let walletIframe = document.getElementById(EMBEDDED_WALLET_ID);
333
+ if (oidcToken && walletIframe) {
334
+ walletIframe.remove();
335
+ walletIframe = null;
336
+ }
296
337
  if (!walletIframe) {
297
338
  walletIframe = document.createElement("iframe");
298
339
  walletIframe.style.cssText = `width: 0; height: 0; background: transparent; background-color: transparent; border: none;`;
@@ -304,40 +345,20 @@ var EmbeddedWallet = class _EmbeddedWallet {
304
345
  dappOrigin: window.location.origin,
305
346
  tomoStage: stage,
306
347
  tomoClientId: clientId,
307
- oidcToken: oidcToken || "",
348
+ oidcToken: this.oidcToken || "",
308
349
  logo: logo || "",
309
350
  name: name || ""
310
351
  });
311
- walletIframe.src = `${walletBaseUrl}#${searchParams.toString()}`;
352
+ const r = Math.random().toString(36).substring(2, 15);
353
+ walletIframe.src = `${walletBaseUrl}?r1=${r}#${searchParams.toString()}`;
312
354
  walletIframe.setAttribute("allowTransparency", "true");
313
355
  walletIframe.allow = "publickey-credentials-get; publickey-credentials-create";
314
356
  this.walletIframe = walletIframe;
315
357
  } catch (error) {
316
358
  console.error("login error", error);
359
+ window.removeEventListener("message", receiveResponse);
317
360
  reject(error);
318
361
  }
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
362
  });
342
363
  }
343
364
  async themeChange(theme) {
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
@@ -1,5 +1,5 @@
1
1
  import { TronProvider, SolanaProvider, DogecoinProvider, BitcoinProvider, EvmProvider } from '@tomo-inc/inject-providers';
2
- import { ChainTypeEnum, ProviderProtocol, ProviderStandard } from '@tomo-inc/wallet-utils';
2
+ import { ChainTypeEnum, ProviderProtocol, ProviderStandard, cache } from '@tomo-inc/wallet-utils';
3
3
  import { OidcAuth } from '@tomo-inc/oidc-auth';
4
4
 
5
5
  var __defProp = Object.defineProperty;
@@ -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,42 @@ var EmbeddedWallet = class _EmbeddedWallet {
283
285
  if (!config) {
284
286
  throw new Error("config is not initialized");
285
287
  }
288
+ const cacheKey = config.clientId + "-cube-oidc-token";
289
+ if (oidcToken) {
290
+ cache.set(cacheKey, oidcToken, false);
291
+ }
292
+ this.oidcToken = oidcToken || cache.get(cacheKey) || "";
293
+ console.log("embedded wallet login with oidcToken:", config, this.oidcToken);
294
+ if (!this.oidcToken) {
295
+ console.warn("oidcToken is not found");
296
+ }
286
297
  return new Promise((resolve, reject) => {
298
+ const receiveResponse = (event) => {
299
+ var _a, _b;
300
+ const { origin, data } = event;
301
+ if ((data == null ? void 0 : data.type) === "wallet-ready" && origin === this.walletOrigin) {
302
+ this.isAvailable = ((_a = data == null ? void 0 : data.data) == null ? void 0 : _a.isAvailable) || false;
303
+ if (this.themeConfig) {
304
+ setTimeout(() => {
305
+ this.request("theme-change", this.themeConfig);
306
+ }, 0);
307
+ }
308
+ const payload = (_b = data == null ? void 0 : data.data) != null ? _b : { isAvailable: false, connectedInfo: null };
309
+ resolve(payload);
310
+ if (this.loginStatusCallback) {
311
+ this == null ? void 0 : this.loginStatusCallback({ type: "login" });
312
+ }
313
+ window.removeEventListener("message", receiveResponse);
314
+ }
315
+ if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
316
+ this.close();
317
+ }
318
+ };
319
+ window.addEventListener("message", receiveResponse);
320
+ if (!this.walletCloseHandlerRegistered) {
321
+ window.addEventListener("message", this.walletCloseHandler);
322
+ this.walletCloseHandlerRegistered = true;
323
+ }
287
324
  try {
288
325
  const { walletBaseUrl, clientId, stage, logo, name } = config || {};
289
326
  if (!walletBaseUrl || !clientId) {
@@ -291,6 +328,10 @@ var EmbeddedWallet = class _EmbeddedWallet {
291
328
  }
292
329
  const EMBEDDED_WALLET_ID = clientId;
293
330
  let walletIframe = document.getElementById(EMBEDDED_WALLET_ID);
331
+ if (oidcToken && walletIframe) {
332
+ walletIframe.remove();
333
+ walletIframe = null;
334
+ }
294
335
  if (!walletIframe) {
295
336
  walletIframe = document.createElement("iframe");
296
337
  walletIframe.style.cssText = `width: 0; height: 0; background: transparent; background-color: transparent; border: none;`;
@@ -302,40 +343,20 @@ var EmbeddedWallet = class _EmbeddedWallet {
302
343
  dappOrigin: window.location.origin,
303
344
  tomoStage: stage,
304
345
  tomoClientId: clientId,
305
- oidcToken: oidcToken || "",
346
+ oidcToken: this.oidcToken || "",
306
347
  logo: logo || "",
307
348
  name: name || ""
308
349
  });
309
- walletIframe.src = `${walletBaseUrl}#${searchParams.toString()}`;
350
+ const r = Math.random().toString(36).substring(2, 15);
351
+ walletIframe.src = `${walletBaseUrl}?r1=${r}#${searchParams.toString()}`;
310
352
  walletIframe.setAttribute("allowTransparency", "true");
311
353
  walletIframe.allow = "publickey-credentials-get; publickey-credentials-create";
312
354
  this.walletIframe = walletIframe;
313
355
  } catch (error) {
314
356
  console.error("login error", error);
357
+ window.removeEventListener("message", receiveResponse);
315
358
  reject(error);
316
359
  }
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
360
  });
340
361
  }
341
362
  async themeChange(theme) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomo-inc/embedded-wallet-providers",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "author": "tomo.inc",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -8,7 +8,7 @@ import {
8
8
  import { onResponse, sendRequest } from "./hub";
9
9
 
10
10
  import { OidcAuth } from "@tomo-inc/oidc-auth";
11
- import { ChainTypeEnum, ProviderProtocol, ProviderStandard } from "@tomo-inc/wallet-utils";
11
+ import { ChainTypeEnum, ProviderProtocol, ProviderStandard, cache } from "@tomo-inc/wallet-utils";
12
12
  import {
13
13
  EmbeddedWalletConfig,
14
14
  EmbeddedWalletConnectors,
@@ -21,6 +21,7 @@ import {
21
21
  export class EmbeddedWallet {
22
22
  private static instance: EmbeddedWallet;
23
23
  private themeConfig: any | null = null;
24
+ private walletCloseHandlerRegistered = false;
24
25
 
25
26
  public walletIframe: HTMLIFrameElement | null = null;
26
27
  public walletOrigin: string = "";
@@ -28,6 +29,7 @@ export class EmbeddedWallet {
28
29
  public isAvailable: boolean = false;
29
30
  public config: EmbeddedWalletConfig | null = null;
30
31
  public info: WalletInfo | null = null;
32
+ private oidcToken: string = "";
31
33
  public connectors: EmbeddedWalletConnectors = {
32
34
  [ChainTypeEnum.BITCOIN]: null,
33
35
  [ChainTypeEnum.DOGECOIN]: null,
@@ -150,7 +152,45 @@ export class EmbeddedWallet {
150
152
  throw new Error("config is not initialized");
151
153
  }
152
154
 
155
+ // cache oidcToken for second login(2nd reason unknown)
156
+ const cacheKey = config.clientId + "-cube-oidc-token";
157
+ if (oidcToken) {
158
+ cache.set(cacheKey, oidcToken, false);
159
+ }
160
+ this.oidcToken = oidcToken || cache.get(cacheKey) || "";
161
+ console.log("embedded wallet login with oidcToken:", config, this.oidcToken);
162
+ if (!this.oidcToken) {
163
+ console.warn("oidcToken is not found");
164
+ }
165
+
153
166
  return new Promise((resolve, reject) => {
167
+ const receiveResponse = (event: MessageEvent) => {
168
+ const { origin, data } = event;
169
+ if (data?.type === "wallet-ready" && origin === this.walletOrigin) {
170
+ this.isAvailable = data?.data?.isAvailable || false;
171
+ if (this.themeConfig) {
172
+ setTimeout(() => {
173
+ this.request("theme-change", this.themeConfig);
174
+ }, 0);
175
+ }
176
+ const payload = data?.data ?? { isAvailable: false, connectedInfo: null };
177
+ resolve(payload);
178
+ if (this.loginStatusCallback) {
179
+ this?.loginStatusCallback({ type: "login" });
180
+ }
181
+ window.removeEventListener("message", receiveResponse);
182
+ }
183
+ if (data?.type === "wallet-close" && origin === this.walletOrigin) {
184
+ this.close();
185
+ }
186
+ };
187
+
188
+ window.addEventListener("message", receiveResponse);
189
+ if (!this.walletCloseHandlerRegistered) {
190
+ window.addEventListener("message", this.walletCloseHandler);
191
+ this.walletCloseHandlerRegistered = true;
192
+ }
193
+
154
194
  try {
155
195
  const { walletBaseUrl, clientId, stage, logo, name } = config || {};
156
196
  if (!walletBaseUrl || !clientId) {
@@ -160,6 +200,11 @@ export class EmbeddedWallet {
160
200
  const EMBEDDED_WALLET_ID = clientId;
161
201
 
162
202
  let walletIframe = document.getElementById(EMBEDDED_WALLET_ID) as HTMLIFrameElement | null;
203
+
204
+ if (oidcToken && walletIframe) {
205
+ walletIframe.remove();
206
+ walletIframe = null;
207
+ }
163
208
  if (!walletIframe) {
164
209
  walletIframe = document.createElement("iframe");
165
210
  walletIframe.style.cssText = `width: 0; height: 0; background: transparent; background-color: transparent; border: none;`;
@@ -167,46 +212,27 @@ export class EmbeddedWallet {
167
212
  walletIframe.setAttribute("allowTransparency", "true");
168
213
  document.body.appendChild(walletIframe);
169
214
  }
215
+
170
216
  const searchParams = new URLSearchParams({
171
217
  dappOrigin: window.location.origin,
172
218
  tomoStage: stage,
173
219
  tomoClientId: clientId,
174
- oidcToken: oidcToken || "",
220
+ oidcToken: this.oidcToken || "",
175
221
  logo: logo || "",
176
222
  name: name || "",
177
223
  });
178
- walletIframe.src = `${walletBaseUrl}#${searchParams.toString()}`;
224
+
225
+ //prevent browser cache, load iframe --force
226
+ const r = Math.random().toString(36).substring(2, 15);
227
+ walletIframe.src = `${walletBaseUrl}?r1=${r}#${searchParams.toString()}`;
179
228
  walletIframe.setAttribute("allowTransparency", "true");
180
229
  walletIframe.allow = "publickey-credentials-get; publickey-credentials-create";
181
230
  this.walletIframe = walletIframe;
182
231
  } catch (error) {
183
232
  console.error("login error", error);
233
+ window.removeEventListener("message", receiveResponse);
184
234
  reject(error);
185
235
  }
186
-
187
- const receiveResponse = ({ origin, data }: { origin: string; data: any }) => {
188
- if (data?.type === "wallet-ready" && origin === this.walletOrigin) {
189
- console.log("------receiveResponse", data, origin);
190
- this.isAvailable = data?.data?.isAvailable || false;
191
- // Apply persisted theme after wallet is ready
192
- if (this.themeConfig) {
193
- setTimeout(() => {
194
- this.request("theme-change", this.themeConfig);
195
- }, 0);
196
- }
197
- resolve(data?.data);
198
- if (this.loginStatusCallback) {
199
- this?.loginStatusCallback({ type: "login" });
200
- }
201
- window.removeEventListener("message", receiveResponse);
202
- }
203
- if (data?.type === "wallet-close" && origin === this.walletOrigin) {
204
- this.close();
205
- }
206
- };
207
- window.addEventListener("message", receiveResponse);
208
- // add wallet-close listener
209
- window.addEventListener("message", this.walletCloseHandler);
210
236
  });
211
237
  }
212
238