zh-web-sdk 1.1.0 → 2.0.1

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/.eslintrc.js CHANGED
@@ -8,7 +8,5 @@ module.exports = {
8
8
  parser: '@typescript-eslint/parser',
9
9
  parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
10
10
  plugins: ['react-refresh'],
11
- rules: {
12
- 'react-refresh/only-export-components': 'warn',
13
- },
11
+ rules: {},
14
12
  }
package/README.md CHANGED
@@ -1,23 +1,59 @@
1
1
  # ZeroHash Platform SDK
2
2
 
3
3
  This SDK exposes functionality that allows platforms to integrate with ZeroHash on the web.
4
+ You should be able to access different UI flows, such as Onboarding and Crypto Withdrawals
5
+ using a single instance of `ZeroHashSDK`.
6
+
7
+ ## Migrating from 1.x.x to 2.x.x
8
+ On SDK v2 we added some new methods that will replace the ones marked with deprecated
9
+ on future releases, if you update our SDK to v2 but don't do the below changes,
10
+ your App should still work fine as we ensured everything is backwards compatible, but
11
+ we highly recommend that when updating to v2 you follow the steps below to ensure your App
12
+ is up to date with our SDK.
13
+ ### Instantiating the SDK
14
+ - `zeroHashOnboardingURL` was renamed to `zeroHashAppsURL`. Please replace `zeroHashOnboardingURL` with `zeroHashAppsURL`.
15
+ ### SDK Methods
16
+ ```javascript
17
+ import { AppIdentifier } from 'zh-web-sdk'
18
+
19
+ // openOnboardingModal replacement
20
+ sdk.openOnboardingModal({userOnboardingJWT: jwt}) // Deprecated method
21
+ sdk.openModal({appIdentifier: AppIdentifier.ONBOARDING, jwt: jwt}) // New method
22
+ sdk.openOnboardingModal({}) // Deprecated method
23
+ sdk.openModal({appIdentifier: AppIdentifier.ONBOARDING}) // New method
24
+
25
+ // closeOnboardingModal replacement
26
+ sdk.closeOnboardingModal() // Deprecated method
27
+ sdk.closeModal(AppIdentifier.ONBOARDING) // New method
28
+
29
+ // setUserOnboardingJWT replacement
30
+ sdk.setUserOnboardingJWT({ userOnboardingJWT: jwt }) // Deprecated method
31
+ sdk.setJWT({ jwt: jwt, appIdentifier: AppIdentifier.ONBOARDING }) // New method
32
+
33
+ // isOnboardingModalOpen replacement
34
+ sdk.isOnboardingModalOpen() // Deprecated method
35
+ sdk.isModalOpen(AppIdentifier.ONBOARDING) // New method
36
+ ```
4
37
 
5
38
  ## Quick setup
6
39
 
7
40
  ```javascript
8
- import ZeroHashSDK from "zh-web-sdk";
41
+ import ZeroHashSDK, { AppIdentifier } from 'zh-web-sdk';
9
42
 
10
43
  // Initialize SDK
11
44
  const sdk = new ZeroHashSDK({
12
- zeroHashOnboardingURL: "https://onboarding.zerohash.com/"
45
+ zeroHashAppsURL: "https://web-sdk.zerohash.com",
46
+ userOnboardingJWT: onboardingJWT // This can also be set later, when opening the Modal using the `openModal` method.
13
47
  });
14
48
 
15
- // Set the user onboarding JWT retrieved for a particular user
16
- // A user onboarding JWT before the user can proceed with the onboarding flow.
17
- sdk.openOnboardingModal({
18
- userOnboardingJWT: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
19
- "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ." +
20
- "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
49
+ // Set the JWT retrieved for a particular App (each App requires a specific JWT,
50
+ // i.e. you should not use your Onboarding JWT for Crypto Witdhrawals)
51
+ // before the user can proceed with the flow. Currently the Apps
52
+ sdk.openModal({
53
+ appIdentifier: AppIdentifier.ONBOARDING,
54
+ jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
55
+ "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ." +
56
+ "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
21
57
  });
22
58
  ```
