Skip to content

Commit

Permalink
sanity check
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Sep 11, 2023
1 parent e4ac5ea commit ba62110
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/components/Modals/ImportKey/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,20 @@ export const ImportKeyModal = ({
let key = identityKey;
if (key.startsWith("identityNo:")) {
const indexOfSeparator = key.indexOf(';');
key = key.substring(indexOfSeparator);
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, key);
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
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 === key.length) {
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 !== 32) {
result = false;
break;
}

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 ba62110

Please sign in to comment.