Skip to content

Commit

Permalink
change function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Sep 26, 2023
1 parent 3bce778 commit 96b8124
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/components/Cards/AddressCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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
);

Expand Down
5 changes: 3 additions & 2 deletions src/components/Modals/AddAddress/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
9 changes: 6 additions & 3 deletions src/components/Modals/EditAddress/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +40,7 @@ export const EditAddressModal = ({
const [newAddress, setNewAddress] = useState<string>('');
const [working, setWorking] = useState(false);
const [regenerate, setRegenerate] = useState(false);
const { relay } = useRelay();

const onSave = async () => {
if (identityNo === null) {
Expand Down Expand Up @@ -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
);

Expand Down
8 changes: 7 additions & 1 deletion src/components/Modals/ShareIdentity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +36,7 @@ export const ShareIdentityModal = ({
const { toastError, toastSuccess } = useToast();
const [checks, setChecks] = useState<Record<number, boolean>>({});
const [sharedKey, setSharedKey] = useState('');
const { relay } = useRelay();

useEffect(() => {
if (identityNo === null) return;
Expand All @@ -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}`);
Expand Down
5 changes: 3 additions & 2 deletions src/pages/transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 14 additions & 13 deletions src/utils/identityKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 96b8124

Please sign in to comment.