diff --git a/src/i18n/locales/en/auth.json b/src/i18n/locales/en/auth.json index 7f02918..5bd6c13 100644 --- a/src/i18n/locales/en/auth.json +++ b/src/i18n/locales/en/auth.json @@ -53,16 +53,20 @@ "account_creation": "could not create account.", "address_not_existing": "address does not exist on blockchain", "block_user": "unable to block user", + "create_simmetric_key": "cannot create symmetric key", "decrypt_data": "could not decrypt data", "decrypt": "unable to decrypt", "encrypt_content": "cannot encrypt content", "fetch_user_account": "unable to fetch user account", "field_not_found_json": "{{ field }} not found in JSON", + "find_secret_key": "cannot find correct secretKey", "incorrect_password": "incorrect password", "invalid_qortal_link": "invalid qortal link", "invalid_secret_key": "secretKey is not valid", + "invalid_uint8": "the Uint8ArrayData you've submitted is invalid", "name_not_existing": "name does not exist", "name_not_registered": "name not registered", + "read_blob_base64": "failed to read the Blob as a base64-encoded string", "reencrypt_secret_key": "unable to re-encrypt secret key", "set_apikey": "failed to set API key:" }, diff --git a/src/i18n/locales/en/question.json b/src/i18n/locales/en/question.json index b097459..5b48eb7 100644 --- a/src/i18n/locales/en/question.json +++ b/src/i18n/locales/en/question.json @@ -14,7 +14,9 @@ "create_sell_order": "failed to Create Sell Order. Try again!", "create_tradebot": "unable to create tradebot", "decrypt": "unable to decrypt", + "decryption_failed": "decryption failed", "encrypt": "unable to encrypt", + "encryption_failed": "encryption failed", "encryption_requires_public_key": "encrypting data requires public keys", "fetch_balance": "unable to fetch balance", "fetch_group": "failed to fetch the group", @@ -33,6 +35,7 @@ "perform_request": "failed to perform request", "process_transaction": "unable to process transaction", "retrieve_file": "failed to retrieve file", + "retrieve_keys": "unable to retrieve keys", "submit_sell_order": "failed to submit sell order", "timeout_request": "request timed out", "update_foreign_fee": "failed to update foreign fee", diff --git a/src/qdn/encryption/group-encryption.ts b/src/qdn/encryption/group-encryption.ts index 7d72ff6..24998e9 100644 --- a/src/qdn/encryption/group-encryption.ts +++ b/src/qdn/encryption/group-encryption.ts @@ -3,6 +3,7 @@ import Base58 from '../../deps/Base58'; import ed2curve from '../../deps/ed2curve'; import nacl from '../../deps/nacl-fast'; +import i18n from '../../i18n/i18n'; export function base64ToUint8Array(base64: string) { const binaryString = atob(base64); @@ -45,7 +46,13 @@ export function objectToBase64(obj: Object) { ); resolve(base64); } else { - reject(new Error('Failed to read the Blob as a base64-encoded string')); + reject( + new Error( + i18n.t('auth:message.error.read_blob_base64', { + postProcess: 'capitalizeFirstChar', + }) + ) + ); } }; reader.onerror = () => { @@ -73,10 +80,14 @@ export const encryptDataGroup = ({ let combinedPublicKeys = [...publicKeys, userPublicKey]; const decodedPrivateKey = Base58.decode(privateKey); const publicKeysDuplicateFree = [...new Set(combinedPublicKeys)]; - const Uint8ArrayData = base64ToUint8Array(data64); + if (!(Uint8ArrayData instanceof Uint8Array)) { - throw new Error("The Uint8ArrayData you've submitted is invalid"); + throw new Error( + i18n.t('auth:message.error.invalid_uint8', { + postProcess: 'capitalizeFirstChar', + }) + ); } try { // Generate a random symmetric key for the message. @@ -89,7 +100,12 @@ export const encryptDataGroup = ({ crypto.getRandomValues(messageKey); } - if (!messageKey) throw new Error('Cannot create symmetric key'); + if (!messageKey) + throw new Error( + i18n.t('auth:message.error.create_simmetric_key', { + postProcess: 'capitalizeFirstChar', + }) + ); const nonce = new Uint8Array(24); crypto.getRandomValues(nonce); // Encrypt the data with the symmetric key. @@ -170,7 +186,11 @@ export const encryptDataGroup = ({ return uint8ArrayToBase64(combinedData); } catch (error) { console.log('error', error); - throw new Error('Error in encrypting data'); + throw new Error( + i18n.t('question:message.error.encryption_failed', { + postProcess: 'capitalizeFirstChar', + }) + ); } }; @@ -192,7 +212,11 @@ export const encryptSingle = async ({ const messageKey = base64ToUint8Array(highestKeyObject.messageKey); if (!(Uint8ArrayData instanceof Uint8Array)) { - throw new Error("The Uint8ArrayData you've submitted is invalid"); + throw new Error( + i18n.t('auth:message.error.invalid_uint8', { + postProcess: 'capitalizeFirstChar', + }) + ); } let nonce, encryptedData, encryptedDataBase64, finalEncryptedData; @@ -267,7 +291,9 @@ export const decodeBase64ForUIChatMessages = (messages) => { ...msg, ...parseDecoded, }); - } catch (error) {} + } catch (error) { + console.log(error); + } } return msgs; }; @@ -291,7 +317,11 @@ export const decryptSingle = async ({ // Check if we have a valid secret key for the extracted highestKey if (!secretKeyObject[highestKey]) { - throw new Error('Cannot find correct secretKey'); + throw new Error( + i18n.t('auth:message.error.find_secret_key', { + postProcess: 'capitalizeFirstChar', + }) + ); } const secretKeyEntry = secretKeyObject[highestKey]; @@ -329,7 +359,11 @@ export const decryptSingle = async ({ // Check if decryption was successful if (!decryptedBytes) { - throw new Error('Decryption failed'); + throw new Error( + i18n.t('question:message.error.decryption_failed', { + postProcess: 'capitalizeFirstChar', + }) + ); } // Convert the decrypted Uint8Array back to a Base64 string @@ -352,7 +386,11 @@ export const decryptSingle = async ({ const messageKey = base64ToUint8Array(secretKeyEntry.messageKey); if (!(Uint8ArrayData instanceof Uint8Array)) { - throw new Error("The Uint8ArrayData you've submitted is invalid"); + throw new Error( + i18n.t('auth:message.error.invalid_uint8', { + postProcess: 'capitalizeFirstChar', + }) + ); } // Decrypt the data using the nonce and messageKey @@ -360,7 +398,11 @@ export const decryptSingle = async ({ // Check if decryption was successful if (!decryptedData) { - throw new Error('Decryption failed'); + throw new Error( + i18n.t('question:message.error.decryption_failed', { + postProcess: 'capitalizeFirstChar', + }) + ); } // Convert the decrypted Uint8Array back to a Base64 string @@ -411,7 +453,11 @@ export const decryptGroupEncryptionWithSharingKey = async ({ // Check if decryption was successful if (!decryptedData) { - throw new Error('Decryption failed'); + throw new Error( + i18n.t('question:message.error.decryption_failed', { + postProcess: 'capitalizeFirstChar', + }) + ); } // Convert the decrypted Uint8Array back to a Base64 string return uint8ArrayToBase64(decryptedData); @@ -461,7 +507,11 @@ export function decryptGroupDataQortalRequest(data64EncryptedData, privateKey) { encryptedDataEndPosition + count * 48 ); if (!privateKey) { - throw new Error('Unable to retrieve keys'); + throw new Error( + i18n.t('question:message.error.retrieve_keys', { + postProcess: 'capitalizeFirstChar', + }) + ); } const decodedPrivateKey = Base58.decode(privateKey); const convertedPrivateKey = ed2curve.convertSecretKey(decodedPrivateKey); @@ -494,7 +544,11 @@ export function decryptGroupDataQortalRequest(data64EncryptedData, privateKey) { } } } - throw new Error('Unable to decrypt data'); + throw new Error( + i18n.t('question:message.error.decrypt', { + postProcess: 'capitalizeFirstChar', + }) + ); } export function decryptGroupData( @@ -544,7 +598,11 @@ export function decryptGroupData( encryptedDataEndPosition + count * 48 ); if (!privateKey) { - throw new Error('Unable to retrieve keys'); // TODO translate + throw new Error( + i18n.t('question:message.error.retrieve_keys', { + postProcess: 'capitalizeFirstChar', + }) + ); } const decodedPrivateKey = Base58.decode(privateKey); const convertedPrivateKey = ed2curve.convertSecretKey(decodedPrivateKey); @@ -578,7 +636,11 @@ export function decryptGroupData( } } } - throw new Error('Unable to decrypt data'); + throw new Error( + i18n.t('question:message.error.decrypt', { + postProcess: 'capitalizeFirstChar', + }) + ); } export function uint8ArrayStartsWith(uint8Array, string) { @@ -609,7 +671,11 @@ export function decryptDeprecatedSingle(uint8Array, publicKey, privateKey) { const _publicKey = window.parent.Base58.decode(publicKey); if (!privateKey || !_publicKey) { - throw new Error('Unable to retrieve keys'); + throw new Error( + i18n.t('question:message.error.retrieve_keys', { + postProcess: 'capitalizeFirstChar', + }) + ); } const convertedPrivateKey = ed2curve.convertSecretKey(privateKey); const convertedPublicKey = ed2curve.convertPublicKey(_publicKey); @@ -628,7 +694,11 @@ export function decryptDeprecatedSingle(uint8Array, publicKey, privateKey) { _chatEncryptionSeed ); if (!_decryptedData) { - throw new Error('Unable to decrypt'); + throw new Error( + i18n.t('question:message.error.decrypt', { + postProcess: 'capitalizeFirstChar', + }) + ); } return uint8ArrayToBase64(_decryptedData); }