@zkproofport-app/sdk 0.2.10 → 0.2.11

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
@@ -613,8 +613,16 @@ function validateMdlInputs(circuit, inputs) {
613
613
  const MAX_RETURN_SCHEME_LENGTH = 128;
614
614
  /** RFC 3986 scheme (`ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )`) followed by exactly `://`. */
615
615
  const BARE_SCHEME_RE = /^[a-z][a-z0-9+.-]*:\/\/$/;
616
- /** `https://host[:port]` with no userinfo, path, query or fragment. */
617
- const HTTPS_ORIGIN_RE = /^https:\/\/[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+(:[0-9]{1,5})?$/;
616
+ /**
617
+ * Schemes we refuse even in the bare `scheme://` shape.
618
+ *
619
+ * `http` and `https` are the back door the shape rule alone leaves open: with
620
+ * no host, `https://` is shaped exactly like a bare custom scheme and would
621
+ * otherwise pass. A browser pointed at nowhere is not a return target, and
622
+ * accepting it would re-open the URL-shaped door this field just closed.
623
+ * `googlechrome://` and `firefox://` are deliberately NOT denied — bare, they
624
+ * foreground that browser without navigating, which is the whole point.
625
+ */
618
626
  const DENIED_RETURN_SCHEMES = new Set([
619
627
  'about',
620
628
  'blob',
@@ -625,6 +633,7 @@ const DENIED_RETURN_SCHEMES = new Set([
625
633
  'file',
626
634
  'ftp',
627
635
  'http',
636
+ 'https',
628
637
  'intent',
629
638
  'javascript',
630
639
  'jar',
@@ -638,6 +647,11 @@ const DENIED_RETURN_SCHEMES = new Set([
638
647
  * value fails fast in the integrator's own code instead of coming back as a
639
648
  * relay 400. The relay re-validates and is the authority.
640
649
  *
650
+ * One shape is accepted: a bare custom scheme such as `mydapp://`. It names an
651
+ * app to switch back to, never a page to visit — so hosts, paths and query
652
+ * strings are rejected, and an https URL is not a valid value. A web page has
653
+ * no app to return to and simply omits the field.
654
+ *
641
655
  * @param value - Candidate return target
642
656
  * @returns `null` when acceptable, otherwise a human-readable error message
643
657
  *
@@ -658,11 +672,8 @@ function validateReturnScheme(value) {
658
672
  return 'returnScheme must not contain whitespace';
659
673
  }
660
674
  const normalized = value.toLowerCase();
661
- if (HTTPS_ORIGIN_RE.test(normalized)) {
662
- return null;
663
- }
664
675
  if (!BARE_SCHEME_RE.test(normalized)) {
665
- return 'returnScheme must be a bare custom scheme such as "mydapp://" or an https origin such as "https://myapp.com" — paths, query strings and fragments are not accepted';
676
+ return 'returnScheme must be a bare custom scheme such as "mydapp://" — hosts, paths, query strings and fragments are not accepted, and an https URL is not a return target';
666
677
  }
667
678
  const schemeName = normalized.slice(0, normalized.indexOf(':'));
668
679
  if (DENIED_RETURN_SCHEMES.has(schemeName)) {
@@ -670,6 +681,90 @@ function validateReturnScheme(value) {
670
681
  }
671
682
  return null;
672
683
  }
684
+ /**
685
+ * The return target to use when the integrator named none.
686
+ *
687
+ * Only the requesting PAGE can answer "which browser is this?" — iOS gives the
688
+ * ZKProofport app nothing to work with (`sourceApplication` is nil across team
689
+ * identifiers, and there is no public API to foreground an app by bundle id).
690
+ * So the detection has to happen here, in the SDK, and travel with the request.
691
+ *
692
+ * Two cases are detected, each from a POSITIVE token, and each verified in the
693
+ * browser's own source to foreground WITHOUT navigating anywhere:
694
+ *
695
+ * Chrome for iOS (`CriOS/`) -> `googlechrome://`
696
+ * Firefox for iOS (`FxiOS/`) -> `firefox://`
697
+ *
698
+ * The BARE scheme is the point in both cases: it carries no URL, so the browser
699
+ * comes forward with nothing to open and the user lands on the tab they were
700
+ * already reading — no new tab, no navigation, no reload, JS state intact.
701
+ *
702
+ * Chrome: `ios/chrome/app/startup/chrome_app_startup_parameters.mm` replaces
703
+ * the scheme with `http`, producing `http://`, which fails GURL validation
704
+ * (`if (!externalURL.is_valid()) return nil;`), so the startup parameters are
705
+ * nil and there is nothing to open.
706
+ *
707
+ * Firefox: `firefox://` has an empty host, and
708
+ * `Coordinators/Router/RouteBuilder.swift` resolves the host through
709
+ * `DeeplinkInput.Host(rawValue: "")`, which is nil because no case has an empty
710
+ * raw value (`Coordinators/Router/DeeplinkInput.swift`). The scheme is not http
711
+ * either, so `makeRoute(url:)` falls to its final `else { return nil }`, and
712
+ * `SceneDelegate.handleOpenURL` then hits `guard let route = ... else
713
+ * { return }` and does nothing at all. Verified on the warm path — the browser
714
+ * is already running, which is always true here, because the user came from a
715
+ * page in it.
716
+ *
717
+ * Everything else deliberately returns undefined, and that is not a gap:
718
+ *
719
+ * - Safari has no equivalent. No scheme means "just come forward";
720
+ * `https://<url>` navigates and opens a new tab, `x-web-search://` opens a
721
+ * search page. Both lose the user's page.
722
+ * - Brave and Arc for iOS are UNDETECTABLE. Brave ships no UA token and
723
+ * identifies itself only via Client Hints, which WebKit does not implement,
724
+ * so it looks exactly like Safari. Inferring "Safari" by elimination and
725
+ * firing a Safari-ish scheme would throw Brave and Arc users into a browser
726
+ * they were not using — strictly worse than doing nothing.
727
+ * - In-app webviews (KakaoTalk, Naver, Line, Instagram) are not tabs at all.
728
+ * Any scheme destroys the page; the system "< Back to X" breadcrumb is the
729
+ * only thing that preserves it.
730
+ * - Edge (`EdgiOS`) and Opera (`OPiOS` / `OPT`) for iOS are NOT included.
731
+ * Both are closed source, so `microsoft-edge://` and `opera-http://` cannot
732
+ * be checked the way Chrome's and Firefox's were. Their bare schemes are
733
+ * plausible analogues, and that is exactly why they stay out: an unverified
734
+ * guess that opens a new tab is the bug this field exists to avoid. Add one
735
+ * only with evidence that it foregrounds WITHOUT navigating.
736
+ * - Android needs nothing: the ZKProofport app brings itself to the back
737
+ * (`moveTaskToBack`), which resumes the browser exactly as it was.
738
+ *
739
+ * When this returns undefined the field is omitted and the ZKProofport app
740
+ * tells the user the proof was delivered and to switch back themselves.
741
+ *
742
+ * @param userAgent - override, for tests. Defaults to `navigator.userAgent`.
743
+ * Returns undefined when there is no navigator at all (Node, SSR).
744
+ * @returns a scheme that is safe to open, or undefined to send nothing
745
+ *
746
+ * @internal
747
+ */
748
+ function detectReturnScheme(userAgent) {
749
+ const ua = (typeof navigator === 'undefined' ? undefined : navigator.userAgent);
750
+ if (typeof ua !== 'string' || ua.length === 0) {
751
+ return undefined;
752
+ }
753
+ // A device token is required alongside the browser token. `CriOS` and `FxiOS`
754
+ // are already iOS-only and unambiguous on their own, but pairing them with the
755
+ // device token costs nothing and stops a synthetic or spoofed desktop user
756
+ // agent from producing a mobile-only scheme.
757
+ if (!/(iPhone|iPad|iPod)/.test(ua)) {
758
+ return undefined;
759
+ }
760
+ if (/CriOS\//.test(ua)) {
761
+ return 'googlechrome://';
762
+ }
763
+ if (/FxiOS\//.test(ua)) {
764
+ return 'firefox://';
765
+ }
766
+ return undefined;
767
+ }
673
768
  function validateProofRequest(request) {
674
769
  if (!request.requestId) {
675
770
  return { valid: false, error: 'Missing requestId' };
@@ -5091,8 +5186,13 @@ class ProofportSDK {
5091
5186
  body.dappIcon = options.dappIcon;
5092
5187
  if (options.nonce)
5093
5188
  body.nonce = options.nonce;
5094
- if (options.returnScheme)
5095
- body.returnScheme = options.returnScheme;
5189
+ // An explicit value always wins. Otherwise ask the SDK to work one out from
5190
+ // the page's own user agent — the requesting page is the only side that can
5191
+ // (iOS tells the app nothing about who launched it). Both paths can end in
5192
+ // "send nothing", which is a legitimate outcome, not a failure.
5193
+ const returnScheme = options.returnScheme ?? detectReturnScheme();
5194
+ if (returnScheme)
5195
+ body.returnScheme = returnScheme;
5096
5196
  const response = await fetch(`${this.relayUrl}/api/v1/proof/request`, {
5097
5197
  method: 'POST',
5098
5198
  headers: {
@@ -5396,8 +5496,10 @@ class ProofportSDK {
5396
5496
  * @param options - Request options (message, dappName, dappIcon, nonce, returnScheme)
5397
5497
  * @param options.returnScheme - Which app the ZKProofport app should switch back
5398
5498
  * to once it has finished. Put **your own** deep link scheme here — the bare
5399
- * scheme your app registers, e.g. `'mydapp://'`. A web app can instead pass its
5400
- * own https origin, e.g. `'https://myapp.com'`.
5499
+ * scheme your app registers, e.g. `'mydapp://'`. Only a native app has an
5500
+ * answer to this question; a web app omits the option entirely, because
5501
+ * opening its https origin would launch a new browser tab on a fresh page
5502
+ * rather than return the user to the tab they started from.
5401
5503
  *
5402
5504
  * This is intentionally **not** a URL and not a return address: no path, query
5403
5505
  * string or fragment is accepted, and the ZKProofport app never sends the proof
@@ -5406,14 +5508,26 @@ class ProofportSDK {
5406
5508
  * "which app should come back to the foreground", exactly like the
5407
5509
  * `redirect.native` value a wallet uses to hand control back after signing.
5408
5510
  *
5409
- * **Omit it and nothing changes** — the user simply stays in the ZKProofport app
5410
- * after the proof and switches back manually. There is no auto-switch on proof
5411
- * failure either; only on success and on the user declining the request.
5511
+ * **Omit it and the SDK decides for you.** When the request is created from a
5512
+ * page running in Chrome for iOS (`CriOS`) or Firefox for iOS (`FxiOS`), the
5513
+ * SDK fills in `'googlechrome://'` or `'firefox://'` by itself: opening either
5514
+ * scheme bare brings that browser forward without navigating, so the user lands
5515
+ * back on the tab they were already reading. Every other browser sends nothing,
5516
+ * because nothing safe exists to send — Safari has no such scheme, and Brave
5517
+ * and Arc are indistinguishable from Safari on iOS, so guessing would eject the
5518
+ * user into the wrong browser.
5519
+ * On Android nothing is needed at all: the ZKProofport app brings itself to the
5520
+ * back and the browser resumes exactly as it was. When no target is sent and
5521
+ * none can be inferred, the ZKProofport app tells the user the proof was
5522
+ * delivered and lets them switch back themselves.
5523
+ *
5524
+ * There is no auto-switch on proof failure; only on success and on the user
5525
+ * declining the request.
5412
5526
  *
5413
5527
  * Rejected with an error before the request is sent: empty or whitespace values,
5414
- * anything longer than 128 characters, values containing a path/query/fragment,
5415
- * and the `http:`, `file:`, `data:`, `javascript:`, `intent:`, `tel:`, `sms:`,
5416
- * `mailto:` families.
5528
+ * anything longer than 128 characters, values carrying a host/path/query/fragment
5529
+ * (including https origins such as `'https://myapp.com'`), and the `http:`,
5530
+ * `file:`, `data:`, `javascript:`, `intent:`, `tel:`, `sms:`, `mailto:` families.
5417
5531
  * @returns Promise resolving to RelayProofRequest with requestId, deepLink, pollUrl
5418
5532
  * @throws Error if signer not set, returnScheme is malformed, or the relay request fails
5419
5533
  *