@swype-org/deposit 0.3.14 → 0.3.15

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/react.cjs CHANGED
@@ -93,6 +93,24 @@ function parseCloseRequest(data) {
93
93
  }
94
94
  return { type: "blink:close-request" };
95
95
  }
96
+ function parseIframeReady(data) {
97
+ if (!data || typeof data !== "object") {
98
+ return null;
99
+ }
100
+ const msg = data;
101
+ if (msg.type !== "blink:iframe-ready") {
102
+ return null;
103
+ }
104
+ return { type: "blink:iframe-ready" };
105
+ }
106
+ function buildSignedPayloadMessage(merchantId, payload, signature) {
107
+ return {
108
+ type: "blink:signed-payload",
109
+ merchantId,
110
+ payload,
111
+ signature
112
+ };
113
+ }
96
114
 
97
115
  // src/walletBridge/discover.ts
98
116
  function createWalletDiscoverer() {
@@ -921,30 +939,16 @@ var Deposit = class {
921
939
  async runSignerFlow(request, requestId, onComplete, onError) {
922
940
  try {
923
941
  const webviewBaseUrl = this.config.webviewBaseUrl ?? DEFAULT_WEBVIEW_BASE_URL;
942
+ this.hostedOrigin = this.config.hostedFlowOrigin ?? new URL(webviewBaseUrl).origin;
924
943
  this.log("Calling signer", {
925
944
  signer: typeof this.config.signer === "string" ? this.config.signer : "<function>"
926
945
  });
927
946
  const signerRequest = buildSignerRequest(request, webviewBaseUrl);
928
- const signerResponse = await callSigner(
929
- this.config.signer,
930
- signerRequest,
931
- this.config.signerTimeoutMs
932
- );
933
- this.lastSignerResponse = signerResponse;
934
- this.log("Signer responded", { merchantId: signerResponse.merchantId });
935
- if (this.requestId !== requestId) {
936
- this.log("Request superseded after signer response");
937
- return;
938
- }
939
- const hostedUrl = new URL(webviewBaseUrl);
940
- hostedUrl.searchParams.set("merchantId", signerResponse.merchantId);
941
- hostedUrl.searchParams.set("payload", signerResponse.payload);
942
- hostedUrl.searchParams.set("signature", signerResponse.signature);
943
- const targetUrl = hostedUrl.toString();
944
- this.hostedOrigin = this.config.hostedFlowOrigin ?? hostedUrl.origin;
945
- const iframeHandle = createIframe(targetUrl, this.config.containerElement);
946
- this.iframe = iframeHandle;
947
- iframeHandle.onClose(() => {
947
+ const preloadUrl = new URL(webviewBaseUrl);
948
+ preloadUrl.searchParams.set("preload", "true");
949
+ const preloadIframe = createIframe(preloadUrl.toString(), this.config.containerElement);
950
+ this.iframe = preloadIframe;
951
+ preloadIframe.onClose(() => {
948
952
  onError(
949
953
  new DepositError(
950
954
  "DEPOSIT_DISMISSED",
@@ -954,9 +958,60 @@ var Deposit = class {
954
958
  this.cleanup();
955
959
  this.emit("close");
956
960
  });
957
- this.startMessageListener(iframeHandle, onComplete);
958
- this.setStatus("iframe-active");
959
- this.log("Iframe opened with hosted flow", { url: targetUrl });
961
+ const signerPromise = callSigner(
962
+ this.config.signer,
963
+ signerRequest,
964
+ this.config.signerTimeoutMs
965
+ );
966
+ const iframeReadyPromise = this.waitForIframeReady(preloadIframe);
967
+ const [signerResponse, iframeReady] = await Promise.all([
968
+ signerPromise,
969
+ iframeReadyPromise
970
+ ]);
971
+ this.lastSignerResponse = signerResponse;
972
+ this.log("Signer responded", { merchantId: signerResponse.merchantId });
973
+ if (this.requestId !== requestId || preloadIframe.isClosed()) {
974
+ this.log("Request superseded or iframe dismissed during signer call");
975
+ return;
976
+ }
977
+ if (iframeReady) {
978
+ const contentWindow = preloadIframe.contentWindow;
979
+ if (contentWindow) {
980
+ contentWindow.postMessage(
981
+ buildSignedPayloadMessage(
982
+ signerResponse.merchantId,
983
+ signerResponse.payload,
984
+ signerResponse.signature
985
+ ),
986
+ this.hostedOrigin
987
+ );
988
+ }
989
+ this.startMessageListener(preloadIframe, onComplete);
990
+ this.setStatus("iframe-active");
991
+ this.log("Payload delivered via postMessage (preload path)");
992
+ } else {
993
+ preloadIframe.destroy();
994
+ const hostedUrl = new URL(webviewBaseUrl);
995
+ hostedUrl.searchParams.set("merchantId", signerResponse.merchantId);
996
+ hostedUrl.searchParams.set("payload", signerResponse.payload);
997
+ hostedUrl.searchParams.set("signature", signerResponse.signature);
998
+ const targetUrl = hostedUrl.toString();
999
+ const iframeHandle = createIframe(targetUrl, this.config.containerElement);
1000
+ this.iframe = iframeHandle;
1001
+ iframeHandle.onClose(() => {
1002
+ onError(
1003
+ new DepositError(
1004
+ "DEPOSIT_DISMISSED",
1005
+ "The deposit was dismissed before the transfer completed."
1006
+ )
1007
+ );
1008
+ this.cleanup();
1009
+ this.emit("close");
1010
+ });
1011
+ this.startMessageListener(iframeHandle, onComplete);
1012
+ this.setStatus("iframe-active");
1013
+ this.log("Iframe opened with hosted flow (fallback path)", { url: targetUrl });
1014
+ }
960
1015
  } catch (err) {
961
1016
  if (this.requestId !== requestId) return;
962
1017
  this.log("Signer flow failed", { error: err instanceof Error ? err.message : String(err) });
@@ -972,6 +1027,25 @@ var Deposit = class {
972
1027
  }
973
1028
  }
974
1029
  }
1030
+ waitForIframeReady(iframeHandle) {
1031
+ return new Promise((resolve) => {
1032
+ const handler = (event) => {
1033
+ if (event.origin !== this.hostedOrigin) return;
1034
+ if (event.source !== iframeHandle.contentWindow) return;
1035
+ if (parseIframeReady(event.data)) {
1036
+ window.removeEventListener("message", handler);
1037
+ clearTimeout(timeout);
1038
+ resolve(true);
1039
+ }
1040
+ };
1041
+ window.addEventListener("message", handler);
1042
+ const timeout = setTimeout(() => {
1043
+ window.removeEventListener("message", handler);
1044
+ this.log("Iframe ready timeout \u2014 falling back to URL params");
1045
+ resolve(false);
1046
+ }, 2e3);
1047
+ });
1048
+ }
975
1049
  startMessageListener(iframeHandle, onComplete) {
976
1050
  const handler = (event) => {
977
1051
  if (this.hostedOrigin && event.origin !== this.hostedOrigin) {