Skip to content

Commit

Permalink
Identity key improvements (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo authored Sep 12, 2023
1 parent 001eee3 commit 996c120
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 30 deletions.
16 changes: 13 additions & 3 deletions src/components/Modals/ImportKey/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ export const ImportKeyModal = ({
toastError("You don't have an identity yet.");
return;
}
let key = identityKey;
if (key.startsWith("identityNo:")) {
const indexOfSeparator = key.indexOf(';');
key = key.substring(indexOfSeparator + 1);
}
confirm({
description:
'This operation updates the identity key and you might lose access to your addresses.',
}).then(() => {
KeyStore.updateIdentityKey(identityNo, identityKey);
toastSuccess('Successfully imported identity key.');
onClose();
try {
KeyStore.updateIdentityKey(identityNo, key);
toastSuccess('Successfully imported identity key.');
} catch (e) {
toastError("Invalid identity key");
} finally {
onClose();
}
});
};

Expand Down
17 changes: 2 additions & 15 deletions src/components/Modals/ShareIdentity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const ShareIdentityModal = ({

try {
const sharedKey = IdentityKey.getSharedKey(identityKey, selectedChains);
setSharedKey(sharedKey);
setSharedKey(`identityNo:${identityNo};`.concat(sharedKey));
} catch (e: any) {
toastError(`Failed to get the identity key. Error: ${e.message}`);
}
Expand All @@ -62,17 +62,6 @@ export const ShareIdentityModal = ({
<Box className='modal-wrapper'>
<DialogTitle>Share Identity</DialogTitle>
<DialogContent>
<Box className={styles.identityNo}>
<Typography>{`Identity No: ${identityNo}`}</Typography>
<CopyToClipboard
text={identityNo.toString()}
onCopy={() => toastSuccess('Identity no copied to clipboard.')}
>
<IconButton>
<ContentCopyIcon />
</IconButton>
</CopyToClipboard>
</Box>
<Typography mt='2em'>
Specify the chains that the receiver of the identity key will be
able to access:
Expand Down Expand Up @@ -110,9 +99,7 @@ export const ShareIdentityModal = ({
</CopyToClipboard>
</Box>
<Typography color='primary' align='center'>
{'This identity key has to be sent alongside the identity no.'}
<br />
{"Without it, the receiver won't have access to your addresses"}
{'This identity key will be sent alongside the identity no.'}
</Typography>
<Button
className='btn-ok'
Expand Down
4 changes: 2 additions & 2 deletions src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type RELAY_CHAIN_OPTION = 'polkadot' | 'kusama';
const RELAY_CHAIN_ENDPOINTS = {
polkadot: "wss://polkadot.api.onfinality.io/public-ws",
kusama: "wss://kusama.api.onfinality.io/public-ws"
polkadot: "wss://rpc.polkadot.io",
kusama: "wss://kusama-rpc.polkadot.io"
};
export const RELAY_CHAIN = (process.env.RELAY_CHAIN || 'polkadot') as RELAY_CHAIN_OPTION;
export const RELAY_CHAIN_ENDPOINT = RELAY_CHAIN_ENDPOINTS[RELAY_CHAIN];
Expand Down
17 changes: 11 additions & 6 deletions src/pages/transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ const TransferPage = () => {
return true;
}

const isSourceParachain = sourceChainId > 0;
// Even if it is we want to get the perspective from the relay chain.
const isSourceParachain = false;

if (
sourceChainId !== destChainId &&
Expand Down Expand Up @@ -430,11 +431,15 @@ const TransferPage = () => {
value={recipientId === undefined ? '' : recipientId}
onChange={(e) => setRecipientId(Number(e.target.value))}
>
{identities.map((identity, index) => (
<MenuItem value={index} key={index}>
{identity.nickName}
</MenuItem>
))}
{identities.filter(
(identity) =>
IdentityKey.containsChainId(
KeyStore.readIdentityKey(identity.identityNo) || '', destChainId
)).map((identity, index) => (
<MenuItem value={index} key={index}>
{identity.nickName}
</MenuItem>
))}
</Select>
</FormControl>
)}
Expand Down
49 changes: 45 additions & 4 deletions src/utils/identityKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@ import aesjs from "aes-js";
import crypto from "crypto";

class IdentityKey {
public static sanityCheck(identityKey: string): boolean {
let key = identityKey;

const separatorIndex = key.indexOf(';')
if (separatorIndex === -1) {
return false;
}

if (key.startsWith("identityNo:")) {
if (key.length === separatorIndex + 1) {
return true;
}
key = key.substring(separatorIndex + 1);
}

let result = true;

while (result) {
const separatorIndex = key.indexOf(';')
if (separatorIndex === -1) {
break;
}
const colonIndex = key.indexOf(':');
if (colonIndex === -1) {
result = false;
break;
}

const cipher = Buffer.from(key.substring(colonIndex + 1, separatorIndex - 1), "base64");
if (cipher.length !== 16) {
result = false;
break;
}

key = key.substring(separatorIndex + 1);
}

return result;
}

public static newCipher(identityKey: string, chainId: number): string {
const regexPattern = new RegExp(`\\b${chainId}:`, "g");
if (regexPattern.test(identityKey)) {
Expand Down Expand Up @@ -55,21 +95,22 @@ class IdentityKey {

if (startIndex >= 0) {
const endIndex = identityKey.indexOf(";", startIndex);
return identityKey.substring(startIndex + chainId.toString().length + 1, endIndex - 1);
return identityKey.substring(startIndex + chainId.toString().length + 1, endIndex);
} else {
throw new Error("Cannot find chainId");
}
}

public static getSharedKey(identityKey: string, selectedChains: number[]): string {
let key = JSON.parse(JSON.stringify(identityKey));
let sharedKey = "";
selectedChains.forEach((chainId) => {
if (!IdentityKey.containsChainId(identityKey, chainId)) {
identityKey = IdentityKey.newCipher(identityKey, chainId);
if (!IdentityKey.containsChainId(key, chainId)) {
key = IdentityKey.newCipher(key, chainId);
throw new Error(`Cipher for chain #${chainId} not found`);
}
sharedKey += `${chainId}:${IdentityKey.getChainCipher(
identityKey,
key,
chainId
)};`;
});
Expand Down
8 changes: 8 additions & 0 deletions src/utils/keyStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import IdentityKey from "./identityKey";

const LS_KEY_STORE = 'dotflow-key-store';

interface IKeyStore {
Expand All @@ -21,6 +23,12 @@ class KeyStore {
}

public static updateKeyStore(ks: IKeyStore) {
for (const key in ks) {
if (!IdentityKey.sanityCheck(JSON.parse(JSON.stringify(ks[key])))) {
throw new Error("The identity key is invalid");
}
}

localStorage.setItem(LS_KEY_STORE, JSON.stringify(ks));
}

Expand Down

0 comments on commit 996c120

Please sign in to comment.