@frak-labs/nexus-sdk 0.0.10 → 0.0.11

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 (37) hide show
  1. package/dist/chunk-5CFD5FM2.js +86 -0
  2. package/dist/chunk-7MVQQ2X6.js +381 -0
  3. package/dist/{chunk-72IEHEQX.js → chunk-GUDT2W6I.js} +32 -10
  4. package/dist/{chunk-NIFJZD3M.cjs → chunk-IQQTTKJL.cjs} +30 -8
  5. package/dist/chunk-S5FVCA2E.cjs +86 -0
  6. package/dist/chunk-V3S6RBRJ.cjs +381 -0
  7. package/dist/{client-6_BJp7Ub.d.ts → client-C7u9zGwC.d.cts} +135 -142
  8. package/dist/{client--U_6SK0l.d.cts → client-gE3fYzkD.d.ts} +135 -142
  9. package/dist/core/actions/index.cjs +2 -7
  10. package/dist/core/actions/index.d.cts +16 -35
  11. package/dist/core/actions/index.d.ts +16 -35
  12. package/dist/core/actions/index.js +5 -10
  13. package/dist/core/index.cjs +2 -4
  14. package/dist/core/index.d.cts +3 -3
  15. package/dist/core/index.d.ts +3 -3
  16. package/dist/core/index.js +2 -4
  17. package/dist/core/interactions/index.cjs +6 -2
  18. package/dist/core/interactions/index.d.cts +20 -6
  19. package/dist/core/interactions/index.d.ts +20 -6
  20. package/dist/core/interactions/index.js +7 -3
  21. package/dist/{interaction-D_CzyqRE.d.cts → interaction-D3-M3nBh.d.cts} +2 -2
  22. package/dist/{interaction-D_CzyqRE.d.ts → interaction-D3-M3nBh.d.ts} +2 -2
  23. package/dist/react/index.cjs +146 -158
  24. package/dist/react/index.d.cts +44 -51
  25. package/dist/react/index.d.ts +44 -51
  26. package/dist/react/index.js +152 -164
  27. package/dist/sendTransaction-BZW627cT.d.cts +30 -0
  28. package/dist/sendTransaction-DpJTfGJc.d.ts +30 -0
  29. package/package.json +8 -5
  30. package/dist/chunk-3T2FNW6E.cjs +0 -100
  31. package/dist/chunk-BPFJZRJ6.cjs +0 -152
  32. package/dist/chunk-HOX3RRO6.js +0 -199
  33. package/dist/chunk-SGLR6RFA.cjs +0 -199
  34. package/dist/chunk-T54VMWHQ.js +0 -100
  35. package/dist/chunk-TPC5PMRC.js +0 -152
  36. package/dist/watchUnlockStatus-CxnibdQx.d.cts +0 -43
  37. package/dist/watchUnlockStatus-g8wIxpeM.d.ts +0 -43
