@frak-labs/nexus-sdk 0.0.14 → 0.0.16
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/README.md +4 -4
- package/dist/FrakContext-LbQht1ep.d.cts +10 -0
- package/dist/FrakContext-LbQht1ep.d.ts +10 -0
- package/dist/bundle/bundle.js +21 -0
- package/dist/chunk-A3XYA7S7.cjs +114 -0
- package/dist/{chunk-5LZQXBFJ.js → chunk-AHUMDUJC.js} +12 -196
- package/dist/chunk-D77BTQYS.js +210 -0
- package/dist/chunk-DAGCBEBQ.cjs +243 -0
- package/dist/chunk-FO2GFD5C.cjs +210 -0
- package/dist/chunk-FYMOOHMT.js +254 -0
- package/dist/chunk-HY6YATLS.js +53 -0
- package/dist/chunk-J655X6YR.cjs +254 -0
- package/dist/chunk-TIKEZMSD.js +114 -0
- package/dist/chunk-UFJ7W6CQ.cjs +53 -0
- package/dist/{client-DJd7-ajw.d.ts → client-ALy_TEwJ.d.ts} +34 -11
- package/dist/{client-DXITs1Kf.d.cts → client-DaEox5q4.d.cts} +34 -11
- package/dist/core/actions/index.cjs +11 -3
- package/dist/core/actions/index.d.cts +28 -5
- package/dist/core/actions/index.d.ts +28 -5
- package/dist/core/actions/index.js +10 -2
- package/dist/core/index.cjs +15 -4
- package/dist/core/index.d.cts +98 -4
- package/dist/core/index.d.ts +98 -4
- package/dist/core/index.js +15 -4
- package/dist/core/interactions/index.cjs +3 -4
- package/dist/core/interactions/index.d.cts +27 -15
- package/dist/core/interactions/index.d.ts +27 -15
- package/dist/core/interactions/index.js +8 -9
- package/dist/{error-C4Zm5nQe.d.cts → error-Dflr3G5x.d.cts} +1 -1
- package/dist/{error-C4Zm5nQe.d.ts → error-Dflr3G5x.d.ts} +1 -1
- package/dist/react/index.cjs +79 -155
- package/dist/react/index.d.cts +4 -5
- package/dist/react/index.d.ts +4 -5
- package/dist/react/index.js +78 -154
- package/dist/{sendTransaction-fLvpfqaQ.d.ts → sendTransaction-BOd-pvXr.d.ts} +2 -2
- package/dist/{sendTransaction-C19oCc6X.d.cts → sendTransaction-MhdJPTWd.d.cts} +2 -2
- package/package.json +6 -6
- package/dist/chunk-22T2NHQK.js +0 -22
- package/dist/chunk-5SGDBU5S.cjs +0 -427
- package/dist/chunk-AANA3LEO.cjs +0 -22
- package/dist/chunk-ETV4XYOV.cjs +0 -7
- package/dist/chunk-IH3QWPWT.js +0 -72
- package/dist/chunk-PKBMQBKP.js +0 -7
- package/dist/chunk-PURWUKEM.cjs +0 -72
- package/dist/chunk-S223FMEJ.js +0 -86
- package/dist/chunk-UOEVM7TR.cjs +0 -86
- package/dist/index.cjs +0 -1
- package/dist/index.d.cts +0 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Deferred,
|
|
3
|
+
FrakContextManager,
|
|
4
|
+
FrakRpcError,
|
|
5
|
+
RpcErrorCodes
|
|
6
|
+
} from "./chunk-FYMOOHMT.js";
|
|
7
|
+
import {
|
|
8
|
+
ReferralInteractionEncoder
|
|
9
|
+
} from "./chunk-TIKEZMSD.js";
|
|
10
|
+
|
|
11
|
+
// src/core/actions/watchWalletStatus.ts
|
|
12
|
+
function watchWalletStatus(client, callback) {
|
|
13
|
+
return client.listenerRequest(
|
|
14
|
+
{
|
|
15
|
+
method: "frak_listenToWalletStatus"
|
|
16
|
+
},
|
|
17
|
+
callback
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/core/utils/computeProductId.ts
|
|
22
|
+
import { keccak256, toHex } from "viem";
|
|
23
|
+
function computeProductId({ domain }) {
|
|
24
|
+
return keccak256(toHex(domain));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/core/actions/sendInteraction.ts
|
|
28
|
+
async function sendInteraction(client, { productId, interaction, validation }) {
|
|
29
|
+
const pId = productId ?? computeProductId(client.config);
|
|
30
|
+
return await client.request({
|
|
31
|
+
method: "frak_sendInteraction",
|
|
32
|
+
params: [pId, interaction, validation]
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/core/actions/displayModal.ts
|
|
37
|
+
async function displayModal(client, { steps, metadata }) {
|
|
38
|
+
return await client.request({
|
|
39
|
+
method: "frak_displayModal",
|
|
40
|
+
params: [steps, client.config.metadata.name, metadata]
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/core/actions/openSso.ts
|
|
45
|
+
async function openSso(client, args) {
|
|
46
|
+
const { metadata } = client.config;
|
|
47
|
+
await client.request({
|
|
48
|
+
method: "frak_sso",
|
|
49
|
+
params: [args, metadata.name, metadata.css]
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/core/actions/wrapper/siweAuthenticate.ts
|
|
54
|
+
import { generateSiweNonce } from "viem/siwe";
|
|
55
|
+
async function siweAuthenticate(client, { siwe, metadata }) {
|
|
56
|
+
const realStatement = siwe?.statement ?? `I confirm that I want to use my Frak wallet on: ${client.config.metadata.name}`;
|
|
57
|
+
const builtSiwe = {
|
|
58
|
+
...siwe,
|
|
59
|
+
statement: realStatement,
|
|
60
|
+
nonce: siwe?.nonce ?? generateSiweNonce(),
|
|
61
|
+
uri: siwe?.uri ?? `https://${client.config.domain}`,
|
|
62
|
+
version: siwe?.version ?? "1",
|
|
63
|
+
domain: client.config.domain
|
|
64
|
+
};
|
|
65
|
+
const result = await displayModal(client, {
|
|
66
|
+
metadata,
|
|
67
|
+
steps: {
|
|
68
|
+
login: {},
|
|
69
|
+
siweAuthenticate: {
|
|
70
|
+
siwe: builtSiwe
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return result.siweAuthenticate;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/core/actions/wrapper/sendTransaction.ts
|
|
78
|
+
async function sendTransaction(client, { tx, metadata }) {
|
|
79
|
+
const result = await displayModal(client, {
|
|
80
|
+
metadata,
|
|
81
|
+
steps: {
|
|
82
|
+
login: {},
|
|
83
|
+
sendTransaction: { tx }
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
return result.sendTransaction;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/core/actions/wrapper/walletStatus.ts
|
|
90
|
+
async function walletStatus(client, callback) {
|
|
91
|
+
const firstResult = new Deferred();
|
|
92
|
+
let hasResolved = false;
|
|
93
|
+
await watchWalletStatus(client, (status) => {
|
|
94
|
+
callback?.(status);
|
|
95
|
+
if (!hasResolved) {
|
|
96
|
+
firstResult.resolve(status);
|
|
97
|
+
hasResolved = true;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return firstResult.promise;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/core/actions/referral/processReferral.ts
|
|
104
|
+
import { isAddressEqual } from "viem";
|
|
105
|
+
async function processReferral(client, {
|
|
106
|
+
walletStatus: walletStatus2,
|
|
107
|
+
frakContext,
|
|
108
|
+
modalConfig,
|
|
109
|
+
productId
|
|
110
|
+
}) {
|
|
111
|
+
try {
|
|
112
|
+
let currentWallet = walletStatus2?.wallet;
|
|
113
|
+
if (!frakContext?.r) {
|
|
114
|
+
if (currentWallet) {
|
|
115
|
+
await FrakContextManager.replaceUrl({
|
|
116
|
+
url: window.location?.href,
|
|
117
|
+
context: { r: currentWallet }
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return "no-referrer";
|
|
121
|
+
}
|
|
122
|
+
if (!currentWallet) {
|
|
123
|
+
currentWallet = await ensureWalletConnected(client, {
|
|
124
|
+
modalConfig,
|
|
125
|
+
walletStatus: walletStatus2
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (currentWallet && isAddressEqual(frakContext.r, currentWallet)) {
|
|
129
|
+
return "self-referral";
|
|
130
|
+
}
|
|
131
|
+
if (!walletStatus2?.interactionSession) {
|
|
132
|
+
currentWallet = await ensureWalletConnected(client, {
|
|
133
|
+
modalConfig,
|
|
134
|
+
walletStatus: walletStatus2
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (currentWallet) {
|
|
138
|
+
await FrakContextManager.replaceUrl({
|
|
139
|
+
url: window.location?.href,
|
|
140
|
+
context: { r: currentWallet }
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
const interaction = ReferralInteractionEncoder.referred({
|
|
144
|
+
referrer: frakContext.r
|
|
145
|
+
});
|
|
146
|
+
await sendInteraction(client, { productId, interaction });
|
|
147
|
+
return "success";
|
|
148
|
+
} catch (error) {
|
|
149
|
+
return mapErrorToState(error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async function ensureWalletConnected(client, {
|
|
153
|
+
modalConfig,
|
|
154
|
+
walletStatus: walletStatus2
|
|
155
|
+
}) {
|
|
156
|
+
if (!walletStatus2?.interactionSession) {
|
|
157
|
+
if (!modalConfig) {
|
|
158
|
+
return void 0;
|
|
159
|
+
}
|
|
160
|
+
const result = await displayModal(client, modalConfig);
|
|
161
|
+
return result?.login?.wallet ?? void 0;
|
|
162
|
+
}
|
|
163
|
+
return walletStatus2.wallet ?? void 0;
|
|
164
|
+
}
|
|
165
|
+
function mapErrorToState(error) {
|
|
166
|
+
if (error instanceof FrakRpcError) {
|
|
167
|
+
switch (error.code) {
|
|
168
|
+
case RpcErrorCodes.walletNotConnected:
|
|
169
|
+
return "no-wallet";
|
|
170
|
+
case RpcErrorCodes.serverErrorForInteractionDelegation:
|
|
171
|
+
return "no-session";
|
|
172
|
+
default:
|
|
173
|
+
return "error";
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return "error";
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/core/actions/referral/referralInteraction.ts
|
|
180
|
+
async function referralInteraction(client, {
|
|
181
|
+
productId,
|
|
182
|
+
modalConfig
|
|
183
|
+
} = {}) {
|
|
184
|
+
const frakContext = await FrakContextManager.parse({
|
|
185
|
+
url: window.location.href
|
|
186
|
+
});
|
|
187
|
+
const currentWalletStatus = await walletStatus(client);
|
|
188
|
+
try {
|
|
189
|
+
return await processReferral(client, {
|
|
190
|
+
walletStatus: currentWalletStatus,
|
|
191
|
+
frakContext,
|
|
192
|
+
modalConfig,
|
|
193
|
+
productId
|
|
194
|
+
});
|
|
195
|
+
} catch (error) {
|
|
196
|
+
return error;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export {
|
|
201
|
+
watchWalletStatus,
|
|
202
|
+
sendInteraction,
|
|
203
|
+
displayModal,
|
|
204
|
+
openSso,
|
|
205
|
+
siweAuthenticate,
|
|
206
|
+
sendTransaction,
|
|
207
|
+
walletStatus,
|
|
208
|
+
processReferral,
|
|
209
|
+
referralInteraction
|
|
210
|
+
};
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
var _chunkJ655X6YRcjs = require('./chunk-J655X6YR.cjs');
|
|
10
|
+
|
|
11
|
+
// src/core/utils/constants.ts
|
|
12
|
+
var BACKUP_KEY = "nexus-wallet-backup";
|
|
13
|
+
|
|
14
|
+
// src/core/clients/transports/iframeChannelManager.ts
|
|
15
|
+
function createIFrameChannelManager() {
|
|
16
|
+
const channels = /* @__PURE__ */ new Map();
|
|
17
|
+
return {
|
|
18
|
+
// TODO: Better id system?? uid stuff?
|
|
19
|
+
createChannel: (resolver) => {
|
|
20
|
+
const id = Math.random().toString(36).substring(7);
|
|
21
|
+
channels.set(id, resolver);
|
|
22
|
+
return id;
|
|
23
|
+
},
|
|
24
|
+
getRpcResolver: (id) => channels.get(id),
|
|
25
|
+
removeChannel: (id) => channels.delete(id),
|
|
26
|
+
destroy: () => channels.clear()
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/core/clients/transports/iframeLifecycleManager.ts
|
|
31
|
+
function createIFrameLifecycleManager({
|
|
32
|
+
iframe
|
|
33
|
+
}) {
|
|
34
|
+
const isConnectedDeferred = new (0, _chunkJ655X6YRcjs.Deferred)();
|
|
35
|
+
const handler = async (messageEvent) => {
|
|
36
|
+
switch (messageEvent.iframeLifecycle) {
|
|
37
|
+
// Resolve the isConnected promise
|
|
38
|
+
case "connected":
|
|
39
|
+
isConnectedDeferred.resolve(true);
|
|
40
|
+
break;
|
|
41
|
+
// Perform a nexus backup
|
|
42
|
+
case "do-backup":
|
|
43
|
+
if (messageEvent.data.backup) {
|
|
44
|
+
localStorage.setItem(BACKUP_KEY, messageEvent.data.backup);
|
|
45
|
+
} else {
|
|
46
|
+
localStorage.removeItem(BACKUP_KEY);
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
// Remove nexus backup
|
|
50
|
+
case "remove-backup":
|
|
51
|
+
localStorage.removeItem(BACKUP_KEY);
|
|
52
|
+
break;
|
|
53
|
+
// Change iframe visibility
|
|
54
|
+
case "show":
|
|
55
|
+
case "hide":
|
|
56
|
+
_chunkJ655X6YRcjs.changeIframeVisibility.call(void 0, {
|
|
57
|
+
iframe,
|
|
58
|
+
isVisible: messageEvent.iframeLifecycle === "show"
|
|
59
|
+
});
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
handleEvent: handler,
|
|
65
|
+
isConnected: isConnectedDeferred.promise
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/core/clients/transports/iframeMessageHandler.ts
|
|
70
|
+
function createIFrameMessageHandler({
|
|
71
|
+
nexusWalletUrl,
|
|
72
|
+
iframe,
|
|
73
|
+
channelManager,
|
|
74
|
+
iframeLifecycleManager
|
|
75
|
+
}) {
|
|
76
|
+
if (typeof window === "undefined") {
|
|
77
|
+
throw new (0, _chunkJ655X6YRcjs.FrakRpcError)(
|
|
78
|
+
_chunkJ655X6YRcjs.RpcErrorCodes.configError,
|
|
79
|
+
"iframe client should be used in the browser"
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (!iframe.contentWindow) {
|
|
83
|
+
throw new (0, _chunkJ655X6YRcjs.FrakRpcError)(
|
|
84
|
+
_chunkJ655X6YRcjs.RpcErrorCodes.configError,
|
|
85
|
+
"The iframe does not have a product window"
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
const contentWindow = iframe.contentWindow;
|
|
89
|
+
const msgHandler = async (event) => {
|
|
90
|
+
if (!event.origin) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (new URL(event.origin).origin.toLowerCase() !== new URL(nexusWalletUrl).origin.toLowerCase()) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if ("iframeLifecycle" in event.data) {
|
|
97
|
+
await iframeLifecycleManager.handleEvent(event.data);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if ("clientLifecycle" in event.data) {
|
|
101
|
+
console.error(
|
|
102
|
+
"Client lifecycle event received on the client side, dismissing it"
|
|
103
|
+
);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const channel = event.data.id;
|
|
107
|
+
const resolver = channelManager.getRpcResolver(channel);
|
|
108
|
+
if (!resolver) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
await resolver(event.data);
|
|
112
|
+
};
|
|
113
|
+
window.addEventListener("message", msgHandler);
|
|
114
|
+
const sendEvent = (message) => {
|
|
115
|
+
contentWindow.postMessage(message, {
|
|
116
|
+
targetOrigin: nexusWalletUrl
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
const cleanup = () => {
|
|
120
|
+
window.removeEventListener("message", msgHandler);
|
|
121
|
+
};
|
|
122
|
+
return {
|
|
123
|
+
sendEvent,
|
|
124
|
+
cleanup
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/core/clients/createIFrameNexusClient.ts
|
|
129
|
+
function createIFrameNexusClient({
|
|
130
|
+
config,
|
|
131
|
+
iframe
|
|
132
|
+
}) {
|
|
133
|
+
const channelManager = createIFrameChannelManager();
|
|
134
|
+
const lifecycleManager = createIFrameLifecycleManager({ iframe });
|
|
135
|
+
const messageHandler = createIFrameMessageHandler({
|
|
136
|
+
nexusWalletUrl: config.walletUrl,
|
|
137
|
+
iframe,
|
|
138
|
+
channelManager,
|
|
139
|
+
iframeLifecycleManager: lifecycleManager
|
|
140
|
+
});
|
|
141
|
+
const request = async (args) => {
|
|
142
|
+
const isConnected = await lifecycleManager.isConnected;
|
|
143
|
+
if (!isConnected) {
|
|
144
|
+
throw new (0, _chunkJ655X6YRcjs.FrakRpcError)(
|
|
145
|
+
_chunkJ655X6YRcjs.RpcErrorCodes.clientNotConnected,
|
|
146
|
+
"The iframe provider isn't connected yet"
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
const result = new (0, _chunkJ655X6YRcjs.Deferred)();
|
|
150
|
+
const channelId = channelManager.createChannel(async (message) => {
|
|
151
|
+
const decompressed = await _chunkJ655X6YRcjs.decompressDataAndCheckHash.call(void 0, message.data);
|
|
152
|
+
if (decompressed.error) {
|
|
153
|
+
result.reject(
|
|
154
|
+
new (0, _chunkJ655X6YRcjs.FrakRpcError)(
|
|
155
|
+
decompressed.error.code,
|
|
156
|
+
decompressed.error.message,
|
|
157
|
+
_optionalChain([decompressed, 'access', _ => _.error, 'optionalAccess', _2 => _2.data])
|
|
158
|
+
)
|
|
159
|
+
);
|
|
160
|
+
} else {
|
|
161
|
+
result.resolve(decompressed.result);
|
|
162
|
+
}
|
|
163
|
+
channelManager.removeChannel(channelId);
|
|
164
|
+
});
|
|
165
|
+
const compressedMessage = await _chunkJ655X6YRcjs.hashAndCompressData.call(void 0, args);
|
|
166
|
+
messageHandler.sendEvent({
|
|
167
|
+
id: channelId,
|
|
168
|
+
topic: args.method,
|
|
169
|
+
data: compressedMessage
|
|
170
|
+
});
|
|
171
|
+
return result.promise;
|
|
172
|
+
};
|
|
173
|
+
const listenerRequest = async (args, callback) => {
|
|
174
|
+
const isConnected = await lifecycleManager.isConnected;
|
|
175
|
+
if (!isConnected) {
|
|
176
|
+
throw new (0, _chunkJ655X6YRcjs.FrakRpcError)(
|
|
177
|
+
_chunkJ655X6YRcjs.RpcErrorCodes.clientNotConnected,
|
|
178
|
+
"The iframe provider isn't connected yet"
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
const channelId = channelManager.createChannel(async (message) => {
|
|
182
|
+
const decompressed = await _chunkJ655X6YRcjs.decompressDataAndCheckHash.call(void 0, message.data);
|
|
183
|
+
if (decompressed.result) {
|
|
184
|
+
callback(decompressed.result);
|
|
185
|
+
} else {
|
|
186
|
+
throw new (0, _chunkJ655X6YRcjs.InternalError)("No valid result in the response");
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
const compressedMessage = await _chunkJ655X6YRcjs.hashAndCompressData.call(void 0, args);
|
|
190
|
+
messageHandler.sendEvent({
|
|
191
|
+
id: channelId,
|
|
192
|
+
topic: args.method,
|
|
193
|
+
data: compressedMessage
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
const destroy = async () => {
|
|
197
|
+
channelManager.destroy();
|
|
198
|
+
messageHandler.cleanup();
|
|
199
|
+
iframe.remove();
|
|
200
|
+
};
|
|
201
|
+
const waitForSetup = postConnectionSetup({
|
|
202
|
+
config,
|
|
203
|
+
messageHandler,
|
|
204
|
+
lifecycleManager
|
|
205
|
+
});
|
|
206
|
+
return {
|
|
207
|
+
config,
|
|
208
|
+
waitForConnection: lifecycleManager.isConnected,
|
|
209
|
+
waitForSetup,
|
|
210
|
+
request,
|
|
211
|
+
listenerRequest,
|
|
212
|
+
destroy
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
async function postConnectionSetup({
|
|
216
|
+
config,
|
|
217
|
+
messageHandler,
|
|
218
|
+
lifecycleManager
|
|
219
|
+
}) {
|
|
220
|
+
await lifecycleManager.isConnected;
|
|
221
|
+
const pushCss = async () => {
|
|
222
|
+
const cssLink = config.metadata.css;
|
|
223
|
+
if (!cssLink) return;
|
|
224
|
+
messageHandler.sendEvent({
|
|
225
|
+
clientLifecycle: "modal-css",
|
|
226
|
+
data: { cssLink }
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
const pushBackup = async () => {
|
|
230
|
+
if (typeof window === "undefined") return;
|
|
231
|
+
const backup = window.localStorage.getItem(BACKUP_KEY);
|
|
232
|
+
if (!backup) return;
|
|
233
|
+
messageHandler.sendEvent({
|
|
234
|
+
clientLifecycle: "restore-backup",
|
|
235
|
+
data: { backup }
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
await Promise.all([pushCss(), pushBackup()]);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
exports.createIFrameNexusClient = createIFrameNexusClient;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _chunkJ655X6YRcjs = require('./chunk-J655X6YR.cjs');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
var _chunkA3XYA7S7cjs = require('./chunk-A3XYA7S7.cjs');
|
|
10
|
+
|
|
11
|
+
// src/core/actions/watchWalletStatus.ts
|
|
12
|
+
function watchWalletStatus(client, callback) {
|
|
13
|
+
return client.listenerRequest(
|
|
14
|
+
{
|
|
15
|
+
method: "frak_listenToWalletStatus"
|
|
16
|
+
},
|
|
17
|
+
callback
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/core/utils/computeProductId.ts
|
|
22
|
+
var _viem = require('viem');
|
|
23
|
+
function computeProductId({ domain }) {
|
|
24
|
+
return _viem.keccak256.call(void 0, _viem.toHex.call(void 0, domain));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/core/actions/sendInteraction.ts
|
|
28
|
+
async function sendInteraction(client, { productId, interaction, validation }) {
|
|
29
|
+
const pId = _nullishCoalesce(productId, () => ( computeProductId(client.config)));
|
|
30
|
+
return await client.request({
|
|
31
|
+
method: "frak_sendInteraction",
|
|
32
|
+
params: [pId, interaction, validation]
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/core/actions/displayModal.ts
|
|
37
|
+
async function displayModal(client, { steps, metadata }) {
|
|
38
|
+
return await client.request({
|
|
39
|
+
method: "frak_displayModal",
|
|
40
|
+
params: [steps, client.config.metadata.name, metadata]
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/core/actions/openSso.ts
|
|
45
|
+
async function openSso(client, args) {
|
|
46
|
+
const { metadata } = client.config;
|
|
47
|
+
await client.request({
|
|
48
|
+
method: "frak_sso",
|
|
49
|
+
params: [args, metadata.name, metadata.css]
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/core/actions/wrapper/siweAuthenticate.ts
|
|
54
|
+
var _siwe = require('viem/siwe');
|
|
55
|
+
async function siweAuthenticate(client, { siwe, metadata }) {
|
|
56
|
+
const realStatement = _nullishCoalesce(_optionalChain([siwe, 'optionalAccess', _ => _.statement]), () => ( `I confirm that I want to use my Frak wallet on: ${client.config.metadata.name}`));
|
|
57
|
+
const builtSiwe = {
|
|
58
|
+
...siwe,
|
|
59
|
+
statement: realStatement,
|
|
60
|
+
nonce: _nullishCoalesce(_optionalChain([siwe, 'optionalAccess', _2 => _2.nonce]), () => ( _siwe.generateSiweNonce.call(void 0, ))),
|
|
61
|
+
uri: _nullishCoalesce(_optionalChain([siwe, 'optionalAccess', _3 => _3.uri]), () => ( `https://${client.config.domain}`)),
|
|
62
|
+
version: _nullishCoalesce(_optionalChain([siwe, 'optionalAccess', _4 => _4.version]), () => ( "1")),
|
|
63
|
+
domain: client.config.domain
|
|
64
|
+
};
|
|
65
|
+
const result = await displayModal(client, {
|
|
66
|
+
metadata,
|
|
67
|
+
steps: {
|
|
68
|
+
login: {},
|
|
69
|
+
siweAuthenticate: {
|
|
70
|
+
siwe: builtSiwe
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return result.siweAuthenticate;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/core/actions/wrapper/sendTransaction.ts
|
|
78
|
+
async function sendTransaction(client, { tx, metadata }) {
|
|
79
|
+
const result = await displayModal(client, {
|
|
80
|
+
metadata,
|
|
81
|
+
steps: {
|
|
82
|
+
login: {},
|
|
83
|
+
sendTransaction: { tx }
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
return result.sendTransaction;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/core/actions/wrapper/walletStatus.ts
|
|
90
|
+
async function walletStatus(client, callback) {
|
|
91
|
+
const firstResult = new (0, _chunkJ655X6YRcjs.Deferred)();
|
|
92
|
+
let hasResolved = false;
|
|
93
|
+
await watchWalletStatus(client, (status) => {
|
|
94
|
+
_optionalChain([callback, 'optionalCall', _5 => _5(status)]);
|
|
95
|
+
if (!hasResolved) {
|
|
96
|
+
firstResult.resolve(status);
|
|
97
|
+
hasResolved = true;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return firstResult.promise;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/core/actions/referral/processReferral.ts
|
|
104
|
+
|
|
105
|
+
async function processReferral(client, {
|
|
106
|
+
walletStatus: walletStatus2,
|
|
107
|
+
frakContext,
|
|
108
|
+
modalConfig,
|
|
109
|
+
productId
|
|
110
|
+
}) {
|
|
111
|
+
try {
|
|
112
|
+
let currentWallet = _optionalChain([walletStatus2, 'optionalAccess', _6 => _6.wallet]);
|
|
113
|
+
if (!_optionalChain([frakContext, 'optionalAccess', _7 => _7.r])) {
|
|
114
|
+
if (currentWallet) {
|
|
115
|
+
await _chunkJ655X6YRcjs.FrakContextManager.replaceUrl({
|
|
116
|
+
url: _optionalChain([window, 'access', _8 => _8.location, 'optionalAccess', _9 => _9.href]),
|
|
117
|
+
context: { r: currentWallet }
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return "no-referrer";
|
|
121
|
+
}
|
|
122
|
+
if (!currentWallet) {
|
|
123
|
+
currentWallet = await ensureWalletConnected(client, {
|
|
124
|
+
modalConfig,
|
|
125
|
+
walletStatus: walletStatus2
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (currentWallet && _viem.isAddressEqual.call(void 0, frakContext.r, currentWallet)) {
|
|
129
|
+
return "self-referral";
|
|
130
|
+
}
|
|
131
|
+
if (!_optionalChain([walletStatus2, 'optionalAccess', _10 => _10.interactionSession])) {
|
|
132
|
+
currentWallet = await ensureWalletConnected(client, {
|
|
133
|
+
modalConfig,
|
|
134
|
+
walletStatus: walletStatus2
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (currentWallet) {
|
|
138
|
+
await _chunkJ655X6YRcjs.FrakContextManager.replaceUrl({
|
|
139
|
+
url: _optionalChain([window, 'access', _11 => _11.location, 'optionalAccess', _12 => _12.href]),
|
|
140
|
+
context: { r: currentWallet }
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
const interaction = _chunkA3XYA7S7cjs.ReferralInteractionEncoder.referred({
|
|
144
|
+
referrer: frakContext.r
|
|
145
|
+
});
|
|
146
|
+
await sendInteraction(client, { productId, interaction });
|
|
147
|
+
return "success";
|
|
148
|
+
} catch (error) {
|
|
149
|
+
return mapErrorToState(error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async function ensureWalletConnected(client, {
|
|
153
|
+
modalConfig,
|
|
154
|
+
walletStatus: walletStatus2
|
|
155
|
+
}) {
|
|
156
|
+
if (!_optionalChain([walletStatus2, 'optionalAccess', _13 => _13.interactionSession])) {
|
|
157
|
+
if (!modalConfig) {
|
|
158
|
+
return void 0;
|
|
159
|
+
}
|
|
160
|
+
const result = await displayModal(client, modalConfig);
|
|
161
|
+
return _nullishCoalesce(_optionalChain([result, 'optionalAccess', _14 => _14.login, 'optionalAccess', _15 => _15.wallet]), () => ( void 0));
|
|
162
|
+
}
|
|
163
|
+
return _nullishCoalesce(walletStatus2.wallet, () => ( void 0));
|
|
164
|
+
}
|
|
165
|
+
function mapErrorToState(error) {
|
|
166
|
+
if (error instanceof _chunkJ655X6YRcjs.FrakRpcError) {
|
|
167
|
+
switch (error.code) {
|
|
168
|
+
case _chunkJ655X6YRcjs.RpcErrorCodes.walletNotConnected:
|
|
169
|
+
return "no-wallet";
|
|
170
|
+
case _chunkJ655X6YRcjs.RpcErrorCodes.serverErrorForInteractionDelegation:
|
|
171
|
+
return "no-session";
|
|
172
|
+
default:
|
|
173
|
+
return "error";
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return "error";
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/core/actions/referral/referralInteraction.ts
|
|
180
|
+
async function referralInteraction(client, {
|
|
181
|
+
productId,
|
|
182
|
+
modalConfig
|
|
183
|
+
} = {}) {
|
|
184
|
+
const frakContext = await _chunkJ655X6YRcjs.FrakContextManager.parse({
|
|
185
|
+
url: window.location.href
|
|
186
|
+
});
|
|
187
|
+
const currentWalletStatus = await walletStatus(client);
|
|
188
|
+
try {
|
|
189
|
+
return await processReferral(client, {
|
|
190
|
+
walletStatus: currentWalletStatus,
|
|
191
|
+
frakContext,
|
|
192
|
+
modalConfig,
|
|
193
|
+
productId
|
|
194
|
+
});
|
|
195
|
+
} catch (error) {
|
|
196
|
+
return error;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
exports.watchWalletStatus = watchWalletStatus; exports.sendInteraction = sendInteraction; exports.displayModal = displayModal; exports.openSso = openSso; exports.siweAuthenticate = siweAuthenticate; exports.sendTransaction = sendTransaction; exports.walletStatus = walletStatus; exports.processReferral = processReferral; exports.referralInteraction = referralInteraction;
|