@openkey/sdk 0.12.1 → 0.12.2-beta.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.
package/dist/index.mjs CHANGED
@@ -829,6 +829,19 @@ var OpenKey = class {
829
829
  * remains preserved and OpenKey never rewrites their bytes.
830
830
  */
831
831
  async authorizeTinyCloud(request, opts) {
832
+ const reserved = request?.protocolVersion === 1 && typeof request.siwe === "string" && request.siwe !== "" && this.resolveMode(opts?.mode) === "popup" ? this.reservePopup("sign") : null;
833
+ try {
834
+ return await this.authorizeTinyCloudFlow(request, opts, reserved);
835
+ } finally {
836
+ if (reserved && !reserved.used) {
837
+ try {
838
+ reserved.window.close();
839
+ } catch {
840
+ }
841
+ }
842
+ }
843
+ }
844
+ async authorizeTinyCloudFlow(request, opts, reserved) {
832
845
  if (request.protocolVersion !== 1) {
833
846
  throw new Error(
834
847
  `authorizeTinyCloud only supports protocolVersion 1; got ${request.protocolVersion}`
@@ -876,7 +889,8 @@ var OpenKey = class {
876
889
  // verified unless the server actually origin-binds a manifest.
877
890
  presentation: request.presentation
878
891
  },
879
- opts?.mode
892
+ opts?.mode,
893
+ reserved
880
894
  );
881
895
  if (typeof raw.signedMessage !== "string" || !raw.signedMessage) {
882
896
  throw new Error(
@@ -1316,14 +1330,14 @@ var OpenKey = class {
1316
1330
  if (typeof window !== "undefined" && window.ethereum) return true;
1317
1331
  return false;
1318
1332
  }
1319
- async openFlow(action, message, modeOverride) {
1333
+ async openFlow(action, message, modeOverride, reserved) {
1320
1334
  const mode = this.resolveMode(modeOverride);
1321
1335
  const origin = encodeURIComponent(window.location.origin);
1322
1336
  const eoaFlag = action === "connect" && this.hasDetectedEoa() ? "&hasEoa=true" : "";
1323
1337
  const passkeysFlag = passkeysQueryFlag(this.passkeysSupported);
1324
1338
  if (mode === "popup") {
1325
1339
  const url2 = `${this.host}/widget/${action}?origin=${origin}${eoaFlag}${passkeysFlag}`;
1326
- return new Promise((resolve, reject) => this.openPopup(action, url2, message, resolve, reject));
1340
+ return new Promise((resolve, reject) => this.openPopup(action, url2, message, resolve, reject, reserved ?? null));
1327
1341
  }
1328
1342
  if (mode === "redirect") {
1329
1343
  const url2 = `${this.host}/widget/${action}?origin=${origin}${eoaFlag}${passkeysFlag}`;
@@ -1554,14 +1568,40 @@ var OpenKey = class {
1554
1568
  }
1555
1569
  });
1556
1570
  }
1557
- openPopup(action, url, message, resolve, reject) {
1571
+ /**
1572
+ * Opens the popup window of a flow in the current tick, blank. `openPopup`
1573
+ * navigates it to the widget later. `null` when the browser did not give a
1574
+ * usable window: the flow then opens its own, as it always did.
1575
+ */
1576
+ reservePopup(action) {
1558
1577
  const left = window.screenX + (window.outerWidth - POPUP_WIDTH) / 2;
1559
1578
  const top = window.screenY + (window.outerHeight - POPUP_HEIGHT) / 2;
1560
- const popup = window.open(
1561
- url,
1579
+ const win = window.open(
1580
+ "about:blank",
1562
1581
  `openkey-${action}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
1563
1582
  `width=${POPUP_WIDTH},height=${POPUP_HEIGHT},left=${left},top=${top},popup=true`
1564
1583
  );
1584
+ return isUsablePopup(win) ? { window: win, used: false } : null;
1585
+ }
1586
+ openPopup(action, url, message, resolve, reject, reserved = null) {
1587
+ const left = window.screenX + (window.outerWidth - POPUP_WIDTH) / 2;
1588
+ const top = window.screenY + (window.outerHeight - POPUP_HEIGHT) / 2;
1589
+ let popup;
1590
+ if (reserved && !reserved.window.closed) {
1591
+ try {
1592
+ reserved.window.location.href = url;
1593
+ reserved.used = true;
1594
+ popup = reserved.window;
1595
+ } catch {
1596
+ popup = null;
1597
+ }
1598
+ } else {
1599
+ popup = window.open(
1600
+ url,
1601
+ `openkey-${action}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
1602
+ `width=${POPUP_WIDTH},height=${POPUP_HEIGHT},left=${left},top=${top},popup=true`
1603
+ );
1604
+ }
1565
1605
  if (!isUsablePopup(popup)) {
1566
1606
  this.popup = null;
1567
1607
  const embedded = isEmbeddedAppWebView();