@getpara/web-sdk 2.12.0 → 2.13.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/dist/ParaWeb.d.ts CHANGED
@@ -4,12 +4,12 @@ import { PortalRequest } from './types/onRamp.js';
4
4
  export declare class Para extends ParaCore {
5
5
  #private;
6
6
  farcasterSdk: any;
7
- isReady: boolean;
7
+ isSetup: boolean;
8
8
  isFarcasterMiniApp: boolean;
9
9
  private isFarcasterSetup;
10
10
  constructor(env: Environment | undefined, apiKey: string, opts?: ConstructorOpts);
11
11
  constructor(apiKey: string, opts?: ConstructorOpts);
12
- ready(): Promise<void>;
12
+ setup(): Promise<void>;
13
13
  protected portalEventListener: (event: MessageEvent<PortalRequest>) => Promise<void>;
14
14
  protected get toStringAdditions(): {
15
15
  isFarcasterMiniApp: boolean;
package/dist/ParaWeb.js CHANGED
@@ -20,7 +20,7 @@ class Para extends ParaCore {
20
20
  constructor(envOrApiKey, apiKeyOrOpts, opts) {
21
21
  super(envOrApiKey, apiKeyOrOpts, opts);
22
22
  this.farcasterSdk = void 0;
23
- this.isReady = false;
23
+ this.isSetup = false;
24
24
  this.isFarcasterMiniApp = false;
25
25
  this.isFarcasterSetup = false;
26
26
  this.portalEventListener = (event) => __async(this, null, function* () {
@@ -89,12 +89,8 @@ class Para extends ParaCore {
89
89
  {
90
90
  const { depositRequest } = event.data.payload;
91
91
  const onRampPurchase = (_f = this.onRampPopup) == null ? void 0 : _f.onRampPurchase;
92
- try {
93
- const { txHash, updatedOnRampPurchase } = yield offRampSend(this, onRampPurchase, depositRequest);
94
- payload = { onRampPurchase: updatedOnRampPurchase, txHash };
95
- } catch (e) {
96
- throw e;
97
- }
92
+ const { txHash, updatedOnRampPurchase } = yield offRampSend(this, onRampPurchase, depositRequest);
93
+ payload = { onRampPurchase: updatedOnRampPurchase, txHash };
98
94
  }
99
95
  break;
100
96
  case "SYNC_WALLETS":
@@ -126,10 +122,10 @@ class Para extends ParaCore {
126
122
  window.addEventListener("message", this.portalEventListener);
127
123
  }
128
124
  }
129
- ready() {
125
+ setup() {
130
126
  return __async(this, null, function* () {
131
127
  var _a, _b, _c, _d;
132
- if (!this.isReady) {
128
+ if (!this.isSetup) {
133
129
  if (!this.isFarcasterSetup) {
134
130
  try {
135
131
  this.farcasterSdk = (_b = (_a = yield import("@farcaster/miniapp-sdk")) == null ? void 0 : _a.sdk) != null ? _b : void 0;
@@ -152,7 +148,7 @@ class Para extends ParaCore {
152
148
  if (!this.isPortal() && Object.values(this.wallets).length > 0 && Object.values(this.wallets).every((wallet) => !wallet.partner)) {
153
149
  yield this.populateWalletAddresses();
154
150
  }
155
- this.isReady = true;
151
+ this.isSetup = true;
156
152
  }
157
153
  });
158
154
  }
@@ -1,10 +1,12 @@
1
- import { Ctx, PlatformUtils, SignatureRes, PopupType } from '@getpara/core-sdk';
1
+ import { Ctx, PlatformUtils, SignatureRes, PopupType, EventHandler, EventData } from '@getpara/core-sdk';
2
2
  import { LocalStorage } from './LocalStorage.js';
3
3
  import { SessionStorage } from './SessionStorage.js';
4
4
  import { BackupKitEmailProps, TWalletType, SDKType } from '@getpara/user-management-client';
5
5
  import { TPregenIdentifierType } from '@getpara/core-sdk';
6
6
  export declare class WebUtils implements PlatformUtils {
7
7
  sdkType: SDKType;
8
+ private eventHandlers;
9
+ private nativeHandlers;
8
10
  getPrivateKey(ctx: Ctx, userId: string, walletId: string, share: string, sessionCookie: string): Promise<string>;
9
11
  keygen(ctx: Ctx, userId: string, type: Exclude<TWalletType, 'SOLANA'>, secretKey: string | null, // should be acceptable as null in RN as we don't pre-gen them
10
12
  sessionCookie: string, emailProps?: BackupKitEmailProps): Promise<{
@@ -45,4 +47,21 @@ export declare class WebUtils implements PlatformUtils {
45
47
  type: PopupType;
46
48
  }): Promise<Window>;
47
49
  initializeWorker(ctx: Ctx): Promise<void>;
50
+ addEventListener<T = EventData>({ ctx, event, handler }: {
51
+ ctx: Ctx;
52
+ event: string;
53
+ handler: EventHandler<T>;
54
+ }): void;
55
+ removeEventListener<T = EventData>({ event, handler }: {
56
+ event: string;
57
+ handler: EventHandler<T>;
58
+ }): void;
59
+ postMessage({ data, target }: {
60
+ data: EventData;
61
+ target?: string;
62
+ }): void;
63
+ emitEvent<T = EventData>({ event, data }: {
64
+ event: string;
65
+ data: T;
66
+ }): void;
48
67
  }
package/dist/WebUtils.js CHANGED
@@ -8,15 +8,19 @@ import { SessionStorage } from "./SessionStorage.js";
8
8
  import { keygen, preKeygen, ed25519Keygen, ed25519PreKeygen, refresh, initializeWorker } from "./wallet/keygen.js";
9
9
  import { signMessage, sendTransaction, signTransaction, ed25519Sign } from "./wallet/signing.js";
10
10
  import { getPrivateKey } from "./wallet/privateKey.js";
11
+ import { validatePortalOrigin } from "./utils/validatePortalOrigin.js";
11
12
  class WebUtils {
12
13
  constructor() {
13
14
  this.sdkType = "WEB";
15
+ this.eventHandlers = /* @__PURE__ */ new Map();
16
+ this.nativeHandlers = /* @__PURE__ */ new Map();
14
17
  this.localStorage = new LocalStorage();
15
18
  this.sessionStorage = new SessionStorage();
16
19
  this.secureStorage = null;
17
20
  this.isSyncStorage = true;
18
21
  this.disableProviderModal = false;
19
22
  }
23
+ // Store native handlers separately
20
24
  getPrivateKey(ctx, userId, walletId, share, sessionCookie) {
21
25
  return getPrivateKey(ctx, userId, walletId, share, sessionCookie);
22
26
  }
@@ -117,6 +121,56 @@ class WebUtils {
117
121
  return initializeWorker(ctx);
118
122
  });
119
123
  }
124
+ addEventListener({ ctx, event, handler }) {
125
+ if (!this.eventHandlers.has(event)) {
126
+ this.eventHandlers.set(event, /* @__PURE__ */ new Set());
127
+ const messageHandler = (e) => {
128
+ var _a;
129
+ if (!validatePortalOrigin(e, ctx)) {
130
+ return;
131
+ }
132
+ if (e.data && typeof e.data === "object" && "type" in e.data) {
133
+ const eventData = e.data;
134
+ if (eventData.type === event || event === "message") {
135
+ (_a = this.eventHandlers.get(event)) == null ? void 0 : _a.forEach((h) => h(e.data));
136
+ }
137
+ }
138
+ };
139
+ if (typeof window !== "undefined") {
140
+ window.addEventListener("message", messageHandler);
141
+ }
142
+ this.nativeHandlers.set(event, messageHandler);
143
+ }
144
+ this.eventHandlers.get(event).add(handler);
145
+ }
146
+ removeEventListener({ event, handler }) {
147
+ const handlers = this.eventHandlers.get(event);
148
+ if (handlers) {
149
+ handlers.delete(handler);
150
+ if (handlers.size === 0) {
151
+ const nativeHandler = this.nativeHandlers.get(event);
152
+ if (nativeHandler) {
153
+ if (typeof window !== "undefined") {
154
+ window.removeEventListener("message", nativeHandler);
155
+ }
156
+ this.nativeHandlers.delete(event);
157
+ }
158
+ this.eventHandlers.delete(event);
159
+ }
160
+ }
161
+ }
162
+ postMessage({ data, target = "*" }) {
163
+ if (typeof window !== "undefined") {
164
+ window.postMessage(data, target);
165
+ }
166
+ }
167
+ emitEvent({ event, data }) {
168
+ const eventData = {
169
+ type: event,
170
+ payload: data
171
+ };
172
+ this.postMessage({ data: eventData });
173
+ }
120
174
  }
121
175
  export {
122
176
  WebUtils
@@ -0,0 +1,2 @@
1
+ import { Ctx } from '@getpara/core-sdk';
2
+ export declare const validatePortalOrigin: (event: MessageEvent, paraCtx: Ctx) => boolean;
@@ -0,0 +1,19 @@
1
+ "use client";
2
+ import "../chunk-YJOFEY2L.js";
3
+ import { getPortalBaseURL } from "@getpara/core-sdk";
4
+ const validatePortalOrigin = (event, paraCtx) => {
5
+ const portalBase = getPortalBaseURL(paraCtx);
6
+ const portalLocalBase = getPortalBaseURL(paraCtx, true);
7
+ const normalizedOrigin = event.origin.replace("usecapsule", "getpara");
8
+ if (paraCtx.portalUrlOverride) {
9
+ const overrideOrigin = new URL(paraCtx.portalUrlOverride).origin;
10
+ if (normalizedOrigin === overrideOrigin) return true;
11
+ }
12
+ if (normalizedOrigin !== portalBase && normalizedOrigin !== portalLocalBase) {
13
+ return false;
14
+ }
15
+ return true;
16
+ };
17
+ export {
18
+ validatePortalOrigin
19
+ };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@getpara/web-sdk",
3
- "version": "2.12.0",
3
+ "version": "2.13.0",
4
4
  "dependencies": {
5
- "@getpara/core-sdk": "2.12.0",
6
- "@getpara/user-management-client": "2.12.0",
5
+ "@getpara/core-sdk": "2.13.0",
6
+ "@getpara/user-management-client": "2.13.0",
7
7
  "base64url": "^3.0.1",
8
8
  "buffer": "6.0.3",
9
9
  "cbor-web": "^9.0.2",
@@ -28,7 +28,7 @@
28
28
  "dist",
29
29
  "package.json"
30
30
  ],
31
- "gitHead": "09c114d148f7a0c2ad250d6346a76aa3b332b2d4",
31
+ "gitHead": "75ac5d1ebe87366c23bb4e485481deb1b593e299",
32
32
  "main": "dist/index.js",
33
33
  "scripts": {
34
34
  "build": "rm -rf dist && yarn typegen && node ./scripts/build.mjs && yarn post-build",