@fivenorth/loop-sdk 0.2.0 → 0.3.0

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.
Files changed (2) hide show
  1. package/dist/index.js +52 -3
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2288,15 +2288,18 @@ class LoopSDK {
2288
2288
  appName = "Unknown";
2289
2289
  connection = null;
2290
2290
  provider = null;
2291
+ openMode = "popup";
2292
+ popupWindow = null;
2291
2293
  onAccept = null;
2292
2294
  onReject = null;
2293
2295
  overlay = null;
2294
2296
  ticketId = null;
2295
2297
  constructor() {}
2296
- init({ appName, network, walletUrl, apiUrl, onAccept, onReject }) {
2298
+ init({ appName, network, walletUrl, apiUrl, onAccept, onReject, openMode }) {
2297
2299
  this.appName = appName;
2298
2300
  this.onAccept = onAccept || null;
2299
2301
  this.onReject = onReject || null;
2302
+ this.openMode = openMode ?? "popup";
2300
2303
  this.connection = new Connection({ network, walletUrl, apiUrl });
2301
2304
  }
2302
2305
  async connect() {
@@ -2310,6 +2313,7 @@ class LoopSDK {
2310
2313
  const existingConnectionRaw = localStorage.getItem("loop_connect");
2311
2314
  if (existingConnectionRaw) {
2312
2315
  try {
2316
+ let canReuseTicket = true;
2313
2317
  const { ticketId, authToken, partyId, publicKey, email } = JSON.parse(existingConnectionRaw);
2314
2318
  if (authToken && partyId && publicKey) {
2315
2319
  try {
@@ -2321,12 +2325,18 @@ class LoopSDK {
2321
2325
  this.connection.connectWebSocket(ticketId, this.handleWebSocketMessage.bind(this));
2322
2326
  }
2323
2327
  return;
2328
+ } else {
2329
+ console.warn("[LoopSDK] Sttored partyId does not march verified account. Clearing cached session.");
2330
+ canReuseTicket = false;
2331
+ localStorage.removeItem("loop_connect");
2324
2332
  }
2325
2333
  } catch (err) {
2326
2334
  console.error("Auto-login failed, token is invalid. Starting new connection.", err);
2335
+ canReuseTicket = false;
2336
+ localStorage.removeItem("loop_connect");
2327
2337
  }
2328
2338
  }
2329
- if (ticketId) {
2339
+ if (ticketId && canReuseTicket) {
2330
2340
  this.ticketId = ticketId;
2331
2341
  const connectUrl = `${this.connection.walletUrl}/.connect/?ticketId=${ticketId}`;
2332
2342
  this.showQrCode(connectUrl);
@@ -2353,7 +2363,9 @@ class LoopSDK {
2353
2363
  }
2354
2364
  handleWebSocketMessage(event) {
2355
2365
  const message = JSON.parse(event.data);
2366
+ console.log("[LoopSDK] WS message received:", message);
2356
2367
  if (message.type === "handshake_accept" /* HANDSHAKE_ACCEPT */) {
2368
+ console.log("[LoopSDK] Entering HANDSHAKE_ACCEPT flow");
2357
2369
  const { authToken, partyId, publicKey, email } = message.payload || {};
2358
2370
  if (authToken && partyId && publicKey) {
2359
2371
  this.provider = new Provider({ connection: this.connection, party_id: partyId, auth_token: authToken, public_key: publicKey, email });
@@ -2369,20 +2381,54 @@ class LoopSDK {
2369
2381
  this.onAccept?.(this.provider);
2370
2382
  this.hideQrCode();
2371
2383
  this.connection?.connectWebSocket(connectionInfo.ticketId, this.handleWebSocketMessage.bind(this));
2384
+ console.log("[LoopSDK] HANDSHAKE_ACCEPT: closing popup (if exists)");
2385
+ if (this.popupWindow && !this.popupWindow.closed) {
2386
+ this.popupWindow.close();
2387
+ }
2388
+ this.popupWindow = null;
2372
2389
  } catch (error) {
2373
2390
  console.error("Failed to update local storage with auth token.", error);
2374
2391
  }
2375
2392
  }
2376
2393
  }
2377
2394
  } else if (message.type === "handshake_reject" /* HANDSHAKE_REJECT */) {
2395
+ console.log("[LoopSDK] Entering HANDSHAKE_REJECT flow");
2378
2396
  localStorage.removeItem("loop_connect");
2379
2397
  this.connection?.ws?.close();
2380
2398
  this.onReject?.();
2381
2399
  this.hideQrCode();
2400
+ console.log("[LoopSDK] HANDSHAKE_REJECT: closing popup (if exists)");
2401
+ if (this.popupWindow && !this.popupWindow.closed) {
2402
+ this.popupWindow.close();
2403
+ }
2404
+ this.popupWindow = null;
2382
2405
  } else if (this.provider) {
2383
2406
  this.provider.handleResponse(message);
2384
2407
  }
2385
2408
  }
2409
+ openWallet(url) {
2410
+ if (typeof window === "undefined") {
2411
+ return;
2412
+ }
2413
+ if (this.openMode === "popup") {
2414
+ const width = 480;
2415
+ const height = 720;
2416
+ const left = (window.innerWidth - width) / 2 + window.screenX;
2417
+ const top = (window.innerWidth - height) / 2 + window.screenY;
2418
+ const features = `width=${width},height=${height},` + `left=${left},top=${top},` + "menubar=no,toolbar=no,location=no," + "resizable=yes,scrollbars=yes,status=no";
2419
+ const popup = window.open(url, "loop-wallet", features);
2420
+ if (!popup) {
2421
+ window.open(url, "_blank", "noopener,noreferrer");
2422
+ return;
2423
+ }
2424
+ this.popupWindow = popup;
2425
+ try {
2426
+ popup.focus();
2427
+ } catch {}
2428
+ return;
2429
+ }
2430
+ window.open(url, "_blank", "noopener,noreferrer");
2431
+ }
2386
2432
  showQrCode(url) {
2387
2433
  if (typeof window === "undefined" || typeof document === "undefined") {
2388
2434
  return;
@@ -2412,7 +2458,10 @@ class LoopSDK {
2412
2458
  link.textContent = "Or click here to connect";
2413
2459
  link.style.color = "white";
2414
2460
  link.style.marginTop = "20px";
2415
- link.target = "_blank";
2461
+ link.onclick = (e) => {
2462
+ e.preventDefault();
2463
+ this.openWallet(url);
2464
+ };
2416
2465
  overlay.appendChild(link);
2417
2466
  overlay.onclick = (e) => {
2418
2467
  if (e.target === overlay) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fivenorth/loop-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "author": "hello@fivenorth.io",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",