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