zh-web-sdk 2.10.13 → 2.11.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zh-web-sdk",
3
- "version": "2.10.13",
3
+ "version": "2.11.0",
4
4
  "private": false,
5
5
  "description": "ZeroHash Web SDK",
6
6
  "homepage": "https://github.com/seedcx/zh-web-sdk",
@@ -256,47 +256,55 @@ const AppContainer = ({
256
256
  }
257
257
  }, []);
258
258
 
259
- const eventHandler = useCallback(
259
+ const worldAppEventHandler = useCallback(
260
260
  async (event: MessageEvent) => {
261
- if (event.data?.type === "MINIKIT_PAY_COMMAND") {
262
- if (!MiniKit.isInstalled()) {
263
- console.log("Minikit not installed. Installing minikit under sdk");
264
- MiniKit.install();
265
- }
266
- if (MiniKit.isInstalled()) {
267
- console.log("Minikit installed. Processing payment");
268
- const payload = event.data.payload;
269
- try {
270
- const minikitPayload: PayCommandInput = {
271
- reference: payload.reference,
272
- to: payload.to,
273
- tokens: [
274
- {
275
- symbol: payload.token as Tokens,
276
- token_amount: tokenToDecimals(
277
- Number(payload.amount),
278
- payload.token
279
- ).toString(),
280
- },
281
- ],
282
- description: payload.description,
283
- };
284
- const { finalPayload } =
285
- await MiniKit.commandsAsync.pay(minikitPayload);
261
+ // Does nothing if not running inside the World App
262
+ if (!isInsideWorldApp()) {
263
+ return;
264
+ }
265
+
266
+ const zhAppsURL = new URL(zeroHashAppURL);
267
+ if (event.origin !== zhAppsURL.origin) {
268
+ console.warn("Received message from unknown origin:", event.origin);
269
+ return;
270
+ }
286
271
 
287
- iRef.current?.contentWindow?.postMessage(
288
- { type: "MINIKIT_PAY_COMMAND_RESULT", result: finalPayload },
289
- zeroHashAppURL
290
- );
291
- } catch (error) {
292
- iRef.current?.contentWindow?.postMessage(
272
+ if (!MiniKit.isInstalled()) {
273
+ const result = MiniKit.install();
274
+ console.log("MiniKit not installed, installing now", { result });
275
+ }
276
+ if (event.data?.type === "MINIKIT_PAY_COMMAND") {
277
+ const payload = event.data.payload;
278
+ try {
279
+ const minikitPayload: PayCommandInput = {
280
+ reference: payload.reference,
281
+ to: payload.to,
282
+ tokens: [
293
283
  {
294
- type: "MINIKIT_PAY_COMMAND_RESULT",
295
- result: { error: String(error), status: "error" },
284
+ symbol: payload.token as Tokens,
285
+ token_amount: tokenToDecimals(
286
+ Number(payload.amount),
287
+ payload.token
288
+ ).toString(),
296
289
  },
297
- zeroHashAppURL
298
- );
299
- }
290
+ ],
291
+ description: payload.description,
292
+ };
293
+ const { finalPayload } =
294
+ await MiniKit.commandsAsync.pay(minikitPayload);
295
+
296
+ iRef.current?.contentWindow?.postMessage(
297
+ { type: "MINIKIT_PAY_COMMAND_RESULT", payload: { finalPayload } },
298
+ zeroHashAppURL
299
+ );
300
+ } catch (error) {
301
+ iRef.current?.contentWindow?.postMessage(
302
+ {
303
+ type: "MINIKIT_PAY_COMMAND_RESULT",
304
+ result: { error: String(error), status: "error" },
305
+ },
306
+ zeroHashAppURL
307
+ );
300
308
  }
301
309
  }
302
310
  },
@@ -304,9 +312,9 @@ const AppContainer = ({
304
312
  );
305
313
 
306
314
  useEffect(() => {
307
- window.addEventListener("message", eventHandler);
308
- return () => window.removeEventListener("message", eventHandler);
309
- }, [eventHandler, zeroHashAppURL]);
315
+ window.addEventListener("message", worldAppEventHandler);
316
+ return () => window.removeEventListener("message", worldAppEventHandler);
317
+ }, [worldAppEventHandler, zeroHashAppURL]);
310
318
 
311
319
  const iframeURL = new URL(zeroHashAppURL);
312
320
  iframeURL.searchParams.set("name", appLoadTime.toString());