@frak-labs/core-sdk 0.0.6 → 0.0.8

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.
@@ -1,350 +0,0 @@
1
- 'use strict';
2
-
3
- var chunk7YDJDVXY_cjs = require('../chunk-7YDJDVXY.cjs');
4
- var chunkPHVGCFDX_cjs = require('../chunk-PHVGCFDX.cjs');
5
- var viem = require('viem');
6
- var siwe = require('viem/siwe');
7
-
8
- // src/actions/watchWalletStatus.ts
9
- function watchWalletStatus(client, callback) {
10
- if (!callback) {
11
- return client.request({ method: "frak_listenToWalletStatus" }).then((result) => {
12
- savePotentialToken(result.interactionToken);
13
- return result;
14
- });
15
- }
16
- const firstResult = new chunk7YDJDVXY_cjs.Deferred();
17
- let hasResolved = false;
18
- return client.listenerRequest(
19
- {
20
- method: "frak_listenToWalletStatus"
21
- },
22
- (status) => {
23
- callback(status);
24
- savePotentialToken(status.interactionToken);
25
- if (!hasResolved) {
26
- firstResult.resolve(status);
27
- hasResolved = true;
28
- }
29
- }
30
- ).then(() => firstResult.promise);
31
- }
32
- function savePotentialToken(interactionToken) {
33
- if (typeof window === "undefined") {
34
- return;
35
- }
36
- if (interactionToken) {
37
- window.sessionStorage.setItem(
38
- "frak-wallet-interaction-token",
39
- interactionToken
40
- );
41
- } else {
42
- window.sessionStorage.removeItem("frak.interaction-token");
43
- }
44
- }
45
- function computeProductId({ domain }) {
46
- const effectiveDomain = domain ?? window.location.host;
47
- return viem.keccak256(viem.toHex(effectiveDomain));
48
- }
49
-
50
- // src/actions/sendInteraction.ts
51
- async function sendInteraction(client, { productId, interaction, validation }) {
52
- const pId = productId ?? computeProductId(client.config);
53
- return await client.request({
54
- method: "frak_sendInteraction",
55
- params: [pId, interaction, validation]
56
- });
57
- }
58
-
59
- // src/actions/displayModal.ts
60
- async function displayModal(client, { steps, metadata }) {
61
- return await client.request({
62
- method: "frak_displayModal",
63
- params: [steps, client.config.metadata.name, metadata]
64
- });
65
- }
66
-
67
- // src/actions/displayEmbededWallet.ts
68
- async function displayEmbededWallet(client, params) {
69
- await client.request({
70
- method: "frak_displayEmbededWallet",
71
- params: [params, client.config.metadata.name]
72
- });
73
- }
74
-
75
- // src/actions/openSso.ts
76
- async function openSso(client, args) {
77
- const { metadata } = client.config;
78
- await client.request({
79
- method: "frak_sso",
80
- params: [args, metadata.name, metadata.css]
81
- });
82
- }
83
-
84
- // src/actions/getProductInformation.ts
85
- async function getProductInformation(client) {
86
- return await client.request({
87
- method: "frak_getProductInformation"
88
- });
89
- }
90
-
91
- // src/actions/trackPurchaseStatus.ts
92
- async function trackPurchaseStatus(args) {
93
- if (typeof window === "undefined") {
94
- console.warn("[Frak] No window found, can't track purchase");
95
- return;
96
- }
97
- const interactionToken = window.sessionStorage.getItem(
98
- "frak-wallet-interaction-token"
99
- );
100
- if (!interactionToken) {
101
- console.warn("[Frak] No frak session found, skipping purchase check");
102
- return;
103
- }
104
- await fetch("https://backend.frak.id/interactions/listenForPurchase", {
105
- method: "POST",
106
- headers: {
107
- Accept: "application/json",
108
- "Content-Type": "application/json",
109
- "x-wallet-sdk-auth": interactionToken
110
- },
111
- body: JSON.stringify(args)
112
- });
113
- }
114
- async function siweAuthenticate(client, { siwe: siwe$1, metadata }) {
115
- const effectiveDomain = client.config?.domain ?? window.location.host;
116
- const realStatement = siwe$1?.statement ?? `I confirm that I want to use my Frak wallet on: ${client.config.metadata.name}`;
117
- const builtSiwe = {
118
- ...siwe$1,
119
- statement: realStatement,
120
- nonce: siwe$1?.nonce ?? siwe.generateSiweNonce(),
121
- uri: siwe$1?.uri ?? `https://${effectiveDomain}`,
122
- version: siwe$1?.version ?? "1",
123
- domain: effectiveDomain
124
- };
125
- const result = await displayModal(client, {
126
- metadata,
127
- steps: {
128
- login: {},
129
- siweAuthenticate: {
130
- siwe: builtSiwe
131
- }
132
- }
133
- });
134
- return result.siweAuthenticate;
135
- }
136
-
137
- // src/actions/wrapper/sendTransaction.ts
138
- async function sendTransaction(client, { tx, metadata }) {
139
- const result = await displayModal(client, {
140
- metadata,
141
- steps: {
142
- login: {},
143
- sendTransaction: { tx }
144
- }
145
- });
146
- return result.sendTransaction;
147
- }
148
-
149
- // src/actions/wrapper/modalBuilder.ts
150
- function modalBuilder(client, {
151
- metadata,
152
- login,
153
- openSession
154
- }) {
155
- const baseParams = {
156
- steps: {
157
- login: login ?? {},
158
- openSession: openSession ?? {}
159
- },
160
- metadata
161
- };
162
- return modalStepsBuilder(client, baseParams);
163
- }
164
- function modalStepsBuilder(client, params) {
165
- function sendTx(options) {
166
- return modalStepsBuilder(client, {
167
- ...params,
168
- steps: {
169
- ...params.steps,
170
- sendTransaction: options
171
- }
172
- });
173
- }
174
- function reward(options) {
175
- return modalStepsBuilder(
176
- client,
177
- {
178
- ...params,
179
- steps: {
180
- ...params.steps,
181
- final: {
182
- ...options,
183
- action: { key: "reward" }
184
- }
185
- }
186
- }
187
- );
188
- }
189
- function sharing(sharingOptions, options) {
190
- return modalStepsBuilder(
191
- client,
192
- {
193
- ...params,
194
- steps: {
195
- ...params.steps,
196
- final: {
197
- ...options,
198
- action: { key: "sharing", options: sharingOptions }
199
- }
200
- }
201
- }
202
- );
203
- }
204
- async function display(metadataOverride) {
205
- if (metadataOverride) {
206
- params.metadata = metadataOverride(params.metadata ?? {});
207
- }
208
- return await displayModal(client, params);
209
- }
210
- return {
211
- // Access current modal params
212
- params,
213
- // Function to add new steps
214
- sendTx,
215
- reward,
216
- sharing,
217
- // Display the modal
218
- display
219
- };
220
- }
221
- async function processReferral(client, {
222
- walletStatus,
223
- frakContext,
224
- modalConfig,
225
- productId,
226
- options
227
- }) {
228
- let walletRequest = false;
229
- async function getFreshWalletStatus() {
230
- if (walletRequest) {
231
- return;
232
- }
233
- walletRequest = true;
234
- return ensureWalletConnected(client, {
235
- modalConfig,
236
- walletStatus
237
- });
238
- }
239
- async function pushReferralInteraction(referrer) {
240
- const interaction = chunkPHVGCFDX_cjs.ReferralInteractionEncoder.referred({
241
- referrer
242
- });
243
- await sendInteraction(client, { productId, interaction });
244
- }
245
- try {
246
- const { status, currentWallet } = await processReferralLogic({
247
- initialWalletStatus: walletStatus,
248
- getFreshWalletStatus,
249
- pushReferralInteraction,
250
- frakContext
251
- });
252
- chunk7YDJDVXY_cjs.FrakContextManager.replaceUrl({
253
- url: window.location?.href,
254
- context: options?.alwaysAppendUrl ? { r: currentWallet } : null
255
- });
256
- return status;
257
- } catch (error) {
258
- console.log("Error processing referral", { error });
259
- chunk7YDJDVXY_cjs.FrakContextManager.replaceUrl({
260
- url: window.location?.href,
261
- context: options?.alwaysAppendUrl ? { r: walletStatus?.wallet } : null
262
- });
263
- return mapErrorToState(error);
264
- }
265
- }
266
- async function processReferralLogic({
267
- initialWalletStatus,
268
- getFreshWalletStatus,
269
- pushReferralInteraction,
270
- frakContext
271
- }) {
272
- let currentWallet = initialWalletStatus?.wallet;
273
- if (!frakContext?.r) {
274
- return { status: "no-referrer", currentWallet };
275
- }
276
- if (!currentWallet) {
277
- currentWallet = await getFreshWalletStatus();
278
- }
279
- if (currentWallet && viem.isAddressEqual(frakContext.r, currentWallet)) {
280
- return { status: "self-referral", currentWallet };
281
- }
282
- if (!initialWalletStatus?.interactionSession) {
283
- currentWallet = await getFreshWalletStatus();
284
- }
285
- await pushReferralInteraction(frakContext.r);
286
- return { status: "success", currentWallet };
287
- }
288
- async function ensureWalletConnected(client, {
289
- modalConfig,
290
- walletStatus
291
- }) {
292
- if (!walletStatus?.interactionSession) {
293
- if (!modalConfig) {
294
- return undefined;
295
- }
296
- const result = await displayModal(client, modalConfig);
297
- return result?.login?.wallet ?? undefined;
298
- }
299
- return walletStatus.wallet ?? undefined;
300
- }
301
- function mapErrorToState(error) {
302
- if (error instanceof chunk7YDJDVXY_cjs.FrakRpcError) {
303
- switch (error.code) {
304
- case chunk7YDJDVXY_cjs.RpcErrorCodes.walletNotConnected:
305
- return "no-wallet";
306
- case chunk7YDJDVXY_cjs.RpcErrorCodes.serverErrorForInteractionDelegation:
307
- return "no-session";
308
- default:
309
- return "error";
310
- }
311
- }
312
- return "error";
313
- }
314
-
315
- // src/actions/referral/referralInteraction.ts
316
- async function referralInteraction(client, {
317
- productId,
318
- modalConfig,
319
- options
320
- } = {}) {
321
- const frakContext = chunk7YDJDVXY_cjs.FrakContextManager.parse({
322
- url: window.location.href
323
- });
324
- const currentWalletStatus = await watchWalletStatus(client);
325
- try {
326
- return await processReferral(client, {
327
- walletStatus: currentWalletStatus,
328
- frakContext,
329
- modalConfig,
330
- productId,
331
- options
332
- });
333
- } catch (error) {
334
- console.warn("Error processing referral", { error });
335
- }
336
- return;
337
- }
338
-
339
- exports.displayEmbededWallet = displayEmbededWallet;
340
- exports.displayModal = displayModal;
341
- exports.getProductInformation = getProductInformation;
342
- exports.modalBuilder = modalBuilder;
343
- exports.openSso = openSso;
344
- exports.processReferral = processReferral;
345
- exports.referralInteraction = referralInteraction;
346
- exports.sendInteraction = sendInteraction;
347
- exports.sendTransaction = sendTransaction;
348
- exports.siweAuthenticate = siweAuthenticate;
349
- exports.trackPurchaseStatus = trackPurchaseStatus;
350
- exports.watchWalletStatus = watchWalletStatus;