23
59
 
@@ -46,26 +82,35 @@ These are the URLs your `WebView` should use on your Mobile App.
46
82
  ### React native guide
47
83
 
48
84
  #### Events
49
- We forward events that come from the UI to the Native App using the `postMessage` API. You can handle these events using `onMessage` from the `WebView` component. Currently these are the events we have:
50
- - **ONBOARDING_APP_LOADED:** Sent when the App is loaded
51
- - **ONBOARDING_CLOSE_BUTTON_CLICKED:** Sent when the user clicks the Close button on the top-right corner
85
+ We forward events that come from the UI to the Native App using the `postMessage` API. You can handle these events using `onMessage` from the `WebView` component. Currently these are the events we have, for the respective Apps:
86
+ - **Onboarding:**
87
+ - `ONBOARDING_APP_LOADED` Sent when the App is loaded
88
+ - `ONBOARDING_CLOSE_BUTTON_CLICKED` Sent when the user clicks the Close button on the top-right corner
89
+ - **Crypto Withdrawals:**
90
+ - `CRYPTO_WITHDRAWALS_APP_LOADED` Sent when the App is loaded
91
+ - `CRYPTO_WITHDRAWALS_CLOSE_BUTTON_CLICKED` Sent when the user clicks the Close button on the top-right corner
52
92
 
53
93
  #### Messages
54
- To control the `WebView` you can also send messages *down*, using the `postMessage` API. Currently the accepted messages are:
55
- - `{type: "OPEN_ONBOARDING_MODAL", payload:{userOnboardingJWT: "<JWT_HERE>"} }`: It will open the onboarding modal with the JWT provided
56
- - `{type: "CLOSE_ONBOARDING_MODAL"}`: It will close the onboarding modal
94
+ To control the `WebView` you can also send messages *down*, using the `postMessage` API. Currently the accepted messages are for the respective Apps are:
95
+ - **Onboarding**
96
+ - `{type: "OPEN_MODAL", payload:{jwt: "<JWT_HERE>", appIdentifier: "onboarding"} }`: It will open the onboarding modal with the JWT provided
97
+ - `{type: "CLOSE_MODAL", payload:{appIdentifier: "onboarding"}}`: It will close the onboarding modal
98
+ - **Crypto Withdrawals**
99
+ - `{type: "OPEN_MODAL", payload:{jwt: "<JWT_HERE>", appIdentifier: "crypto-withdrawals"} }`: It will open the Crypto Withdrawals modal with the JWT provided
100
+ - `{type: "CLOSE_MODAL", payload:{appIdentifier: "crypto-withdrawals"}}`: It will close the Crypto Withdrawals modal
57
101
 
58
102
  The example below shows how you can implement the mentioned methods in a `react-native` project
59
103
 
