SHA256
Полностью поддерживает английский язык
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { t } from '../i18n/index.js';
|
||||
const DEFAULT_ARWEAVE_GATEWAY = 'https://arweave.net';
|
||||
const WINSTON_PER_AR = 1_000_000_000_000n;
|
||||
const MAX_AVATAR_SOURCE_BYTES = 10 * 1024 * 1024;
|
||||
@@ -16,10 +17,10 @@ function normalizeGateway(rawGateway) {
|
||||
try {
|
||||
parsed = new URL(raw);
|
||||
} catch {
|
||||
throw new Error('Некорректный Arweave gateway URL');
|
||||
throw new Error(t('remaining.arweave_file_errors.invalid_arweave_gateway_url'));
|
||||
}
|
||||
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
||||
throw new Error('Arweave gateway должен использовать http или https');
|
||||
throw new Error(t('remaining.arweave_file_errors.arweave_gateway_must_use_http_or_https'));
|
||||
}
|
||||
const normalized = `${parsed.protocol}//${parsed.host}${parsed.pathname}`.replace(/\/+$/g, '');
|
||||
return normalized || DEFAULT_ARWEAVE_GATEWAY;
|
||||
@@ -30,11 +31,11 @@ function parseGatewayForArweaveInit(gateway) {
|
||||
try {
|
||||
parsed = new URL(gateway);
|
||||
} catch {
|
||||
throw new Error('Некорректный Arweave gateway URL');
|
||||
throw new Error(t('remaining.arweave_file_errors.invalid_arweave_gateway_url'));
|
||||
}
|
||||
const protocol = parsed.protocol.replace(':', '');
|
||||
if (protocol !== 'http' && protocol !== 'https') {
|
||||
throw new Error('Arweave gateway должен использовать http или https');
|
||||
throw new Error(t('remaining.arweave_file_errors.arweave_gateway_must_use_http_or_https'));
|
||||
}
|
||||
const port = parsed.port
|
||||
? Number(parsed.port)
|
||||
@@ -79,7 +80,7 @@ async function createBitmapWithFallback(file) {
|
||||
const image = await new Promise((resolve, reject) => {
|
||||
const img = new Image();
|
||||
img.onload = () => resolve(img);
|
||||
img.onerror = () => reject(new Error('Не удалось прочитать изображение'));
|
||||
img.onerror = () => reject(new Error(t('remaining.arweave_files.could_not_read_image')));
|
||||
img.src = url;
|
||||
});
|
||||
return image;
|
||||
@@ -109,7 +110,7 @@ export function validateSha256Hex(sha256Hex) {
|
||||
}
|
||||
|
||||
export async function sha256HexFromArrayBuffer(buffer) {
|
||||
if (!(buffer instanceof ArrayBuffer)) throw new Error('Некорректные данные для SHA256');
|
||||
if (!(buffer instanceof ArrayBuffer)) throw new Error(t('remaining.arweave_file_errors.invalid_data_for_sha_256'));
|
||||
const digest = await crypto.subtle.digest('SHA-256', buffer);
|
||||
return bytesToHex(new Uint8Array(digest));
|
||||
}
|
||||
@@ -117,12 +118,12 @@ export async function sha256HexFromArrayBuffer(buffer) {
|
||||
export function buildArweaveAvatarValue(txId, sha256Hex = '') {
|
||||
const cleanTxId = String(txId || '').trim();
|
||||
if (!validateArweaveTxId(cleanTxId)) {
|
||||
throw new Error('Некорректный Transaction ID Arweave');
|
||||
throw new Error(t('remaining.arweave_file_errors.invalid_arweave_transaction_id'));
|
||||
}
|
||||
const cleanSha = String(sha256Hex || '').trim().toLowerCase();
|
||||
if (!cleanSha) return `AR:${cleanTxId}`;
|
||||
if (!validateSha256Hex(cleanSha)) {
|
||||
throw new Error('Некорректный SHA256 хэш аватара');
|
||||
throw new Error(t('remaining.arweave_file_errors.invalid_avatar_sha_256_hash'));
|
||||
}
|
||||
return `SHA256:${cleanSha},AR:${cleanTxId}`;
|
||||
}
|
||||
@@ -158,7 +159,7 @@ export function buildArweaveDataUrl({ gateway, txId }) {
|
||||
const normalizedGateway = normalizeGateway(gateway);
|
||||
const cleanTxId = String(txId || '').trim();
|
||||
if (!validateArweaveTxId(cleanTxId)) {
|
||||
throw new Error('Некорректный Transaction ID Arweave');
|
||||
throw new Error(t('remaining.arweave_file_errors.invalid_arweave_transaction_id'));
|
||||
}
|
||||
return `${normalizedGateway}/${cleanTxId}`;
|
||||
}
|
||||
@@ -167,7 +168,7 @@ export async function getArweaveUploadPrice({ gateway, byteLength }) {
|
||||
const normalizedGateway = normalizeGateway(gateway);
|
||||
const bytes = Number(byteLength);
|
||||
if (!Number.isInteger(bytes) || bytes <= 0) {
|
||||
throw new Error('Некорректный размер файла');
|
||||
throw new Error(t('remaining.arweave_file_errors.invalid_file_size'));
|
||||
}
|
||||
|
||||
const response = await fetch(`${normalizedGateway}/price/${bytes}`, { method: 'GET' });
|
||||
@@ -176,7 +177,7 @@ export async function getArweaveUploadPrice({ gateway, byteLength }) {
|
||||
throw new Error(`Не удалось получить цену загрузки (${response.status} ${response.statusText})`);
|
||||
}
|
||||
if (!/^\d+$/.test(raw)) {
|
||||
throw new Error('Arweave gateway вернул некорректную цену');
|
||||
throw new Error(t('remaining.arweave_file_errors.arweave_gateway_returned_an_invalid_price'));
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -193,13 +194,13 @@ export function isSupportedAvatarImageType(type) {
|
||||
|
||||
export function validateAvatarSourceFile(file) {
|
||||
if (!(file instanceof File)) {
|
||||
throw new Error('Выберите файл изображения.');
|
||||
throw new Error(t('remaining.arweave_files.select_an_image_file'));
|
||||
}
|
||||
if (!isSupportedAvatarImageType(file.type)) {
|
||||
throw new Error('Поддерживаются только JPEG, PNG или WebP.');
|
||||
throw new Error(t('remaining.arweave_files.only_jpeg_png_and_webp_files_are_supported'));
|
||||
}
|
||||
if (Number(file.size || 0) > MAX_AVATAR_SOURCE_BYTES) {
|
||||
throw new Error('Файл слишком большой. Максимум 10 MB.');
|
||||
throw new Error(t('remaining.arweave_files.file_too_large_maximum_size_10_mb'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,14 +211,14 @@ export async function prepareAvatarImageFile(file) {
|
||||
try {
|
||||
bitmap = await createBitmapWithFallback(file);
|
||||
} catch {
|
||||
throw new Error('Не удалось подготовить изображение.');
|
||||
throw new Error(t('remaining.arweave_files.could_not_process_the_image'));
|
||||
}
|
||||
|
||||
try {
|
||||
const originalWidth = Number(bitmap.width || 0);
|
||||
const originalHeight = Number(bitmap.height || 0);
|
||||
if (!originalWidth || !originalHeight) {
|
||||
throw new Error('Не удалось подготовить изображение.');
|
||||
throw new Error(t('remaining.arweave_files.could_not_process_the_image'));
|
||||
}
|
||||
|
||||
const scale = Math.min(1, MAX_AVATAR_SIDE_PX / Math.max(originalWidth, originalHeight));
|
||||
@@ -229,7 +230,7 @@ export async function prepareAvatarImageFile(file) {
|
||||
canvas.height = height;
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) {
|
||||
throw new Error('Не удалось подготовить изображение.');
|
||||
throw new Error(t('remaining.arweave_files.could_not_process_the_image'));
|
||||
}
|
||||
ctx.drawImage(bitmap, 0, 0, width, height);
|
||||
|
||||
@@ -242,7 +243,7 @@ export async function prepareAvatarImageFile(file) {
|
||||
blob = await canvasToBlob(canvas, contentType, AVATAR_QUALITY);
|
||||
}
|
||||
if (!blob) {
|
||||
throw new Error('Не удалось подготовить изображение.');
|
||||
throw new Error(t('remaining.arweave_files.could_not_process_the_image'));
|
||||
}
|
||||
|
||||
const optimizedFile = blobToFile(blob, fileName, contentType);
|
||||
@@ -261,7 +262,7 @@ export async function prepareAvatarImageFile(file) {
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof Error) throw error;
|
||||
throw new Error('Не удалось подготовить изображение.');
|
||||
throw new Error(t('remaining.arweave_files.could_not_process_the_image'));
|
||||
} finally {
|
||||
if (bitmap && typeof bitmap.close === 'function') {
|
||||
bitmap.close();
|
||||
@@ -301,10 +302,10 @@ export async function loadArweaveFileMetadata({ gateway, txId, maxBytes = MAX_AR
|
||||
|
||||
export async function uploadArweaveFile({ gateway, jwk, file, tags = [], shineType = 'avatar' }) {
|
||||
if (!jwk || typeof jwk !== 'object') {
|
||||
throw new Error('Arweave-кошелёк не инициализирован.');
|
||||
throw new Error(t('remaining.arweave_files.arweave_wallet_is_not_initialized'));
|
||||
}
|
||||
if (!(file instanceof File)) {
|
||||
throw new Error('Выберите файл.');
|
||||
throw new Error(t('remaining.arweave_files.select_a_file'));
|
||||
}
|
||||
|
||||
const normalizedGateway = normalizeGateway(gateway);
|
||||
|
||||
Reference in New Issue
Block a user