@verified-network/verified-custody 0.1.9 → 0.2.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.
@@ -0,0 +1,244 @@
1
+ import React, { useEffect, useState } from "react";
2
+ import { debounce } from "lodash";
3
+ import { countriesDetails, imageAssets } from "../utils/constants";
4
+ import { useVaultStore } from "../services/store";
5
+ import { initWalletAndContract, loadAllGoogleFonts } from "../utils/helpers";
6
+ import ContactPage from "./contact";
7
+ import OTPPage from "./otp";
8
+ import CreatePinPage from "./createPin";
9
+ import Success from "../components/success";
10
+ import AddCosignersPage from "./addCosigners";
11
+ import { VerifiedCustodyHelpers } from "../utils/types";
12
+
13
+ const FTUPage = (props: {
14
+ fontsLoaded?: boolean;
15
+ popupView?: boolean;
16
+ action?: string;
17
+ vaultData?: any;
18
+ txData?: any;
19
+ chainId?: number;
20
+ defaultCountry?: {
21
+ area: string;
22
+ flag: string;
23
+ name: string;
24
+ };
25
+ showLogoPage?: boolean;
26
+ logoPageElement?: React.JSX.Element;
27
+ logoTimeoutTime?: number;
28
+ showStartupPage?: boolean;
29
+ startupPageElement?: React.JSX.Element;
30
+ setisOnboard?: (isOnboard: boolean) => void;
31
+ helperFunctions?: VerifiedCustodyHelpers;
32
+ }) => {
33
+ const [fontsLoaded, setFontsLoaded] = useState<boolean>(
34
+ props.fontsLoaded || false
35
+ );
36
+ const [showLogo, setShowLogo] = useState<boolean>(
37
+ props.showLogoPage || false
38
+ );
39
+ const [allCountries, setAllCountries] = useState([]);
40
+ const [step, setStep] = useState<string>(props.showStartupPage ? "1" : "2");
41
+ const [selectedCountry, setSelectedCountry] = useState(
42
+ props.defaultCountry || {
43
+ area: "1",
44
+ flag: imageAssets.usaFlag,
45
+ name: "United States",
46
+ }
47
+ );
48
+ const [userNumberFmt, setUserNumberFmt] = useState<string>("");
49
+ const [userEmail, setUserEmail] = useState<string>("");
50
+ const [isLoading, setIsLoading] = useState<boolean>(false);
51
+ const [showSuccessBtn, setShowSuccessBtn] = useState<boolean>(false);
52
+ const [successBtnText, setSuccessBtnText] = useState<string>("");
53
+ const [successBtnNeutText, setSuccessBtnNeutText] = useState<string>("");
54
+ const [actionHeaderText, setActionHeaderText] = useState<string>("");
55
+ const [actionFooterText, setActionFooterText] = useState<string>("");
56
+ const [successBtnFunc, setSuccessBtnFunc] = useState(null);
57
+ const [successBtnNeutFunc, setSuccessBtnNeutFunc] = useState(null);
58
+
59
+ const {
60
+ setChainId,
61
+ setAddress,
62
+ setPk,
63
+ setProvider,
64
+ setSigner,
65
+ setVaultContract,
66
+ setVaultData,
67
+ vaultData,
68
+ } = useVaultStore();
69
+
70
+ useEffect(() => {
71
+ const PreloadGoogleFonts = () => {
72
+ const isFontsLoaded = loadAllGoogleFonts(document);
73
+ setFontsLoaded(isFontsLoaded);
74
+ };
75
+ if (!props.fontsLoaded) {
76
+ PreloadGoogleFonts();
77
+ }
78
+ }, []);
79
+
80
+ useEffect(() => {
81
+ const initializeUserWalletAndContract = async () => {
82
+ const walletAndContractDetails = await initWalletAndContract(
83
+ props.chainId
84
+ );
85
+ setChainId(walletAndContractDetails.chainId);
86
+ setAddress(walletAndContractDetails.address);
87
+ setPk(walletAndContractDetails.pk);
88
+ setProvider(walletAndContractDetails.provider);
89
+ setSigner(walletAndContractDetails.signer);
90
+ setVaultContract(walletAndContractDetails.vaultContract);
91
+ setVaultData(walletAndContractDetails.vaultData);
92
+ };
93
+ initializeUserWalletAndContract();
94
+ }, []);
95
+
96
+ useEffect(() => {
97
+ const handleLogoDisplay = debounce(
98
+ () => {
99
+ setShowLogo(false);
100
+ },
101
+ props.logoTimeoutTime ? props.logoTimeoutTime : 3000
102
+ ); //3 seconds??
103
+ handleLogoDisplay();
104
+ }, []);
105
+
106
+ useEffect(() => {
107
+ const getAndFormatAllCountries = () => {
108
+ const fmtCountries: any = countriesDetails
109
+ .map((cnt: any) => {
110
+ if (cnt) {
111
+ return {
112
+ name: cnt?.name,
113
+ countryName: cnt?.name,
114
+ officialName: cnt?.name,
115
+ currencySymbol: cnt?.currency,
116
+ currencyName: cnt?.currency,
117
+ officialCurrencySymbol: cnt?.currency,
118
+ countryCode: cnt?.code,
119
+ flag: cnt?.flag,
120
+ continent: cnt?.continent,
121
+ phone_code: cnt?.phone_code,
122
+ code: cnt?.code,
123
+ };
124
+ }
125
+ })
126
+ .flat();
127
+ setAllCountries(fmtCountries?.filter((cnt: any) => cnt !== undefined));
128
+ };
129
+ getAndFormatAllCountries();
130
+ }, []);
131
+
132
+ return (
133
+ <React.Fragment>
134
+ {fontsLoaded && showLogo && props.logoPageElement && (
135
+ <React.Fragment> {props.logoPageElement}</React.Fragment>
136
+ )}
137
+ {fontsLoaded && !showLogo && (
138
+ <React.Fragment>
139
+ {step === "1" && props.showStartupPage && (
140
+ <React.Fragment>{props.startupPageElement}</React.Fragment>
141
+ )}
142
+ {step === "2" && (
143
+ <ContactPage
144
+ setStep={setStep}
145
+ setIsLoading={setIsLoading}
146
+ setUserNumberFmt={setUserNumberFmt}
147
+ userNumberFmt={userNumberFmt}
148
+ setUserEmail={setUserEmail}
149
+ userEmail={userEmail}
150
+ setSelectedCountry={setSelectedCountry}
151
+ selectedCountry={selectedCountry}
152
+ allCountries={allCountries}
153
+ />
154
+ )}
155
+ {step === "3" && (
156
+ <OTPPage
157
+ setStep={setStep}
158
+ setIsLoading={setIsLoading}
159
+ userNumberFmt={userNumberFmt}
160
+ setUserNumberFmt={setUserNumberFmt}
161
+ setUserEmail={setUserEmail}
162
+ userEmail={userEmail}
163
+ selectedCountry={selectedCountry}
164
+ />
165
+ )}
166
+
167
+ {step === "4" && (
168
+ <CreatePinPage
169
+ setStep={setStep}
170
+ setIsLoading={setIsLoading}
171
+ setShowSuccessBtn={setShowSuccessBtn}
172
+ setSuccessBtnText={setSuccessBtnText}
173
+ setSuccessBtnNeutText={setSuccessBtnNeutText}
174
+ setSuccessBtnFunc={setSuccessBtnFunc}
175
+ setSuccessBtnNeutFunc={setSuccessBtnNeutFunc}
176
+ userNumberFmt={userNumberFmt}
177
+ userEmail={userEmail}
178
+ selectedCountry={selectedCountry}
179
+ action={props.action}
180
+ vaultData={props.vaultData}
181
+ txData={props.txData}
182
+ setActionHeaderText={setActionHeaderText}
183
+ setActionFooterText={setActionFooterText}
184
+ helperFunctions={props.helperFunctions}
185
+ />
186
+ )}
187
+
188
+ {step === "5" && (
189
+ <Success
190
+ setStep={setStep}
191
+ stepToSet="6"
192
+ customTextHeader="Successful"
193
+ customTextFooter="Congratulations! You have successfully created a PIN to protect your account."
194
+ action={props.action}
195
+ showButton={showSuccessBtn}
196
+ buttonText={successBtnText}
197
+ buttonNeutText={successBtnNeutText}
198
+ buttonFunc={successBtnFunc}
199
+ buttonNeutFunc={successBtnNeutFunc}
200
+ actionHeaderText={actionHeaderText}
201
+ actionFooterText={actionFooterText}
202
+ />
203
+ )}
204
+
205
+ {step === "6" && (
206
+ <AddCosignersPage
207
+ setStep={setStep}
208
+ setIsLoading={setIsLoading}
209
+ userNumberFmt={userNumberFmt}
210
+ userEmail={userEmail}
211
+ selectedCountry={selectedCountry}
212
+ allCountries={allCountries}
213
+ helperFunctions={props.helperFunctions}
214
+ />
215
+ )}
216
+
217
+ {step === "7" && (
218
+ <Success
219
+ action={props.action}
220
+ messageTosend={
221
+ props.action === "connect_wallet"
222
+ ? { type: "vaultData", key: "vaultData", value: vaultData }
223
+ : props.action === "connect_wallet"
224
+ ? {
225
+ type: "signRecovery",
226
+ value: {
227
+ vaultData: props.vaultData,
228
+ txData: { newUser: true, ...props.txData },
229
+ },
230
+ }
231
+ : { type: "closePopup", key: "", value: "" }
232
+ }
233
+ customTextHeader="Successful"
234
+ customTextFooter="Congratulations! You have successfully added Co-signers to your account."
235
+ />
236
+ )}
237
+ </React.Fragment>
238
+ )}
239
+ {!fontsLoaded && <h2>Loading...</h2>}
240
+ </React.Fragment>
241
+ );
242
+ };
243
+
244
+ export default FTUPage;
@@ -0,0 +1,170 @@
1
+ import React, { useEffect, useState } from "react";
2
+ import { VaultContextProvider } from "../services/store";
3
+ import {
4
+ VerifiedCustodyProps,
5
+ VerifiedWalletAction,
6
+ VerifiedWalletTx,
7
+ VerifiedWalletVault,
8
+ } from "../utils/types";
9
+ import FTUPage from "./ftu";
10
+ import EnterPinPage from "./enterPin";
11
+ import { hashTheString, loadAllGoogleFonts } from "../utils/helpers";
12
+ import { actionTexts } from "../utils/constants";
13
+
14
+ const VerifiedCustody = (props: VerifiedCustodyProps) => {
15
+ const [fontsLoaded, setFontsLoaded] = useState<boolean>(false);
16
+ const [isOnboard, setisOnboard] = useState<boolean | null>(null);
17
+
18
+ const [action, _setAction] = useState<VerifiedWalletAction | undefined>(
19
+ props.action
20
+ );
21
+
22
+ const [actionText, _setActionText] = useState<string | undefined>(
23
+ props.actionText
24
+ );
25
+
26
+ const [origin, _setOrigin] = useState<string | undefined>(props.origin);
27
+
28
+ const [title, _setTitle] = useState<string | undefined>(props.title);
29
+
30
+ const [chainId, _setChainId] = useState<number | undefined>(props.chainId);
31
+
32
+ const [vaultData, setVaultData] = useState<VerifiedWalletVault | undefined>(
33
+ props.vaultData
34
+ );
35
+
36
+ const [txData, _setTxData] = useState<VerifiedWalletTx | undefined>(
37
+ props.txData
38
+ );
39
+
40
+ const [vaultDataQuery, _setVaultDataQuery] = useState<
41
+ VerifiedWalletVault | undefined
42
+ >(props.reqVaultData);
43
+
44
+ const [isLoading, setIsLoading] = useState<boolean>(false);
45
+
46
+ useEffect(() => {
47
+ const PreloadGoogleFonts = () => {
48
+ const isFontsLoaded = loadAllGoogleFonts(document);
49
+ setFontsLoaded(isFontsLoaded);
50
+ };
51
+ PreloadGoogleFonts();
52
+ }, []);
53
+
54
+ useEffect(() => {
55
+ const checkUserOnboardingStatus = () => {
56
+ if (chrome?.storage?.local) {
57
+ chrome.storage.local.get("myVault", (res: any) => {
58
+ // Existing user
59
+ if (res.myVault && JSON.parse(res.myVault)?.vaultId) {
60
+ setVaultData(JSON.parse(res.myVault));
61
+ setisOnboard(true);
62
+ }
63
+ //FTU(First Time User)
64
+ else setisOnboard(false);
65
+ });
66
+ } else if (localStorage) {
67
+ const myVault = JSON.parse(localStorage.getItem("myVault"));
68
+ if (myVault && myVault?.vaultId) {
69
+ setVaultData(myVault);
70
+ setisOnboard(true);
71
+ } else setisOnboard(false);
72
+ } else {
73
+ setisOnboard(false);
74
+ }
75
+ };
76
+
77
+ checkUserOnboardingStatus();
78
+ }, []);
79
+
80
+ return (
81
+ <React.Fragment>
82
+ {fontsLoaded && (
83
+ <VaultContextProvider>
84
+ <div className="verified-custd-mdl-container">
85
+ {/* default blank page to capture delay??? */}
86
+ {isOnboard === null && props.delayPageElement && (
87
+ <React.Fragment>{props.delayPageElement} </React.Fragment>
88
+ )}
89
+ {/* default blank screen to capture delay??? */}
90
+ {isOnboard === null && !props.delayPageElement && (
91
+ <div
92
+ style={
93
+ props.delayPageStyle
94
+ ? props.delayPageStyle
95
+ : {
96
+ width: "100%",
97
+ height: "100%",
98
+ background: "trasparent",
99
+ border: "none",
100
+ }
101
+ }
102
+ >
103
+ {" "}
104
+ </div>
105
+ )}
106
+
107
+ {/* first time users */}
108
+ {isOnboard !== null && !isOnboard && (
109
+ <FTUPage
110
+ fontsLoaded={fontsLoaded}
111
+ helperFunctions={props.helperFunctions}
112
+ />
113
+ )}
114
+
115
+ {/* existing users */}
116
+ {isOnboard !== null && isOnboard && (
117
+ <EnterPinPage
118
+ fontsLoaded={fontsLoaded}
119
+ setisOnboard={setisOnboard}
120
+ vaultId={vaultData?.vaultId}
121
+ hashedVaultId={vaultData?.hashedVaultId}
122
+ encrptedPk={vaultData?.pk}
123
+ vaultAddress={vaultData?.address}
124
+ vaultChannel={vaultData?.channel}
125
+ origin={origin}
126
+ title={title}
127
+ actionText={
128
+ props.actionText ? props.actionText : actionTexts[action]
129
+ }
130
+ setIsLoading={setIsLoading}
131
+ newChainId={Number(chainId)}
132
+ action={action}
133
+ reqVaultData={
134
+ vaultDataQuery
135
+ ? {
136
+ vaultId: decodeURIComponent(
137
+ vaultDataQuery?.vId || vaultDataQuery?.vaultId
138
+ ),
139
+ hashedVaultId: hashTheString(
140
+ decodeURIComponent(
141
+ vaultDataQuery?.vId || vaultDataQuery?.vaultId
142
+ )
143
+ ),
144
+ hashedVaultPin: decodeURIComponent(
145
+ vaultDataQuery?.vPh || vaultDataQuery?.hashedVaultPin
146
+ ),
147
+ coSignerId: decodeURIComponent(vaultDataQuery?.cId),
148
+ }
149
+ : null
150
+ }
151
+ reqTxData={
152
+ txData
153
+ ? Object.keys(txData)?.map((ky) => {
154
+ txData[ky] = decodeURIComponent(txData[ky]);
155
+ return txData;
156
+ })[Object.keys(txData)?.length - 1]
157
+ : null
158
+ }
159
+ helperFunctions={props.helperFunctions}
160
+ />
161
+ )}
162
+ </div>
163
+ </VaultContextProvider>
164
+ )}
165
+ {!fontsLoaded && <h2>Loading...</h2>}
166
+ </React.Fragment>
167
+ );
168
+ };
169
+
170
+ export default VerifiedCustody;