@frak-labs/nexus-sdk 0.0.10 → 0.0.12

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 (39) hide show
  1. package/dist/chunk-5CFD5FM2.js +86 -0
  2. package/dist/chunk-BJ3CCN5P.cjs +424 -0
  3. package/dist/{chunk-72IEHEQX.js → chunk-GUDT2W6I.js} +32 -10
  4. package/dist/chunk-HXVDI2IY.js +424 -0
  5. package/dist/{chunk-NIFJZD3M.cjs → chunk-IQQTTKJL.cjs} +30 -8
  6. package/dist/chunk-S5FVCA2E.cjs +86 -0
  7. package/dist/client-B6_BIGc3.d.ts +357 -0
  8. package/dist/client-Oily5ph8.d.cts +357 -0
  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 +19 -19
  15. package/dist/core/index.d.ts +19 -19
  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 +156 -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 +162 -164
  27. package/dist/sendTransaction-BHqCq_9X.d.ts +30 -0
  28. package/dist/sendTransaction-BKKYEt8p.d.cts +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/client--U_6SK0l.d.cts +0 -339
  37. package/dist/client-6_BJp7Ub.d.ts +0 -339
  38. package/dist/watchUnlockStatus-CxnibdQx.d.cts +0 -43
  39. package/dist/watchUnlockStatus-g8wIxpeM.d.ts +0 -43
