Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file import on importview #2495

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion src/renderer/hooks/useKeyImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,82 @@ export default function useKeyImport() {
const [key, setKey] = useState<ImportData>({});
const [error, setError] = useState<string | null>(null);

const isValidFileName = (fileName: string) => {
const utcRegex =
/^UTC--\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}Z--[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}.json$/i;
return utcRegex.test(fileName);
};

//파일 형식 검증
const isImageFile = (fileName: string) => {
const imageExtensions = /\.(png|jpg|jpeg)$/i;
return imageExtensions.test(fileName);
};

// 파일 안 내용 검증
function validateWeb3SecretStorage(json: any): boolean {
const schema: { [key: string]: any } = {
version: "number",
id: "string",
address: "string",
crypto: {
ciphertext: "string",
cipherparams: {
iv: "string",
},
cipher: "string",
kdf: "string",
kdfparams: {
c: "number",
dklen: "number",
prf: "string",
salt: "string",
},
mac: "string",
},
};

function validate(obj: any, schema: any): boolean {
for (const key in schema) {
if (typeof schema[key] === "object") {
if (!obj[key] || typeof obj[key] !== "object") return false;
if (!validate(obj[key], schema[key])) return false;
} else if (typeof obj[key] !== schema[key]) {
return false;
}
}
return true;
}

return validate(json, schema);
}

const handleSubmit = async () => {
let privateKey: RawPrivateKey;
if (key.keyFile) {
const fileName = key.keyFile.name;

try {
const keystore = await decodeQRCode(key.keyFile);
//qr디코딩 없이 일반 파일로 처리
const keyFileText = await key.keyFile.text();
let keystore;

if (isImageFile(fileName)) {
keystore = await decodeQRCode(key.keyFile);
} else if (isValidFileName(fileName)) {
const parsedKeyFile = JSON.parse(keyFileText);

// JSON 내용 검증
if (!validateWeb3SecretStorage(parsedKeyFile)) {
setError(t("Invalid keystore JSON"));
return;
}
keystore = keyFileText;
} else {
setError(t("Invalid keyFile text"));
return;
}

const { id, address }: { id: string; address: string } =
JSON.parse(keystore);
try {
Expand Down
Loading