From 37672c309b276820fae2759e1816bcc42eeaf74f Mon Sep 17 00:00:00 2001 From: Nicola Benaglia Date: Thu, 22 May 2025 20:25:35 +0200 Subject: [PATCH] Refactor unable_ from auth --- src/backgroundFunctions/encryption.ts | 39 ++++++-- src/components/Chat/AdminSpaceInner.tsx | 4 +- src/components/Embeds/AttachmentEmbed.tsx | 2 +- src/components/Embeds/Embed.tsx | 2 +- src/components/Group/BlockedUsersModal.tsx | 2 +- src/components/NotAuthenticated.tsx | 10 +- src/i18n/locales/en/auth.json | 11 ++- src/i18n/locales/en/question.json | 12 ++- src/qortalRequests/get.ts | 106 +++++++++++++++------ 9 files changed, 137 insertions(+), 51 deletions(-) diff --git a/src/backgroundFunctions/encryption.ts b/src/backgroundFunctions/encryption.ts index 6cc0150..aa2c023 100644 --- a/src/backgroundFunctions/encryption.ts +++ b/src/backgroundFunctions/encryption.ts @@ -1,4 +1,5 @@ import { getBaseApi } from '../background'; +import i18n from '../i18n/i18n'; import { createSymmetricKeyAndNonce, decryptGroupData, @@ -40,7 +41,11 @@ async function findUsableApi() { } } - throw new Error('No usable API found'); + throw new Error( + i18n.t('question:message.error.no_api_found', { + postProcess: 'capitalizeFirstChar', + }) + ); } async function getSaveWallet() { @@ -178,7 +183,11 @@ export const encryptAndPublishSymmetricKeyGroupChat = async ({ numberOfMembers: groupmemberPublicKeys.length, }; } else { - throw new Error('Cannot encrypt content'); + throw new Error( + i18n.t('auth:message.error.encrypt_content', { + postProcess: 'capitalizeFirstChar', + }) + ); } } catch (error: any) { throw new Error(error.message); @@ -242,7 +251,11 @@ export const encryptAndPublishSymmetricKeyGroupChatForAdmins = async ({ numberOfMembers: groupmemberPublicKeys.length, }; } else { - throw new Error('Cannot encrypt content'); + throw new Error( + i18n.t('auth:message.error.encrypt_content', { + postProcess: 'capitalizeFirstChar', + }) + ); } } catch (error: any) { throw new Error(error.message); @@ -256,7 +269,12 @@ export const publishGroupEncryptedResource = async ({ try { if (encryptedData && identifier) { const registeredName = await getNameInfo(); - if (!registeredName) throw new Error('You need a name to publish'); + if (!registeredName) + throw new Error( + i18n.t('core:message.generic.name_publish', { + postProcess: 'capitalizeFirstChar', + }) + ); const data = await publishData({ registeredName, file: encryptedData, @@ -268,7 +286,11 @@ export const publishGroupEncryptedResource = async ({ }); return data; } else { - throw new Error('Cannot encrypt content'); + throw new Error( + i18n.t('auth:message.error.encrypt_content', { + postProcess: 'capitalizeFirstChar', + }) + ); } } catch (error: any) { throw new Error(error.message); @@ -291,7 +313,12 @@ export const publishOnQDN = async ({ }) => { if (data && service) { const registeredName = await getNameInfo(); - if (!registeredName) throw new Error('You need a name to publish'); + if (!registeredName) + throw new Error( + i18n.t('core:message.generic.name_publish', { + postProcess: 'capitalizeFirstChar', + }) + ); const res = await publishData({ registeredName, diff --git a/src/components/Chat/AdminSpaceInner.tsx b/src/components/Chat/AdminSpaceInner.tsx index 1dae3af..0312e5d 100644 --- a/src/components/Chat/AdminSpaceInner.tsx +++ b/src/components/Chat/AdminSpaceInner.tsx @@ -162,7 +162,7 @@ export const AdminSpaceInner = ({ type: 'error', message: response?.error || - t('auth:message.error.unable_reencrypt_secret_key', { + t('auth:message.error.reencrypt_secret_key', { postProcess: 'capitalizeFirstChar', }), }); @@ -173,7 +173,7 @@ export const AdminSpaceInner = ({ type: 'error', message: error?.message || - t('auth:message.error.unable_reencrypt_secret_key', { + t('auth:message.error.reencrypt_secret_key', { postProcess: 'capitalizeFirstChar', }), }); diff --git a/src/components/Embeds/AttachmentEmbed.tsx b/src/components/Embeds/AttachmentEmbed.tsx index 33f2dcd..5a30d2d 100644 --- a/src/components/Embeds/AttachmentEmbed.tsx +++ b/src/components/Embeds/AttachmentEmbed.tsx @@ -85,7 +85,7 @@ export const AttachmentCard = ({ } } catch (error) { throw new Error( - t('auth:message.error.unable_decrypt', { + t('auth:message.error.decrypt', { postProcess: 'capitalizeFirstChar', }) ); diff --git a/src/components/Embeds/Embed.tsx b/src/components/Embeds/Embed.tsx index 227db97..fd9a581 100644 --- a/src/components/Embeds/Embed.tsx +++ b/src/components/Embeds/Embed.tsx @@ -180,7 +180,7 @@ export const Embed = ({ embedLink }) => { } } catch (error) { throw new Error( - t('auth:message.error.unable_decrypt', { + t('auth:message.error.decrypt', { postProcess: 'capitalizeFirstChar', }) ); diff --git a/src/components/Group/BlockedUsersModal.tsx b/src/components/Group/BlockedUsersModal.tsx index adc0a10..99c9191 100644 --- a/src/components/Group/BlockedUsersModal.tsx +++ b/src/components/Group/BlockedUsersModal.tsx @@ -141,7 +141,7 @@ export const BlockedUsersModal = () => { type: 'error', message: error?.message || - t('auth:message.error.unable_block_user', { + t('auth:message.error.block_user', { postProcess: 'capitalizeFirstChar', }), }); diff --git a/src/components/NotAuthenticated.tsx b/src/components/NotAuthenticated.tsx index 6100aed..a87fdce 100644 --- a/src/components/NotAuthenticated.tsx +++ b/src/components/NotAuthenticated.tsx @@ -359,7 +359,7 @@ export const NotAuthenticated = ({ }) .catch((error) => { console.error( - it('auth:message.error.unable_set_apikey', { + it('auth:message.error.set_apikey', { postProcess: 'capitalizeFirstChar', }), error.message || @@ -398,7 +398,7 @@ export const NotAuthenticated = ({ }) .catch((error) => { console.error( - it('auth:message.error.unable_set_apikey', { + it('auth:message.error.set_apikey', { postProcess: 'capitalizeFirstChar', }), error.message || @@ -682,7 +682,7 @@ export const NotAuthenticated = ({ }) .catch((error) => { console.error( - it('auth:message.error.unable_set_apikey', { + it('auth:message.error.set_apikey', { postProcess: 'capitalizeFirstChar', }), error.message || @@ -836,7 +836,7 @@ export const NotAuthenticated = ({ }) .catch((error) => { console.error( - it('auth:message.error.unable_set_apikey', { + it('auth:message.error.set_apikey', { postProcess: 'capitalizeFirstChar', }), error.message || @@ -902,7 +902,7 @@ export const NotAuthenticated = ({ }) .catch((error) => { console.error( - it('auth:message.error.unable_set_apikey', { + it('auth:message.error.set_apikey', { postProcess: 'capitalizeFirstChar', }), error.message || diff --git a/src/i18n/locales/en/auth.json b/src/i18n/locales/en/auth.json index 0f67622..7f02918 100644 --- a/src/i18n/locales/en/auth.json +++ b/src/i18n/locales/en/auth.json @@ -52,18 +52,19 @@ "error": { "account_creation": "could not create account.", "address_not_existing": "address does not exist on blockchain", + "block_user": "unable to block user", "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", "incorrect_password": "incorrect password", "invalid_qortal_link": "invalid qortal link", "invalid_secret_key": "secretKey is not valid", "name_not_existing": "name does not exist", "name_not_registered": "name not registered", - "unable_block_user": "unable to block user", - "unable_set_apikey": "failed to set API key:", - "unable_fetch_user_account": "unable to fetch user account", - "unable_decrypt": "unable to decrypt", - "unable_reencrypt_secret_key": "unable to re-encrypt secret key" + "reencrypt_secret_key": "unable to re-encrypt secret key", + "set_apikey": "failed to set API key:" }, "generic": { "blocked_addresses": "blocked addresses- blocks processing of txs", diff --git a/src/i18n/locales/en/question.json b/src/i18n/locales/en/question.json index 8e95f09..b097459 100644 --- a/src/i18n/locales/en/question.json +++ b/src/i18n/locales/en/question.json @@ -15,14 +15,18 @@ "create_tradebot": "unable to create tradebot", "decrypt": "unable to decrypt", "encrypt": "unable to encrypt", + "encryption_requires_public_key": "encrypting data requires public keys", "fetch_balance": "unable to fetch balance", "fetch_group": "failed to fetch the group", "fetch_list": "failed to fetch the list", "fetch_poll": "failed to fetch poll", "insufficient_balance_qort": "your QORT balance is insufficient", "insufficient_balance": "your asset balance is insufficient", + "insufficient_funds": "insufficient funds", "missing_fields": "missing fields: {{ fields }}", + "no_api_found": "no usable API found", "no_data_encrypted_resource": "no data in the encrypted resource", + "no_data_file_submitted": "no data or file was submitted", "no_group_found": "group not found", "no_group_key": "no group key found", "no_poll": "poll not found", @@ -32,7 +36,9 @@ "submit_sell_order": "failed to submit sell order", "timeout_request": "request timed out", "update_foreign_fee": "failed to update foreign fee", - "user_declined_request": "user declined request" + "upload_encryption": "upload failed due to failed encryption", + "user_declined_request": "user declined request", + "user_qortal_name": "user has no Qortal name" }, "generic": { "include_data_decrypt": "please include data to decrypt", @@ -52,8 +58,10 @@ "permission_list_hosted_data": "do you give this application permission to get a list of your hosted data?", "permission_delete_hosts_resources": "do you give this application permission to delete {{ size }} hosted resources?", "permission_pay_publish": "do you give this application permission to make the following payments and publishes?", + "permission_send_coins": "do you give this application permission to send coins?", "permission_transfer_asset": "do you give this application permission to transfer the following asset?", "poll": "poll: {{ name }}", "request_create_poll": "you are requesting to create the poll below:", - "request_vote_poll": "you are being requested to vote on the poll below:" + "request_vote_poll": "you are being requested to vote on the poll below:", + "to_recipient": "to: {{ recipient }}" } diff --git a/src/qortalRequests/get.ts b/src/qortalRequests/get.ts index 345642e..66db2ed 100644 --- a/src/qortalRequests/get.ts +++ b/src/qortalRequests/get.ts @@ -538,7 +538,7 @@ export const getUserAccount = async ({ } } catch (error) { throw new Error( - i18n.t('auth:message.error.unable_fetch_user_account', { + i18n.t('auth:message.error.fetch_user_account', { postProcess: 'capitalizeFirstChar', }) ); @@ -1288,7 +1288,11 @@ export const publishQDNResource = async ( throw new Error(errorMsg); } if (!data.file && !data.data64 && !data.base64) { - throw new Error('No data or file was submitted'); + throw new Error( + i18n.t('question:message.error.no_data_file_submitted', { + postProcess: 'capitalizeFirstChar', + }) + ); } // Use "default" if user hasn't specified an identifier const service = data.service; @@ -1301,8 +1305,13 @@ export const publishQDNResource = async ( const registeredName = await getNameInfo(); const name = registeredName; if (!name) { - throw new Error('User has no Qortal name'); + throw new Error( + i18n.t('question:message.error.user_qortal_name', { + postProcess: 'capitalizeFirstChar', + }) + ); } + let identifier = data.identifier; let data64 = data.data64 || data.base64; const filename = data.filename; @@ -1332,7 +1341,11 @@ export const publishQDNResource = async ( (!data.publicKeys || (Array.isArray(data.publicKeys) && data.publicKeys.length === 0)) ) { - throw new Error('Encrypting data requires public keys'); + throw new Error( + i18n.t('question:message.error.encryption_requires_public_key', { + postProcess: 'capitalizeFirstChar', + }) + ); } if (data.encrypt) { @@ -1352,7 +1365,10 @@ export const publishQDNResource = async ( } } catch (error) { throw new Error( - error.message || 'Upload failed due to failed encryption' + error.message || + i18n.t('question:message.error.upload_encryption', { + postProcess: 'capitalizeFirstChar', + }) ); } } @@ -1693,7 +1709,10 @@ export const publishMultipleQDNResources = async ( } } catch (error) { const errorMsg = - error?.message || 'Upload failed due to failed encryption'; + error?.message || + i18n.t('question:message.error.upload_encryption', { + postProcess: 'capitalizeFirstChar', + }); failedPublishesIdentifiers.push({ reason: errorMsg, identifier: resource.identifier, @@ -3558,7 +3577,7 @@ export const sendCoin = async (data, isFromExtension) => { walletBalance = await response.text(); } if (isNaN(Number(walletBalance))) { - let errorMsg = 'Failed to Fetch QORT Balance. Try again!'; + const errorMsg = 'Failed to Fetch QORT Balance. Try again!'; throw new Error(errorMsg); } @@ -3569,21 +3588,25 @@ export const sendCoin = async (data, isFromExtension) => { const amountDecimals = Number(amount) * QORT_DECIMALS; const fee: number = await sendQortFee(); if (amountDecimals + fee * QORT_DECIMALS > walletBalanceDecimals) { - let errorMsg = 'Insufficient Funds!'; + const errorMsg = i18n.t('question:message.error.insufficient_funds', { + postProcess: 'capitalizeFirstChar', + }); throw new Error(errorMsg); } if (amount <= 0) { - let errorMsg = 'Invalid Amount!'; + const errorMsg = 'Invalid Amount!'; throw new Error(errorMsg); } if (recipient.length === 0) { - let errorMsg = 'Receiver cannot be empty!'; + const errorMsg = 'Receiver cannot be empty!'; throw new Error(errorMsg); } const resPermission = await getUserPermission( { - text1: 'Do you give this application permission to send coins?', + text1: i18n.t('question:permission_send_coins', { + postProcess: 'capitalizeFirstChar', + }), text2: `To: ${recipient}`, highlightedText: `${amount} ${checkCoin}`, fee: fee, @@ -3626,7 +3649,9 @@ export const sendCoin = async (data, isFromExtension) => { const resPermission = await getUserPermission( { - text1: 'Do you give this application permission to send coins?', + text1: i18n.t('question:permission_send_coins', { + postProcess: 'capitalizeFirstChar', + }), text2: `To: ${recipient}`, highlightedText: `${amount} ${checkCoin}`, foreignFee: `${fee} BTC`, @@ -3675,18 +3700,24 @@ export const sendCoin = async (data, isFromExtension) => { const ltcWalletBalance = await getWalletBalance({ coin: checkCoin }, true); if (isNaN(Number(ltcWalletBalance))) { - let errorMsg = 'Failed to Fetch LTC Balance. Try again!'; + const errorMsg = 'Failed to Fetch LTC Balance. Try again!'; throw new Error(errorMsg); } const ltcWalletBalanceDecimals = Number(ltcWalletBalance); const ltcAmountDecimals = Number(amount); const fee = feePerByte * 1000; // default 0.00030000 if (ltcAmountDecimals + fee > ltcWalletBalanceDecimals) { - throw new Error('Insufficient Funds!'); + throw new Error( + i18n.t('question:message.error.insufficient_funds', { + postProcess: 'capitalizeFirstChar', + }) + ); } const resPermission = await getUserPermission( { - text1: 'Do you give this application permission to send coins?', + text1: i18n.t('question:permission_send_coins', { + postProcess: 'capitalizeFirstChar', + }), text2: `To: ${recipient}`, highlightedText: `${amount} ${checkCoin}`, foreignFee: `${fee} LTC`, @@ -3733,21 +3764,28 @@ export const sendCoin = async (data, isFromExtension) => { const feePerByte = data.fee ? data.fee : dogeFeePerByte; const dogeWalletBalance = await getWalletBalance({ coin: checkCoin }, true); if (isNaN(Number(dogeWalletBalance))) { - let errorMsg = 'Failed to Fetch DOGE Balance. Try again!'; + const errorMsg = 'Failed to Fetch DOGE Balance. Try again!'; throw new Error(errorMsg); } const dogeWalletBalanceDecimals = Number(dogeWalletBalance); const dogeAmountDecimals = Number(amount); const fee = feePerByte * 5000; // default 0.05000000 if (dogeAmountDecimals + fee > dogeWalletBalanceDecimals) { - let errorMsg = 'Insufficient Funds!'; + const errorMsg = i18n.t('question:message.error.insufficient_funds', { + postProcess: 'capitalizeFirstChar', + }); throw new Error(errorMsg); } const resPermission = await getUserPermission( { - text1: 'Do you give this application permission to send coins?', - text2: `To: ${recipient}`, + text1: i18n.t('question:permission_send_coins', { + postProcess: 'capitalizeFirstChar', + }), + text2: i18n.t('question:to_recipient', { + recipient: recipient, + postProcess: 'capitalizeFirstChar', + }), highlightedText: `${amount} ${checkCoin}`, foreignFee: `${fee} DOGE`, }, @@ -3794,20 +3832,24 @@ export const sendCoin = async (data, isFromExtension) => { const feePerByte = data.fee ? data.fee : dgbFeePerByte; const dgbWalletBalance = await getWalletBalance({ coin: checkCoin }, true); if (isNaN(Number(dgbWalletBalance))) { - let errorMsg = 'Failed to Fetch DGB Balance. Try again!'; + const errorMsg = 'Failed to Fetch DGB Balance. Try again!'; throw new Error(errorMsg); } const dgbWalletBalanceDecimals = Number(dgbWalletBalance); const dgbAmountDecimals = Number(amount); const fee = feePerByte * 500; // default 0.00005000 if (dgbAmountDecimals + fee > dgbWalletBalanceDecimals) { - let errorMsg = 'Insufficient Funds!'; + const errorMsg = i18n.t('question:message.error.insufficient_funds', { + postProcess: 'capitalizeFirstChar', + }); throw new Error(errorMsg); } const resPermission = await getUserPermission( { - text1: 'Do you give this application permission to send coins?', + text1: i18n.t('question:permission_send_coins', { + postProcess: 'capitalizeFirstChar', + }), text2: `To: ${recipient}`, highlightedText: `${amount} ${checkCoin}`, foreignFee: `${fee} DGB`, @@ -3855,20 +3897,24 @@ export const sendCoin = async (data, isFromExtension) => { const feePerByte = data.fee ? data.fee : rvnFeePerByte; const rvnWalletBalance = await getWalletBalance({ coin: checkCoin }, true); if (isNaN(Number(rvnWalletBalance))) { - let errorMsg = 'Failed to Fetch RVN Balance. Try again!'; + const errorMsg = 'Failed to Fetch RVN Balance. Try again!'; throw new Error(errorMsg); } const rvnWalletBalanceDecimals = Number(rvnWalletBalance); const rvnAmountDecimals = Number(amount); const fee = feePerByte * 500; // default 0.00562500 if (rvnAmountDecimals + fee > rvnWalletBalanceDecimals) { - let errorMsg = 'Insufficient Funds!'; + const errorMsg = i18n.t('question:message.error.insufficient_funds', { + postProcess: 'capitalizeFirstChar', + }); throw new Error(errorMsg); } const resPermission = await getUserPermission( { - text1: 'Do you give this application permission to send coins?', + text1: i18n.t('question:permission_send_coins', { + postProcess: 'capitalizeFirstChar', + }), text2: `To: ${recipient}`, highlightedText: `${amount} ${checkCoin}`, foreignFee: `${fee} RVN`, @@ -3916,20 +3962,24 @@ export const sendCoin = async (data, isFromExtension) => { const arrrWalletBalance = await getWalletBalance({ coin: checkCoin }, true); if (isNaN(Number(arrrWalletBalance))) { - let errorMsg = 'Failed to Fetch ARRR Balance. Try again!'; + const errorMsg = 'Failed to Fetch ARRR Balance. Try again!'; throw new Error(errorMsg); } const arrrWalletBalanceDecimals = Number(arrrWalletBalance); const arrrAmountDecimals = Number(amount); const fee = 0.0001; if (arrrAmountDecimals + fee > arrrWalletBalanceDecimals) { - let errorMsg = 'Insufficient Funds!'; + const errorMsg = i18n.t('question:message.error.insufficient_funds', { + postProcess: 'capitalizeFirstChar', + }); throw new Error(errorMsg); } const resPermission = await getUserPermission( { - text1: 'Do you give this application permission to send coins?', + text1: i18n.t('question:permission_send_coins', { + postProcess: 'capitalizeFirstChar', + }), text2: `To: ${recipient}`, highlightedText: `${amount} ${checkCoin}`, foreignFee: `${fee} ARRR`,