@frak-labs/core-sdk 0.1.0 → 0.1.1-beta.1e44255d

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 (131) hide show
  1. package/README.md +58 -0
  2. package/cdn/bundle.js +3 -8
  3. package/dist/actions.cjs +1 -1
  4. package/dist/actions.d.cts +3 -1400
  5. package/dist/actions.d.ts +3 -1400
  6. package/dist/actions.js +1 -1
  7. package/dist/bundle.cjs +1 -13
  8. package/dist/bundle.d.cts +4 -1927
  9. package/dist/bundle.d.ts +4 -1927
  10. package/dist/bundle.js +1 -13
  11. package/dist/computeLegacyProductId-BkyJ4rEY.d.ts +538 -0
  12. package/dist/computeLegacyProductId-Raks6FXg.d.cts +538 -0
  13. package/dist/index.cjs +1 -13
  14. package/dist/index.d.cts +3 -1269
  15. package/dist/index.d.ts +3 -1269
  16. package/dist/index.js +1 -13
  17. package/dist/openSso-BCJGchIb.d.cts +1022 -0
  18. package/dist/openSso-DG-_9CED.d.ts +1022 -0
  19. package/dist/setupClient-Cfwpu08d.js +13 -0
  20. package/dist/setupClient-Dh8ljuhV.cjs +13 -0
  21. package/dist/siweAuthenticate-BH7Dn7nZ.d.cts +536 -0
  22. package/dist/siweAuthenticate-BJHbtty4.js +1 -0
  23. package/dist/siweAuthenticate-Btem4QHs.d.ts +536 -0
  24. package/dist/siweAuthenticate-Cwj3HP0m.cjs +1 -0
  25. package/dist/trackEvent-M2RLTQ2p.js +1 -0
  26. package/dist/trackEvent-T_R9ER2S.cjs +1 -0
  27. package/package.json +25 -31
  28. package/src/actions/displayEmbeddedWallet.test.ts +194 -0
  29. package/src/actions/displayEmbeddedWallet.ts +21 -0
  30. package/src/actions/displayModal.test.ts +388 -0
  31. package/src/actions/displayModal.ts +120 -0
  32. package/src/actions/ensureIdentity.ts +68 -0
  33. package/src/actions/getMerchantInformation.test.ts +116 -0
  34. package/src/actions/getMerchantInformation.ts +16 -0
  35. package/src/actions/index.ts +30 -0
  36. package/src/actions/openSso.ts +118 -0
  37. package/src/actions/prepareSso.test.ts +223 -0
  38. package/src/actions/prepareSso.ts +48 -0
  39. package/src/actions/referral/processReferral.test.ts +248 -0
  40. package/src/actions/referral/processReferral.ts +232 -0
  41. package/src/actions/referral/referralInteraction.test.ts +147 -0
  42. package/src/actions/referral/referralInteraction.ts +52 -0
  43. package/src/actions/sendInteraction.ts +56 -0
  44. package/src/actions/trackPurchaseStatus.test.ts +500 -0
  45. package/src/actions/trackPurchaseStatus.ts +90 -0
  46. package/src/actions/watchWalletStatus.test.ts +372 -0
  47. package/src/actions/watchWalletStatus.ts +93 -0
  48. package/src/actions/wrapper/modalBuilder.test.ts +239 -0
  49. package/src/actions/wrapper/modalBuilder.ts +203 -0
  50. package/src/actions/wrapper/sendTransaction.test.ts +164 -0
  51. package/src/actions/wrapper/sendTransaction.ts +62 -0
  52. package/src/actions/wrapper/siweAuthenticate.test.ts +290 -0
  53. package/src/actions/wrapper/siweAuthenticate.ts +94 -0
  54. package/src/bundle.ts +2 -0
  55. package/src/clients/DebugInfo.test.ts +418 -0
  56. package/src/clients/DebugInfo.ts +182 -0
  57. package/src/clients/createIFrameFrakClient.ts +292 -0
  58. package/src/clients/index.ts +3 -0
  59. package/src/clients/setupClient.test.ts +343 -0
  60. package/src/clients/setupClient.ts +73 -0
  61. package/src/clients/transports/iframeLifecycleManager.test.ts +558 -0
  62. package/src/clients/transports/iframeLifecycleManager.ts +229 -0
  63. package/src/constants/interactionTypes.ts +15 -0
  64. package/src/constants/locales.ts +14 -0
  65. package/src/index.ts +109 -0
  66. package/src/types/client.ts +14 -0
  67. package/src/types/compression.ts +22 -0
  68. package/src/types/config.ts +117 -0
  69. package/src/types/context.ts +13 -0
  70. package/src/types/index.ts +74 -0
  71. package/src/types/lifecycle/client.ts +69 -0
  72. package/src/types/lifecycle/iframe.ts +41 -0
  73. package/src/types/lifecycle/index.ts +2 -0
  74. package/src/types/rpc/displayModal.ts +82 -0
  75. package/src/types/rpc/embedded/index.ts +68 -0
  76. package/src/types/rpc/embedded/loggedIn.ts +55 -0
  77. package/src/types/rpc/embedded/loggedOut.ts +28 -0
  78. package/src/types/rpc/interaction.ts +30 -0
  79. package/src/types/rpc/merchantInformation.ts +77 -0
  80. package/src/types/rpc/modal/final.ts +46 -0
  81. package/src/types/rpc/modal/generic.ts +46 -0
  82. package/src/types/rpc/modal/index.ts +16 -0
  83. package/src/types/rpc/modal/login.ts +36 -0
  84. package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
  85. package/src/types/rpc/modal/transaction.ts +33 -0
  86. package/src/types/rpc/sso.ts +80 -0
  87. package/src/types/rpc/walletStatus.ts +29 -0
  88. package/src/types/rpc.ts +150 -0
  89. package/src/types/tracking.ts +60 -0
  90. package/src/types/transport.ts +34 -0
  91. package/src/utils/FrakContext.test.ts +407 -0
  92. package/src/utils/FrakContext.ts +158 -0
  93. package/src/utils/backendUrl.test.ts +83 -0
  94. package/src/utils/backendUrl.ts +62 -0
  95. package/src/utils/clientId.test.ts +41 -0
  96. package/src/utils/clientId.ts +43 -0
  97. package/src/utils/compression/b64.test.ts +181 -0
  98. package/src/utils/compression/b64.ts +29 -0
  99. package/src/utils/compression/compress.test.ts +123 -0
  100. package/src/utils/compression/compress.ts +11 -0
  101. package/src/utils/compression/decompress.test.ts +149 -0
  102. package/src/utils/compression/decompress.ts +11 -0
  103. package/src/utils/compression/index.ts +3 -0
  104. package/src/utils/computeLegacyProductId.ts +11 -0
  105. package/src/utils/constants.test.ts +23 -0
  106. package/src/utils/constants.ts +9 -0
  107. package/src/utils/deepLinkWithFallback.test.ts +243 -0
  108. package/src/utils/deepLinkWithFallback.ts +103 -0
  109. package/src/utils/formatAmount.test.ts +113 -0
  110. package/src/utils/formatAmount.ts +24 -0
  111. package/src/utils/getCurrencyAmountKey.test.ts +44 -0
  112. package/src/utils/getCurrencyAmountKey.ts +15 -0
  113. package/src/utils/getSupportedCurrency.test.ts +51 -0
  114. package/src/utils/getSupportedCurrency.ts +14 -0
  115. package/src/utils/getSupportedLocale.test.ts +64 -0
  116. package/src/utils/getSupportedLocale.ts +16 -0
  117. package/src/utils/iframeHelper.test.ts +463 -0
  118. package/src/utils/iframeHelper.ts +150 -0
  119. package/src/utils/index.ts +36 -0
  120. package/src/utils/merchantId.test.ts +653 -0
  121. package/src/utils/merchantId.ts +143 -0
  122. package/src/utils/sso.ts +126 -0
  123. package/src/utils/ssoUrlListener.test.ts +252 -0
  124. package/src/utils/ssoUrlListener.ts +60 -0
  125. package/src/utils/trackEvent.test.ts +180 -0
  126. package/src/utils/trackEvent.ts +41 -0
  127. package/cdn/bundle.js.LICENSE.txt +0 -10
  128. package/dist/interactions.cjs +0 -1
  129. package/dist/interactions.d.cts +0 -182
  130. package/dist/interactions.d.ts +0 -182
  131. package/dist/interactions.js +0 -1