@@ -0,0 +1,86 @@
1
+ // src/core/actions/watchWalletStatus.ts
2
+ function watchWalletStatus(client, callback) {
3
+ return client.listenerRequest(
4
+ {
5
+ method: "frak_listenToWalletStatus"
6
+ },
7
+ callback
8
+ );
9
+ }
10
+
11
+ // src/core/utils/computeContentId.ts
12
+ import { keccak256, toHex } from "viem";
13
+ function computeContentId({ domain }) {
14
+ return keccak256(toHex(domain));
15
+ }
16
+
17
+ // src/core/actions/sendInteraction.ts
18
+ async function sendInteraction(client, { contentId, interaction, validation }) {
19
+ const cId = contentId ?? computeContentId(client.config);
20
+ return await client.request({
21
+ method: "frak_sendInteraction",
22
+ params: [cId, interaction, validation]
23
+ });
24
+ }
25
+
26
+ // src/core/actions/displayModal.ts
27
+ async function displayModal(client, { steps, metadata }) {
28
+ return await client.request({
29
+ method: "frak_displayModal",
30
+ params: [steps, client.config.metadata.name, metadata]
31
+ });
32
+ }
33
+
34
+ // src/core/actions/openSso.ts
35
+ async function openSso(client, args) {
36
+ const { metadata } = client.config;
37
+ await client.request({
38
+ method: "frak_sso",
39
+ params: [args, metadata.name, metadata.css]
40
+ });
41
+ }
42
+
43
+ // src/core/actions/wrapper/siweAuthenticate.ts
44
+ import { generateSiweNonce } from "viem/siwe";
45
+ async function siweAuthenticate(client, { siwe, metadata }) {
46
+ const realStatement = siwe?.statement ?? `I confirm that I want to use my Nexus wallet on: ${client.config.metadata.name}`;
47
+ const builtSiwe = {
48
+ ...siwe,
49
+ statement: realStatement,
50
+ nonce: siwe?.nonce ?? generateSiweNonce(),
51
+ uri: siwe?.uri ?? `https://${client.config.domain}`,
52
+ version: siwe?.version ?? "1",
53
+ domain: client.config.domain
54
+ };
55
+ const result = await displayModal(client, {
56
+ metadata,
57
+ steps: {
58
+ login: {},
59
+ siweAuthenticate: {
60
+ siwe: builtSiwe
61
+ }
62
+ }
63
+ });
64
+ return result.siweAuthenticate;
65
+ }
66
+
67
+ // src/core/actions/wrapper/sendTransaction.ts
68
+ async function sendTransaction(client, { tx, metadata }) {
69
+ const result = await displayModal(client, {
70
+ metadata,
71
+ steps: {
72
+ login: {},
73
+ sendTransaction: { tx }
74
+ }
75
+ });
76
+ return result.sendTransaction;
77
+ }
78
+
79
+ export {
80
+ watchWalletStatus,
81
+ sendInteraction,
82
+ displayModal,
83
+ openSso,
84
+ siweAuthenticate,
85
+ sendTransaction
86
+ };
@@ -0,0 +1,424 @@
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 InternalError = class extends FrakRpcError {
14
+ constructor(message) {
15
+ super(RpcErrorCodes.internalError, message);
16
+ }
17
+ };
18
+ var ClientNotFound = class extends FrakRpcError {
19
+ constructor() {
20
+ super(RpcErrorCodes.clientNotConnected, "Client not found");
21
+ }
22
+ };
23
+ var RpcErrorCodes = {
24
+ // Standard JSON-RPC 2.0 errors
25
+ parseError: -32700,
26
+ invalidRequest: -32600,
27
+ methodNotFound: -32601,
28
+ invalidParams: -32602,
29
+ internalError: -32603,
30
+ serverError: -32e3,
31
+ // Frak specific errors (from -32001 to -32099)
32
+ clientNotConnected: -32001,
33
+ configError: -32002,
34
+ corruptedResponse: -32003,
35
+ clientAborted: -32004,
36
+ walletNotConnected: -32005,
37
+ noInteractionSession: -32006
38
+ };
39
+
40
+ // src/core/utils/Deferred.ts
41
+ var Deferred = class {
42
+ constructor() {
43
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "_promise");
44
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "_resolve");
45
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "_reject");
46
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "resolve", (value) => {
47
+ _optionalChain([this, 'access', _2 => _2._resolve, 'optionalCall', _3 => _3(value)]);
48
+ });
49
+ _chunkETV4XYOVcjs.__publicField.call(void 0, this, "reject", (reason) => {
50
+ _optionalChain([this, 'access', _4 => _4._reject, 'optionalCall', _5 => _5(reason)]);
51
+ });
52
+ this._promise = new Promise((resolve, reject) => {
53
+ this._resolve = resolve;
54
+ this._reject = reject;
55
+ });
56
+ }
57
+ get promise() {
58
+ return this._promise;
59
+ }
60
+ };
61
+
62
+ // src/core/utils/compression/compress.ts
63
+ var _asynclzstring = require('async-lz-string');
64
+ var _jssha256 = require('js-sha256');
65
+ async function hashAndCompressData(data) {
66
+ const validationHash = _jssha256.sha256.call(void 0, JSON.stringify(data));
67
+ const hashProtectedData = {
68
+ ...data,
69
+ validationHash
70
+ };
71
+ const compressed = await compressJson(hashProtectedData);
72
+ const compressedHash = _jssha256.sha256.call(void 0, compressed);
73
+ return {
74
+ compressed,
75
+ compressedHash
76
+ };
77
+ }
78
+ async function compressJson(data) {
79
+ return _asynclzstring.compressToBase64.call(void 0, JSON.stringify(data));
80
+ }
81
+
82
+ // src/core/utils/compression/decompress.ts
83
+
84
+
85
+ async function decompressDataAndCheckHash(compressedData) {
86
+ if (!(_optionalChain([compressedData, 'optionalAccess', _6 => _6.compressed]) && _optionalChain([compressedData, 'optionalAccess', _7 => _7.compressedHash]))) {
87
+ throw new FrakRpcError(
88
+ RpcErrorCodes.corruptedResponse,
89
+ "Missing compressed data"
90
+ );
91
+ }
92
+ const parsedData = await decompressJson(
93
+ compressedData.compressed
94
+ );
95
+ if (!parsedData) {
96
+ throw new FrakRpcError(
97
+ RpcErrorCodes.corruptedResponse,
98
+ "Invalid compressed data"
99
+ );
100
+ }
101
+ if (!_optionalChain([parsedData, 'optionalAccess', _8 => _8.validationHash])) {
102
+ throw new FrakRpcError(
103
+ RpcErrorCodes.corruptedResponse,
104
+ "Missing validation hash"
105
+ );
106
+ }
107
+ const expectedCompressedHash = _jssha256.sha256.call(void 0, compressedData.compressed);
108
+ if (expectedCompressedHash !== compressedData.compressedHash) {
109
+ throw new FrakRpcError(
110
+ RpcErrorCodes.corruptedResponse,
111
+ "Invalid compressed hash"
112
+ );
113
+ }
114
+ const { validationHash: _, ...rawResultData } = parsedData;
115
+ const expectedValidationHash = _jssha256.sha256.call(void 0, JSON.stringify(rawResultData));
116
+ if (expectedValidationHash !== parsedData.validationHash) {
117
+ throw new FrakRpcError(
118
+ RpcErrorCodes.corruptedResponse,
119
+ "Invalid data validation hash"
120
+ );
121
+ }
122
+ return parsedData;
123
+ }
124
+ async function decompressJson(data) {
125
+ const decompressed = await _asynclzstring.decompressFromBase64.call(void 0, data);
126
+ try {
127
+ return JSON.parse(decompressed);
128
+ } catch (e) {
129
+ console.error("Invalid compressed data", e);
130
+ return null;
131
+ }
132
+ }
133
+
134
+ // src/core/utils/constants.ts
135
+ var BACKUP_KEY = "nexus-wallet-backup";
136
+
137
+ // src/core/clients/transports/iframeChannelManager.ts
138
+ function createIFrameChannelManager() {
139
+ const channels = /* @__PURE__ */ new Map();
140
+ return {
141
+ // TODO: Better id system?? uid stuff?
142
+ createChannel: (resolver) => {
143
+ const id = Math.random().toString(36).substring(7);
144
+ channels.set(id, resolver);
145
+ return id;
146
+ },
147
+ getRpcResolver: (id) => channels.get(id),
148
+ removeChannel: (id) => channels.delete(id),
149
+ destroy: () => channels.clear()
150
+ };
151
+ }
152
+
153
+ // src/core/utils/iframeHelper.ts
154
+ var baseIframeProps = {
155
+ id: "nexus-wallet",
156
+ name: "nexus-wallet",
157
+ allow: "publickey-credentials-get *; clipboard-write; web-share *",
158
+ style: {
159
+ width: "0",
160
+ height: "0",
161
+ border: "0",
162
+ position: "absolute",
163
+ zIndex: 1e3,
164
+ top: "-1000px",
165
+ left: "-1000px"
166
+ }
167
+ };
168
+ function createIframe({
169
+ walletBaseUrl
170
+ }) {
171
+ const alreadyCreatedIFrame = document.querySelector("#nexus-wallet");
172
+ if (alreadyCreatedIFrame) {
173
+ return Promise.resolve(void 0);
174
+ }
175
+ const iframe = document.createElement("iframe");
176
+ iframe.id = baseIframeProps.id;
177
+ iframe.name = baseIframeProps.name;
178
+ iframe.allow = baseIframeProps.allow;
179
+ iframe.style.zIndex = baseIframeProps.style.zIndex.toString();
180
+ changeIframeVisibility({ iframe, isVisible: false });
181
+ document.body.appendChild(iframe);
182
+ return new Promise((resolve) => {
183
+ _optionalChain([iframe, 'optionalAccess', _9 => _9.addEventListener, 'call', _10 => _10("load", () => resolve(iframe))]);
184
+ iframe.src = `${walletBaseUrl}/listener`;
185
+ });
186
+ }
187
+ function changeIframeVisibility({
188
+ iframe,
189
+ isVisible
190
+ }) {
191
+ if (!isVisible) {
192
+ iframe.style.width = "0";
193
+ iframe.style.height = "0";
194
+ iframe.style.border = "0";
195
+ iframe.style.position = "fixed";
196
+ iframe.style.top = "-1000px";
197
+ iframe.style.left = "-1000px";
198
+ return;
199
+ }
200
+ iframe.style.position = "fixed";
201
+ iframe.style.top = "0";
202
+ iframe.style.left = "0";
203
+ iframe.style.width = "100%";
204
+ iframe.style.height = "100%";
205
+ iframe.style.pointerEvents = "auto";
206
+ }
207
+
208
+ // src/core/clients/transports/iframeLifecycleManager.ts
209
+ function createIFrameLifecycleManager({
210
+ iframe
211
+ }) {
212
+ const isConnectedDeferred = new Deferred();
213
+ const handler = async (messageEvent) => {
214
+ switch (messageEvent.iframeLifecycle) {
215
+ case "connected":
216
+ isConnectedDeferred.resolve(true);
217
+ break;
218
+ case "do-backup":
219
+ if (messageEvent.data.backup) {
220
+ localStorage.setItem(BACKUP_KEY, messageEvent.data.backup);
221
+ } else {
222
+ localStorage.removeItem(BACKUP_KEY);
223
+ }
224
+ break;
225
+ case "show":
226
+ case "hide":
227
+ changeIframeVisibility({
228
+ iframe,
229
+ isVisible: messageEvent.iframeLifecycle === "show"
230
+ });
231
+ break;
232
+ }
233
+ };
234
+ return {
235
+ handleEvent: handler,
236
+ isConnected: isConnectedDeferred.promise
237
+ };
238
+ }
239
+
240
+ // src/core/clients/transports/iframeMessageHandler.ts
241
+ function createIFrameMessageHandler({
242
+ nexusWalletUrl,
243
+ iframe,
244
+ channelManager,
245
+ iframeLifecycleManager
246
+ }) {
247
+ if (typeof window === "undefined") {
248
+ throw new FrakRpcError(
249
+ RpcErrorCodes.configError,
250
+ "iframe client should be used in the browser"
251
+ );
252
+ }
253
+ if (!iframe.contentWindow) {
254
+ throw new FrakRpcError(
255
+ RpcErrorCodes.configError,
256
+ "The iframe does not have a content window"
257
+ );
258
+ }
259
+ const contentWindow = iframe.contentWindow;
260
+ const msgHandler = async (event) => {
261
+ if (!event.origin) {
262
+ return;
263
+ }
264
+ if (new URL(event.origin).origin.toLowerCase() !== new URL(nexusWalletUrl).origin.toLowerCase()) {
265
+ return;
266
+ }
267
+ if ("iframeLifecycle" in event.data) {
268
+ await iframeLifecycleManager.handleEvent(event.data);
269
+ return;
270
+ }
271
+ if ("clientLifecycle" in event.data) {
272
+ console.error(
273
+ "Client lifecycle event received on the client side, dismissing it"
274
+ );
275
+ return;
276
+ }
277
+ const channel = event.data.id;
278
+ const resolver = channelManager.getRpcResolver(channel);
279
+ if (!resolver) {
280
+ return;
281
+ }
282
+ await resolver(event.data);
283
+ };
284
+ window.addEventListener("message", msgHandler);
285
+ const sendEvent = (message) => {
286
+ contentWindow.postMessage(message, {
287
+ targetOrigin: nexusWalletUrl
288
+ });
289
+ };
290
+ const cleanup = () => {
291
+ window.removeEventListener("message", msgHandler);
292
+ };
293
+ return {
294
+ sendEvent,
295
+ cleanup
296
+ };
297
+ }
298
+
299
+ // src/core/clients/createIFrameNexusClient.ts
300
+ function createIFrameNexusClient({
301
+ config,
302
+ iframe
303
+ }) {
304
+ const channelManager = createIFrameChannelManager();
305
+ const lifecycleManager = createIFrameLifecycleManager({ iframe });
306
+ const messageHandler = createIFrameMessageHandler({
307
+ nexusWalletUrl: config.walletUrl,
308
+ iframe,
309
+ channelManager,
310
+ iframeLifecycleManager: lifecycleManager
311
+ });
312
+ const request = async (args) => {
313
+ const isConnected = await lifecycleManager.isConnected;
314
+ if (!isConnected) {
315
+ throw new FrakRpcError(
316
+ RpcErrorCodes.clientNotConnected,
317
+ "The iframe provider isn't connected yet"
318
+ );
319
+ }
320
+ const result = new Deferred();
321
+ const channelId = channelManager.createChannel(async (message) => {
322
+ const decompressed = await decompressDataAndCheckHash(message.data);
323
+ if (decompressed.error) {
324
+ result.reject(
325
+ new FrakRpcError(
326
+ decompressed.error.code,
327
+ decompressed.error.message,
328
+ _optionalChain([decompressed, 'access', _11 => _11.error, 'optionalAccess', _12 => _12.data])
329
+ )
330
+ );
331
+ } else {
332
+ result.resolve(decompressed.result);
333
+ }
334
+ channelManager.removeChannel(channelId);
335
+ });
336
+ const compressedMessage = await hashAndCompressData(args);
337
+ messageHandler.sendEvent({
338
+ id: channelId,
339
+ topic: args.method,
340
+ data: compressedMessage
341
+ });
342
+ return result.promise;
343
+ };
344
+ const listenerRequest = async (args, callback) => {
345
+ const isConnected = await lifecycleManager.isConnected;
346
+ if (!isConnected) {
347
+ throw new FrakRpcError(
348
+ RpcErrorCodes.clientNotConnected,
349
+ "The iframe provider isn't connected yet"
350
+ );
351
+ }
352
+ const channelId = channelManager.createChannel(async (message) => {
353
+ const decompressed = await decompressDataAndCheckHash(message.data);
354
+ if (decompressed.result) {
355
+ callback(decompressed.result);
356
+ } else {
357
+ throw new InternalError("No valid result in the response");
358
+ }
359
+ });
360
+ const compressedMessage = await hashAndCompressData(args);
361
+ messageHandler.sendEvent({
362
+ id: channelId,
363
+ topic: args.method,
364
+ data: compressedMessage
365
+ });
366
+ };
367
+ const destroy = async () => {
368
+ channelManager.destroy();
369
+ messageHandler.cleanup();
370
+ iframe.remove();
371
+ };
372
+ const waitForSetup = postConnectionSetup({
373
+ config,
374
+ messageHandler,
375
+ lifecycleManager
376
+ });
377
+ return {
378
+ config,
379
+ waitForConnection: lifecycleManager.isConnected,
380
+ waitForSetup,
381
+ request,
382
+ listenerRequest,
383
+ destroy
384
+ };
385
+ }
386
+ async function postConnectionSetup({
387
+ config,
388
+ messageHandler,
389
+ lifecycleManager
390
+ }) {
391
+ await lifecycleManager.isConnected;
392
+ const pushCss = async () => {
393
+ const cssLink = config.metadata.css;
394
+ if (!cssLink) return;
395
+ messageHandler.sendEvent({
396
+ clientLifecycle: "modal-css",
397
+ data: { cssLink }
398
+ });
399
+ };
400
+ const pushBackup = async () => {
401
+ if (typeof window === "undefined") return;
402
+ const backup = window.localStorage.getItem(BACKUP_KEY);
403
+ if (!backup) return;
404
+ messageHandler.sendEvent({
405
+ clientLifecycle: "restore-backup",
406
+ data: { backup }
407
+ });
408
+ };
409
+ await Promise.all([pushCss(), pushBackup()]);
410
+ }
411
+
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+
422
+
423
+
424
+ 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;
@@ -3,8 +3,7 @@ import { concatHex, pad } from "viem";
3
3
  var PressTypeSelector = "0x02";
