@frak-labs/core-sdk 0.0.19 → 0.1.0-beta.00226d62
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/cdn/bundle.iife.js +14 -0
- package/dist/actions-CEEObPYc.js +1 -0
- package/dist/actions-DbQhWYx8.cjs +1 -0
- package/dist/actions.cjs +1 -1
- package/dist/actions.d.cts +3 -1400
- package/dist/actions.d.ts +3 -1400
- package/dist/actions.js +1 -1
- package/dist/bundle.cjs +1 -13
- package/dist/bundle.d.cts +6 -2022
- package/dist/bundle.d.ts +6 -2022
- package/dist/bundle.js +1 -13
- package/dist/index-7OZ39x1U.d.ts +195 -0
- package/dist/index-C6FxkWPC.d.cts +511 -0
- package/dist/index-UFX7xCg3.d.ts +351 -0
- package/dist/index-d8xS4ryI.d.ts +511 -0
- package/dist/index-p4FqSp8z.d.cts +351 -0
- package/dist/index-zDq-VlKx.d.cts +195 -0
- package/dist/index.cjs +1 -13
- package/dist/index.d.cts +4 -1373
- package/dist/index.d.ts +4 -1373
- package/dist/index.js +1 -13
- package/dist/interaction-DMJ3ZfaF.d.cts +45 -0
- package/dist/interaction-KX1h9a7V.d.ts +45 -0
- package/dist/interactions-DnfM3oe0.js +1 -0
- package/dist/interactions-EIXhNLf6.cjs +1 -0
- package/dist/interactions.cjs +1 -1
- package/dist/interactions.d.cts +2 -182
- package/dist/interactions.d.ts +2 -182
- package/dist/interactions.js +1 -1
- package/dist/openSso-D--Airj6.d.cts +1018 -0
- package/dist/openSso-DsKJ4y0j.d.ts +1018 -0
- package/dist/productTypes-BUkXJKZ7.cjs +1 -0
- package/dist/productTypes-CGb1MmBF.js +1 -0
- package/dist/src-B_xO0AR6.cjs +13 -0
- package/dist/src-D2d52OZa.js +13 -0
- package/dist/trackEvent-CHnYa85W.js +1 -0
- package/dist/trackEvent-GuQm_1Nm.cjs +1 -0
- package/package.json +27 -18
- package/src/actions/displayEmbeddedWallet.test.ts +194 -0
- package/src/actions/displayEmbeddedWallet.ts +20 -0
- package/src/actions/displayModal.test.ts +387 -0
- package/src/actions/displayModal.ts +131 -0
- package/src/actions/getProductInformation.test.ts +133 -0
- package/src/actions/getProductInformation.ts +14 -0
- package/src/actions/index.ts +29 -0
- package/src/actions/openSso.test.ts +407 -0
- package/src/actions/openSso.ts +116 -0
- package/src/actions/prepareSso.test.ts +223 -0
- package/src/actions/prepareSso.ts +48 -0
- package/src/actions/referral/processReferral.test.ts +357 -0
- package/src/actions/referral/processReferral.ts +230 -0
- package/src/actions/referral/referralInteraction.test.ts +153 -0
- package/src/actions/referral/referralInteraction.ts +57 -0
- package/src/actions/sendInteraction.test.ts +219 -0
- package/src/actions/sendInteraction.ts +32 -0
- package/src/actions/trackPurchaseStatus.test.ts +287 -0
- package/src/actions/trackPurchaseStatus.ts +53 -0
- package/src/actions/watchWalletStatus.test.ts +372 -0
- package/src/actions/watchWalletStatus.ts +94 -0
- package/src/actions/wrapper/modalBuilder.test.ts +253 -0
- package/src/actions/wrapper/modalBuilder.ts +212 -0
- package/src/actions/wrapper/sendTransaction.test.ts +164 -0
- package/src/actions/wrapper/sendTransaction.ts +62 -0
- package/src/actions/wrapper/siweAuthenticate.test.ts +290 -0
- package/src/actions/wrapper/siweAuthenticate.ts +94 -0
- package/src/bundle.ts +3 -0
- package/src/clients/DebugInfo.test.ts +418 -0
- package/src/clients/DebugInfo.ts +182 -0
- package/src/clients/createIFrameFrakClient.ts +287 -0
- package/src/clients/index.ts +3 -0
- package/src/clients/setupClient.test.ts +343 -0
- package/src/clients/setupClient.ts +73 -0
- package/src/clients/transports/iframeLifecycleManager.test.ts +399 -0
- package/src/clients/transports/iframeLifecycleManager.ts +90 -0
- package/src/constants/interactionTypes.test.ts +128 -0
- package/src/constants/interactionTypes.ts +44 -0
- package/src/constants/locales.ts +14 -0
- package/src/constants/productTypes.test.ts +130 -0
- package/src/constants/productTypes.ts +33 -0
- package/src/index.ts +101 -0
- package/src/interactions/index.ts +5 -0
- package/src/interactions/pressEncoder.test.ts +215 -0
- package/src/interactions/pressEncoder.ts +53 -0
- package/src/interactions/purchaseEncoder.test.ts +291 -0
- package/src/interactions/purchaseEncoder.ts +99 -0
- package/src/interactions/referralEncoder.test.ts +170 -0
- package/src/interactions/referralEncoder.ts +47 -0
- package/src/interactions/retailEncoder.test.ts +107 -0
- package/src/interactions/retailEncoder.ts +37 -0
- package/src/interactions/webshopEncoder.test.ts +56 -0
- package/src/interactions/webshopEncoder.ts +30 -0
- package/src/types/client.ts +14 -0
- package/src/types/compression.ts +22 -0
- package/src/types/config.ts +111 -0
- package/src/types/context.ts +13 -0
- package/src/types/index.ts +71 -0
- package/src/types/lifecycle/client.ts +46 -0
- package/src/types/lifecycle/iframe.ts +35 -0
- package/src/types/lifecycle/index.ts +2 -0
- package/src/types/rpc/displayModal.ts +84 -0
- package/src/types/rpc/embedded/index.ts +68 -0
- package/src/types/rpc/embedded/loggedIn.ts +55 -0
- package/src/types/rpc/embedded/loggedOut.ts +28 -0
- package/src/types/rpc/interaction.ts +43 -0
- package/src/types/rpc/modal/final.ts +46 -0
- package/src/types/rpc/modal/generic.ts +46 -0
- package/src/types/rpc/modal/index.ts +20 -0
- package/src/types/rpc/modal/login.ts +32 -0
- package/src/types/rpc/modal/openSession.ts +25 -0
- package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
- package/src/types/rpc/modal/transaction.ts +33 -0
- package/src/types/rpc/productInformation.ts +59 -0
- package/src/types/rpc/sso.ts +80 -0
- package/src/types/rpc/walletStatus.ts +35 -0
- package/src/types/rpc.ts +158 -0
- package/src/types/transport.ts +34 -0
- package/src/utils/FrakContext.test.ts +407 -0
- package/src/utils/FrakContext.ts +158 -0
- package/src/utils/compression/b64.test.ts +181 -0
- package/src/utils/compression/b64.ts +29 -0
- package/src/utils/compression/compress.test.ts +123 -0
- package/src/utils/compression/compress.ts +11 -0
- package/src/utils/compression/decompress.test.ts +145 -0
- package/src/utils/compression/decompress.ts +11 -0
- package/src/utils/compression/index.ts +3 -0
- package/src/utils/computeProductId.test.ts +80 -0
- package/src/utils/computeProductId.ts +11 -0
- package/src/utils/constants.test.ts +23 -0
- package/src/utils/constants.ts +4 -0
- package/src/utils/formatAmount.test.ts +113 -0
- package/src/utils/formatAmount.ts +18 -0
- package/src/utils/getCurrencyAmountKey.test.ts +44 -0
- package/src/utils/getCurrencyAmountKey.ts +15 -0
- package/src/utils/getSupportedCurrency.test.ts +51 -0
- package/src/utils/getSupportedCurrency.ts +14 -0
- package/src/utils/getSupportedLocale.test.ts +64 -0
- package/src/utils/getSupportedLocale.ts +16 -0
- package/src/utils/iframeHelper.test.ts +450 -0
- package/src/utils/iframeHelper.ts +143 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/sso.test.ts +361 -0
- package/src/utils/sso.ts +119 -0
- package/src/utils/ssoUrlListener.test.ts +252 -0
- package/src/utils/ssoUrlListener.ts +60 -0
- package/src/utils/trackEvent.test.ts +162 -0
- package/src/utils/trackEvent.ts +26 -0
- package/cdn/bundle.js +0 -19
- package/cdn/bundle.js.LICENSE.txt +0 -10
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { beforeEach, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("../displayModal", () => ({
|
|
4
|
+
displayModal: vi.fn(),
|
|
5
|
+
}));
|
|
6
|
+
|
|
7
|
+
vi.mock("viem/siwe", () => ({
|
|
8
|
+
generateSiweNonce: vi.fn(() => "mock-nonce-123456"),
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
import type { Address, Hex } from "viem";
|
|
12
|
+
import { describe, expect, it } from "../../../tests/vitest-fixtures";
|
|
13
|
+
import type { FrakClient } from "../../types";
|
|
14
|
+
import { siweAuthenticate } from "./siweAuthenticate";
|
|
15
|
+
|
|
16
|
+
describe("siweAuthenticate", () => {
|
|
17
|
+
const mockClient = {
|
|
18
|
+
config: {
|
|
19
|
+
domain: "example.com",
|
|
20
|
+
metadata: {
|
|
21
|
+
name: "Test App",
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
request: vi.fn(),
|
|
25
|
+
} as unknown as FrakClient;
|
|
26
|
+
|
|
27
|
+
const mockClientWithoutDomain = {
|
|
28
|
+
config: {
|
|
29
|
+
metadata: {
|
|
30
|
+
name: "Test App",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
request: vi.fn(),
|
|
34
|
+
} as unknown as FrakClient;
|
|
35
|
+
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
vi.stubGlobal("window", {
|
|
38
|
+
location: {
|
|
39
|
+
host: "window.example.com",
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("basic usage", () => {
|
|
45
|
+
it("should call displayModal with SIWE params", async () => {
|
|
46
|
+
const { displayModal } = await import("../displayModal");
|
|
47
|
+
|
|
48
|
+
const mockResponse = {
|
|
49
|
+
login: {
|
|
50
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
51
|
+
},
|
|
52
|
+
siweAuthenticate: {
|
|
53
|
+
message: "Sign in to Test App",
|
|
54
|
+
signature: "0xsignature" as Hex,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
vi.mocked(displayModal).mockResolvedValue(mockResponse as any);
|
|
58
|
+
|
|
59
|
+
await siweAuthenticate(mockClient, {});
|
|
60
|
+
|
|
61
|
+
expect(displayModal).toHaveBeenCalledWith(mockClient, {
|
|
62
|
+
metadata: undefined,
|
|
63
|
+
steps: {
|
|
64
|
+
login: {},
|
|
65
|
+
siweAuthenticate: {
|
|
66
|
+
siwe: expect.objectContaining({
|
|
67
|
+
domain: "example.com",
|
|
68
|
+
nonce: "mock-nonce-123456",
|
|
69
|
+
uri: "https://example.com",
|
|
70
|
+
version: "1",
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should return SIWE authentication result", async () => {
|
|
78
|
+
const { displayModal } = await import("../displayModal");
|
|
79
|
+
|
|
80
|
+
const mockSiweResult = {
|
|
81
|
+
message: "I confirm that I want to use my Frak wallet",
|
|
82
|
+
signature:
|
|
83
|
+
"0xsig1234567890123456789012345678901234567890123456789012345678901234" as Hex,
|
|
84
|
+
};
|
|
85
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
86
|
+
login: {},
|
|
87
|
+
siweAuthenticate: mockSiweResult,
|
|
88
|
+
} as any);
|
|
89
|
+
|
|
90
|
+
const result = await siweAuthenticate(mockClient, {});
|
|
91
|
+
|
|
92
|
+
expect(result).toEqual(mockSiweResult);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe("SIWE parameter handling", () => {
|
|
97
|
+
it("should use default statement when not provided", async () => {
|
|
98
|
+
const { displayModal } = await import("../displayModal");
|
|
99
|
+
|
|
100
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
101
|
+
login: {},
|
|
102
|
+
siweAuthenticate: { message: "", signature: "0x" as Hex },
|
|
103
|
+
} as any);
|
|
104
|
+
|
|
105
|
+
await siweAuthenticate(mockClient, {});
|
|
106
|
+
|
|
107
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
108
|
+
mockClient,
|
|
109
|
+
expect.objectContaining({
|
|
110
|
+
steps: expect.objectContaining({
|
|
111
|
+
siweAuthenticate: expect.objectContaining({
|
|
112
|
+
siwe: expect.objectContaining({
|
|
113
|
+
statement:
|
|
114
|
+
"I confirm that I want to use my Frak wallet on: Test App",
|
|
115
|
+
}),
|
|
116
|
+
}),
|
|
117
|
+
}),
|
|
118
|
+
})
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("should use custom statement when provided", async () => {
|
|
123
|
+
const { displayModal } = await import("../displayModal");
|
|
124
|
+
|
|
125
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
126
|
+
login: {},
|
|
127
|
+
siweAuthenticate: { message: "", signature: "0x" as Hex },
|
|
128
|
+
} as any);
|
|
129
|
+
|
|
130
|
+
await siweAuthenticate(mockClient, {
|
|
131
|
+
siwe: { statement: "Custom sign in message" },
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
135
|
+
mockClient,
|
|
136
|
+
expect.objectContaining({
|
|
137
|
+
steps: expect.objectContaining({
|
|
138
|
+
siweAuthenticate: expect.objectContaining({
|
|
139
|
+
siwe: expect.objectContaining({
|
|
140
|
+
statement: "Custom sign in message",
|
|
141
|
+
}),
|
|
142
|
+
}),
|
|
143
|
+
}),
|
|
144
|
+
})
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("should use custom nonce when provided", async () => {
|
|
149
|
+
const { displayModal } = await import("../displayModal");
|
|
150
|
+
|
|
151
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
152
|
+
login: {},
|
|
153
|
+
siweAuthenticate: { message: "", signature: "0x" as Hex },
|
|
154
|
+
} as any);
|
|
155
|
+
|
|
156
|
+
await siweAuthenticate(mockClient, {
|
|
157
|
+
siwe: { nonce: "custom-nonce" },
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
161
|
+
mockClient,
|
|
162
|
+
expect.objectContaining({
|
|
163
|
+
steps: expect.objectContaining({
|
|
164
|
+
siweAuthenticate: expect.objectContaining({
|
|
165
|
+
siwe: expect.objectContaining({
|
|
166
|
+
nonce: "custom-nonce",
|
|
167
|
+
}),
|
|
168
|
+
}),
|
|
169
|
+
}),
|
|
170
|
+
})
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("should use window.location.host when domain not in config", async () => {
|
|
175
|
+
const { displayModal } = await import("../displayModal");
|
|
176
|
+
|
|
177
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
178
|
+
login: {},
|
|
179
|
+
siweAuthenticate: { message: "", signature: "0x" as Hex },
|
|
180
|
+
} as any);
|
|
181
|
+
|
|
182
|
+
await siweAuthenticate(mockClientWithoutDomain, {});
|
|
183
|
+
|
|
184
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
185
|
+
mockClientWithoutDomain,
|
|
186
|
+
expect.objectContaining({
|
|
187
|
+
steps: expect.objectContaining({
|
|
188
|
+
siweAuthenticate: expect.objectContaining({
|
|
189
|
+
siwe: expect.objectContaining({
|
|
190
|
+
domain: "window.example.com",
|
|
191
|
+
uri: "https://window.example.com",
|
|
192
|
+
}),
|
|
193
|
+
}),
|
|
194
|
+
}),
|
|
195
|
+
})
|
|
196
|
+
);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should use custom uri when provided", async () => {
|
|
200
|
+
const { displayModal } = await import("../displayModal");
|
|
201
|
+
|
|
202
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
203
|
+
login: {},
|
|
204
|
+
siweAuthenticate: { message: "", signature: "0x" as Hex },
|
|
205
|
+
} as any);
|
|
206
|
+
|
|
207
|
+
await siweAuthenticate(mockClient, {
|
|
208
|
+
siwe: { uri: "https://custom-uri.com" },
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
212
|
+
mockClient,
|
|
213
|
+
expect.objectContaining({
|
|
214
|
+
steps: expect.objectContaining({
|
|
215
|
+
siweAuthenticate: expect.objectContaining({
|
|
216
|
+
siwe: expect.objectContaining({
|
|
217
|
+
uri: "https://custom-uri.com",
|
|
218
|
+
}),
|
|
219
|
+
}),
|
|
220
|
+
}),
|
|
221
|
+
})
|
|
222
|
+
);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("should use custom version when provided", async () => {
|
|
226
|
+
const { displayModal } = await import("../displayModal");
|
|
227
|
+
|
|
228
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
229
|
+
login: {},
|
|
230
|
+
siweAuthenticate: { message: "", signature: "0x" as Hex },
|
|
231
|
+
} as any);
|
|
232
|
+
|
|
233
|
+
await siweAuthenticate(mockClient, {
|
|
234
|
+
siwe: { version: "2" as any },
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
238
|
+
mockClient,
|
|
239
|
+
expect.objectContaining({
|
|
240
|
+
steps: expect.objectContaining({
|
|
241
|
+
siweAuthenticate: expect.objectContaining({
|
|
242
|
+
siwe: expect.objectContaining({
|
|
243
|
+
version: "2",
|
|
244
|
+
}),
|
|
245
|
+
}),
|
|
246
|
+
}),
|
|
247
|
+
})
|
|
248
|
+
);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe("with metadata", () => {
|
|
253
|
+
it("should pass metadata to displayModal", async () => {
|
|
254
|
+
const { displayModal } = await import("../displayModal");
|
|
255
|
+
|
|
256
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
257
|
+
login: {},
|
|
258
|
+
siweAuthenticate: { message: "", signature: "0x" as Hex },
|
|
259
|
+
} as any);
|
|
260
|
+
|
|
261
|
+
const metadata = {
|
|
262
|
+
header: {
|
|
263
|
+
title: "Sign In",
|
|
264
|
+
},
|
|
265
|
+
context: "Sign in to access your account",
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
await siweAuthenticate(mockClient, { metadata });
|
|
269
|
+
|
|
270
|
+
expect(displayModal).toHaveBeenCalledWith(mockClient, {
|
|
271
|
+
metadata,
|
|
272
|
+
steps: expect.any(Object),
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
describe("error handling", () => {
|
|
278
|
+
it("should propagate errors from displayModal", async () => {
|
|
279
|
+
const { displayModal } = await import("../displayModal");
|
|
280
|
+
|
|
281
|
+
vi.mocked(displayModal).mockRejectedValue(
|
|
282
|
+
new Error("SIWE authentication rejected")
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
await expect(siweAuthenticate(mockClient, {})).rejects.toThrow(
|
|
286
|
+
"SIWE authentication rejected"
|
|
287
|
+
);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { generateSiweNonce } from "viem/siwe";
|
|
2
|
+
import type {
|
|
3
|
+
FrakClient,
|
|
4
|
+
ModalRpcMetadata,
|
|
5
|
+
SiweAuthenticateReturnType,
|
|
6
|
+
SiweAuthenticationParams,
|
|
7
|
+
} from "../../types";
|
|
8
|
+
import { displayModal } from "../displayModal";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Parameter used to directly show a modal used to authenticate with SIWE
|
|
12
|
+
* @inline
|
|
13
|
+
*/
|
|
14
|
+
export type SiweAuthenticateModalParams = {
|
|
15
|
+
/**
|
|
16
|
+
* Partial SIWE params, since we can rebuild them from the SDK if they are empty
|
|
17
|
+
*
|
|
18
|
+
* If no parameters provider, some fields will be recomputed from the current configuration and environment.
|
|
19
|
+
* - `statement` will be set to a default value
|
|
20
|
+
* - `nonce` will be generated
|
|
21
|
+
* - `uri` will be set to the current domain
|
|
22
|
+
* - `version` will be set to "1"
|
|
23
|
+
* - `domain` will be set to the current window domain
|
|
24
|
+
*
|
|
25
|
+
* @default {}
|
|
26
|
+
*/
|
|
27
|
+
siwe?: Partial<SiweAuthenticationParams>;
|
|
28
|
+
/**
|
|
29
|
+
* Custom metadata to be passed to the modal
|
|
30
|
+
*/
|
|
31
|
+
metadata?: ModalRpcMetadata;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Function used to launch a siwe authentication
|
|
36
|
+
* @param client - The current Frak Client
|
|
37
|
+
* @param args - The parameters
|
|
38
|
+
* @returns The SIWE authentication result (message + signature) in a promise
|
|
39
|
+
*
|
|
40
|
+
* @description This function will display a modal to the user with the provided SIWE parameters and metadata.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* import { siweAuthenticate } from "@frak-labs/core-sdk/actions";
|
|
44
|
+
* import { parseSiweMessage } from "viem/siwe";
|
|
45
|
+
*
|
|
46
|
+
* const { signature, message } = await siweAuthenticate(frakConfig, {
|
|
47
|
+
* siwe: {
|
|
48
|
+
* statement: "Sign in to My App",
|
|
49
|
+
* domain: "my-app.com",
|
|
50
|
+
* expirationTimeTimestamp: Date.now() + 1000 * 60 * 5,
|
|
51
|
+
* },
|
|
52
|
+
* metadata: {
|
|
53
|
+
* header: {
|
|
54
|
+
* title: "Sign in",
|
|
55
|
+
* },
|
|
56
|
+
* context: "Sign in to My App",
|
|
57
|
+
* },
|
|
58
|
+
* });
|
|
59
|
+
* console.log("Parsed final message:", parseSiweMessage(message));
|
|
60
|
+
* console.log("Siwe signature:", signature);
|
|
61
|
+
*/
|
|
62
|
+
export async function siweAuthenticate(
|
|
63
|
+
client: FrakClient,
|
|
64
|
+
{ siwe, metadata }: SiweAuthenticateModalParams
|
|
65
|
+
): Promise<SiweAuthenticateReturnType> {
|
|
66
|
+
const effectiveDomain = client.config?.domain ?? window.location.host;
|
|
67
|
+
const realStatement =
|
|
68
|
+
siwe?.statement ??
|
|
69
|
+
`I confirm that I want to use my Frak wallet on: ${client.config.metadata.name}`;
|
|
70
|
+
|
|
71
|
+
// Fill up the siwe request params
|
|
72
|
+
const builtSiwe: SiweAuthenticationParams = {
|
|
73
|
+
...siwe,
|
|
74
|
+
statement: realStatement,
|
|
75
|
+
nonce: siwe?.nonce ?? generateSiweNonce(),
|
|
76
|
+
uri: siwe?.uri ?? `https://${effectiveDomain}`,
|
|
77
|
+
version: siwe?.version ?? "1",
|
|
78
|
+
domain: effectiveDomain,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// Trigger a modal with login options
|
|
82
|
+
const result = await displayModal(client, {
|
|
83
|
+
metadata,
|
|
84
|
+
steps: {
|
|
85
|
+
login: {},
|
|
86
|
+
siweAuthenticate: {
|
|
87
|
+
siwe: builtSiwe,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// Return the SIWE result only
|
|
93
|
+
return result.siweAuthenticate;
|
|
94
|
+
}
|
package/src/bundle.ts
ADDED