@frak-labs/core-sdk 0.1.1 → 0.2.0-beta.514ef378

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 (125) hide show
  1. package/README.md +58 -0
  2. package/cdn/bundle.js +14 -0
  3. package/dist/actions.cjs +1 -1
  4. package/dist/actions.d.cts +3 -3
  5. package/dist/actions.d.ts +3 -3
  6. package/dist/actions.js +1 -1
  7. package/dist/bundle.cjs +1 -1
  8. package/dist/bundle.d.cts +4 -6
  9. package/dist/bundle.d.ts +4 -6
  10. package/dist/bundle.js +1 -1
  11. package/dist/{index-CRsQWnTs.d.cts → computeLegacyProductId-BkyJ4rEY.d.ts} +197 -10
  12. package/dist/{index-Ck1hudEi.d.ts → computeLegacyProductId-Raks6FXg.d.cts} +197 -10
  13. package/dist/index.cjs +1 -1
  14. package/dist/index.d.cts +3 -4
  15. package/dist/index.d.ts +3 -4
  16. package/dist/index.js +1 -1
  17. package/dist/{openSso-D--Airj6.d.cts → openSso-BCJGchIb.d.cts} +135 -131
  18. package/dist/{openSso-DsKJ4y0j.d.ts → openSso-DG-_9CED.d.ts} +135 -131
  19. package/dist/setupClient-BEiAE56h.js +13 -0
  20. package/dist/setupClient-Ls3vKSlH.cjs +13 -0
  21. package/dist/{index-d8xS4ryI.d.ts → siweAuthenticate-BH7Dn7nZ.d.cts} +90 -65
  22. package/dist/siweAuthenticate-BJHbtty4.js +1 -0
  23. package/dist/{index-C6FxkWPC.d.cts → siweAuthenticate-Btem4QHs.d.ts} +90 -65
  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 +11 -22
  28. package/src/actions/displayEmbeddedWallet.ts +1 -0
  29. package/src/actions/displayModal.test.ts +12 -11
  30. package/src/actions/displayModal.ts +7 -18
  31. package/src/actions/ensureIdentity.ts +68 -0
  32. package/src/actions/{getProductInformation.test.ts → getMerchantInformation.test.ts} +33 -50
  33. package/src/actions/getMerchantInformation.ts +16 -0
  34. package/src/actions/index.ts +3 -2
  35. package/src/actions/openSso.ts +4 -2
  36. package/src/actions/referral/processReferral.test.ts +42 -151
  37. package/src/actions/referral/processReferral.ts +18 -42
  38. package/src/actions/referral/referralInteraction.test.ts +1 -7
  39. package/src/actions/referral/referralInteraction.ts +1 -6
  40. package/src/actions/sendInteraction.ts +46 -22
  41. package/src/actions/trackPurchaseStatus.test.ts +354 -141
  42. package/src/actions/trackPurchaseStatus.ts +48 -11
  43. package/src/actions/watchWalletStatus.ts +2 -3
  44. package/src/actions/wrapper/modalBuilder.test.ts +0 -14
  45. package/src/actions/wrapper/modalBuilder.ts +3 -12
  46. package/src/bundle.ts +0 -1
  47. package/src/clients/createIFrameFrakClient.ts +10 -5
  48. package/src/clients/transports/iframeLifecycleManager.test.ts +163 -4
  49. package/src/clients/transports/iframeLifecycleManager.ts +172 -33
  50. package/src/constants/interactionTypes.ts +12 -41
  51. package/src/index.ts +24 -16
  52. package/src/types/config.ts +6 -0
  53. package/src/types/index.ts +13 -10
  54. package/src/types/lifecycle/client.ts +24 -1
  55. package/src/types/lifecycle/iframe.ts +6 -0
  56. package/src/types/rpc/displayModal.ts +2 -4
  57. package/src/types/rpc/embedded/index.ts +2 -2
  58. package/src/types/rpc/interaction.ts +26 -39
  59. package/src/types/rpc/merchantInformation.ts +77 -0
  60. package/src/types/rpc/modal/index.ts +0 -4
  61. package/src/types/rpc/modal/login.ts +5 -1
  62. package/src/types/rpc/walletStatus.ts +1 -7
  63. package/src/types/rpc.ts +22 -30
  64. package/src/types/tracking.ts +60 -0
  65. package/src/utils/backendUrl.test.ts +83 -0
  66. package/src/utils/backendUrl.ts +62 -0
  67. package/src/utils/clientId.test.ts +41 -0
  68. package/src/utils/clientId.ts +43 -0
  69. package/src/utils/compression/compress.test.ts +1 -1
  70. package/src/utils/compression/compress.ts +2 -2
  71. package/src/utils/compression/decompress.test.ts +8 -4
  72. package/src/utils/compression/decompress.ts +2 -2
  73. package/src/utils/{computeProductId.ts → computeLegacyProductId.ts} +2 -2
  74. package/src/utils/constants.ts +5 -0
  75. package/src/utils/deepLinkWithFallback.test.ts +243 -0
  76. package/src/utils/deepLinkWithFallback.ts +103 -0
  77. package/src/utils/formatAmount.ts +6 -0
  78. package/src/utils/iframeHelper.test.ts +18 -5
  79. package/src/utils/iframeHelper.ts +10 -3
  80. package/src/utils/index.ts +16 -1
  81. package/src/utils/merchantId.test.ts +653 -0
  82. package/src/utils/merchantId.ts +143 -0
  83. package/src/utils/sso.ts +18 -11
  84. package/src/utils/trackEvent.test.ts +23 -5
  85. package/src/utils/trackEvent.ts +13 -0
  86. package/cdn/bundle.iife.js +0 -14
  87. package/dist/actions-B5j-i1p0.cjs +0 -1
  88. package/dist/actions-q090Z0oR.js +0 -1
  89. package/dist/index-7OZ39x1U.d.ts +0 -195
  90. package/dist/index-zDq-VlKx.d.cts +0 -195
  91. package/dist/interaction-DMJ3ZfaF.d.cts +0 -45
  92. package/dist/interaction-KX1h9a7V.d.ts +0 -45
  93. package/dist/interactions-DnfM3oe0.js +0 -1
  94. package/dist/interactions-EIXhNLf6.cjs +0 -1
  95. package/dist/interactions.cjs +0 -1
  96. package/dist/interactions.d.cts +0 -2
  97. package/dist/interactions.d.ts +0 -2
  98. package/dist/interactions.js +0 -1
  99. package/dist/productTypes-BUkXJKZ7.cjs +0 -1
  100. package/dist/productTypes-CGb1MmBF.js +0 -1
  101. package/dist/src-1LQ4eLq5.js +0 -13
  102. package/dist/src-hW71KjPN.cjs +0 -13
  103. package/dist/trackEvent-CHnYa85W.js +0 -1
  104. package/dist/trackEvent-GuQm_1Nm.cjs +0 -1
  105. package/src/actions/getProductInformation.ts +0 -14
  106. package/src/actions/openSso.test.ts +0 -407
  107. package/src/actions/sendInteraction.test.ts +0 -219
  108. package/src/constants/interactionTypes.test.ts +0 -128
  109. package/src/constants/productTypes.test.ts +0 -130
  110. package/src/constants/productTypes.ts +0 -33
  111. package/src/interactions/index.ts +0 -5
  112. package/src/interactions/pressEncoder.test.ts +0 -215
  113. package/src/interactions/pressEncoder.ts +0 -53
  114. package/src/interactions/purchaseEncoder.test.ts +0 -291
  115. package/src/interactions/purchaseEncoder.ts +0 -99
  116. package/src/interactions/referralEncoder.test.ts +0 -170
  117. package/src/interactions/referralEncoder.ts +0 -47
  118. package/src/interactions/retailEncoder.test.ts +0 -107
  119. package/src/interactions/retailEncoder.ts +0 -37
  120. package/src/interactions/webshopEncoder.test.ts +0 -56
  121. package/src/interactions/webshopEncoder.ts +0 -30
  122. package/src/types/rpc/modal/openSession.ts +0 -25
  123. package/src/types/rpc/productInformation.ts +0 -59
  124. package/src/utils/computeProductId.test.ts +0 -80
  125. package/src/utils/sso.test.ts +0 -361
