@frak-labs/nexus-sdk 0.0.3-alpha → 0.0.6

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.
@@ -3,75 +3,13 @@
3
3
 
4
4
 
5
5
 
6
- var _chunkN4N55SQ6cjs = require('../../chunk-N4N55SQ6.cjs');
7
6
 
8
- // src/core/actions/getUnlockOptions.ts
9
- function getArticleUnlockOptions(client, { articleId }) {
10
- return client.request({
11
- method: "frak_getArticleUnlockOptions",
12
- params: [client.config.contentId, articleId]
13
- });
14
- }
7
+ var _chunk3LF3FGI6cjs = require('../../chunk-3LF3FGI6.cjs');
8
+ require('../../chunk-2XUJYDD3.cjs');
15
9
 
16
- // src/core/actions/watchUnlockStatus.ts
17
- function watchUnlockStatus(client, { articleId }, callback) {
18
- return client.listenerRequest(
19
- {
20
- method: "frak_listenToArticleUnlockStatus",
21
- params: [client.config.contentId, articleId]
22
- },
23
- callback
24
- );
25
- }
26
10
 
27
- // src/core/actions/watchWalletStatus.ts
28
- function watchWalletStatus(client, callback) {
29
- return client.listenerRequest(
30
- {
31
- method: "frak_listenToWalletStatus"
32
- },
33
- callback
34
- );
35
- }
36
11
 
37
- // src/core/actions/startUnlock.ts
38
- async function getStartArticleUnlockUrl(config, params) {
39
- const { compressed, compressedHash } = await _chunkN4N55SQ6cjs.hashAndCompressData.call(void 0,
40
- {
41
- method: "frak_startArticleUnlock",
42
- params: {
43
- ...params,
44
- contentId: config.contentId,
45
- contentTitle: config.contentTitle
46
- }
47
- },
48
- _chunkN4N55SQ6cjs.redirectRequestKeyProvider
49
- );
50
- const outputUrl = new URL(config.walletUrl);
51
- outputUrl.pathname = "/paywall";
52
- outputUrl.searchParams.set("params", encodeURIComponent(compressed));
53
- outputUrl.searchParams.set("hash", encodeURIComponent(compressedHash));
54
- return outputUrl.toString();
55
- }
56
- async function decodeStartUnlockReturn({
57
- result,
58
- hash
59
- }) {
60
- const keyProvider = _chunkN4N55SQ6cjs.getRedirectResponseResponseKeyProvider.call(void 0,
61
- "frak_startArticleUnlock"
62
- );
63
- return _chunkN4N55SQ6cjs.decompressDataAndCheckHash.call(void 0,
64
- {
65
- compressed: decodeURIComponent(result),
66
- compressedHash: decodeURIComponent(hash)
67
- },
68
- keyProvider
69
- );
70
- }
71
12
 
72
13
 
73
14
 
74
-
75
-
76
-
77
- exports.decodeStartUnlockReturn = decodeStartUnlockReturn; exports.getArticleUnlockOptions = getArticleUnlockOptions; exports.getStartArticleUnlockUrl = getStartArticleUnlockUrl; exports.watchUnlockStatus = watchUnlockStatus; exports.watchWalletStatus = watchWalletStatus;
15
+ exports.decodeStartUnlockReturn = _chunk3LF3FGI6cjs.decodeStartUnlockReturn; exports.getArticleUnlockOptions = _chunk3LF3FGI6cjs.getArticleUnlockOptions; exports.getStartArticleUnlockUrl = _chunk3LF3FGI6cjs.getStartArticleUnlockUrl; exports.watchUnlockStatus = _chunk3LF3FGI6cjs.watchUnlockStatus; exports.watchWalletStatus = _chunk3LF3FGI6cjs.watchWalletStatus;
@@ -1,41 +1,8 @@
1
- import { Hex } from 'viem';
2
- import { N as NexusClient, A as ArticleUnlockStatusReturnType, W as WalletStatusReturnType, a as NexusWalletSdkConfig, S as StartArticleUnlockReturnType, b as StartArticleUnlockParams } from '../../client-CkvisXxD.cjs';
1
+ export { G as GetUnlockOptionsParams, W as WatchUnlockStatusParams, g as getArticleUnlockOptions, w as watchUnlockStatus } from '../../watchUnlockStatus-B4kRztOM.cjs';
2
+ import { N as NexusClient, W as WalletStatusReturnType, a as NexusWalletSdkConfig, S as StartArticleUnlockReturnType, b as StartArticleUnlockParams } from '../../client-DWLJ8zR-.cjs';
3
+ import 'viem';
3
4
  import 'viem/chains';
