From 9bd82a0023b2c484f572ec1cc1db4d259213dd7b Mon Sep 17 00:00:00 2001 From: Nabarun Date: Fri, 26 Jul 2024 16:49:29 +0000 Subject: [PATCH] Use testnet-onboarding-api to generate sumsub access token (#3) Part of [Sumsub KYC integration in onboarding app](https://www.notion.so/Sumsub-KYC-integration-in-onboarding-app-607b598c9c1d4d12adc71725e2ab5e7e) Co-authored-by: Shreerang Kale Co-authored-by: Adw8 Reviewed-on: https://git.vdb.to/deep-stack/testnet-onboarding-app/pulls/3 Co-authored-by: Nabarun Co-committed-by: Nabarun --- .env.example | 2 +- package.json | 2 +- src/App.tsx | 8 +- src/components/TermsAndConditionsCard.tsx | 20 ++-- src/components/TermsAndConditionsDialog.tsx | 4 +- src/constants.ts | 22 +++- src/pages/ConnectWallet.tsx | 9 +- src/pages/OnboardingSuccess.tsx | 5 +- src/pages/SignWithCosmos.tsx | 32 +++--- src/pages/SignWithNitroKey.tsx | 14 +-- src/pages/TermsAndConditions.tsx | 12 ++- src/pages/UserVerification.tsx | 112 +++++++++++++------- src/utils/sumsub.ts | 16 +++ yarn.lock | 8 +- 14 files changed, 173 insertions(+), 93 deletions(-) create mode 100644 src/utils/sumsub.ts diff --git a/.env.example b/.env.example index 2006948..0557672 100644 --- a/.env.example +++ b/.env.example @@ -6,4 +6,4 @@ REACT_APP_LACONICD_RPC_ENDPOINT=http://localhost:26657 REACT_APP_LACONICD_DENOM=photon REACT_APP_FAUCET_ENDPOINT=http://localhost:4000 REACT_APP_WALLET_META_URL=http://localhost:3000 -REACT_APP_SUMSUB_TOKEN= +REACT_APP_SUMSUB_API_ENDPOINT= diff --git a/package.json b/package.json index b99f9ec..9df0524 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "private": true, "dependencies": { - "@cerc-io/registry-sdk": "^0.2.2", + "@cerc-io/registry-sdk": "^0.2.3", "@cosmjs/stargate": "^0.32.4", "@emotion/react": "^11.11.4", "@emotion/styled": "^11.11.0", diff --git a/src/App.tsx b/src/App.tsx index af3f1ed..596a474 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -19,13 +19,13 @@ function App() { } /> - } - /> } /> }> } /> + } + /> } diff --git a/src/components/TermsAndConditionsCard.tsx b/src/components/TermsAndConditionsCard.tsx index ec547e6..413390d 100644 --- a/src/components/TermsAndConditionsCard.tsx +++ b/src/components/TermsAndConditionsCard.tsx @@ -1,15 +1,16 @@ import React, { useState } from 'react'; import { Typography, Button, Box, Paper, Radio, RadioGroup, FormControlLabel, FormControl, FormLabel, Link, Checkbox } from '@mui/material'; -import { Role } from '@cerc-io/registry-sdk/dist/proto/cerc/onboarding/v1/onboarding'; import TermsAndConditionsDialog from './TermsAndConditionsDialog'; -const VALIDATOR_OPTION = "validator"; -const PARTICIPANT_OPTION = "participant"; +export enum Role { + Validator = 'validator', + Participant = 'participant' +} const TermsAndConditionsCard = ({ handleAccept, handleRoleChange }: { handleAccept: () => void, handleRoleChange: (role: Role) => void }) => { - const [selectedRole, setSelectedRole] = useState(PARTICIPANT_OPTION); + const [selectedRole, setSelectedRole] = useState(Role.Participant); const [checked, setChecked] = useState(false); const [isHidden, setIsHidden] = useState(false); @@ -25,8 +26,9 @@ const TermsAndConditionsCard = ({ handleAccept, handleRoleChange }: { handleAcce } const handleRadioChange = (event: React.ChangeEvent) => { - setSelectedRole((event.target as HTMLInputElement).value); - handleRoleChange((event.target as HTMLInputElement).value === VALIDATOR_OPTION ? Role.ROLE_VALIDATOR : Role.ROLE_PARTICIPANT); + setSelectedRole(event.target.value as Role); + handleRoleChange((event.target as HTMLInputElement).value === Role.Validator ? Role.Validator : Role.Participant); + setChecked(false); }; return ( @@ -40,12 +42,12 @@ const TermsAndConditionsCard = ({ handleAccept, handleRoleChange }: { handleAcce onChange={handleRadioChange} > } label="Validator" /> } label="Participant" /> @@ -73,7 +75,7 @@ const TermsAndConditionsCard = ({ handleAccept, handleRoleChange }: { handleAcce Continue - setisDialogOpen(false)}/> + setisDialogOpen(false)}/> ); }; diff --git a/src/components/TermsAndConditionsDialog.tsx b/src/components/TermsAndConditionsDialog.tsx index a29bb63..4ad3c6f 100644 --- a/src/components/TermsAndConditionsDialog.tsx +++ b/src/components/TermsAndConditionsDialog.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, Button, Typography } from '@mui/material'; -import { TNC_CONTENT } from '../constants'; +import { TNC_PARTICIPANT_CONTENT, TNC_VALIDATOR_CONTENT } from '../constants'; interface TermsDialogProps { isValidator: boolean; @@ -19,7 +19,7 @@ const TermsAndConditionsDialog: React.FC = ({ isValidator, ope Onboard as a {isValidator ? "validator" : "participant"} -
+
diff --git a/src/constants.ts b/src/constants.ts index 9910297..99003b8 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,4 +1,24 @@ -export const TNC_CONTENT = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+export const TNC_GENERIC_CONTENT = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`; + +export const TNC_VALIDATOR_CONTENT = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`; + +export const TNC_PARTICIPANT_CONTENT = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
diff --git a/src/pages/ConnectWallet.tsx b/src/pages/ConnectWallet.tsx index 58ccc7d..776621f 100644 --- a/src/pages/ConnectWallet.tsx +++ b/src/pages/ConnectWallet.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from "react"; -import { useLocation, useNavigate } from "react-router-dom"; +import {useNavigate } from "react-router-dom"; import { Button, Box, Container } from "@mui/material"; @@ -10,16 +10,13 @@ const ConnectWallet = () => { const navigate = useNavigate(); - const location = useLocation(); - const queryParams = new URLSearchParams(location.search); - const kycId = queryParams.get('kycId'); useEffect(() => { if (session) { - navigate(`/sign-with-nitro-key?kycId=${kycId}`); + navigate("/sign-with-nitro-key"); } - }, [session, navigate, kycId]); + }, [session, navigate,]); const handler = async () => { await connect(); diff --git a/src/pages/OnboardingSuccess.tsx b/src/pages/OnboardingSuccess.tsx index 051a65b..3a1fcfc 100644 --- a/src/pages/OnboardingSuccess.tsx +++ b/src/pages/OnboardingSuccess.tsx @@ -16,8 +16,9 @@ const registry = new Registry( const OnboardingSuccess = () => { const location = useLocation(); - const queryParams = new URLSearchParams(location.search); - const cosmosAddress = queryParams.get('cosmosAddress'); + const { cosmosAddress } = location.state as { + cosmosAddress?: string + } const [participant, setParticipant] = useState(); diff --git a/src/pages/SignWithCosmos.tsx b/src/pages/SignWithCosmos.tsx index b70941b..1107b0e 100644 --- a/src/pages/SignWithCosmos.tsx +++ b/src/pages/SignWithCosmos.tsx @@ -8,30 +8,34 @@ import { MsgOnboardParticipantEncodeObject, typeUrlMsgOnboardParticipant, } from "@cerc-io/registry-sdk"; -import { Role } from "@cerc-io/registry-sdk/dist/proto/cerc/onboarding/v1/onboarding"; import { StargateClient } from "@cosmjs/stargate"; import { useWalletConnectContext } from "../context/WalletConnectContext"; -import TermsAndConditionsCard from "../components/TermsAndConditionsCard"; +import TermsAndConditionsCard, {Role} from "../components/TermsAndConditionsCard"; const SignWithCosmos = () => { const { session, signClient } = useWalletConnectContext(); const location = useLocation(); - const queryParams = new URLSearchParams(location.search); - const cosmosAddress = queryParams.get('cosmosAddress'); - const ethSignature = queryParams.get('ethSignature'); - const kycId = queryParams.get('kycId'); - const [isLoading, setIsLoading] = useState(false); const [balance, setBalance] = useState(''); const [isRequesting, setIsRequesting] = useState(false); const [isTncAccepted, setIsTncAccepted] = useState(false); - const [role, setRole] = useState(Role.ROLE_PARTICIPANT); + const [role, setRole] = useState(Role.Participant); const navigate = useNavigate(); - const innerMessage = location.state; - const ethAddress = innerMessage.address; + + const {message: innerMessage, cosmosAddress, receivedEthSig: ethSignature, kycId} = location.state as { + message?: { + msg: string; + address: string; + }; + cosmosAddress?: string; + receivedEthSig?: string; + kycId?: string; + }; + + const ethAddress = innerMessage!.address; const createCosmosClient = useCallback(async (endpoint: string) => { return await StargateClient.connect(endpoint); @@ -109,7 +113,11 @@ const SignWithCosmos = () => { if (responseFromWallet.code !== 0) { enqueueSnackbar("Transaction not sent", { variant: "error" }); } else { - navigate(`/onboarding-success?cosmosAddress=${cosmosAddress}`); + navigate("/onboarding-success", { + state: { + cosmosAddress + } + }); } } catch (error) { console.error(error); @@ -143,7 +151,7 @@ const SignWithCosmos = () => { gap: "10px", }} > - Please accept terms and conditions to continue + Please accept terms and conditions to continue setIsTncAccepted(true)} handleRoleChange={setRole}/> Send transaction to chain Cosmos Account: diff --git a/src/pages/SignWithNitroKey.tsx b/src/pages/SignWithNitroKey.tsx index cc97a26..8785a37 100644 --- a/src/pages/SignWithNitroKey.tsx +++ b/src/pages/SignWithNitroKey.tsx @@ -1,5 +1,5 @@ import React, { useState, useMemo, useEffect } from "react"; -import { useLocation, useNavigate } from "react-router-dom"; +import { useNavigate } from "react-router-dom"; import { SnackbarProvider, enqueueSnackbar } from "notistack"; import canonicalStringify from "canonical-json"; @@ -15,9 +15,6 @@ import { utf8ToHex } from "@walletconnect/encoding"; import { useWalletConnectContext } from "../context/WalletConnectContext"; const SignWithNitroKey = () => { - const location = useLocation(); - const queryParams = new URLSearchParams(location.search); - const kycId = queryParams.get('kycId'); const { session, signClient, checkPersistedState } = useWalletConnectContext(); @@ -59,8 +56,12 @@ const SignWithNitroKey = () => { }); setIsLoading(false) setEthSignature(ethSignature); - navigate(`/sign-with-cosmos?cosmosAddress=${cosmosAddress}ðSignature=${receivedEthSig}&kycId=${kycId}`, { - state: message, + navigate("/user-verification", { + state: { + message, + cosmosAddress, + receivedEthSig, + }, }); } catch (error) { console.log("err in signing ", error); @@ -70,7 +71,6 @@ const SignWithNitroKey = () => { } }; - return (
{session ? ( diff --git a/src/pages/TermsAndConditions.tsx b/src/pages/TermsAndConditions.tsx index 3962d93..b84bfe9 100644 --- a/src/pages/TermsAndConditions.tsx +++ b/src/pages/TermsAndConditions.tsx @@ -3,13 +3,13 @@ import { useNavigate } from 'react-router-dom'; import { Container, Typography, Button, Box, Paper } from '@mui/material'; -import { TNC_CONTENT } from '../constants'; +import { TNC_GENERIC_CONTENT } from '../constants'; const TermsAndConditions = () => { const navigate = useNavigate(); const handleAccept = () => { - navigate('/user-verification'); + navigate('/connect-wallet'); }; return ( @@ -26,8 +26,12 @@ const TermsAndConditions = () => { marginBottom: '1rem', }} > - -
+ +
diff --git a/src/pages/UserVerification.tsx b/src/pages/UserVerification.tsx index f0710c6..3efa720 100644 --- a/src/pages/UserVerification.tsx +++ b/src/pages/UserVerification.tsx @@ -1,59 +1,91 @@ -import React, { useEffect, useState } from 'react' -import { useNavigate } from 'react-router-dom'; +import React, { useEffect, useState } from 'react'; +import { useLocation, useNavigate } from 'react-router-dom'; -import { Box, Typography } from '@mui/material' -import SumsubWebSdk from '@sumsub/websdk-react' +import { Box, Typography } from '@mui/material'; +import SumsubWebSdk from '@sumsub/websdk-react'; +import { MessageHandler } from '@sumsub/websdk'; +import { EventPayload } from '@sumsub/websdk/types/types'; + +import { fetchAccessToken } from '../utils/sumsub'; const config = { - lang: "en", //language of WebSDK texts and comments (ISO 639-1 format) + lang: "en", // language of WebSDK texts and comments (ISO 639-1 format) theme: "light", -} +}; const options = { addViewportTag: false, - adaptIframeHeight: true -} + adaptIframeHeight: true, +}; const UserVerification = () => { - const [kycId, setKycId] = useState(''); + const [kycId, setKycId] = useState('unknown'); const [applicationSubmitted, setApplicationSubmitted] = useState(false); + const [token, setToken] = useState(''); + const [loading, setLoading] = useState(true); + const location = useLocation(); const navigate = useNavigate(); - useEffect(()=>{ - if (applicationSubmitted && kycId !== '') { - navigate(`/connect-wallet?kycId=${kycId}`) - } - }, [applicationSubmitted, kycId, navigate]); + const {message, cosmosAddress, receivedEthSig} = location.state as { + message?: string; + cosmosAddress?: string; + receivedEthSig?: string; + }; + + const userId = cosmosAddress; + + useEffect(() => { + const getToken = async (userId: string) => { + console.log(userId); + const newToken = await fetchAccessToken(userId); + + setToken(newToken); + setLoading(false); + }; + + if (userId) { + getToken(userId).catch(error => { + console.error(error); + alert("Failed to fetch token"); + }); + } + }, [userId]); + + useEffect(() => { + if (applicationSubmitted && kycId !== '') { + navigate("/sign-with-cosmos", { + state: { + message, + cosmosAddress, + receivedEthSig, + kycId, + }}) + } + }, [applicationSubmitted, kycId, navigate, cosmosAddress, message, receivedEthSig]); - // TODO: Implement const accessTokenExpirationHandler = async () => { alert("Please renew token"); return "Token expired"; - } + }; - const accessToken = { - // TODO: Programmatically fetch access-token using sumsub API - "token": process.env.REACT_APP_SUMSUB_TOKEN!, - // TODO: Generate random user-id string - "userId": "alice" - } + const messageHandler: MessageHandler = (event, payload) => { + console.log('sumsubEvent:', event); - const messageHandler = (event: any, payload: any ) => { if (event === 'idCheck.onApplicantLoaded') { - setKycId(payload.applicantId); + setKycId((payload as EventPayload<'idCheck.onApplicantLoaded'>).applicantId); } - if (event === 'idCheck.onApplicantSubmitted'){ + if (event === 'idCheck.onApplicantSubmitted') { setApplicationSubmitted(true); } if (event === 'idCheck.onApplicantStatusChanged') { - if (payload.reviewStatus === 'pending') { + if ((payload as EventPayload<'idCheck.onApplicantStatusChanged'>).reviewStatus === 'pending') { setApplicationSubmitted(true); } } - } + }; return ( <> @@ -66,19 +98,19 @@ const UserVerification = () => { }} > User verification -
- -
+
- + {!loading && token && ( + + )} - ) -} + ); +}; -export default UserVerification +export default UserVerification; diff --git a/src/utils/sumsub.ts b/src/utils/sumsub.ts new file mode 100644 index 0000000..62a48c2 --- /dev/null +++ b/src/utils/sumsub.ts @@ -0,0 +1,16 @@ +export const fetchAccessToken = async (userId: string): Promise => { + const response = await fetch(`${process.env.REACT_APP_SUMSUB_API_ENDPOINT}/generate-token`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ userId }) + }); + + if (!response.ok) { + throw new Error('Network response was not ok'); + } + + const data = await response.json(); + return data.token; +}; diff --git a/yarn.lock b/yarn.lock index 3012d49..a9a2ad6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1183,10 +1183,10 @@ resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== -"@cerc-io/registry-sdk@^0.2.2": - version "0.2.2" - resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fregistry-sdk/-/0.2.2/registry-sdk-0.2.2.tgz#2e8a533f069b4bb9f4cd4798e783f52e29167d0d" - integrity sha512-ocRMbWtdewOg02ORfK1U+qbTqB46anHP4ApXokGkY4d+mFSApR3sdUEi2geHcs8oh+SG8YAp7LUJ9AAJneNY8g== +"@cerc-io/registry-sdk@^0.2.3": + version "0.2.3" + resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fregistry-sdk/-/0.2.3/registry-sdk-0.2.3.tgz#73e955b4d49d7c97eea40c351bbc21f98bb330f9" + integrity sha512-8fXRdyiTXn8WsJ8r3DCSBYzUBNEZYPPk5JGUrEmkGQhKOJr+ZeakN+2t6HrqEVB9IMYTJK9BtVLPA0KlaXILYA== dependencies: "@cosmjs/amino" "^0.28.1" "@cosmjs/crypto" "^0.28.1"