4
4
  var PressActionsSelector = {
5
5
  OpenArticle: "0xc0a24ffb",
6
- ReadArticle: "0xd5bd0fbe",
7
- Referred: "0x3d1508ad"
6
+ ReadArticle: "0xd5bd0fbe"
8
7
  };
9
8
  function openArticle({ articleId }) {
10
9
  const interactionData = concatHex([
@@ -26,23 +25,46 @@ function readArticle({ articleId }) {
26
25
  interactionData
27
26
  };
28
27
  }
28
+ var PressInteractionEncoder = {
29
+ openArticle,
30
+ readArticle
31
+ };
32
+
33
+ // src/core/interactions/referralEncoder.ts
34
+ import { concatHex as concatHex2, pad as pad2 } from "viem";
35
+ var ReferralTypeSelector = "0x1E";
36
+ var ReferralActionsSelector = {
37
+ CreateLink: "0xb2c0f17c",
38
+ Referred: "0x010cc3b9"
39
+ };
40
+ function createLink() {
41
+ const interactionData = concatHex2([
42
+ ReferralActionsSelector.CreateLink,
43
+ "0x"
44
+ ]);
45
+ return {
46
+ handlerTypeDenominator: ReferralTypeSelector,
47
+ interactionData
48
+ };
49
+ }
29
50
  function referred({ referrer }) {
30
- const interactionData = concatHex([
31
- PressActionsSelector.Referred,
32
- pad(referrer, { size: 32 })
51
+ const interactionData = concatHex2([
52
+ ReferralActionsSelector.Referred,
53
+ pad2(referrer, { size: 32 })
33
54
  ]);
34
55
  return {
35
- handlerTypeDenominator: PressTypeSelector,
56
+ handlerTypeDenominator: ReferralTypeSelector,
36
57
  interactionData
37
58
  };
38
59
  }
39
- var PressInteractionEncoder = {
40
- openArticle,
41
- readArticle,
60
+ var ReferralInteractionEncoder = {
61
+ createLink,
42
62
  referred
43
63
  };
44
64
 
45
65
  export {
46
66
  PressActionsSelector,
47
- PressInteractionEncoder
67
+ PressInteractionEncoder,
68
+ ReferralActionsSelector,
69
+ ReferralInteractionEncoder
48
70
  };