60
104
  ```jsx
61
105
  const sdkMobileServer = 'https://sdk-mobile.cert.zerohash.com/'
62
- const zeroHashOnboardingURL = 'https://onboarding.cert.zerohash.com'
106
+ const zeroHashAppsURL = 'https://web-sdk.zerohash.com/'
63
107
  const App = () => {
64
108
  const webViewRef = useRef<WebView>(null)
65
109
  const jwt = `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`
66
110
  /*
67
111
  Receive messages from sdk-mobile. Currently we expose the following event types:
68
- "ONBOARDING_APP_LOADED" and "ONBOARDING_CLOSE_BUTTON_CLICKED"
112
+ "ONBOARDING_APP_LOADED", "ONBOARDING_CLOSE_BUTTON_CLICKED",
113
+ "CRYPTO_WITHDRAWALS_APP_LOADED", "CRYPTO_WITHDRAWALS_CLOSE_BUTTON_CLICKED"
69
114
  you should be able to take action based on these events.
70
115
  */
71
116
  const handleMessage = (event: WebViewMessageEvent) => {
@@ -86,23 +131,23 @@ const App = () => {
86
131
  }
87
132
  }
88
133
  /* You can also send messages to sdk-mobile, currently we accept the following messages:
89
- * "OPEN_ONBOARDING_MODAL" and "CLOSE_ONBOARDING_MODAL"
134
+ * "OPEN_MODAL" and "CLOSE_MODAL"
90
135
  * Note that the "true;" after the postMessage is required
91
136
  * and recommended by the official react-native-webview docs
92
137
  * https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md#the-injectedjavascript-prop
93
138
  */
94
- const openModalType = `{
95
- type: 'OPEN_ONBOARDING_MODAL',
96
- payload: { userOnboardingJWT: '${jwt}' } }`
139
+ const openOnboardingModalType = `{
140
+ type: 'OPEN_MODAL',
141
+ payload: { jwt: '${jwt}', appIdentifier: 'onboarding' } }`
97
142
  const openOnboardingModal = () => {
98
143
  webViewRef.current?.injectJavaScript(
99
- `window.postMessage(${openModalType});true;`,
144
+ `window.postMessage(${openOnboardingModalType});true;`,
100
145
  )
101
146
  }
102
- const closeModalType = `{ type: 'CLOSE_ONBOARDING_MODAL' }`
103
- const closeModal = () =>
147
+ const closeOnboardingModalType = `{ type: 'CLOSE_MODAL', payload: 'onboarding' }`
148
+ const closeOnboardingModal = () =>
104
149
  webViewRef.current?.injectJavaScript(
105
- `window.postMessage(${closeModalType});true;`,
150
+ `window.postMessage(${closeOnboardingModalType});true;`,
106
151
  )
107
152
  return (
108
153
  <SafeAreaProvider style={{ flex: 1 }}>
@@ -117,7 +162,8 @@ const App = () => {
117
162
  ref={webViewRef}
118
163
  onMessage={handleMessage}
119
164
  source={{
120
- uri: `${sdkMobileServer}?userOnboardingJWT=${jwt}&zeroHashOnboardingURL=${zeroHashOnboardingURL}`,
165
+ uri: `${sdkMobileServer}?
166
+ cryptoWithdrawalsJWT=${cwJWT}userOnboardingJWT=${jwt}&zeroHashAppsURL=${zeroHashOnboardingURL}`,
121
167
  }}
122
168
  />
123
169
  </SafeAreaView>
@@ -1,2 +1,2 @@
1
- export declare const DEFAULT_ZH_ONBOARDING_URL = "https://onboarding.zerohash.com/";
1
+ export declare const DEFAULT_ZH_APPS_URL = "https://web-sdk.zerohash.com/";
2
2
  export declare const DEFAULT_ROOT_ID_PREFIX = "zerohash";
@@ -0,0 +1,19 @@
1
+ import React from "react";
2
+ import { AppIdentifier } from "../types";
3
+ interface AppContainerProps {
4
+ isAppActive?: boolean;
5
+ isAppLoaded?: boolean;
6
+ jwt?: string;
7
+ zeroHashAppURL: string;
8
+ appIdentifier: AppIdentifier;
9
+ }
10
+ declare const _default: import("react-redux").ConnectedComponent<({ isAppActive, isAppLoaded, jwt, zeroHashAppURL, appIdentifier, }: AppContainerProps) => import("react/jsx-runtime").JSX.Element, {
11
+ zeroHashAppURL: string;
12
+ appIdentifier: AppIdentifier;
13
+ isAppActive?: boolean | undefined;
14
+ isAppLoaded?: boolean | undefined;
15
+ jwt?: string | undefined;
16
+ context?: React.Context<import("react-redux").ReactReduxContextValue<any, import("redux").AnyAction>> | undefined;
17
+ store?: import("redux").Store<any, import("redux").AnyAction> | undefined;
18
+ }>;
19
+ export default _default;
@@ -0,0 +1,2 @@
1
+ export declare const MESSAGE_TYPE_SEND_JWT_TOKEN: string;
2
+ export declare const MESSAGE_TYPE_SEND_JWT_TOKEN_WITHDRAWALS: string;
package/dist/index.d.ts CHANGED
@@ -1,13 +1,30 @@
1
- import { IInitializeParameters, IOpenOnboardingModalParameters, ISetUserOnboardingJWTParameters, IZeroHashSDK } from './types';
1
+ import { AppIdentifier, IInitializeParameters, IOpenModalParameters, IOpenOnboardingModalParameters, ISetJWTParameters, ISetUserOnboardingJWTParameters, IZeroHashSDK } from "./types";
2
2
  export declare class ZeroHashSDK implements IZeroHashSDK {
3
3
  private rootQuerySelector;
4
4
  private onboardingInitialized;
5
+ /**
6
+ * initializedApps is a map that keeps track of which apps have been initialized.
7
+ * This might grow as we start supporting new apps.
8
+ */
9
+ private initializedApps;
5
10
  /**
6
11
  * Sets up the ZeroHash SDK and appends the ZeroHash DOM elements onto the page.
7
12
  *
8
13
  * For more information, see {@code IInitializeParameters}
9
14
  */
10
- constructor({ zeroHashOnboardingURL, rootQuerySelector, userOnboardingJWT }: IInitializeParameters);
15
+ constructor({ zeroHashOnboardingURL, rootQuerySelector, userOnboardingJWT, cryptoWithdrawalsJWT, zeroHashAppsURL, }: IInitializeParameters);
16
+ /**
17
+ * setJWT sets the JWT for the appIdentifier provided.
18
+ * The JWT should be the JWT provided by ZeroHash via the platform
19
+ * API proxied through your servers. As ZeroHash cannot authenticate
20
+ * your users' requests, it is paramount that the user be authenticated
21
+ * and validated on your servers, and exchanged for the JWT using your
22
+ * API key. DO NOT have the JWT exchange logic be on your front-end.
23
+ *
24
+ * As a precaution, we may restrict traffic to the JWT exchange API to
25
+ * whitelisted IPs that come from your server.
26
+ */
27
+ setJWT({ jwt, appIdentifier }: ISetJWTParameters): void;
11
28
  /**
12
29
  * setUserOnboardingJWT sets the JWT to be whatever value is provided.
13
30
  * The JWT should be the UserJWT provided by ZeroHash via the platform
@@ -18,20 +35,38 @@ export declare class ZeroHashSDK implements IZeroHashSDK {
18
35
  *
19
36
  * As a precaution, we may restrict traffic to the JWT exchange API to
20
37
  * whitelisted IPs that come from your server.
38
+ * @deprecated in favor of setJWT({jwt: <userOnboardingJWT>, appIdentifier: "onboarding"})
21
39
  */
22
40
  setUserOnboardingJWT(params: ISetUserOnboardingJWTParameters): void;
23
41
  /**
24
42
  * isOnboardingModalOpen returns true if the onboarding modal is open,
25
43
  * false otherwise
44
+ * @deprecated in favor of isModalOpen("onboarding")
26
45
  */
27
46
  isOnboardingModalOpen(): boolean;
47
+ /**
48
+ * isModalOpen returns true if the modal is open,
49
+ * for the appIdentifier provided, false otherwise
50
+ */
51
+ isModalOpen(appIdentifier: AppIdentifier): boolean;
52
+ /**
53
+ * closeModal hides the modal for the appIdentifier provided.
54
+ */
55
+ closeModal(appIdentifier: AppIdentifier): void;
56
+ /**
57
+ * openModal opens the modal for the appIdentifier provided.
58
+ */
59
+ openModal({ jwt, appIdentifier }: IOpenModalParameters): void;
28
60
  /**
29
61
  * openOnboardingModal opens the onboarding modal
62
+ * @deprecated in favor of openModal({appIdentifier: "onboarding", jwt: <userOnboardingJWT>})
30
63
  */
31
64
  openOnboardingModal(params: IOpenOnboardingModalParameters): void;
32
65
  /**
33
66
  * closeOnboardingModalModal hides the onboarding modal
67
+ * @deprecated in favor of closeModal("onboarding")
34
68
  */
35
69
  closeOnboardingModal(): void;
36
70
  }
37
71
  export default ZeroHashSDK;
72
+ export * from "./types";