From 96b8124ba3e8deda1c679f846f4847f610b9249b Mon Sep 17 00:00:00 2001 From: Sergej Sakac Date: Tue, 26 Sep 2023 20:24:12 +0200 Subject: [PATCH] change function signatures --- src/components/Cards/AddressCard/index.tsx | 5 +++- src/components/Modals/AddAddress/index.tsx | 5 ++-- src/components/Modals/EditAddress/index.tsx | 9 ++++--- src/components/Modals/ShareIdentity/index.tsx | 8 +++++- src/pages/transfer.tsx | 5 ++-- src/utils/identityKey.ts | 27 ++++++++++--------- 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/components/Cards/AddressCard/index.tsx b/src/components/Cards/AddressCard/index.tsx index 075c8e6..868607a 100644 --- a/src/components/Cards/AddressCard/index.tsx +++ b/src/components/Cards/AddressCard/index.tsx @@ -22,6 +22,7 @@ import { useIdentity } from '@/contracts'; import { Address, ChainId } from '@/contracts/types'; import styles from './index.module.scss'; +import { useRelay } from '@/contexts/RelayApi'; interface AddressCardProps { data: Address; onEdit?: () => void; @@ -37,6 +38,7 @@ export const AddressCard = ({ data, onEdit }: AddressCardProps) => { const { api, activeAccount } = useInkathon(); const { toastSuccess, toastError } = useToast(); const { identityNo, chains, contract, fetchAddresses } = useIdentity(); + const { relay } = useRelay(); const [working, setWorking] = useState(false); @@ -82,10 +84,11 @@ export const AddressCard = ({ data, onEdit }: AddressCardProps) => { const identityKey = KeyStore.readIdentityKey(identityNo) || ''; let decryptedAddress = address; - if (IdentityKey.containsChainId(identityKey, chainId)) { + if (IdentityKey.containsChainId(identityKey, chainId, relay)) { decryptedAddress = IdentityKey.decryptAddress( identityKey, chainId, + relay, address ); diff --git a/src/components/Modals/AddAddress/index.tsx b/src/components/Modals/AddAddress/index.tsx index e87e753..f90fc4b 100644 --- a/src/components/Modals/AddAddress/index.tsx +++ b/src/components/Modals/AddAddress/index.tsx @@ -72,14 +72,15 @@ export const AddAddressModal = ({ open, onClose }: AddAddressModalProps) => { let identityKey = KeyStore.readIdentityKey(identityNo) || ''; - if (!IdentityKey.containsChainId(identityKey, chainId)) { - identityKey = IdentityKey.newCipher(identityKey, chainId); + if (!IdentityKey.containsChainId(identityKey, chainId, relay)) { + identityKey = IdentityKey.newCipher(identityKey, chainId, relay); KeyStore.updateIdentityKey(identityNo, identityKey); } const encryptedAddress = IdentityKey.encryptAddress( identityKey, chainId, + relay, chainAddress ); diff --git a/src/components/Modals/EditAddress/index.tsx b/src/components/Modals/EditAddress/index.tsx index 46ab3ad..9d30f4b 100644 --- a/src/components/Modals/EditAddress/index.tsx +++ b/src/components/Modals/EditAddress/index.tsx @@ -22,6 +22,7 @@ import KeyStore from '@/utils/keyStore'; import { useToast } from '@/contexts/Toast'; import { useIdentity } from '@/contracts'; import { ChainId } from '@/contracts/types'; +import { useRelay } from '@/contexts/RelayApi'; interface EditAddressModalProps { open: boolean; @@ -39,6 +40,7 @@ export const EditAddressModal = ({ const [newAddress, setNewAddress] = useState(''); const [working, setWorking] = useState(false); const [regenerate, setRegenerate] = useState(false); + const { relay } = useRelay(); const onSave = async () => { if (identityNo === null) { @@ -69,17 +71,18 @@ export const EditAddressModal = ({ let identityKey = KeyStore.readIdentityKey(identityNo) || ''; - if (!IdentityKey.containsChainId(identityKey, chainId)) { - identityKey = IdentityKey.newCipher(identityKey, chainId); + if (!IdentityKey.containsChainId(identityKey, chainId, relay)) { + identityKey = IdentityKey.newCipher(identityKey, chainId, relay); KeyStore.updateIdentityKey(identityNo, identityKey); } if (regenerate) - identityKey = IdentityKey.updateCipher(identityKey, chainId); + identityKey = IdentityKey.updateCipher(identityKey, chainId, relay); const encryptedAddress = IdentityKey.encryptAddress( identityKey, chainId, + relay, newAddress ); diff --git a/src/components/Modals/ShareIdentity/index.tsx b/src/components/Modals/ShareIdentity/index.tsx index 9517c03..e80b15a 100644 --- a/src/components/Modals/ShareIdentity/index.tsx +++ b/src/components/Modals/ShareIdentity/index.tsx @@ -21,6 +21,7 @@ import { useToast } from '@/contexts/Toast'; import { useIdentity } from '@/contracts'; import styles from './index.module.scss'; +import { useRelay } from '@/contexts/RelayApi'; interface ShareIdentityModalProps { open: boolean; @@ -35,6 +36,7 @@ export const ShareIdentityModal = ({ const { toastError, toastSuccess } = useToast(); const [checks, setChecks] = useState>({}); const [sharedKey, setSharedKey] = useState(''); + const { relay } = useRelay(); useEffect(() => { if (identityNo === null) return; @@ -45,8 +47,12 @@ export const ShareIdentityModal = ({ const identityKey = KeyStore.readIdentityKey(identityNo) || ''; + if (identityKey === "") { + return; + } + try { - const sharedKey = IdentityKey.getSharedKey(identityKey, selectedChains); + const sharedKey = IdentityKey.getSharedKey(identityKey, selectedChains, relay); setSharedKey(`identityNo:${identityNo};`.concat(sharedKey)); } catch (e: any) { toastError(`Failed to get the identity key. Error: ${e.message}`); diff --git a/src/pages/transfer.tsx b/src/pages/transfer.tsx index 5fc5a07..13631a9 100644 --- a/src/pages/transfer.tsx +++ b/src/pages/transfer.tsx @@ -157,11 +157,12 @@ const TransferPage = () => { const recepientIdentityNo = identities[recipientId].identityNo; const identityKey = KeyStore.readIdentityKey(recepientIdentityNo) || ''; const destAddressRaw = addresses[index].address; - if (IdentityKey.containsChainId(identityKey, destChainId)) { + if (IdentityKey.containsChainId(identityKey, destChainId, relay)) { const decryptedAddress = IdentityKey.decryptAddress( identityKey, destChainId, - destAddressRaw + relay, + destAddressRaw, ); setRecipientAddress(decryptedAddress); } else { diff --git a/src/utils/identityKey.ts b/src/utils/identityKey.ts index 8ef3585..2d06420 100644 --- a/src/utils/identityKey.ts +++ b/src/utils/identityKey.ts @@ -42,7 +42,7 @@ class IdentityKey { return result; } - public static newCipher(identityKey: string, chainId: number): string { + public static newCipher(identityKey: string, chainId: number, relay: string): string { const regexPattern = new RegExp(`\\b${chainId}:`, "g"); if (regexPattern.test(identityKey)) { throw new Error("There already exists a cipher that is attached to the provided chainId"); @@ -54,7 +54,7 @@ class IdentityKey { return identityKey; } - public static updateCipher(identityKey: string, chainId: number): string { + public static updateCipher(identityKey: string, chainId: number, relay: string): string { const startIndex = identityKey.indexOf(`${chainId}:`); if (startIndex >= 0) { @@ -70,8 +70,8 @@ class IdentityKey { return identityKey; } - public static encryptAddress(identityKey: string, chainId: number, address: string): string { - const cipher = this.getChainCipher(identityKey, chainId); + public static encryptAddress(identityKey: string, chainId: number, address: string, relay: string): string { + const cipher = this.getChainCipher(identityKey, chainId, relay); const cipherBase64 = Buffer.from(cipher, "base64"); const aesCtr = new aesjs.ModeOfOperation.ctr(cipherBase64); @@ -80,8 +80,8 @@ class IdentityKey { return Buffer.from(encryptedAddress).toString("base64"); } - public static decryptAddress(identityKey: string, chainId: number, address: string): string { - const cipher = this.getChainCipher(identityKey, chainId); + public static decryptAddress(identityKey: string, chainId: number, address: string, relay: string): string { + const cipher = this.getChainCipher(identityKey, chainId, relay); const cipherBase64 = Buffer.from(cipher, "base64"); const aesCtr = new aesjs.ModeOfOperation.ctr(cipherBase64); @@ -90,7 +90,7 @@ class IdentityKey { return Buffer.from(decryptedAddress.buffer).toString(); } - public static getChainCipher(identityKey: string, chainId: number): string { + public static getChainCipher(identityKey: string, chainId: number, relay: string): string { const startIndex = identityKey.indexOf(`${chainId}:`); if (startIndex >= 0) { @@ -101,25 +101,26 @@ class IdentityKey { } } - public static getSharedKey(identityKey: string, selectedChains: number[]): string { + public static getSharedKey(identityKey: string, selectedChains: number[], relay: string): string { let key = JSON.parse(JSON.stringify(identityKey)); let sharedKey = ""; selectedChains.forEach((chainId) => { - if (!IdentityKey.containsChainId(key, chainId)) { - key = IdentityKey.newCipher(key, chainId); + if (!IdentityKey.containsChainId(key, chainId, relay)) { + key = IdentityKey.newCipher(key, chainId, relay); throw new Error(`Cipher for chain #${chainId} not found`); } sharedKey += `${chainId}:${IdentityKey.getChainCipher( key, - chainId + chainId, + relay )};`; }); return sharedKey; } - public static containsChainId(identityKey: string, chainId: number): boolean { - const startIndex = identityKey.indexOf(`${chainId}:`); + public static containsChainId(identityKey: string, chainId: number, relay: string): boolean { + const startIndex = identityKey.indexOf(`${chainId}${relay}:`); return startIndex >= 0 ? true : false; }