4
5
 
5
- /**
6
- * Type used to get the unlock options
7
- */
8
- type GetUnlockOptionsParams = {
9
- articleId: Hex;
10
- };
11
- /**
12
- * Function used to fetch the unlock option for the given client
13
- * @param client
14
- * @param articleId
15
- */
16
- declare function getArticleUnlockOptions(client: NexusClient, { articleId }: GetUnlockOptionsParams): Promise<Readonly<{
17
- prices: {
18
- index: number;
19
- unlockDurationInSec: number;
20
- frkAmount: `0x${string}`;
21
- isUserAccessible: boolean | null;
22
- }[];
23
- }>>;
24
-
25
- /**
26
- * Type used to get the unlock options
27
- */
28
- type WatchUnlockStatusParams = {
29
- articleId: Hex;
30
- };
31
- /**
32
- * Function used to watch a current article unlock status
33
- * @param client
34
- * @param articleId
35
- * @param callback
36
- */
37
- declare function watchUnlockStatus(client: NexusClient, { articleId }: WatchUnlockStatusParams, callback: (status: ArticleUnlockStatusReturnType) => void): Promise<void>;
38
-
39
6
  /**
40
7
  * Function used to watch the current nexus wallet status
41
8
  * @param client
@@ -60,4 +27,4 @@ declare function decodeStartUnlockReturn({ result, hash, }: {
60
27
  validationHash: string;
61
28
  }>>;
62
29
 
63
- export { decodeStartUnlockReturn, getArticleUnlockOptions, getStartArticleUnlockUrl, watchUnlockStatus, watchWalletStatus };
30
+ export { decodeStartUnlockReturn, getStartArticleUnlockUrl, watchWalletStatus };
@@ -1,41 +1,8 @@
1
- import { Hex } from 'viem';
2
- import { N as NexusClient, A as ArticleUnlockStatusReturnType, W as WalletStatusReturnType, a as NexusWalletSdkConfig, S as StartArticleUnlockReturnType, b as StartArticleUnlockParams } from '../../client-CkvisXxD.js';
1
+ export { G as GetUnlockOptionsParams, W as WatchUnlockStatusParams, g as getArticleUnlockOptions, w as watchUnlockStatus } from '../../watchUnlockStatus--7dFtOJr.js';
2
+ import { N as NexusClient, W as WalletStatusReturnType, a as NexusWalletSdkConfig, S as StartArticleUnlockReturnType, b as StartArticleUnlockParams } from '../../client-DWLJ8zR-.js';
3
+ import 'viem';
3
4
  import 'viem/chains';
4
5
 
5
- /**
6
- * Type used to get the unlock options
7
- */
8
- type GetUnlockOptionsParams = {
9
- articleId: Hex;
10
- };
11
- /**
12
- * Function used to fetch the unlock option for the given client
13
- * @param client
14
- * @param articleId
15
- */
16
- declare function getArticleUnlockOptions(client: NexusClient, { articleId }: GetUnlockOptionsParams): Promise<Readonly<{
17
- prices: {
18
- index: number;
19
- unlockDurationInSec: number;
20
- frkAmount: `0x${string}`;
21
- isUserAccessible: boolean | null;
22
- }[];
23
- }>>;
24
-
25
- /**
26
- * Type used to get the unlock options
27
- */
28
- type WatchUnlockStatusParams = {
29
- articleId: Hex;
30
- };
31
- /**
32
- * Function used to watch a current article unlock status
33
- * @param client
34
- * @param articleId
35
- * @param callback
36
- */
37
- declare function watchUnlockStatus(client: NexusClient, { articleId }: WatchUnlockStatusParams, callback: (status: ArticleUnlockStatusReturnType) => void): Promise<void>;
38
-
39
6
  /**
40
7
  * Function used to watch the current nexus wallet status
41
8
  * @param client
@@ -60,4 +27,4 @@ declare function decodeStartUnlockReturn({ result, hash, }: {
60
27
  validationHash: string;
61
28
  }>>;
62
29
 
63
- export { decodeStartUnlockReturn, getArticleUnlockOptions, getStartArticleUnlockUrl, watchUnlockStatus, watchWalletStatus };
30
+ export { decodeStartUnlockReturn, getStartArticleUnlockUrl, watchWalletStatus };
@@ -1,73 +1,11 @@
1
1
  import {
2
- decompressDataAndCheckHash,
3
- getRedirectResponseResponseKeyProvider,
4
- hashAndCompressData,
5
- redirectRequestKeyProvider
6
- } from "../../chunk-6ZFDRVV6.js";
7
-
8
- // src/core/actions/getUnlockOptions.ts
9
- function getArticleUnlockOptions(client, { articleId }) {
10
- return client.request({
11
- method: "frak_getArticleUnlockOptions",
12
- params: [client.config.contentId, articleId]
13
- });
14
- }
15
-
16
- // src/core/actions/watchUnlockStatus.ts
17
- function watchUnlockStatus(client, { articleId }, callback) {
18
- return client.listenerRequest(
19
- {
20
- method: "frak_listenToArticleUnlockStatus",
21
- params: [client.config.contentId, articleId]
22
- },
23
- callback
24
- );
25
- }
26
-
27
- // src/core/actions/watchWalletStatus.ts
28
- function watchWalletStatus(client, callback) {
29
- return client.listenerRequest(
30
- {
31
- method: "frak_listenToWalletStatus"
32
- },
33
- callback
34
- );
35
- }
36
-
37
- // src/core/actions/startUnlock.ts
38
- async function getStartArticleUnlockUrl(config, params) {
39
- const { compressed, compressedHash } = await hashAndCompressData(
40
- {
41
- method: "frak_startArticleUnlock",
42
- params: {
43
- ...params,
44
- contentId: config.contentId,
45
- contentTitle: config.contentTitle
46
- }
47
- },
48
- redirectRequestKeyProvider
49
- );
50
- const outputUrl = new URL(config.walletUrl);
51
- outputUrl.pathname = "/paywall";
52
- outputUrl.searchParams.set("params", encodeURIComponent(compressed));
53
- outputUrl.searchParams.set("hash", encodeURIComponent(compressedHash));
54
- return outputUrl.toString();
55
- }
56
- async function decodeStartUnlockReturn({
57
- result,
58
- hash
59
- }) {
60
- const keyProvider = getRedirectResponseResponseKeyProvider(
61
- "frak_startArticleUnlock"
62
- );
63
- return decompressDataAndCheckHash(
64
- {
65
- compressed: decodeURIComponent(result),
66
- compressedHash: decodeURIComponent(hash)
67
- },
68
- keyProvider
69
- );
70
- }
2
+ decodeStartUnlockReturn,
3
+ getArticleUnlockOptions,
4
+ getStartArticleUnlockUrl,
5
+ watchUnlockStatus,
6
+ watchWalletStatus
7
+ } from "../../chunk-DI2REDPX.js";
8
+ import "../../chunk-5QWG35A2.js";
71
9
  export {
72
10
  decodeStartUnlockReturn,
73
11
  getArticleUnlockOptions,
@@ -1,179 +1,22 @@
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
-
10
- var _chunkN4N55SQ6cjs = require('../chunk-N4N55SQ6.cjs');
11
-
12
- // src/core/utils/Deferred.ts
13
- var Deferred = class {
14
- constructor() {
15
- _chunkN4N55SQ6cjs.__publicField.call(void 0, this, "_promise");
16
- _chunkN4N55SQ6cjs.__publicField.call(void 0, this, "_resolve");
17
- _chunkN4N55SQ6cjs.__publicField.call(void 0, this, "_reject");
18
- _chunkN4N55SQ6cjs.__publicField.call(void 0, this, "resolve", (value) => {
19
- _optionalChain([this, 'access', _ => _._resolve, 'optionalCall', _2 => _2(value)]);
20
- });
21
- _chunkN4N55SQ6cjs.__publicField.call(void 0, this, "reject", (reason) => {
22
- _optionalChain([this, 'access', _3 => _3._reject, 'optionalCall', _4 => _4(reason)]);
23
- });
24
- this._promise = new Promise((resolve, reject) => {
25
- this._resolve = resolve;
26
- this._reject = reject;
27
- });
28
- }
29
- get promise() {
30
- return this._promise;
31
- }
32
- };
33
-
34
- // src/core/clients/transports/iframeChannelManager.ts
35
- function createIFrameChannelManager() {
36
- const channels = /* @__PURE__ */ new Map();
37
- return {
38
- // TODO: Better id system?? uid stuff?
39
- createChannel: (resolver) => {
40
- const id = Math.random().toString(36).substring(7);
41
- channels.set(id, resolver);
42
- return id;
43
- },
44
- getRpcResolver: (id) => channels.get(id),
45
- removeChannel: (id) => channels.delete(id),
46
- destroy: () => channels.clear()
47
- };
48
- }
49
-
50
- // src/core/clients/transports/iframeMessageHandler.ts
51
- function createIFrameMessageHandler({
52
- nexusWalletUrl,
53
- iframe,
54
- channelManager
55
- }) {
56
- if (typeof window === "undefined") {
57
- throw new Error("iframe client should be used in the browser");
58
- }
59
- if (!iframe.contentWindow) {
60
- throw new Error("The iframe does not have a content window");
61
- }
62
- const contentWindow = iframe.contentWindow;
63
- const isConnected = new Deferred();
64
- const msgHandler = async (event) => {
65
- if (!event.origin) {
66
- return;
67
- }
68
- if (new URL(event.origin).origin.toLowerCase() !== new URL(nexusWalletUrl).origin.toLowerCase()) {
69
- return;
70
- }
71
- if ("lifecycle" in event.data) {
72
- isConnected.resolve(event.data.lifecycle === "connected");
73
- return;
74
- }
75
- const channel = event.data.id;
76
- const resolver = channelManager.getRpcResolver(channel);
77
- if (!resolver) {
78
- return;
79
- }
80
- await resolver(event.data);
81
- };
82
- window.addEventListener("message", msgHandler);
83
- const sendEvent = (message) => {
84
- contentWindow.postMessage(message, {
85
- targetOrigin: nexusWalletUrl
86
- });
87
- };
88
- const cleanup = () => {
89
- window.removeEventListener("message", msgHandler);
90
- };
91
- return {
92
- isConnected: isConnected.promise,
93
- sendEvent,
94
- cleanup
95
- };
96
- }
97
-
98
- // src/core/clients/createIFrameNexusClient.ts
99
- function createIFrameNexusClient({
100
- config,
101
- iframe
102
- }) {
103
- const channelManager = createIFrameChannelManager();
104
- const messageHandler = createIFrameMessageHandler({
105
- nexusWalletUrl: config.walletUrl,
106
- iframe,
107
- channelManager
108
- });
109
- const request = async (args) => {
110
- const isConnected = await messageHandler.isConnected;
111
- if (!isConnected) {
112
- throw new Error("The iframe provider isn't connected yet");
113
- }
114
- const result = new Deferred();
115
- const resultCompressionKeyProvider = _chunkN4N55SQ6cjs.getIFrameResponseKeyProvider.call(void 0, args);
116
- const channelId = channelManager.createChannel(async (message) => {
117
- const decompressed = await _chunkN4N55SQ6cjs.decompressDataAndCheckHash.call(void 0,
118
- message.data,
119
- resultCompressionKeyProvider
120
- );
121
- result.resolve(decompressed);
122
- channelManager.removeChannel(channelId);
123
- });
124
- const compressedMessage = await _chunkN4N55SQ6cjs.hashAndCompressData.call(void 0,
125
- args,
126
- _chunkN4N55SQ6cjs.iFrameRequestKeyProvider
127
- );
128
- messageHandler.sendEvent({
129
- id: channelId,
130
- topic: args.method,
131
- data: compressedMessage
132
- });
133
- return result.promise;
134
- };
135
- const listenerRequest = async (args, callback) => {
136
- const isConnected = await messageHandler.isConnected;
137
- if (!isConnected) {
138
- throw new Error("The iframe provider isn't connected yet");
139
- }
140
- const resultCompressionKeyProvider = _chunkN4N55SQ6cjs.getIFrameResponseKeyProvider.call(void 0, args);
141
- const channelId = channelManager.createChannel(async (message) => {
142
- const decompressed = await _chunkN4N55SQ6cjs.decompressDataAndCheckHash.call(void 0,
143
- message.data,
144
- resultCompressionKeyProvider
145
- );
146
- callback(decompressed);
147
- });
148
- const compressedMessage = await _chunkN4N55SQ6cjs.hashAndCompressData.call(void 0,
149
- args,
150
- _chunkN4N55SQ6cjs.iFrameRequestKeyProvider
151
- );
152
- messageHandler.sendEvent({
153
- id: channelId,
154
- topic: args.method,
155
- data: compressedMessage
156
- });
157
- };
158
- const destroy = async () => {
159
- channelManager.destroy();
160
- messageHandler.cleanup();
161
- };
162
- return {
163
- config,
164
- waitForConnection: messageHandler.isConnected,
165
- request,
166
- listenerRequest,
167
- destroy
168
- };
169
- }
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
- exports.createIFrameNexusClient = createIFrameNexusClient; exports.createIframe = _chunkN4N55SQ6cjs.createIframe; exports.decompressDataAndCheckHash = _chunkN4N55SQ6cjs.decompressDataAndCheckHash; exports.getIFrameResponseKeyProvider = _chunkN4N55SQ6cjs.getIFrameResponseKeyProvider; exports.getRedirectResponseResponseKeyProvider = _chunkN4N55SQ6cjs.getRedirectResponseResponseKeyProvider; exports.hashAndCompressData = _chunkN4N55SQ6cjs.hashAndCompressData; exports.iFrameRequestKeyProvider = _chunkN4N55SQ6cjs.iFrameRequestKeyProvider; exports.redirectRequestKeyProvider = _chunkN4N55SQ6cjs.redirectRequestKeyProvider;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkJXQKTLEEcjs = require('../chunk-JXQKTLEE.cjs');
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+ var _chunk2XUJYDD3cjs = require('../chunk-2XUJYDD3.cjs');
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+ exports.createIFrameNexusClient = _chunkJXQKTLEEcjs.createIFrameNexusClient; exports.createIframe = _chunk2XUJYDD3cjs.createIframe; exports.decompressDataAndCheckHash = _chunk2XUJYDD3cjs.decompressDataAndCheckHash; exports.getIFrameResponseKeyProvider = _chunk2XUJYDD3cjs.getIFrameResponseKeyProvider; exports.getRedirectResponseResponseKeyProvider = _chunk2XUJYDD3cjs.getRedirectResponseResponseKeyProvider; exports.hashAndCompressData = _chunk2XUJYDD3cjs.hashAndCompressData; exports.iFrameRequestKeyProvider = _chunk2XUJYDD3cjs.iFrameRequestKeyProvider; exports.redirectRequestKeyProvider = _chunk2XUJYDD3cjs.redirectRequestKeyProvider;
@@ -1,10 +1,10 @@
1
- import { a as NexusWalletSdkConfig, N as NexusClient, E as ExtractedParametersFromRpc, I as IFrameRpcSchema, c as ExtractedReturnTypeFromRpc, R as RedirectRpcSchema } from '../client-CkvisXxD.cjs';
2
- export { A as ArticleUnlockStatusReturnType, f as IFrameEvent, e as IFrameRpcEvent, d as IFrameTransport, P as PaidArticleUnlockPrice, b as StartArticleUnlockParams, S as StartArticleUnlockReturnType, U as UnlockOptionsReturnType, W as WalletStatusReturnType } from '../client-CkvisXxD.cjs';
1
+ import { a as NexusWalletSdkConfig, N as NexusClient, E as ExtractedParametersFromRpc, I as IFrameRpcSchema, c as ExtractedReturnTypeFromRpc, R as RedirectRpcSchema } from '../client-DWLJ8zR-.cjs';
2
+ export { A as ArticleUnlockStatusReturnType, f as IFrameEvent, e as IFrameRpcEvent, d as IFrameTransport, P as PaidArticleUnlockPrice, b as StartArticleUnlockParams, S as StartArticleUnlockReturnType, U as UnlockOptionsReturnType, W as WalletStatusReturnType } from '../client-DWLJ8zR-.cjs';
3
3
  import 'viem';
