From 66e1a370dfa4b0de651a488cfa0c4e63bb7bfa9b Mon Sep 17 00:00:00 2001 From: bhavanakarwade Date: Mon, 11 Mar 2024 22:51:17 +0530 Subject: [PATCH 01/27] feat: support multiple did methods Signed-off-by: bhavanakarwade --- src/api/Agent.ts | 46 +- src/common/enums.ts | 7 + src/components/organization/Dashboard.tsx | 2 +- src/components/organization/WalletSpinup.tsx | 1156 ++++++++++++++---- src/config/apiRoutes.ts | 2 + 5 files changed, 973 insertions(+), 240 deletions(-) diff --git a/src/api/Agent.ts b/src/api/Agent.ts index 81bbc687e..e2312aaef 100644 --- a/src/api/Agent.ts +++ b/src/api/Agent.ts @@ -1,8 +1,52 @@ -import { axiosGet } from "../services/apiRequests"; +import { axiosGet, axiosPost } from "../services/apiRequests"; import { apiRoutes } from "../config/apiRoutes"; import { getFromLocalStorage } from "./Auth"; import { storageKeys } from "../config/CommonConstant"; +export const getLedgerConfig = async () => { + const token = await getFromLocalStorage(storageKeys.TOKEN) + const details = { + url: `${apiRoutes.organizations.root}${apiRoutes.Agent.getLedgerConfig}`, + config: { + headers: { + 'Content-type': 'application/json', + 'Authorization': `Bearer ${token}`, + }, + }, + }; + + try { + const response = await axiosGet(details) + return response + } + catch (error) { + const err = error as Error + return err?.message + } +} + +export const createPolygonKeyValuePair = async (orgId:string) => { + const token = await getFromLocalStorage(storageKeys.TOKEN) + const details = { + url: `${apiRoutes.organizations.root}/${orgId}${apiRoutes.Agent.createPolygonKeys}`, + config: { + headers: { + 'Content-type': 'application/json', + 'Authorization': `Bearer ${token}`, + }, + }, + }; + + try { + const response = await axiosPost(details) + return response + } + catch (error) { + const err = error as Error + return err?.message + } +} + export const getAgentHealth = async (orgId:string) => { const token = await getFromLocalStorage(storageKeys.TOKEN) const details = { diff --git a/src/common/enums.ts b/src/common/enums.ts index 22c7e63b5..7abdc1dff 100644 --- a/src/common/enums.ts +++ b/src/common/enums.ts @@ -95,3 +95,10 @@ export enum BulkIssuanceStatus { successful= 'Successful', failed= 'Failed' } + +export enum DidMethod { + INDY = 'indy', + KEY = 'key', + WEB = 'web', + POLYGON = 'polygon' +} \ No newline at end of file diff --git a/src/components/organization/Dashboard.tsx b/src/components/organization/Dashboard.tsx index 37e3c3a81..a478dc5aa 100644 --- a/src/components/organization/Dashboard.tsx +++ b/src/components/organization/Dashboard.tsx @@ -192,7 +192,7 @@ const Dashboard = () => {
-
+
void - orgName: string - loading: boolean - submitSharedWallet: (values: ValuesShared) => void + seeds: string; + isCopied: boolean; + copyTextVal: (e: any) => void; + orgName: string; + loading: boolean; + submitSharedWallet: ( + values: ValuesShared, + privateKey: string, + domain: string, + endPoint: string, + ) => void; } interface IDedicatedAgentForm { - seeds: string - loading: boolean - submitDedicatedWallet: (values: Values) => void + seeds: string; + loading: boolean; + submitDedicatedWallet: (values: Values) => void; +} + +interface IPolygonKeys { + privateKey: string; + publicKeyBase58: string; + address: string; } const fetchNetworks = async () => { try { - const { data } = await getLedgers() as AxiosResponse + const { data } = (await getLedgers()) as AxiosResponse; if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) { - return data?.data + return data?.data; } - return [] + return []; } catch (err) { - console.log("Fetch Network ERROR::::", err) + console.log('Fetch Network ERROR::::', err); } -} +}; -const NetworkInput = ({formikHandlers}) => { - const [networks, setNetworks] = useState([]) - const getLedgerList = async () => { - const res = await fetchNetworks() - setNetworks(res) - } - useEffect(() => { - getLedgerList() - }, []) +const NetworkInput = ({ formikHandlers }) => { return (
@@ -98,187 +113,834 @@ const NetworkInput = ({formikHandlers}) => { className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 h-11" > - {networks && networks.length > 0 && networks.map((item: INetworks) => ( - - ))} + {networks && + networks.length > 0 && + networks.map((item: INetworks) => ( + + ))} - {formikHandlers?.errors?.network && - formikHandlers?.touched?.network && ( - - {formikHandlers?.errors?.network} - - )} + {formikHandlers?.errors?.network && formikHandlers?.touched?.network && ( + + {formikHandlers?.errors?.network} + + )}
- ) -} + ); +}; -const SharedAgentForm = ({ orgName, seeds, isCopied, loading, copyTextVal, submitSharedWallet }: ISharedAgentForm) => { - const [haveDidShared, setHaveDidShared] = useState(false) - const validation = { - label: yup.string() - .required('Wallet label is required') - .trim() - .test('no-spaces', 'Spaces are not allowed', value => !value || !value.includes(' ')) - .matches( - /^[A-Za-z0-9-][^ !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]*$/, - 'Wallet label must be alphanumeric only', - ) - .min(2, 'Wallet label must be at least 2 characters') - .max(25, 'Wallet label must be at most 25 characters'), - seed: haveDidShared ? yup.string().required("Seed is required") : yup.string(), - did: haveDidShared ? yup.string().required("DID is required") : yup.string(), - network: yup.string().required("Network is required") +const SharedAgentForm = ({ + orgName, + seeds, + loading, + submitSharedWallet, +}: ISharedAgentForm) => { + const [haveDidShared, setHaveDidShared] = useState(false); + const [networks, setNetworks] = useState([]); + const [selectedLedger, setSelectedLedger] = useState(''); + const [seedVal, setSeedVal] = useState(''); + const [selectedNetwork, setSelectedNetwork] = useState(''); + const [selectedDid, setSelectedDid] = useState(''); + const [mappedData, setMappedData] = useState(null); + const [generatedKeys, setGeneratedKeys] = useState(null); + const [domainValue, setDomainValue] = useState(''); + const [endPointValue, setEndPointValue] = useState(''); + const [privateKeyValue, setPrivateKeyValue] = useState(''); + + const fetchLedgerConfig = async () => { + try { + const { data } = await getLedgerConfig(); + if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) { + const obj = {}; + data.data.forEach((item) => { + obj[item.name.toLowerCase()] = { ...item.details }; + }); + setMappedData(obj); + } + } catch (err) { + console.log('Fetch Network ERROR::::', err); + } + }; + + + const handleLedgerChange = (e) => { + setSelectedLedger(e.target.value); + setSelectedNetwork(''); + setSelectedDid(''); + }; + + const generatePolygonKeyValuePair = async () => { + try { + const orgId = await getFromLocalStorage(storageKeys.ORG_ID); + const resCreatePolygonKeys = await createPolygonKeyValuePair(orgId); + const { data } = resCreatePolygonKeys as AxiosResponse; + + if (data?.statusCode === apiStatusCodes.API_STATUS_CREATED) { + setGeneratedKeys(data?.data); + } + } catch (err) { + console.log('Generate private key ERROR::::', err); + } + }; + + const getLedgerList = async () => { + const res = await fetchNetworks(); + setNetworks(res); + }; + useEffect(() => { + getLedgerList(); + fetchLedgerConfig(); + }, []); + + useEffect(() => { + setSeedVal(seeds) + }, [seeds]) + + const showMethod = (method: string): ReactElement => { + switch (method) { + case DidMethod.POLYGON: { + return mappedData && selectedLedger && selectedDid ? ( + {mappedData[selectedLedger][selectedDid] || ''} + ) : ( + <> + ); + } + case DidMethod.INDY: { + return mappedData && + selectedLedger && + selectedNetwork && + selectedDid ? ( + + {mappedData[selectedLedger][selectedNetwork][selectedDid] || ''} + + ) : ( + <> + ); + } + + case DidMethod.KEY: + case DidMethod.WEB: { + return mappedData && selectedLedger ? ( + {mappedData[selectedLedger][selectedLedger] || ''} + ) : ( + <> + ); + } + default: + return <>; + } + }; + + const validations = { + label: yup.string().required('Wallet label is required'), + method: yup.string().required('Method is required'), + ...(DidMethod.INDY === selectedLedger || DidMethod.POLYGON === selectedLedger) && { network: yup.string().required('Network is required') }, + ...(DidMethod.INDY === selectedLedger) && { ledger: yup.string().required('Ledger is required') }, } return ( -
+
- setHaveDidShared(e.target.checked)} /> -
- { - !haveDidShared && -
-
-
-
- {seeds} - - - + {!haveDidShared ? ( + <> +
+
+
+
+ +
-
- } - submitSharedWallet(values)} - > - {(formikHandlers): JSX.Element => ( -
{ + submitSharedWallet( + values, + privateKeyValue, + domainValue, + endPointValue, + ); + + }} > - { - haveDidShared && - <> -
-
-