@@ -0,0 +1,120 @@
1
+ import type {
2
+ DisplayModalParamsType,
3
+ FrakClient,
4
+ ModalRpcStepsResultType,
5
+ ModalStepTypes,
6
+ } from "../types";
7
+
8
+ /**
9
+ * Function used to display a modal
10
+ * @param client - The current Frak Client
11
+ * @param args
12
+ * @param args.steps - The different steps of the modal
13
+ * @param args.metadata - The metadata for the modal (customization, etc)
14
+ * @returns The result of each modal steps
15
+ *
16
+ * @description This function will display a modal to the user with the provided steps and metadata.
17
+ *
18
+ * @remarks
19
+ * - The UI of the displayed modal can be configured with the `customCss` property in the `customizations.css` field of the top-level config.
20
+ * - The `login` step will be automatically skipped if the user is already logged in. It's safe to include this step in all cases to ensure proper user state.
21
+ * - Steps are automatically reordered in the following sequence:
22
+ * 1. `login` (if needed)
23
+ * 2. All other steps in the order specified
24
+ * 3. `success` (if included, always last)
25
+ *
26
+ * @example
27
+ * Simple sharing modal with steps:
28
+ * 1. Login (Skipped if already logged in)
29
+ * 2. Display a success message with sharing link option
30
+ *
31
+ * ```ts
32
+ * const results = await displayModal(frakConfig, {
33
+ * steps: {
34
+ * // Simple login with no SSO, nor customization
35
+ * login: { allowSso: false },
36
+ * // Success message
37
+ * final: {
38
+ * action: { key: "reward" },
39
+ * // Skip this step, it will be only displayed in the stepper within the modal
40
+ * autoSkip: true,
41
+ * },
42
+ * },
43
+ * });
44
+ *
45
+ * console.log("Login step - wallet", results.login.wallet);
46
+ * ```
47
+ *
48
+ * @example
49
+ * A full modal example, with a few customization options, with the steps:
50
+ * 1. Login (Skipped if already logged in)
51
+ * 2. Authenticate via SIWE
52
+ * 3. Send a transaction
53
+ * 4. Display a success message with sharing link options
54
+ *
55
+ * ```ts
56
+ * const results = await displayModal(frakConfig, {
57
+ * steps: {
58
+ * // Login step
59
+ * login: {
60
+ * allowSso: true,
61
+ * ssoMetadata: {
62
+ * logoUrl: "https://my-app.com/logo.png",
63
+ * homepageLink: "https://my-app.com",
64
+ * },
65
+ * },
66
+ * // Siwe authentication
67
+ * siweAuthenticate: {
68
+ * siwe: {
69
+ * domain: "my-app.com",
70
+ * uri: "https://my-app.com/",
71
+ * nonce: generateSiweNonce(),
72
+ * version: "1",
73
+ * },
74
+ * },
75
+ * // Send batched transaction
76
+ * sendTransaction: {
77
+ * tx: [
78
+ * { to: "0xdeadbeef", data: "0xdeadbeef" },
79
+ * { to: "0xdeadbeef", data: "0xdeadbeef" },
80
+ * ],
81
+ * },
82
+ * // Success message with sharing options
83
+ * final: {
84
+ * action: {
85
+ * key: "sharing",
86
+ * options: {
87
+ * popupTitle: "Share the app",
88
+ * text: "Discover my super app website",
89
+ * link: "https://my-app.com",
90
+ * },
91
+ * },
92
+ * dismissedMetadata: {
93
+ * title: "Dismiss",
94
+ * description: "You won't be rewarded for this sharing action",
95
+ * },
96
+ * },
97
+ * },
98
+ * metadata: {
99
+ * // Header of desktop modals
100
+ * header: {
101
+ * title: "My-App",
102
+ * icon: "https://my-app.com/logo.png",
103
+ * },
104
+ * // Context that will be present in every modal steps
105
+ * context: "My-app overkill flow",
106
+ * },
107
+ * });
108
+ * ```
109
+ */
110
+ export async function displayModal<
111
+ T extends ModalStepTypes[] = ModalStepTypes[],
112
+ >(
113
+ client: FrakClient,
114
+ { steps, metadata }: DisplayModalParamsType<T>
115
+ ): Promise<ModalRpcStepsResultType<T>> {
116
+ return (await client.request({
117
+ method: "frak_displayModal",
118
+ params: [steps, metadata, client.config.metadata],
119
+ })) as ModalRpcStepsResultType<T>;
120
+ }
@@ -0,0 +1,68 @@
1
+ import { getBackendUrl } from "../utils/backendUrl";
2
+ import { getClientId } from "../utils/clientId";
3
+ import { fetchMerchantId } from "../utils/merchantId";
4
+
5
+ const ENSURE_STORAGE_PREFIX = "frak-identity-ensured-";
6
+
7
+ /**
8
+ * Ensure the current wallet ↔ clientId link exists on the backend.
9
+ *
10
+ * Called automatically by {@link watchWalletStatus} when a connected wallet
11
+ * status is received. Acts as a failsafe: if the primary merge (SSO, pairing,
12
+ * login/register) missed or silently failed, this ensures the link is
13
+ * eventually established.
14
+ *
15
+ * The call is:
16
+ * - **Idempotent** — if already linked, backend returns immediately
17
+ * - **Deduplicated** — only fires once per browser session per merchant
18
+ * - **Fire-and-forget** — errors are logged but never thrown
19
+ *
20
+ * @param interactionToken - The SDK JWT from wallet status (x-wallet-sdk-auth)
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * // Usually called automatically via watchWalletStatus side effect.
25
+ * // Can also be called manually if needed:
26
+ * await ensureIdentity("eyJhbGciOi...");
27
+ * ```
28
+ */
29
+ export async function ensureIdentity(interactionToken: string): Promise<void> {
30
+ if (typeof window === "undefined") {
31
+ return;
32
+ }
33
+
34
+ const clientId = getClientId();
35
+ if (!clientId) {
36
+ return;
37
+ }
38
+
39
+ const merchantId = await fetchMerchantId();
40
+ if (!merchantId) {
41
+ return;
42
+ }
43
+
44
+ const storageKey = `${ENSURE_STORAGE_PREFIX}${merchantId}`;
45
+ if (window.sessionStorage.getItem(storageKey)) {
46
+ return;
47
+ }
48
+
49
+ try {
50
+ const backendUrl = getBackendUrl();
51
+ const response = await fetch(`${backendUrl}/user/identity/ensure`, {
52
+ method: "POST",
53
+ headers: {
54
+ Accept: "application/json",
55
+ "Content-Type": "application/json",
56
+ "x-wallet-sdk-auth": interactionToken,
57
+ "x-frak-client-id": clientId,
58
+ },
59
+ body: JSON.stringify({ merchantId }),
60
+ });
61
+
62
+ if (response.ok) {
63
+ window.sessionStorage.setItem(storageKey, "1");
64
+ }
65
+ } catch {
66
+ // Fire-and-forget — retry on next session
67
+ }
68
+ }
@@ -0,0 +1,116 @@
1
+ import type { Address, Hex } from "viem";
2
+ import { describe, expect, it, vi } from "../../tests/vitest-fixtures";
3
+ import type { FrakClient, GetMerchantInformationReturnType } from "../types";
4
+ import { getMerchantInformation } from "./getMerchantInformation";
5
+
6
+ describe("getMerchantInformation", () => {
7
+ describe("success cases", () => {
8
+ it("should call client.request with correct method", async () => {
9
+ const mockResponse: GetMerchantInformationReturnType = {
10
+ id: "0x1234567890123456789012345678901234567890123456789012345678901234" as Hex,
11
+ onChainMetadata: {
12
+ name: "Test Merchant",
13
+ domain: "example.com",
14
+ },
15
+ rewards: [],
16
+ };
17
+
18
+ const mockClient = {
19
+ request: vi.fn().mockResolvedValue(mockResponse),
20
+ } as unknown as FrakClient;
21
+
22
+ await getMerchantInformation(mockClient);
23
+
24
+ expect(mockClient.request).toHaveBeenCalledWith({
25
+ method: "frak_getMerchantInformation",
26
+ });
27
+ });
28
+
29
+ it("should return merchant information", async () => {
30
+ const mockResponse: GetMerchantInformationReturnType = {
31
+ id: "0x1234567890123456789012345678901234567890123456789012345678901234" as Hex,
32
+ onChainMetadata: {
33
+ name: "Test Merchant",
34
+ domain: "example.com",
35
+ },
36
+ rewards: [],
37
+ };
38
+
39
+ const mockClient = {
40
+ request: vi.fn().mockResolvedValue(mockResponse),
41
+ } as unknown as FrakClient;
42
+
43
+ const result = await getMerchantInformation(mockClient);
44
+
45
+ expect(result).toEqual(mockResponse);
46
+ });
47
+
48
+ it("should return merchant information with rewards", async () => {
49
+ const mockResponse: GetMerchantInformationReturnType = {
50
+ id: "0x1234567890123456789012345678901234567890123456789012345678901234" as Hex,
51
+ onChainMetadata: {
52
+ name: "Test Merchant",
53
+ domain: "example.com",
54
+ },
55
+ rewards: [
56
+ {
57
+ token: "0x1234567890123456789012345678901234567890" as Address,
58
+ campaignId: "campaign-1",
59
+ interactionTypeKey: "referral",
60
+ referrer: {
61
+ payoutType: "fixed",
62
+ amount: {
63
+ amount: 10,
64
+ eurAmount: 1,
65
+ usdAmount: 1.2,
66
+ gbpAmount: 0.9,
67
+ },
68
+ },
69
+ referee: {
70
+ payoutType: "fixed",
71
+ amount: {
72
+ amount: 5,
73
+ eurAmount: 0.5,
74
+ usdAmount: 0.6,
75
+ gbpAmount: 0.45,
76
+ },
77
+ },
78
+ },
79
+ ],
80
+ };
81
+
82
+ const mockClient = {
83
+ request: vi.fn().mockResolvedValue(mockResponse),
84
+ } as unknown as FrakClient;
85
+
86
+ const result = await getMerchantInformation(mockClient);
87
+
88
+ expect(result).toEqual(mockResponse);
89
+ expect(result.rewards).toHaveLength(1);
90
+ });
91
+ });
92
+
93
+ describe("error handling", () => {
94
+ it("should propagate errors from client.request", async () => {
95
+ const error = new Error("RPC request failed");
96
+ const mockClient = {
97
+ request: vi.fn().mockRejectedValue(error),
98
+ } as unknown as FrakClient;
99
+
100
+ await expect(getMerchantInformation(mockClient)).rejects.toThrow(
101
+ "RPC request failed"
102
+ );
103
+ });
104
+
105
+ it("should handle network timeout errors", async () => {
106
+ const error = new Error("Request timeout");
107
+ const mockClient = {
108
+ request: vi.fn().mockRejectedValue(error),
109
+ } as unknown as FrakClient;
110
+
111
+ await expect(getMerchantInformation(mockClient)).rejects.toThrow(
112
+ "Request timeout"
113
+ );
114
+ });
115
+ });
116
+ });
@@ -0,0 +1,16 @@
1
+ import type { FrakClient, GetMerchantInformationReturnType } from "../types";
2
+
3
+ /**
4
+ * Fetch the current merchant information (name, rewards, tiers) from the wallet iframe
5
+ * @param client - The current Frak Client
6
+ * @returns The merchant information including available reward tiers
7
+ *
8
+ * @see {@link @frak-labs/core-sdk!index.GetMerchantInformationReturnType | `GetMerchantInformationReturnType`} for the return type shape
9
+ */
10
+ export async function getMerchantInformation(
11
+ client: FrakClient
12
+ ): Promise<GetMerchantInformationReturnType> {
13
+ return await client.request({
14
+ method: "frak_getMerchantInformation",
15
+ });
16
+ }
@@ -0,0 +1,30 @@
1
+ export { displayEmbeddedWallet } from "./displayEmbeddedWallet";
2
+ export { displayModal } from "./displayModal";
3
+ export { ensureIdentity } from "./ensureIdentity";
4
+ export { getMerchantInformation } from "./getMerchantInformation";
5
+ export { openSso } from "./openSso";
6
+ export { prepareSso } from "./prepareSso";
7
+ export {
8
+ type ProcessReferralOptions,
9
+ processReferral,
10
+ } from "./referral/processReferral";
11
+ // Referral interaction
12
+ export { referralInteraction } from "./referral/referralInteraction";
13
+ export { sendInteraction } from "./sendInteraction";
14
+ // Helper to track the purchase status
15
+ export { trackPurchaseStatus } from "./trackPurchaseStatus";
16
+ export { watchWalletStatus } from "./watchWalletStatus";
17
+ // Modal wrappers
18
+ export {
19
+ type ModalBuilder,
20
+ type ModalStepBuilder,
21
+ modalBuilder,
22
+ } from "./wrapper/modalBuilder";
23
+ export {
24
+ type SendTransactionParams,
25
+ sendTransaction,
26
+ } from "./wrapper/sendTransaction";
27
+ export {
28
+ type SiweAuthenticateModalParams,
29
+ siweAuthenticate,
30
+ } from "./wrapper/siweAuthenticate";
@@ -0,0 +1,118 @@
1
+ import type {
2
+ FrakClient,
3
+ OpenSsoParamsType,
4
+ OpenSsoReturnType,
5
+ } from "../types";
6
+ import { getClientId } from "../utils/clientId";
7
+ import { computeLegacyProductId } from "../utils/computeLegacyProductId";
8
+ import { generateSsoUrl } from "../utils/sso";
9
+
10
+ // SSO popup configuration
11
+ export const ssoPopupFeatures =
12
+ "menubar=no,status=no,scrollbars=no,fullscreen=no,width=500, height=800";
13
+ export const ssoPopupName = "frak-sso";
14
+
15
+ /**
16
+ * Function used to open the SSO
17
+ * @param client - The current Frak Client
18
+ * @param args - The SSO parameters
19
+ *
20
+ * @description Two SSO flow modes:
21
+ *
22
+ * **Redirect Mode** (openInSameWindow: true):
23
+ * - Wallet generates URL and triggers redirect
24
+ * - Used when redirectUrl is provided
25
+ *
26
+ * **Popup Mode** (openInSameWindow: false/omitted):
27
+ * - SDK generates URL client-side (or uses provided ssoPopupUrl)
28
+ * - Opens popup synchronously (prevents popup blockers)
29
+ * - Waits for SSO completion via postMessage
30
+ *
31
+ * @example
32
+ * First we build the sso metadata
33
+ * ```ts
34
+ * // Build the metadata
35
+ * const metadata: SsoMetadata = {
36
+ * logoUrl: "https://my-app.com/logo.png",
37
+ * homepageLink: "https://my-app.com",
38
+ * };
39
+ * ```
40
+ *
41
+ * Then, either use it with direct exit (and so user is directly redirected to your website), or a custom redirect URL
42
+ * :::code-group
43
+ * ```ts [Popup (default)]
44
+ * // Opens in popup, SDK generates URL automatically
45
+ * await openSso(frakConfig, {
46
+ * directExit: true,
47
+ * metadata,
48
+ * });
49
+ * ```
50
+ * ```ts [Redirect]
51
+ * // Opens in same window with redirect
52
+ * await openSso(frakConfig, {
53
+ * redirectUrl: "https://my-app.com/frak-sso",
54
+ * metadata,
55
+ * openInSameWindow: true,
56
+ * });
57
+ * ```
58
+ * ```ts [Custom popup URL]
59
+ * // Advanced: provide custom SSO URL
60
+ * const { ssoUrl } = await prepareSso(frakConfig, { metadata });
61
+ * await openSso(frakConfig, {
62
+ * metadata,
63
+ * ssoPopupUrl: `${ssoUrl}&custom=param`,
64
+ * });
65
+ * ```
66
+ * :::
67
+ */
68
+ export async function openSso(
69
+ client: FrakClient,
70
+ args: OpenSsoParamsType
71
+ ): Promise<OpenSsoReturnType> {
72
+ const { metadata, customizations, walletUrl } = client.config;
73
+
74
+ // Check if redirect mode (default to true if redirectUrl present)
75
+ const isRedirectMode = args.openInSameWindow ?? !!args.redirectUrl;
76
+
77
+ if (isRedirectMode) {
78
+ // Redirect flow: Wallet generates URL and triggers redirect via lifecycle event
79
+ // This must happen on wallet side because only the iframe can trigger the redirect
80
+ return await client.request({
81
+ method: "frak_openSso",
82
+ params: [args, metadata.name, customizations?.css],
83
+ });
84
+ }
85
+
86
+ // Popup flow: Generate URL on SDK side and open synchronously
87
+ // This ensures window.open() is called in same tick as user gesture (no popup blocker)
88
+
89
+ // Step 1: Generate or use provided SSO URL
90
+ const ssoUrl =
91
+ args.ssoPopupUrl ??
92
+ generateSsoUrl(
93
+ walletUrl ?? "https://wallet.frak.id",
94
+ args,
95
+ computeLegacyProductId(),
96
+ metadata.name,
97
+ getClientId(),
98
+ customizations?.css
99
+ );
100
+
101
+ // Step 2: Open popup synchronously (critical for popup blocker prevention)
102
+ const popup = window.open(ssoUrl, ssoPopupName, ssoPopupFeatures);
103
+ if (!popup) {
104
+ throw new Error(
105
+ "Popup was blocked. Please allow popups for this site."
106
+ );
107
+ }
108
+ popup.focus();
109
+
110
+ // Step 3: Wait for SSO completion via RPC
111
+ // The wallet iframe will resolve this when SSO page sends sso_complete message
112
+ const result = await client.request({
113
+ method: "frak_openSso",
114
+ params: [args, metadata.name, customizations?.css],
115
+ });
116
+
117
+ return result ?? {};
118
+ }
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Tests for prepareSso action
3
+ * Tests SSO URL generation via RPC
4
+ */
5
+
6
+ import { describe, expect, it, vi } from "../../tests/vitest-fixtures";
7
+ import type {
8
+ FrakClient,
9
+ PrepareSsoParamsType,
10
+ PrepareSsoReturnType,
11
+ } from "../types";
12
+ import { prepareSso } from "./prepareSso";
13
+
14
+ describe("prepareSso", () => {
15
+ describe("success cases", () => {
16
+ it("should call client.request with correct method and params", async () => {
17
+ const mockResponse: PrepareSsoReturnType = {
18
+ ssoUrl: "https://wallet.frak.id/sso?params=xyz",
19
+ };
20
+
21
+ const mockClient = {
22
+ config: {
23
+ metadata: {
24
+ name: "Test App",
25
+ },
26
+ },
27
+ request: vi.fn().mockResolvedValue(mockResponse),
28
+ } as unknown as FrakClient;
29
+
30
+ const params: PrepareSsoParamsType = {
31
+ directExit: true,
32
+ };
33
+
34
+ await prepareSso(mockClient, params);
35
+
36
+ expect(mockClient.request).toHaveBeenCalledWith({
37
+ method: "frak_prepareSso",
38
+ params: [params, "Test App", undefined],
39
+ });
40
+ });
41
+
42
+ it("should return SSO URL", async () => {
43
+ const mockResponse: PrepareSsoReturnType = {
44
+ ssoUrl: "https://wallet.frak.id/sso?params=abc123",
45
+ };
46
+
47
+ const mockClient = {
48
+ config: {
49
+ metadata: {
50
+ name: "Test App",
51
+ },
52
+ },
53
+ request: vi.fn().mockResolvedValue(mockResponse),
54
+ } as unknown as FrakClient;
55
+
56
+ const params: PrepareSsoParamsType = {
57
+ directExit: false,
58
+ };
59
+
60
+ const result = await prepareSso(mockClient, params);
61
+
62
+ expect(result).toEqual(mockResponse);
63
+ expect(result.ssoUrl).toBe(
64
+ "https://wallet.frak.id/sso?params=abc123"
65
+ );
66
+ });
67
+
68
+ it("should include custom CSS when provided", async () => {
69
+ const mockResponse: PrepareSsoReturnType = {
70
+ ssoUrl: "https://wallet.frak.id/sso?params=custom",
71
+ };
72
+
73
+ const mockClient = {
74
+ config: {
75
+ metadata: {
76
+ name: "Styled App",
77
+ },
78
+ customizations: {
79
+ css: "body { background: red; }",
80
+ },
81
+ },
82
+ request: vi.fn().mockResolvedValue(mockResponse),
83
+ } as unknown as FrakClient;
84
+
85
+ const params: PrepareSsoParamsType = {
86
+ directExit: true,
87
+ };
88
+
89
+ await prepareSso(mockClient, params);
90
+
91
+ expect(mockClient.request).toHaveBeenCalledWith({
92
+ method: "frak_prepareSso",
93
+ params: [params, "Styled App", "body { background: red; }"],
94
+ });
95
+ });
96
+
97
+ it("should handle params with redirectUrl", async () => {
98
+ const mockResponse: PrepareSsoReturnType = {
99
+ ssoUrl: "https://wallet.frak.id/sso?redirect=https://example.com",
100
+ };
101
+
102
+ const mockClient = {
103
+ config: {
104
+ metadata: {
105
+ name: "Test App",
106
+ },
107
+ },
108
+ request: vi.fn().mockResolvedValue(mockResponse),
109
+ } as unknown as FrakClient;
110
+
111
+ const params: PrepareSsoParamsType = {
112
+ redirectUrl: "https://example.com/callback",
113
+ directExit: false,
114
+ };
115
+
116
+ await prepareSso(mockClient, params);
117
+
118
+ expect(mockClient.request).toHaveBeenCalledWith({
119
+ method: "frak_prepareSso",
120
+ params: [params, "Test App", undefined],
121
+ });
122
+ });
123
+
124
+ it("should handle params with metadata", async () => {
125
+ const mockResponse: PrepareSsoReturnType = {
126
+ ssoUrl: "https://wallet.frak.id/sso?metadata=xyz",
127
+ };
128
+
129
+ const mockClient = {
130
+ config: {
131
+ metadata: {
132
+ name: "App with Metadata",
133
+ logoUrl: "https://example.com/logo.png",
134
+ },
135
+ },
136
+ request: vi.fn().mockResolvedValue(mockResponse),
137
+ } as unknown as FrakClient;
138
+
139
+ const params: PrepareSsoParamsType = {
140
+ metadata: {
141
+ logoUrl: "https://custom.com/logo.png",
142
+ homepageLink: "https://custom.com",
143
+ },
144
+ directExit: true,
145
+ };
146
+
147
+ await prepareSso(mockClient, params);
148
+
149
+ expect(mockClient.request).toHaveBeenCalledWith({
150
+ method: "frak_prepareSso",
151
+ params: [params, "App with Metadata", undefined],
152
+ });
153
+ });
154
+
155
+ it("should pass client metadata name to request", async () => {
156
+ const mockResponse: PrepareSsoReturnType = {
157
+ ssoUrl: "https://wallet.frak.id/sso?name=MyApp",
158
+ };
159
+
160
+ const mockClient = {
161
+ config: {
162
+ metadata: {
163
+ name: "MyApp",
164
+ logoUrl: "https://example.com/logo.png",
165
+ },
166
+ customizations: {
167
+ css: "body { color: blue; }",
168
+ },
169
+ },
170
+ request: vi.fn().mockResolvedValue(mockResponse),
171
+ } as unknown as FrakClient;
172
+
173
+ const params: PrepareSsoParamsType = {};
174
+
175
+ await prepareSso(mockClient, params);
176
+
177
+ expect(mockClient.request).toHaveBeenCalledWith({
178
+ method: "frak_prepareSso",
179
+ params: [params, "MyApp", "body { color: blue; }"],
180
+ });
181
+ });
182
+ });
183
+
184
+ describe("error handling", () => {
185
+ it("should propagate errors from client.request", async () => {
186
+ const error = new Error("SSO preparation failed");
187
+ const mockClient = {
188
+ config: {
189
+ metadata: {
190
+ name: "Test App",
191
+ },
192
+ },
193
+ request: vi.fn().mockRejectedValue(error),
194
+ } as unknown as FrakClient;
195
+
196
+ const params: PrepareSsoParamsType = {
197
+ directExit: true,
198
+ };
199
+
200
+ await expect(prepareSso(mockClient, params)).rejects.toThrow(
201
+ "SSO preparation failed"
202
+ );
203
+ });
204
+
205
+ it("should handle network errors", async () => {
206
+ const error = new Error("Network timeout");
207
+ const mockClient = {
208
+ config: {
209
+ metadata: {
210
+ name: "Test App",
211
+ },
212
+ },
213
+ request: vi.fn().mockRejectedValue(error),
214
+ } as unknown as FrakClient;
215
+
216
+ const params: PrepareSsoParamsType = {};
217
+
218
+ await expect(prepareSso(mockClient, params)).rejects.toThrow(
219
+ "Network timeout"
220
+ );
221
+ });
222
+ });
223
+ });