4
4
  import 'viem/chains';
5
5
 
6
6
  /**
7
- * Create a new iframe Frak client
7
+ * Create a new iframe Nexus client
8
8
  */
9
9
  declare function createIFrameNexusClient({ config, iframe, }: {
10
10
  config: NexusWalletSdkConfig;
@@ -1,10 +1,10 @@
1
- import { a as NexusWalletSdkConfig, N as NexusClient, E as ExtractedParametersFromRpc, I as IFrameRpcSchema, c as ExtractedReturnTypeFromRpc, R as RedirectRpcSchema } from '../client-CkvisXxD.js';
2
- export { A as ArticleUnlockStatusReturnType, f as IFrameEvent, e as IFrameRpcEvent, d as IFrameTransport, P as PaidArticleUnlockPrice, b as StartArticleUnlockParams, S as StartArticleUnlockReturnType, U as UnlockOptionsReturnType, W as WalletStatusReturnType } from '../client-CkvisXxD.js';
1
+ import { a as NexusWalletSdkConfig, N as NexusClient, E as ExtractedParametersFromRpc, I as IFrameRpcSchema, c as ExtractedReturnTypeFromRpc, R as RedirectRpcSchema } from '../client-DWLJ8zR-.js';
2
+ export { A as ArticleUnlockStatusReturnType, f as IFrameEvent, e as IFrameRpcEvent, d as IFrameTransport, P as PaidArticleUnlockPrice, b as StartArticleUnlockParams, S as StartArticleUnlockReturnType, U as UnlockOptionsReturnType, W as WalletStatusReturnType } from '../client-DWLJ8zR-.js';
3
3
  import 'viem';
4
4
  import 'viem/chains';
5
5
 
6
6
  /**
7
- * Create a new iframe Frak client
7
+ * Create a new iframe Nexus client
8
8
  */
9
9
  declare function createIFrameNexusClient({ config, iframe, }: {
10
10
  config: NexusWalletSdkConfig;
@@ -1,5 +1,7 @@
1
1
  import {
2
- __publicField,
2
+ createIFrameNexusClient
3
+ } from "../chunk-OXP3VK26.js";
4
+ import {
3
5
  createIframe,
4
6
  decompressDataAndCheckHash,
5
7
  getIFrameResponseKeyProvider,
@@ -7,166 +9,7 @@ import {
7
9
  hashAndCompressData,
8
10
  iFrameRequestKeyProvider,
9
11
  redirectRequestKeyProvider
10
- } from "../chunk-6ZFDRVV6.js";
11
-
12
- // src/core/utils/Deferred.ts
13
- var Deferred = class {
14
- constructor() {
15
- __publicField(this, "_promise");
16
- __publicField(this, "_resolve");
17
- __publicField(this, "_reject");
18
- __publicField(this, "resolve", (value) => {
19
- this._resolve?.(value);
20
- });
21
- __publicField(this, "reject", (reason) => {
22
- this._reject?.(reason);
23
- });
24
- this._promise = new Promise((resolve, reject) => {
25
- this._resolve = resolve;
26
- this._reject = reject;
27
- });
28
- }
29
- get promise() {
30
- return this._promise;
31
- }
32
- };
33
-
34
- // src/core/clients/transports/iframeChannelManager.ts
35
- function createIFrameChannelManager() {
36
- const channels = /* @__PURE__ */ new Map();
37
- return {
38
- // TODO: Better id system?? uid stuff?
39
- createChannel: (resolver) => {
40
- const id = Math.random().toString(36).substring(7);
41
- channels.set(id, resolver);
42
- return id;
43
- },
44
- getRpcResolver: (id) => channels.get(id),
45
- removeChannel: (id) => channels.delete(id),
46
- destroy: () => channels.clear()
47
- };
48
- }
49
-
50
- // src/core/clients/transports/iframeMessageHandler.ts
51
- function createIFrameMessageHandler({
52
- nexusWalletUrl,
53
- iframe,
54
- channelManager
55
- }) {
56
- if (typeof window === "undefined") {
57
- throw new Error("iframe client should be used in the browser");
58
- }
59
- if (!iframe.contentWindow) {
60
- throw new Error("The iframe does not have a content window");
61
- }
62
- const contentWindow = iframe.contentWindow;
63
- const isConnected = new Deferred();
64
- const msgHandler = async (event) => {
65
- if (!event.origin) {
66
- return;
67
- }
68
- if (new URL(event.origin).origin.toLowerCase() !== new URL(nexusWalletUrl).origin.toLowerCase()) {
69
- return;
70
- }
71
- if ("lifecycle" in event.data) {
72
- isConnected.resolve(event.data.lifecycle === "connected");
73
- return;
74
- }
75
- const channel = event.data.id;
76
- const resolver = channelManager.getRpcResolver(channel);
77
- if (!resolver) {
78
- return;
79
- }
80
- await resolver(event.data);
81
- };
82
- window.addEventListener("message", msgHandler);
83
- const sendEvent = (message) => {
84
- contentWindow.postMessage(message, {
85
- targetOrigin: nexusWalletUrl
86
- });
87
- };
88
- const cleanup = () => {
89
- window.removeEventListener("message", msgHandler);
90
- };
91
- return {
92
- isConnected: isConnected.promise,
93
- sendEvent,
94
- cleanup
95
- };
96
- }
97
-
98
- // src/core/clients/createIFrameNexusClient.ts
99
- function createIFrameNexusClient({
100
- config,
101
- iframe
102
- }) {
103
- const channelManager = createIFrameChannelManager();
104
- const messageHandler = createIFrameMessageHandler({
105
- nexusWalletUrl: config.walletUrl,
106
- iframe,
107
- channelManager
108
- });
109
- const request = async (args) => {
110
- const isConnected = await messageHandler.isConnected;
111
- if (!isConnected) {
112
- throw new Error("The iframe provider isn't connected yet");
113
- }
114
- const result = new Deferred();
115
- const resultCompressionKeyProvider = getIFrameResponseKeyProvider(args);
116
- const channelId = channelManager.createChannel(async (message) => {
117
- const decompressed = await decompressDataAndCheckHash(
118
- message.data,
119
- resultCompressionKeyProvider
120
- );
121
- result.resolve(decompressed);
122
- channelManager.removeChannel(channelId);
123
- });
124
- const compressedMessage = await hashAndCompressData(
125
- args,
126
- iFrameRequestKeyProvider
127
- );
128
- messageHandler.sendEvent({
129
- id: channelId,
130
- topic: args.method,
131
- data: compressedMessage
132
- });
133
- return result.promise;
134
- };
135
- const listenerRequest = async (args, callback) => {
136
- const isConnected = await messageHandler.isConnected;
137
- if (!isConnected) {
138
- throw new Error("The iframe provider isn't connected yet");
139
- }
140
- const resultCompressionKeyProvider = getIFrameResponseKeyProvider(args);
141
- const channelId = channelManager.createChannel(async (message) => {
142
- const decompressed = await decompressDataAndCheckHash(
143
- message.data,
144
- resultCompressionKeyProvider
145
- );
146
- callback(decompressed);
147
- });
148
- const compressedMessage = await hashAndCompressData(
149
- args,
150
- iFrameRequestKeyProvider
151
- );
152
- messageHandler.sendEvent({
153
- id: channelId,
154
- topic: args.method,
155
- data: compressedMessage
156
- });
157
- };
158
- const destroy = async () => {
159
- channelManager.destroy();
160
- messageHandler.cleanup();
161
- };
162
- return {
163
- config,
164
- waitForConnection: messageHandler.isConnected,
165
- request,
166
- listenerRequest,
167
- destroy
168
- };
169
- }
12
+ } from "../chunk-5QWG35A2.js";
170
13
  export {
171
14
  createIFrameNexusClient,
172
15
  createIframe,