@frak-labs/core-sdk 0.1.0 → 0.1.1-beta.4dfea079

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 (130) 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-CscYhyUi.d.cts +525 -0
  12. package/dist/computeLegacyProductId-WbD1gXV9.d.ts +525 -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-CC1-loUk.d.cts +1019 -0
  18. package/dist/openSso-tkqaDQLV.d.ts +1019 -0
  19. package/dist/setupClient-BjIbK6XJ.cjs +13 -0
  20. package/dist/setupClient-D_HId3e2.js +13 -0
  21. package/dist/siweAuthenticate-B_Z2OZmj.cjs +1 -0
  22. package/dist/siweAuthenticate-CQ4OfPuA.js +1 -0
  23. package/dist/siweAuthenticate-CR4Dpji6.d.cts +467 -0
  24. package/dist/siweAuthenticate-udoruuy9.d.ts +467 -0
  25. package/dist/trackEvent-CGIryq5h.cjs +1 -0
  26. package/dist/trackEvent-YfUh4jrx.js +1 -0
  27. package/package.json +24 -30
  28. package/src/actions/displayEmbeddedWallet.test.ts +194 -0
  29. package/src/actions/displayEmbeddedWallet.ts +20 -0
  30. package/src/actions/displayModal.test.ts +388 -0
  31. package/src/actions/displayModal.ts +120 -0
  32. package/src/actions/getMerchantInformation.test.ts +116 -0
  33. package/src/actions/getMerchantInformation.ts +9 -0
  34. package/src/actions/index.ts +29 -0
  35. package/src/actions/openSso.ts +116 -0
  36. package/src/actions/prepareSso.test.ts +223 -0
  37. package/src/actions/prepareSso.ts +48 -0
  38. package/src/actions/referral/processReferral.test.ts +248 -0
  39. package/src/actions/referral/processReferral.ts +232 -0
  40. package/src/actions/referral/referralInteraction.test.ts +147 -0
  41. package/src/actions/referral/referralInteraction.ts +52 -0
  42. package/src/actions/sendInteraction.ts +24 -0
  43. package/src/actions/trackPurchaseStatus.test.ts +287 -0
  44. package/src/actions/trackPurchaseStatus.ts +56 -0
  45. package/src/actions/watchWalletStatus.test.ts +372 -0
  46. package/src/actions/watchWalletStatus.ts +93 -0
  47. package/src/actions/wrapper/modalBuilder.test.ts +239 -0
  48. package/src/actions/wrapper/modalBuilder.ts +203 -0
  49. package/src/actions/wrapper/sendTransaction.test.ts +164 -0
  50. package/src/actions/wrapper/sendTransaction.ts +62 -0
  51. package/src/actions/wrapper/siweAuthenticate.test.ts +290 -0
  52. package/src/actions/wrapper/siweAuthenticate.ts +94 -0
  53. package/src/bundle.ts +2 -0
  54. package/src/clients/DebugInfo.test.ts +418 -0
  55. package/src/clients/DebugInfo.ts +182 -0
  56. package/src/clients/createIFrameFrakClient.ts +289 -0
  57. package/src/clients/index.ts +3 -0
  58. package/src/clients/setupClient.test.ts +343 -0
  59. package/src/clients/setupClient.ts +73 -0
  60. package/src/clients/transports/iframeLifecycleManager.test.ts +558 -0
  61. package/src/clients/transports/iframeLifecycleManager.ts +174 -0
  62. package/src/constants/interactionTypes.ts +15 -0
  63. package/src/constants/locales.ts +14 -0
  64. package/src/index.ts +110 -0
  65. package/src/types/client.ts +14 -0
  66. package/src/types/compression.ts +22 -0
  67. package/src/types/config.ts +117 -0
  68. package/src/types/context.ts +13 -0
  69. package/src/types/index.ts +75 -0
  70. package/src/types/lifecycle/client.ts +69 -0
  71. package/src/types/lifecycle/iframe.ts +41 -0
  72. package/src/types/lifecycle/index.ts +2 -0
  73. package/src/types/rpc/displayModal.ts +82 -0
  74. package/src/types/rpc/embedded/index.ts +68 -0
  75. package/src/types/rpc/embedded/loggedIn.ts +55 -0
  76. package/src/types/rpc/embedded/loggedOut.ts +28 -0
  77. package/src/types/rpc/interaction.ts +30 -0
  78. package/src/types/rpc/merchantInformation.ts +77 -0
  79. package/src/types/rpc/modal/final.ts +46 -0
  80. package/src/types/rpc/modal/generic.ts +46 -0
  81. package/src/types/rpc/modal/index.ts +16 -0
  82. package/src/types/rpc/modal/login.ts +36 -0
  83. package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
  84. package/src/types/rpc/modal/transaction.ts +33 -0
  85. package/src/types/rpc/sso.ts +80 -0
  86. package/src/types/rpc/walletStatus.ts +29 -0
  87. package/src/types/rpc.ts +146 -0
  88. package/src/types/tracking.ts +60 -0
  89. package/src/types/transport.ts +34 -0
  90. package/src/utils/FrakContext.test.ts +407 -0
  91. package/src/utils/FrakContext.ts +158 -0
  92. package/src/utils/backendUrl.test.ts +83 -0
  93. package/src/utils/backendUrl.ts +62 -0
  94. package/src/utils/clientId.test.ts +41 -0
  95. package/src/utils/clientId.ts +40 -0
  96. package/src/utils/compression/b64.test.ts +181 -0
  97. package/src/utils/compression/b64.ts +29 -0
  98. package/src/utils/compression/compress.test.ts +123 -0
  99. package/src/utils/compression/compress.ts +11 -0
  100. package/src/utils/compression/decompress.test.ts +149 -0
  101. package/src/utils/compression/decompress.ts +11 -0
  102. package/src/utils/compression/index.ts +3 -0
  103. package/src/utils/computeLegacyProductId.ts +11 -0
  104. package/src/utils/constants.test.ts +23 -0
  105. package/src/utils/constants.ts +14 -0
  106. package/src/utils/deepLinkWithFallback.test.ts +243 -0
  107. package/src/utils/deepLinkWithFallback.ts +97 -0
  108. package/src/utils/formatAmount.test.ts +113 -0
  109. package/src/utils/formatAmount.ts +18 -0
  110. package/src/utils/getCurrencyAmountKey.test.ts +44 -0
  111. package/src/utils/getCurrencyAmountKey.ts +15 -0
  112. package/src/utils/getSupportedCurrency.test.ts +51 -0
  113. package/src/utils/getSupportedCurrency.ts +14 -0
  114. package/src/utils/getSupportedLocale.test.ts +64 -0
  115. package/src/utils/getSupportedLocale.ts +16 -0
  116. package/src/utils/iframeHelper.test.ts +450 -0
  117. package/src/utils/iframeHelper.ts +147 -0
  118. package/src/utils/index.ts +36 -0
  119. package/src/utils/merchantId.test.ts +564 -0
  120. package/src/utils/merchantId.ts +122 -0
  121. package/src/utils/sso.ts +126 -0
  122. package/src/utils/ssoUrlListener.test.ts +252 -0
  123. package/src/utils/ssoUrlListener.ts +60 -0
  124. package/src/utils/trackEvent.test.ts +180 -0
  125. package/src/utils/trackEvent.ts +31 -0
  126. package/cdn/bundle.js.LICENSE.txt +0 -10
  127. package/dist/interactions.cjs +0 -1
  128. package/dist/interactions.d.cts +0 -182
  129. package/dist/interactions.d.ts +0 -182
  130. package/dist/interactions.js +0 -1