@@ -1,107 +0,0 @@
1
- /**
2
- * Tests for RetailInteractionEncoder
3
- * Tests encoding of retail-related user interactions
4
- */
5
-
6
- import { pad, toHex } from "viem";
7
- import { describe, expect, test } from "../../tests/vitest-fixtures";
8
- import { interactionTypes } from "../constants/interactionTypes";
9
- import { productTypes } from "../constants/productTypes";
10
- import { RetailInteractionEncoder } from "./retailEncoder";
11
-
12
- describe("RetailInteractionEncoder", () => {
13
- describe("customerMeeting", () => {
14
- test("should encode customer meeting interaction with correct structure", ({
15
- mockAgencyId,
16
- }) => {
17
- const interaction = RetailInteractionEncoder.customerMeeting({
18
- agencyId: mockAgencyId,
19
- });
20
-
21
- // Should return PreparedInteraction structure
22
- expect(interaction).toHaveProperty("handlerTypeDenominator");
23
- expect(interaction).toHaveProperty("interactionData");
24
- });
25
-
26
- test("should use retail product type in handlerTypeDenominator", ({
27
- mockAgencyId,
28
- }) => {
29
- const interaction = RetailInteractionEncoder.customerMeeting({
30
- agencyId: mockAgencyId,
31
- });
32
-
33
- // Should use retail product type (4)
34
- const expectedDenominator = toHex(productTypes.retail);
35
- expect(interaction.handlerTypeDenominator).toBe(
36
- expectedDenominator
37
- );
38
- });
39
-
40
- test("should include customerMeeting interaction type in data", ({
41
- mockAgencyId,
42
- }) => {
43
- const interaction = RetailInteractionEncoder.customerMeeting({
44
- agencyId: mockAgencyId,
45
- });
46
-
47
- // Should start with customerMeeting interaction type
48
- expect(interaction.interactionData).toContain(
49
- interactionTypes.retail.customerMeeting
50
- );
51
- });
52
-
53
- test("should pad agency ID to 32 bytes", ({ mockAgencyId }) => {
54
- const interaction = RetailInteractionEncoder.customerMeeting({
55
- agencyId: mockAgencyId,
56
- });
57
-
58
- // Agency ID should be padded to 32 bytes
59
- const paddedAgencyId = pad(mockAgencyId, { size: 32 });
60
- expect(interaction.interactionData).toContain(
61
- paddedAgencyId.slice(2)
62
- );
63
- });
64
-
65
- test("should produce consistent output for same agency ID", ({
66
- mockAgencyId,
67
- }) => {
68
- const interaction1 = RetailInteractionEncoder.customerMeeting({
69
- agencyId: mockAgencyId,
70
- });
71
- const interaction2 = RetailInteractionEncoder.customerMeeting({
72
- agencyId: mockAgencyId,
73
- });
74
-
75
- // Same input should produce same output
76
- expect(interaction1).toEqual(interaction2);
77
- });
78
-
79
- test("should produce different output for different agency IDs", () => {
80
- const agencyId1 =
81
- "0x0000000000000000000000000000000000000000000000000000000000000001" as const;
82
- const agencyId2 =
83
- "0x0000000000000000000000000000000000000000000000000000000000000002" as const;
84
-
85
- const interaction1 = RetailInteractionEncoder.customerMeeting({
86
- agencyId: agencyId1,
87
- });
88
- const interaction2 = RetailInteractionEncoder.customerMeeting({
89
- agencyId: agencyId2,
90
- });
91
-
92
- // Different agency IDs should produce different interaction data
93
- expect(interaction1.interactionData).not.toBe(
94
- interaction2.interactionData
95
- );
96
- });
97
-
98
- test("should produce valid hex string", ({ mockAgencyId }) => {
99
- const interaction = RetailInteractionEncoder.customerMeeting({
100
- agencyId: mockAgencyId,
101
- });
102
-
103
- // Should be valid hex string starting with 0x
104
- expect(interaction.interactionData).toMatch(/^0x[0-9a-f]+$/);
105
- });
106
- });
107
- });
@@ -1,37 +0,0 @@
1
- import { concatHex, type Hex, pad, toHex } from "viem";
2
- import { interactionTypes } from "../constants/interactionTypes";
3
- import { productTypes } from "../constants/productTypes";
4
- import type { PreparedInteraction } from "../types";
5
-
6
- /**
7
- * Retail interactions allow you to track user activities on your retails products.
8
- *
9
- * :::info
10
- * To properly handle retail interactions, ensure that the "Retail" product type is enabled in your Business dashboard.
11
- * :::
12
- *
13
- * @description Encode retail related user interactions
14
- *
15
- * @group Interactions Encoder
16
- *
17
- * @see {@link PreparedInteraction} The prepared interaction object that can be sent
18
- * @see {@link !actions.sendInteraction | `sendInteraction()`} Action used to send the prepared interaction to the Frak Wallet
19
- */
20
- export const RetailInteractionEncoder = {
21
- /**
22
- * Encode a customer meeting retail interaction
23
- * @param args
24
- * @param args.agencyId - The id of the agency that the customer is meeting with
25
- *
26
- */
27
- customerMeeting({ agencyId }: { agencyId: Hex }): PreparedInteraction {
28
- const interactionData = concatHex([
29
- interactionTypes.retail.customerMeeting,
30
- pad(agencyId, { size: 32 }),
31
- ]);
32
- return {
33
- handlerTypeDenominator: toHex(productTypes.retail),
34
- interactionData,
35
- };
36
- },
37
- };
@@ -1,56 +0,0 @@
1
- /**
2
- * Tests for WebShopInteractionEncoder
3
- * Tests encoding of webshop-related user interactions
4
- */
5
-
6
- import { toHex } from "viem";
7
- import { describe, expect, it } from "../../tests/vitest-fixtures";
8
- import { interactionTypes } from "../constants/interactionTypes";
9
- import { productTypes } from "../constants/productTypes";
10
- import { WebShopInteractionEncoder } from "./webshopEncoder";
11
-
12
- describe("WebShopInteractionEncoder", () => {
13
- describe("open", () => {
14
- it("should encode open webshop interaction with correct structure", () => {
15
- const interaction = WebShopInteractionEncoder.open();
16
-
17
- // Should return PreparedInteraction structure
18
- expect(interaction).toHaveProperty("handlerTypeDenominator");
19
- expect(interaction).toHaveProperty("interactionData");
20
- });
21
-
22
- it("should use webshop product type in handlerTypeDenominator", () => {
23
- const interaction = WebShopInteractionEncoder.open();
24
-
25
- // Should use webshop product type (3)
26
- const expectedDenominator = toHex(productTypes.webshop);
27
- expect(interaction.handlerTypeDenominator).toBe(
28
- expectedDenominator
29
- );
30
- });
31
-
32
- it("should use open interaction type in data", () => {
33
- const interaction = WebShopInteractionEncoder.open();
34
-
35
- // Should use open interaction type
36
- expect(interaction.interactionData).toBe(
37
- interactionTypes.webshop.open
38
- );
39
- });
40
-
41
- it("should produce consistent output (no parameters)", () => {
42
- const interaction1 = WebShopInteractionEncoder.open();
43
- const interaction2 = WebShopInteractionEncoder.open();
44
-
45
- // Should always produce the same output
46
- expect(interaction1).toEqual(interaction2);
47
- });
48
-
49
- it("should produce valid hex string", () => {
50
- const interaction = WebShopInteractionEncoder.open();
51
-
52
- // Should be valid hex string starting with 0x
53
- expect(interaction.interactionData).toMatch(/^0x[0-9a-f]+$/);
54
- });
55
- });
56
- });
@@ -1,30 +0,0 @@
1
- import { toHex } from "viem";
2
- import { interactionTypes } from "../constants/interactionTypes";
3
- import { productTypes } from "../constants/productTypes";
4
- import type { PreparedInteraction } from "../types";
5
-
6
- /**
7
- * Webshop interactions allow you to track user activities on your webshop.
8
- *
9
- * :::info
10
- * To properly handle webshop interactions, ensure that the "WebShop" product type is enabled in your Business dashboard.
11
- * :::
12
- *
13
- * @description Encode webshop related user interactions
14
- *
15
- * @group Interactions Encoder
16
- *
17
- * @see {@link PreparedInteraction} The prepared interaction object that can be sent
18
- * @see {@link !actions.sendInteraction | `sendInteraction()`} Action used to send the prepared interaction to the Frak Wallet
19
- */
20
- export const WebShopInteractionEncoder = {
21
- /**
22
- * Encode an open webshop interaction
23
- */
24
- open(): PreparedInteraction {
25
- return {
26
- handlerTypeDenominator: toHex(productTypes.webshop),
27
- interactionData: interactionTypes.webshop.open,
28
- };
29
- },
30
- };
@@ -1,25 +0,0 @@
1
- import type { GenericModalStepType } from "./generic";
2
-
3
- /**
4
- * Return type of the open session modal step
5
- * @inline
6
- * @ignore
7
- */
8
- export type OpenInteractionSessionReturnType = {
9
- startTimestamp: number;
10
- endTimestamp: number;
11
- };
12
-
13
- /**
14
- * The open interaction session step for a Modal
15
- *
16
- * **Input**: None
17
- * **Output**: The interactions session period (start and end timestamp)
18
- *
19
- * @group Modal Display
20
- */
21
- export type OpenInteractionSessionModalStepType = GenericModalStepType<
22
- "openSession",
23
- object,
24
- OpenInteractionSessionReturnType
25
- >;
@@ -1,59 +0,0 @@
1
- import type { Address, Hex } from "viem";
2
- import type { FullInteractionTypesKey } from "../../constants/interactionTypes";
3
- import type { ProductTypesKey } from "../../constants/productTypes";
4
-
5
- /**
6
- * The type for the amount of tokens
7
- */
8
- export type TokenAmountType = {
9
- amount: number;
10
- eurAmount: number;
11
- usdAmount: number;
12
- gbpAmount: number;
13
- };
14
-
15
- /**
16
- * Response of the `frak_getProductInformation` RPC method
17
- * @group RPC Schema
18
- */
19
- export type GetProductInformationReturnType = {
20
- /**
21
- * Current product id
22
- */
23
- id: Hex;
24
- /**
25
- * Some metadata
26
- */
27
- onChainMetadata: {
28
- /**
29
- * Name of the product on-chain
30
- */
31
- name: string;
32
- /**
33
- * Domain of the product on-chain
34
- */
35
- domain: string;
36
- /**
37
- * The supported product types
38
- */
39
- productTypes: ProductTypesKey[];
40
- };
41
- /**
42
- * The max potential reward for the referrer
43
- */
44
- maxReferrer?: TokenAmountType;
45
- /**
46
- * The max potential reward for the referee
47
- */
48
- maxReferee?: TokenAmountType;
49
- /**
50
- * List of all the potentials reward arround this product
51
- */
52
- rewards: {
53
- token: Address;
54
- campaign: Address;
55
- interactionTypeKey: FullInteractionTypesKey;
56
- referrer: TokenAmountType;
57
- referee: TokenAmountType;
58
- }[];
59
- };
@@ -1,80 +0,0 @@
1
- /**
2
- * Tests for computeProductId utility function
3
- * Tests product ID computation from domain names with normalization
4
- */
5
-
6
- import { keccak256, toHex } from "viem";
7
- import { describe, expect, it } from "../../tests/vitest-fixtures";
8
- import { computeProductId } from "./computeProductId";
9
-
10
- describe("computeProductId", () => {
11
- it("should compute product ID from current domain (window.location.host)", () => {
12
- // In JSDOM test environment, window.location.host is "localhost:3000"
13
- const productId = computeProductId();
14
-
15
- // Should compute keccak256 hash of window.location.host
16
- const expectedId = keccak256(toHex(window.location.host));
17
- expect(productId).toBe(expectedId);
18
- });
19
-
20
- it("should compute product ID from custom domain", () => {
21
- const customDomain = "custom-domain.com";
22
- const productId = computeProductId({ domain: customDomain });
23
-
24
- // Should compute keccak256 hash of custom domain
25
- const expectedId = keccak256(toHex(customDomain));
26
- expect(productId).toBe(expectedId);
27
- });
28
-
29
- it("should remove www. prefix from domain before hashing", () => {
30
- const domainWithWww = "www.example.com";
31
- const productId = computeProductId({ domain: domainWithWww });
32
-
33
- // Should compute keccak256 hash of "example.com" (www. removed)
34
- const expectedId = keccak256(toHex("example.com"));
35
- expect(productId).toBe(expectedId);
36
- });
37
-
38
- it("should produce same product ID for domain with and without www", () => {
39
- const withWww = computeProductId({ domain: "www.example.com" });
40
- const withoutWww = computeProductId({ domain: "example.com" });
41
-
42
- // Both should produce the same hash
43
- expect(withWww).toBe(withoutWww);
44
- });
45
-
46
- it("should produce different product IDs for different domains", () => {
47
- const domain1ProductId = computeProductId({ domain: "domain1.com" });
48
- const domain2ProductId = computeProductId({ domain: "domain2.com" });
49
-
50
- // Different domains should produce different hashes
51
- expect(domain1ProductId).not.toBe(domain2ProductId);
52
- });
53
-
54
- it("should use window.location.host when domain is not provided", () => {
55
- // When no domain is provided, it should default to window.location.host
56
- const productId = computeProductId();
57
-
58
- // Should produce same result as using window.location.host explicitly
59
- const expectedId = keccak256(toHex(window.location.host));
60
- expect(productId).toBe(expectedId);
61
- });
62
-
63
- it("should handle subdomains correctly", () => {
64
- const subdomain = "blog.example.com";
65
- const productId = computeProductId({ domain: subdomain });
66
-
67
- // Should compute keccak256 hash of "blog.example.com" (subdomain preserved)
68
- const expectedId = keccak256(toHex(subdomain));
69
- expect(productId).toBe(expectedId);
70
- });
71
-
72
- it("should only remove www prefix, not other w-prefixed subdomains", () => {
73
- const domain = "web.example.com";
74
- const productId = computeProductId({ domain });
75
-
76
- // Should NOT remove "web." prefix, only "www."
77
- const expectedId = keccak256(toHex("web.example.com"));
78
- expect(productId).toBe(expectedId);
79
- });
80
- });