@@ -0,0 +1,381 @@
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
+ var _chunkETV4XYOVcjs = require('./chunk-ETV4XYOV.cjs');
4
+
5
+ // src/core/types/rpc/error.ts
6
+ var FrakRpcError = class extends Error {
7
+ constructor(code, message, data) {
8
+ super(message);
9
+ this.code = code;
10
+ this.data = data;
11
+ }
12
+ };
13
+ var ClientNotFound = class extends FrakRpcError {
14
+ constructor() {
15
+ super(RpcErrorCodes.clientNotConnected, "Client not found");
16
+ }
17
+ };
18
+ var RpcErrorCodes = {
19
+ // Standard JSON-RPC 2.0 errors
20
+ parseError: -32700,
21
+ invalidRequest: -32600,
22
+ methodNotFound: -32601,
23
+ invalidParams: -32602,
24
+ internalError: -32603,
25
+ serverError: -32e3,
26
+ // Frak specific errors (from -32001 to -32099)
27
+ clientNotConnected: -32001,
28
+ configError: -32002,
29
+ corruptedResponse: -32003,
30
+ clientAborted: -32004,
31
+ walletNotConnected: -32005,
32
+ noInteractionSession: -32006
33
+ };
34
+
35
+ // src/core/utils/Deferred.ts
36
+ var Deferred = class {
37
+ constructor() {
38
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "_promise");
39
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "_resolve");
40
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "_reject");
41
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "resolve", (value) => {
42
+ _optionalChain([this, 'access', _2 => _2._resolve, 'optionalCall', _3 => _3(value)]);
43
+ });
44
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "reject", (reason) => {
45
+ _optionalChain([this, 'access', _4 => _4._reject, 'optionalCall', _5 => _5(reason)]);
46
+ });
47
+ this._promise = new Promise((resolve, reject) => {
48
+ this._resolve = resolve;
49
+ this._reject = reject;
50
+ });
51
+ }
52
+ get promise() {
53
+ return this._promise;
54
+ }
55
+ };
56
+
57
+ // src/core/utils/compression/compress.ts
58
+ var _asynclzstring = require('async-lz-string');
59
+ var _jssha256 = require('js-sha256');
60
+ async function hashAndCompressData(data) {
61
+ const validationHash = _jssha256.sha256.call(void 0, JSON.stringify(data));
62
+ const hashProtectedData = {
63
+ ...data,
64
+ validationHash
65
+ };
66
+ const compressed = await compressJson(hashProtectedData);
67
+ const compressedHash = _jssha256.sha256.call(void 0, compressed);
68
+ return {
69
+ compressed,
70
+ compressedHash
71
+ };
72
+ }
73
+ async function compressJson(data) {
74
+ return _asynclzstring.compressToBase64.call(void 0, JSON.stringify(data));
75
+ }
76
+
77
+ // src/core/utils/compression/decompress.ts
78
+
79
+
80
+ async function decompressDataAndCheckHash(compressedData) {
81
+ if (!(_optionalChain([compressedData, 'optionalAccess', _6 => _6.compressed]) && _optionalChain([compressedData, 'optionalAccess', _7 => _7.compressedHash]))) {
82
+ throw new FrakRpcError(
83
+ RpcErrorCodes.corruptedResponse,
84
+ "Missing compressed data"
85
+ );
86
+ }
87
+ const parsedData = await decompressJson(
88
+ compressedData.compressed
89
+ );
90
+ if (!parsedData) {
91
+ throw new FrakRpcError(
92
+ RpcErrorCodes.corruptedResponse,
93
+ "Invalid compressed data"
94
+ );
95
+ }
96
+ if (!_optionalChain([parsedData, 'optionalAccess', _8 => _8.validationHash])) {
97
+ throw new FrakRpcError(
98
+ RpcErrorCodes.corruptedResponse,
99
+ "Missing validation hash"
100
+ );
101
+ }
102
+ const expectedCompressedHash = _jssha256.sha256.call(void 0, compressedData.compressed);
103
+ if (expectedCompressedHash !== compressedData.compressedHash) {
104
+ throw new FrakRpcError(
105
+ RpcErrorCodes.corruptedResponse,
106
+ "Invalid compressed hash"
107
+ );
108
+ }
109
+ const { validationHash: _, ...rawResultData } = parsedData;
110
+ const expectedValidationHash = _jssha256.sha256.call(void 0, JSON.stringify(rawResultData));
111
+ if (expectedValidationHash !== parsedData.validationHash) {
112
+ throw new FrakRpcError(
113
+ RpcErrorCodes.corruptedResponse,
114
+ "Invalid data validation hash"
115
+ );
116
+ }
117
+ return parsedData;
118
+ }
119
+ async function decompressJson(data) {
120
+ const decompressed = await _asynclzstring.decompressFromBase64.call(void 0, data);
121
+ try {
122
+ return JSON.parse(decompressed);
123
+ } catch (e) {
124
+ console.error("Invalid compressed data", e);
125
+ return null;
126
+ }
127
+ }
128
+
129
+ // src/core/clients/transports/iframeChannelManager.ts
130
+ function createIFrameChannelManager() {
131
+ const channels = /* @__PURE__ */ new Map();
132
+ return {
133
+ // TODO: Better id system?? uid stuff?
134
+ createChannel: (resolver) => {
135
+ const id = Math.random().toString(36).substring(7);
136
+ channels.set(id, resolver);
137
+ return id;
138
+ },
139
+ getRpcResolver: (id) => channels.get(id),
140
+ removeChannel: (id) => channels.delete(id),
141
+ destroy: () => channels.clear()
142
+ };
143
+ }
144
+
145
+ // src/core/utils/iframeHelper.ts
146
+ var baseIframeProps = {
147
+ id: "nexus-wallet",
148
+ name: "nexus-wallet",
149
+ allow: "publickey-credentials-get *; clipboard-write; web-share *",
150
+ style: {
151
+ width: "0",
152
+ height: "0",
153
+ border: "0",
154
+ position: "absolute",
155
+ zIndex: 1e3,
156
+ top: "-1000px",
157
+ left: "-1000px"
158
+ }
159
+ };
160
+ function createIframe({
161
+ walletBaseUrl
162
+ }) {
163
+ const alreadyCreatedIFrame = document.querySelector("#nexus-wallet");
164
+ if (alreadyCreatedIFrame) {
165
+ return Promise.resolve(void 0);
166
+ }
167
+ const iframe = document.createElement("iframe");
168
+ iframe.id = baseIframeProps.id;
169
+ iframe.name = baseIframeProps.name;
170
+ iframe.allow = baseIframeProps.allow;
171
+ iframe.style.zIndex = baseIframeProps.style.zIndex.toString();
172
+ changeIframeVisibility({ iframe, isVisible: false });
173
+ document.body.appendChild(iframe);
174
+ return new Promise((resolve) => {
175
+ _optionalChain([iframe, 'optionalAccess', _9 => _9.addEventListener, 'call', _10 => _10("load", () => resolve(iframe))]);
176
+ iframe.src = `${walletBaseUrl}/listener`;
177
+ });
178
+ }
179
+ function changeIframeVisibility({
180
+ iframe,
181
+ isVisible
182
+ }) {
183
+ if (!isVisible) {
184
+ iframe.style.width = "0";
185
+ iframe.style.height = "0";
186
+ iframe.style.border = "0";
187
+ iframe.style.position = "fixed";
188
+ iframe.style.top = "-1000px";
189
+ iframe.style.left = "-1000px";
190
+ return;
191
+ }
192
+ iframe.style.position = "fixed";
193
+ iframe.style.top = "0";
194
+ iframe.style.left = "0";
195
+ iframe.style.width = "100%";
196
+ iframe.style.height = "100%";
197
+ iframe.style.pointerEvents = "auto";
198
+ }
199
+
200
+ // src/core/clients/transports/iframeMessageHandler.ts
201
+ function createIFrameMessageHandler({
202
+ nexusWalletUrl,
203
+ metadata,
204
+ iframe,
205
+ channelManager
206
+ }) {
207
+ if (typeof window === "undefined") {
208
+ throw new FrakRpcError(
209
+ RpcErrorCodes.configError,
210
+ "iframe client should be used in the browser"
211
+ );
212
+ }
213
+ if (!iframe.contentWindow) {
214
+ throw new FrakRpcError(
215
+ RpcErrorCodes.configError,
216
+ "The iframe does not have a content window"
217
+ );
218
+ }
219
+ const contentWindow = iframe.contentWindow;
220
+ const isConnectedDeferred = new Deferred();
221
+ const msgHandler = async (event) => {
222
+ if (!event.origin) {
223
+ return;
224
+ }
225
+ if (new URL(event.origin).origin.toLowerCase() !== new URL(nexusWalletUrl).origin.toLowerCase()) {
226
+ return;
227
+ }
228
+ if ("lifecycle" in event.data) {
229
+ switch (event.data.lifecycle) {
230
+ case "connected":
231
+ isConnectedDeferred.resolve(true);
232
+ break;
233
+ case "show":
234
+ case "hide":
235
+ changeIframeVisibility({
236
+ iframe,
237
+ isVisible: event.data.lifecycle === "show"
238
+ });
239
+ break;
240
+ }
241
+ return;
242
+ }
243
+ const channel = event.data.id;
244
+ const resolver = channelManager.getRpcResolver(channel);
245
+ if (!resolver) {
246
+ return;
247
+ }
248
+ await resolver(event.data);
249
+ };
250
+ window.addEventListener("message", msgHandler);
251
+ const sendEvent = (message) => {
252
+ contentWindow.postMessage(message, {
253
+ targetOrigin: nexusWalletUrl
254
+ });
255
+ };
256
+ const cleanup = () => {
257
+ window.removeEventListener("message", msgHandler);
258
+ };
259
+ const isConnected = injectCssOnConnect({
260
+ isConnected: isConnectedDeferred.promise,
261
+ metadata,
262
+ sendEvent
263
+ });
264
+ return {
265
+ isConnected,
266
+ sendEvent,
267
+ cleanup
268
+ };
269
+ }
270
+ function injectCssOnConnect({
271
+ isConnected,
272
+ metadata,
273
+ sendEvent
274
+ }) {
275
+ const css = _optionalChain([metadata, 'optionalAccess', _11 => _11.css]);
276
+ if (!css) {
277
+ return isConnected;
278
+ }
279
+ return isConnected.then((connected) => {
280
+ sendEvent({
281
+ lifecycle: "modal-css",
282
+ data: css
283
+ });
284
+ return connected;
285
+ });
286
+ }
287
+
288
+ // src/core/clients/createIFrameNexusClient.ts
289
+ function createIFrameNexusClient({
290
+ config,
291
+ iframe
292
+ }) {
293
+ const channelManager = createIFrameChannelManager();
294
+ const messageHandler = createIFrameMessageHandler({
295
+ nexusWalletUrl: config.walletUrl,
296
+ metadata: config.metadata,
297
+ iframe,
298
+ channelManager
299
+ });
300
+ const request = async (args) => {
301
+ const isConnected = await messageHandler.isConnected;
302
+ if (!isConnected) {
303
+ throw new FrakRpcError(
304
+ RpcErrorCodes.clientNotConnected,
305
+ "The iframe provider isn't connected yet"
306
+ );
307
+ }
308
+ const result = new Deferred();
309
+ const channelId = channelManager.createChannel(async (message) => {
310
+ const decompressed = await decompressDataAndCheckHash(message.data);
311
+ if (decompressed.error) {
312
+ result.reject(
313
+ new FrakRpcError(
314
+ decompressed.error.code,
315
+ decompressed.error.message,
316
+ _optionalChain([decompressed, 'access', _12 => _12.error, 'optionalAccess', _13 => _13.data])
317
+ )
318
+ );
319
+ } else {
320
+ result.resolve(decompressed.result);
321
+ }
322
+ channelManager.removeChannel(channelId);
323
+ });
324
+ const compressedMessage = await hashAndCompressData(args);
325
+ messageHandler.sendEvent({
326
+ id: channelId,
327
+ // @ts-ignore, todo: idk why the fck it's needed
328
+ topic: args.method,
329
+ data: compressedMessage
330
+ });
331
+ return result.promise;
332
+ };
333
+ const listenerRequest = async (args, callback) => {
334
+ const isConnected = await messageHandler.isConnected;
335
+ if (!isConnected) {
336
+ throw new FrakRpcError(
337
+ RpcErrorCodes.clientNotConnected,
338
+ "The iframe provider isn't connected yet"
339
+ );
340
+ }
341
+ const channelId = channelManager.createChannel(async (message) => {
342
+ const decompressed = await decompressDataAndCheckHash(message.data);
343
+ if (decompressed.result) {
344
+ callback(decompressed.result);
345
+ } else {
346
+ }
347
+ });
348
+ const compressedMessage = await hashAndCompressData(args);
349
+ messageHandler.sendEvent({
350
+ id: channelId,
351
+ topic: args.method,
352
+ data: compressedMessage
353
+ });
354
+ };
355
+ const destroy = async () => {
356
+ channelManager.destroy();
357
+ messageHandler.cleanup();
358
+ iframe.remove();
359
+ };
360
+ return {
361
+ config,
362
+ waitForConnection: messageHandler.isConnected,
363
+ request,
364
+ listenerRequest,
365
+ destroy
366
+ };
367
+ }
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+ exports.FrakRpcError = FrakRpcError; exports.ClientNotFound = ClientNotFound; exports.RpcErrorCodes = RpcErrorCodes; exports.Deferred = Deferred; exports.hashAndCompressData = hashAndCompressData; exports.compressJson = compressJson; exports.decompressDataAndCheckHash = decompressDataAndCheckHash; exports.decompressJson = decompressJson; exports.baseIframeProps = baseIframeProps; exports.createIframe = createIframe; exports.createIFrameNexusClient = createIFrameNexusClient;