@@ -0,0 +1,203 @@
1
+ import type {
2
+ DisplayModalParamsType,
3
+ FinalActionType,
4
+ FinalModalStepType,
5
+ FrakClient,
6
+ LoginModalStepType,
7
+ ModalRpcMetadata,
8
+ ModalRpcStepsResultType,
9
+ ModalStepTypes,
10
+ SendTransactionModalStepType,
11
+ } from "../../types";
12
+ import { displayModal } from "../displayModal";
13
+
14
+ /**
15
+ * Represent the type of the modal step builder
16
+ */
17
+ export type ModalStepBuilder<
18
+ Steps extends ModalStepTypes[] = ModalStepTypes[],
19
+ > = {
20
+ /**
21
+ * The current modal params
22
+ */
23
+ params: DisplayModalParamsType<Steps>;
24
+ /**
25
+ * Add a send transaction step to the modal
26
+ */
27
+ sendTx: (
28
+ options: SendTransactionModalStepType["params"]
29
+ ) => ModalStepBuilder<[...Steps, SendTransactionModalStepType]>;
30
+ /**
31
+ * Add a final step of type reward to the modal
32
+ */
33
+ reward: (
34
+ options?: Omit<FinalModalStepType["params"], "action">
35
+ ) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
36
+ /**
37
+ * Add a final step of type sharing to the modal
38
+ */
39
+ sharing: (
40
+ sharingOptions?: Extract<
41
+ FinalActionType,
42
+ { key: "sharing" }
43
+ >["options"],
44
+ options?: Omit<FinalModalStepType["params"], "action">
45
+ ) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
46
+ /**
47
+ * Display the modal
48
+ * @param metadataOverride - Function returning optional metadata to override the current modal metadata
49
+ */
50
+ display: (
51
+ metadataOverride?: (
52
+ current?: ModalRpcMetadata
53
+ ) => ModalRpcMetadata | undefined
54
+ ) => Promise<ModalRpcStepsResultType<Steps>>;
55
+ };
56
+
57
+ /**
58
+ * Represent the output type of the modal builder
59
+ */
60
+ export type ModalBuilder = ModalStepBuilder<[LoginModalStepType]>;
61
+
62
+ /**
63
+ * Helper to craft Frak modal, and share a base initial config
64
+ * @param client - The current Frak Client
65
+ * @param args
66
+ * @param args.metadata - Common modal metadata (customisation, language etc)
67
+ * @param args.login - Login step parameters
68
+ *
69
+ * @description This function will create a modal builder with the provided metadata and login parameters.
70
+ *
71
+ * @example
72
+ * Here is an example of how to use the `modalBuilder` to create and display a sharing modal:
73
+ *
74
+ * ```js
75
+ * // Create the modal builder
76
+ * const modalBuilder = window.FrakSDK.modalBuilder(frakClient, baseModalConfig);
77
+ *
78
+ * // Configure the information to be shared via the sharing link
79
+ * const sharingConfig = {
80
+ * popupTitle: "Share this with your friends",
81
+ * text: "Discover our product!",
82
+ * link: window.location.href,
83
+ * };
84
+ *
85
+ * // Display the sharing modal
86
+ * function modalShare() {
87
+ * modalBuilder.sharing(sharingConfig).display();
88
+ * }
89
+ * ```
90
+ *
91
+ * @see {@link ModalStepTypes} for more info about each modal step types and their parameters
92
+ * @see {@link ModalRpcMetadata} for more info about the metadata that can be passed to the modal
93
+ * @see {@link ModalRpcStepsResultType} for more info about the result of each modal steps
94
+ * @see {@link displayModal} for more info about how the modal is displayed
95
+ */
96
+ export function modalBuilder(
97
+ client: FrakClient,
98
+ {
99
+ metadata,
100
+ login,
101
+ }: {
102
+ metadata?: ModalRpcMetadata;
103
+ login?: LoginModalStepType["params"];
104
+ }
105
+ ): ModalBuilder {
106
+ // Build the initial modal params
107
+ const baseParams: DisplayModalParamsType<[LoginModalStepType]> = {
108
+ steps: {
109
+ login: login ?? {},
110
+ },
111
+ metadata,
112
+ };
113
+
114
+ // Return the step builder
115
+ return modalStepsBuilder(client, baseParams);
116
+ }
117
+
118
+ /**
119
+ * Modal step builder, allowing to add new steps to the modal, and to build and display it
120
+ */
121
+ function modalStepsBuilder<CurrentSteps extends ModalStepTypes[]>(
122
+ client: FrakClient,
123
+ params: DisplayModalParamsType<CurrentSteps>
124
+ ): ModalStepBuilder<CurrentSteps> {
125
+ // Function add the send tx step
126
+ function sendTx(options: SendTransactionModalStepType["params"]) {
127
+ return modalStepsBuilder<
128
+ [...CurrentSteps, SendTransactionModalStepType]
129
+ >(client, {
130
+ ...params,
131
+ steps: {
132
+ ...params.steps,
133
+ sendTransaction: options,
134
+ },
135
+ } as DisplayModalParamsType<
136
+ [...CurrentSteps, SendTransactionModalStepType]
137
+ >);
138
+ }
139
+
140
+ // Function to add a reward step at the end
141
+ function reward(options?: Omit<FinalModalStepType["params"], "action">) {
142
+ return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
143
+ client,
144
+ {
145
+ ...params,
146
+ steps: {
147
+ ...params.steps,
148
+ final: {
149
+ ...options,
150
+ action: { key: "reward" },
151
+ },
152
+ },
153
+ } as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
154
+ );
155
+ }
156
+
157
+ // Function to add sharing step at the end
158
+ function sharing(
159
+ sharingOptions?: Extract<
160
+ FinalActionType,
161
+ { key: "sharing" }
162
+ >["options"],
163
+ options?: Omit<FinalModalStepType["params"], "action">
164
+ ) {
165
+ return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
166
+ client,
167
+ {
168
+ ...params,
169
+ steps: {
170
+ ...params.steps,
171
+ final: {
172
+ ...options,
173
+ action: { key: "sharing", options: sharingOptions },
174
+ },
175
+ },
176
+ } as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
177
+ );
178
+ }
179
+
180
+ // Function to display it
181
+ async function display(
182
+ metadataOverride?: (
183
+ current?: ModalRpcMetadata
184
+ ) => ModalRpcMetadata | undefined
185
+ ) {
186
+ // If we have a metadata override, apply it
187
+ if (metadataOverride) {
188
+ params.metadata = metadataOverride(params.metadata ?? {});
189
+ }
190
+ return await displayModal(client, params);
191
+ }
192
+
193
+ return {
194
+ // Access current modal params
195
+ params,
196
+ // Function to add new steps
197
+ sendTx,
198
+ reward,
199
+ sharing,
200
+ // Display the modal
201
+ display,
202
+ };
203
+ }
@@ -0,0 +1,164 @@
1
+ import { vi } from "vitest";
2
+
3
+ vi.mock("../displayModal", () => ({
4
+ displayModal: vi.fn(),
5
+ }));
6
+
7
+ import type { Address, Hex } from "viem";
8
+ import { describe, expect, it } from "../../../tests/vitest-fixtures";
9
+ import type { FrakClient } from "../../types";
10
+ import { sendTransaction } from "./sendTransaction";
11
+
12
+ describe("sendTransaction", () => {
13
+ const mockClient = {
14
+ config: {
15
+ metadata: {
16
+ name: "Test App",
17
+ },
18
+ },
19
+ request: vi.fn(),
20
+ } as unknown as FrakClient;
21
+
22
+ describe("basic usage", () => {
23
+ it("should call displayModal with correct params for single tx", async () => {
24
+ const { displayModal } = await import("../displayModal");
25
+
26
+ const mockTxHash =
27
+ "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as Hex;
28
+ vi.mocked(displayModal).mockResolvedValue({
29
+ login: {
30
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
31
+ },
32
+ sendTransaction: { hash: mockTxHash },
33
+ } as any);
34
+
35
+ const txParams = {
36
+ to: "0x1234567890123456789012345678901234567890" as Address,
37
+ data: "0xdeadbeef" as Hex,
38
+ };
39
+
40
+ await sendTransaction(mockClient, { tx: txParams });
41
+
42
+ expect(displayModal).toHaveBeenCalledWith(mockClient, {
43
+ metadata: undefined,
44
+ steps: {
45
+ login: {},
46
+ sendTransaction: { tx: txParams },
47
+ },
48
+ });
49
+ });
50
+
51
+ it("should return transaction hash", async () => {
52
+ const { displayModal } = await import("../displayModal");
53
+
54
+ const mockTxHash =
55
+ "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as Hex;
56
+ vi.mocked(displayModal).mockResolvedValue({
57
+ login: {
58
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
59
+ },
60
+ sendTransaction: { hash: mockTxHash },
61
+ } as any);
62
+
63
+ const result = await sendTransaction(mockClient, {
64
+ tx: {
65
+ to: "0x1234567890123456789012345678901234567890" as Address,
66
+ },
67
+ });
68
+
69
+ expect(result).toEqual({ hash: mockTxHash });
70
+ });
71
+ });
72
+
73
+ describe("batched transactions", () => {
74
+ it("should handle multiple transactions", async () => {
75
+ const { displayModal } = await import("../displayModal");
76
+
77
+ const mockTxHash =
78
+ "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as Hex;
79
+ vi.mocked(displayModal).mockResolvedValue({
80
+ login: {},
81
+ sendTransaction: { hash: mockTxHash },
82
+ } as any);
83
+
84
+ const txParams = [
85
+ {
86
+ to: "0x1234567890123456789012345678901234567890" as Address,
87
+ data: "0xdata1" as Hex,
88
+ },
89
+ {
90
+ to: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as Address,
91
+ data: "0xdata2" as Hex,
92
+ },
93
+ ];
94
+
95
+ await sendTransaction(mockClient, { tx: txParams });
96
+
97
+ expect(displayModal).toHaveBeenCalledWith(mockClient, {
98
+ metadata: undefined,
99
+ steps: {
100
+ login: {},
101
+ sendTransaction: { tx: txParams },
102
+ },
103
+ });
104
+ });
105
+ });
106
+
107
+ describe("with metadata", () => {
108
+ it("should pass metadata to displayModal", async () => {
109
+ const { displayModal } = await import("../displayModal");
110
+
111
+ vi.mocked(displayModal).mockResolvedValue({
112
+ login: {},
113
+ sendTransaction: { hash: "0x123" as Hex },
114
+ } as any);
115
+
116
+ const metadata = {
117
+ header: {
118
+ title: "Send ETH",
119
+ icon: "https://example.com/icon.png",
120
+ },
121
+ context: "Send 100 wei to recipient",
122
+ };
123
+
124
+ await sendTransaction(mockClient, {
125
+ tx: {
126
+ to: "0x1234567890123456789012345678901234567890" as Address,
127
+ value: "0x64" as Hex,
128
+ },
129
+ metadata,
130
+ });
131
+
132
+ expect(displayModal).toHaveBeenCalledWith(mockClient, {
133
+ metadata,
134
+ steps: {
135
+ login: {},
136
+ sendTransaction: {
137
+ tx: {
138
+ to: "0x1234567890123456789012345678901234567890",
139
+ value: "0x64",
140
+ },
141
+ },
142
+ },
143
+ });
144
+ });
145
+ });
146
+
147
+ describe("error handling", () => {
148
+ it("should propagate errors from displayModal", async () => {
149
+ const { displayModal } = await import("../displayModal");
150
+
151
+ vi.mocked(displayModal).mockRejectedValue(
152
+ new Error("Transaction rejected")
153
+ );
154
+
155
+ await expect(
156
+ sendTransaction(mockClient, {
157
+ tx: {
158
+ to: "0x1234567890123456789012345678901234567890" as Address,
159
+ },
160
+ })
161
+ ).rejects.toThrow("Transaction rejected");
162
+ });
163
+ });
164
+ });
@@ -0,0 +1,62 @@
1
+ import type {
2
+ FrakClient,
3
+ ModalRpcMetadata,
4
+ SendTransactionModalStepType,
5
+ SendTransactionReturnType,
6
+ } from "../../types";
7
+ import { displayModal } from "../displayModal";
8
+
9
+ /**
10
+ * Parameters to directly show a modal used to send a transaction
11
+ * @inline
12
+ */
13
+ export type SendTransactionParams = {
14
+ /**
15
+ * The transaction to be sent (either a single tx or multiple ones)
16
+ */
17
+ tx: SendTransactionModalStepType["params"]["tx"];
18
+ /**
19
+ * Custom metadata to be passed to the modal
20
+ */
21
+ metadata?: ModalRpcMetadata;
22
+ };
23
+
24
+ /**
25
+ * Function used to send a user transaction, simple wrapper around the displayModal function to ease the send transaction process
26
+ * @param client - The current Frak Client
27
+ * @param args - The parameters
28
+ * @returns The hash of the transaction that was sent in a promise
29
+ *
30
+ * @description This function will display a modal to the user with the provided transaction and metadata.
31
+ *
32
+ * @example
33
+ * const { hash } = await sendTransaction(frakConfig, {
34
+ * tx: {
35
+ * to: "0xdeadbeef",
36
+ * value: toHex(100n),
37
+ * },
38
+ * metadata: {
39
+ * header: {
40
+ * title: "Sending eth",
41
+ * },
42
+ * context: "Send 100wei to 0xdeadbeef",
43
+ * },
44
+ * });
45
+ * console.log("Transaction hash:", hash);
46
+ */
47
+ export async function sendTransaction(
48
+ client: FrakClient,
49
+ { tx, metadata }: SendTransactionParams
50
+ ): Promise<SendTransactionReturnType> {
51
+ // Trigger a modal with login options
52
+ const result = await displayModal(client, {
53
+ metadata,
54
+ steps: {
55
+ login: {},
56
+ sendTransaction: { tx },
57
+ },
58
+ });
59
+
60
+ // Return the tx result only
61
+ return result.sendTransaction;
62
+ }