SHA256
Обновить публичные идентификаторы файлов
This commit is contained in:
@@ -1,10 +1,6 @@
|
||||
import { t } from '../i18n/index.js';
|
||||
import {
|
||||
buildArweaveDataUrl,
|
||||
sha256HexFromArrayBuffer,
|
||||
validateArweaveTxId,
|
||||
validateSha256Hex,
|
||||
} from './arweave-file-service.js';
|
||||
import { buildArweaveDataUrl, validateArweaveTxId } from './arweave-file-service.js';
|
||||
import { preparePublicFile } from './file-preparation-service.js';
|
||||
import { validatePublicPiecesRoot } from './public-pieces-root.js';
|
||||
|
||||
const DB_NAME = 'shine-ui-avatar-cache';
|
||||
const DB_VERSION = 1;
|
||||
@@ -61,12 +57,9 @@ async function putRecord(record) {
|
||||
});
|
||||
}
|
||||
|
||||
async function verifyBlobSha256(blob, expectedSha256Hex) {
|
||||
const expected = String(expectedSha256Hex || '').trim().toLowerCase();
|
||||
if (!validateSha256Hex(expected)) return true;
|
||||
if (!(blob instanceof Blob)) return false;
|
||||
const actual = await sha256HexFromArrayBuffer(await blob.arrayBuffer());
|
||||
return actual === expected;
|
||||
async function verifyBlobPr(blob, expectedPr) {
|
||||
if (!validatePublicPiecesRoot(expectedPr) || !(blob instanceof Blob)) return false;
|
||||
return (await preparePublicFile(blob)).pr === expectedPr;
|
||||
}
|
||||
|
||||
async function getAllRecords() {
|
||||
@@ -160,62 +153,30 @@ function detectImageMime(bytes) {
|
||||
return '';
|
||||
}
|
||||
|
||||
async function getBlobFromCacheOrGateway({ gateway, txId, expectedSha256Hex = '' }) {
|
||||
const expected = String(expectedSha256Hex || '').trim().toLowerCase();
|
||||
async function getBlobFromCacheOrGateway({ gateway, txId, expectedPr }) {
|
||||
if (!validatePublicPiecesRoot(expectedPr)) throw new Error('Некорректный PR аватара');
|
||||
try {
|
||||
const cached = await getRecord(txId);
|
||||
if (cached?.blob instanceof Blob) {
|
||||
if (!validateSha256Hex(expected)) return cached.blob;
|
||||
const cacheSha = String(cached?.sha256Hex || '').trim().toLowerCase();
|
||||
if (cacheSha && cacheSha === expected) {
|
||||
return cached.blob;
|
||||
}
|
||||
const ok = await verifyBlobSha256(cached.blob, expected);
|
||||
if (ok) {
|
||||
return cached.blob;
|
||||
}
|
||||
// кэш повреждён или не совпадает с ожидаемым хэшем: удаляем и перекачиваем
|
||||
if (await verifyBlobPr(cached.blob, expectedPr)) return cached.blob;
|
||||
await deleteRecords([txId]);
|
||||
}
|
||||
} catch {
|
||||
// ignore IndexedDB errors and fallback to fetch
|
||||
}
|
||||
} catch { /* cache is optional */ }
|
||||
|
||||
const blob = await fetchAvatarBlob({ gateway, txId });
|
||||
if (validateSha256Hex(expected)) {
|
||||
const ok = await verifyBlobSha256(blob, expected);
|
||||
if (!ok) {
|
||||
throw new Error('SHA256_MISMATCH');
|
||||
}
|
||||
}
|
||||
const computedSha256Hex = await sha256HexFromArrayBuffer(await blob.arrayBuffer());
|
||||
const record = {
|
||||
txId,
|
||||
blob,
|
||||
contentType: String(blob.type || 'application/octet-stream'),
|
||||
sizeBytes: Number(blob.size || 0),
|
||||
sha256Hex: computedSha256Hex,
|
||||
cachedAtMs: Date.now(),
|
||||
};
|
||||
if (!(await verifyBlobPr(blob, expectedPr))) throw new Error('AVATAR_PR_MISMATCH');
|
||||
try {
|
||||
await putRecord(record);
|
||||
await putRecord({ txId, blob, contentType: String(blob.type || 'application/octet-stream'),
|
||||
sizeBytes: Number(blob.size || 0), pr: expectedPr, cachedAtMs: Date.now() });
|
||||
await ensureCacheLimits();
|
||||
} catch {
|
||||
// ignore cache write errors
|
||||
}
|
||||
} catch { /* cache is optional */ }
|
||||
return blob;
|
||||
}
|
||||
|
||||
export async function getCachedAvatarObjectUrl({ gateway, txId, expectedSha256Hex = '' }) {
|
||||
export async function getCachedAvatarObjectUrl({ gateway, txId, expectedPr }) {
|
||||
const cleanTxId = String(txId || '').trim();
|
||||
if (!validateArweaveTxId(cleanTxId)) {
|
||||
throw new Error(t('remaining.avatar_errors.invalid_arweave_transaction_id'));
|
||||
}
|
||||
const blob = await getBlobFromCacheOrGateway({
|
||||
gateway,
|
||||
txId: cleanTxId,
|
||||
expectedSha256Hex,
|
||||
});
|
||||
if (!validateArweaveTxId(cleanTxId)) throw new Error(t('remaining.avatar_errors.invalid_arweave_transaction_id'));
|
||||
const blob = await getBlobFromCacheOrGateway({ gateway, txId: cleanTxId, expectedPr });
|
||||
return URL.createObjectURL(blob);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,86 +1,43 @@
|
||||
import { validateArweaveTxId, validateSha256Hex } from './arweave-file-service.js';
|
||||
|
||||
const AVATAR_TAG_RE = /^<(?:SHiNE|S):(ava|avatar);([^>]*)>$/u;
|
||||
import { validateArweaveTxId } from './arweave-file-service.js';
|
||||
import { validatePublicPiecesRoot } from './public-pieces-root.js';
|
||||
|
||||
const AVATAR_TAG_RE = /^<S:ava;([^>]*)>$/u;
|
||||
function parseFields(raw = '') {
|
||||
const out = {};
|
||||
String(raw || '').split(';').forEach((part) => {
|
||||
for (const part of String(raw).split(';')) {
|
||||
const eq = part.indexOf('=');
|
||||
if (eq <= 0) return;
|
||||
const key = part.slice(0, eq).trim();
|
||||
const value = part.slice(eq + 1).trim();
|
||||
if (key) out[key] = value;
|
||||
});
|
||||
if (eq > 0) out[part.slice(0, eq).trim()] = part.slice(eq + 1).trim();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function buildAvatarReference({ size, sha256, sha256Hex, ar = '' } = {}) {
|
||||
const cleanSha = String(sha256 || sha256Hex || '').trim().toLowerCase();
|
||||
export function buildAvatarReference({ size, pr, ar = '' } = {}) {
|
||||
const cleanPr = String(pr || '').trim(); // Base58 is case-sensitive. NEVER lowercase.
|
||||
const cleanAr = String(ar || '').trim();
|
||||
const cleanSize = Number(size || 0);
|
||||
if (!Number.isInteger(cleanSize) || cleanSize <= 0) throw new Error('Некорректный размер аватара.');
|
||||
if (!validateSha256Hex(cleanSha)) throw new Error('Некорректный SHA-256 аватара.');
|
||||
if (!Number.isSafeInteger(cleanSize) || cleanSize <= 0) throw new Error('Некорректный размер аватара.');
|
||||
if (!validatePublicPiecesRoot(cleanPr)) throw new Error('Некорректный PR аватара.');
|
||||
if (cleanAr && !validateArweaveTxId(cleanAr)) throw new Error('Некорректный Arweave txId аватара.');
|
||||
return `<S:ava;v=1;sz=${cleanSize};sha256=${cleanSha}${cleanAr ? `;ar=${cleanAr}` : ''}>`;
|
||||
return `<S:ava;v=2;sz=${cleanSize};pr=${cleanPr}${cleanAr ? `;ar=${cleanAr}` : ''}>`;
|
||||
}
|
||||
|
||||
export function parseAvatarReference(value) {
|
||||
const raw = String(value || '').trim();
|
||||
if (!raw) return { ok: false, source: '', ar: '', txId: '', sha256Hex: '', size: 0, legacy: false, value: '' };
|
||||
|
||||
const invalid = { ok: false, source: '', ar: '', txId: '', pr: '', size: 0, value: raw };
|
||||
const tag = raw.match(AVATAR_TAG_RE);
|
||||
if (tag) {
|
||||
const fields = parseFields(tag[2]);
|
||||
const version = String(fields.v || '').trim();
|
||||
const sha256Hex = String(fields.sha256 || '').trim().toLowerCase();
|
||||
const ar = String(fields.ar || '').trim();
|
||||
const size = Number(fields.sz || fields.size || 0);
|
||||
if (version !== '1' || !validateSha256Hex(sha256Hex) || !Number.isInteger(size) || size <= 0) {
|
||||
return { ok: false, source: '', ar: '', txId: '', sha256Hex: '', size: 0, legacy: false, value: raw };
|
||||
}
|
||||
if (ar && !validateArweaveTxId(ar)) {
|
||||
return { ok: false, source: '', ar: '', txId: '', sha256Hex: '', size: 0, legacy: false, value: raw };
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
source: ar ? 'arweave' : 'server',
|
||||
ar,
|
||||
txId: ar,
|
||||
sha256Hex,
|
||||
size,
|
||||
legacy: false,
|
||||
value: raw,
|
||||
};
|
||||
}
|
||||
|
||||
// Legacy USER_PROFILE ava: SHA256:<hash>,AR:<txid> or AR:<txid>.
|
||||
const arMatch = raw.match(/AR:([A-Za-z0-9_-]{43})/u);
|
||||
const shaMatch = raw.match(/SHA256:([A-Fa-f0-9]{64})/u);
|
||||
const ar = String(arMatch?.[1] || '').trim();
|
||||
const sha256Hex = String(shaMatch?.[1] || '').trim().toLowerCase();
|
||||
if (validateArweaveTxId(ar)) {
|
||||
return {
|
||||
ok: true,
|
||||
source: 'arweave',
|
||||
ar,
|
||||
txId: ar,
|
||||
sha256Hex: validateSha256Hex(sha256Hex) ? sha256Hex : '',
|
||||
size: 0,
|
||||
legacy: true,
|
||||
value: raw,
|
||||
};
|
||||
}
|
||||
|
||||
return { ok: false, source: '', ar: '', txId: '', sha256Hex: '', size: 0, legacy: false, value: raw };
|
||||
if (!tag) return invalid; // old v1 / SHA256 / hex deliberately not supported.
|
||||
const fields = parseFields(tag[1]);
|
||||
const pr = String(fields.pr || '').trim();
|
||||
const ar = String(fields.ar || '').trim();
|
||||
const size = Number(fields.sz || '');
|
||||
if (fields.v !== '2' || !validatePublicPiecesRoot(pr) || !Number.isSafeInteger(size) || size <= 0) return invalid;
|
||||
if (ar && !validateArweaveTxId(ar)) return invalid;
|
||||
return { ok: true, source: ar ? 'arweave' : 'r2', ar, txId: ar, pr, size, value: raw };
|
||||
}
|
||||
|
||||
export function avatarReferenceToRenderModel(value) {
|
||||
const parsed = typeof value === 'string' ? parseAvatarReference(value) : value;
|
||||
if (!parsed?.ok) return null;
|
||||
return {
|
||||
ar: String(parsed.ar || parsed.txId || '').trim(),
|
||||
sha256Hex: String(parsed.sha256Hex || '').trim().toLowerCase(),
|
||||
size: Number(parsed.size || 0),
|
||||
source: String(parsed.source || ''),
|
||||
};
|
||||
return { ar: String(parsed.ar || parsed.txId || '').trim(), pr: String(parsed.pr || '').trim(),
|
||||
size: Number(parsed.size || 0), source: String(parsed.source || '') };
|
||||
}
|
||||
|
||||
@@ -91,6 +91,7 @@ function parseChannelMetaText(rawText) {
|
||||
const rows = String(rawText || '').replace(/\r\n/g, '\n').replace(/\r/g, '\n').split('\n');
|
||||
let title = '';
|
||||
let avatarAr = '';
|
||||
let avatarPr = '';
|
||||
const descriptionRows = [];
|
||||
for (const row of rows) {
|
||||
const titleMatch = row.match(/^<S:title;v=1;([^>]*)>$/u);
|
||||
@@ -98,10 +99,12 @@ function parseChannelMetaText(rawText) {
|
||||
title = String(titleMatch[1] || '').trim();
|
||||
continue;
|
||||
}
|
||||
const avatarMatch = row.match(/^<S:ava;v=1;([^>]*)>$/u);
|
||||
const avatarMatch = row.match(/^<S:ava;v=2;([^>]*)>$/u);
|
||||
if (avatarMatch) {
|
||||
const arMatch = String(avatarMatch[1] || '').match(/(?:^|;)ar=([A-Za-z0-9_-]{43})(?:;|$)/u);
|
||||
avatarAr = String(arMatch?.[1] || '').trim();
|
||||
const prMatch = String(avatarMatch[1] || '').match(/(?:^|;)pr=([1-9A-HJ-NP-Za-km-z]{32,44})(?:;|$)/u);
|
||||
avatarPr = String(prMatch?.[1] || '').trim();
|
||||
continue;
|
||||
}
|
||||
descriptionRows.push(row);
|
||||
@@ -109,6 +112,7 @@ function parseChannelMetaText(rawText) {
|
||||
return {
|
||||
title,
|
||||
avatarAr,
|
||||
avatarPr,
|
||||
description: descriptionRows.join('\n').trim(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ async function loadAvatarSnapshot(login) {
|
||||
}
|
||||
|
||||
function renderPeerAvatar(login, avatar = null) {
|
||||
const hasAvatar = Boolean(String(avatar?.ar || '').trim() || String(avatar?.sha256Hex || '').trim());
|
||||
const hasAvatar = Boolean(String(avatar?.ar || '').trim() || String(avatar?.pr || '').trim());
|
||||
const avatarEl = renderUserAvatar({
|
||||
login: String(login || '').trim() || 'unknown',
|
||||
avatar,
|
||||
@@ -141,7 +141,7 @@ function updatePeerAvatar(login) {
|
||||
const avatar = snapshot?.avatar?.source
|
||||
? {
|
||||
ar: String(snapshot.avatar.txId || snapshot.avatar.ar || '').trim(),
|
||||
sha256Hex: String(snapshot.avatar.sha256Hex || '').trim().toLowerCase(),
|
||||
pr: String(snapshot.avatar.pr || '').trim(),
|
||||
}
|
||||
: null;
|
||||
const upgraded = renderPeerAvatar(cleanLogin, avatar);
|
||||
|
||||
@@ -2,7 +2,7 @@ import { t } from '../i18n/index.js';
|
||||
import { preparePublicFile, R2_MAX_FILE_BYTES } from './file-preparation-service.js';
|
||||
import { validatePublicPiecesRoot } from './public-pieces-root.js';
|
||||
import { importPkcs8Ed25519, signBase64 } from './crypto-utils.js';
|
||||
import { sha256HexFromArrayBuffer, validateSha256Hex } from './arweave-file-service.js';
|
||||
import { validateSha256Hex } from './arweave-file-service.js';
|
||||
import { loadSessionMaterial } from './key-vault.js';
|
||||
import {
|
||||
DEFAULT_SHINE_SERVER_HTTP,
|
||||
@@ -75,86 +75,18 @@ async function createUploadContext({ login, sessionId, wsUrl }) {
|
||||
};
|
||||
}
|
||||
|
||||
/** Avatars share the same PR/Base58 R2 namespace and upload protocol as other public files. */
|
||||
export async function uploadPublicFile({ file, login, sessionId, wsUrl, onStatus = null, purpose = 'attachment' } = {}) {
|
||||
if (purpose !== 'avatar') return uploadR2PublicFile({ file, login, sessionId, wsUrl, onStatus });
|
||||
if (!(file instanceof File)) throw new Error(t('remaining.file_upload.no_file_selected'));
|
||||
if (Number(file.size || 0) <= 0) throw new Error(t('remaining.file_upload.cannot_upload_an_empty_file'));
|
||||
|
||||
onStatus?.(t('remaining.file_upload.calculating_sha_256'));
|
||||
const bytes = await file.arrayBuffer();
|
||||
const sha256 = await sha256HexFromArrayBuffer(bytes);
|
||||
const context = await createUploadContext({ login, sessionId, wsUrl });
|
||||
const url = publicFileUrl(context.serverBase, sha256);
|
||||
|
||||
onStatus?.(t('remaining.file_upload.checking_whether_the_file_is_on_the_server'));
|
||||
try {
|
||||
const existing = await fetch(url, { method: 'HEAD', cache: 'no-store' });
|
||||
if (existing.ok) {
|
||||
const storedSize = Number(existing.headers.get('Content-Length') || -1);
|
||||
if (storedSize < 0 || storedSize === file.size) {
|
||||
return {
|
||||
v: 1,
|
||||
name: String(file.name || 'file'),
|
||||
size: Number(file.size),
|
||||
sha256,
|
||||
ar: '',
|
||||
uploadedAtMs: Date.now(),
|
||||
alreadyExists: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// HEAD — только оптимизация. PUT сам проверит hash и дедупликацию.
|
||||
if (purpose === 'avatar' && (!(file instanceof Blob) || !file.size || file.size > 10 * 1024 * 1024
|
||||
|| !['image/webp', 'image/jpeg'].includes(file.type))) {
|
||||
throw new Error('Аватар должен быть оптимизированным WebP/JPEG размером до 10 МиБ');
|
||||
}
|
||||
return uploadR2PublicFile({ file, login, sessionId, wsUrl, onStatus });
|
||||
}
|
||||
|
||||
const timeMs = Date.now();
|
||||
const signature = await signBase64(context.privateKey, uploadPreimage({
|
||||
sessionId: context.sessionId,
|
||||
sha256,
|
||||
size: file.size,
|
||||
timeMs,
|
||||
}));
|
||||
|
||||
onStatus?.(t('remaining.file_upload.uploading_to_access_server'));
|
||||
let response;
|
||||
try {
|
||||
response = await fetch(url, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'X-Shine-Session-Id': context.sessionId,
|
||||
'X-Shine-Time-Ms': String(timeMs),
|
||||
'X-Shine-Content-Length': String(file.size),
|
||||
'X-Shine-Signature': signature,
|
||||
},
|
||||
body: bytes,
|
||||
cache: 'no-store',
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(`Не удалось загрузить файл на сервер доступа: ${error?.message || 'network error'}`);
|
||||
}
|
||||
if (!response.ok) {
|
||||
const detail = await readErrorMessage(response);
|
||||
throw new Error(detail || `Сервер отклонил файл (HTTP ${response.status}).`);
|
||||
}
|
||||
|
||||
let alreadyExists = false;
|
||||
try {
|
||||
const payload = await response.clone().json();
|
||||
alreadyExists = Boolean(payload?.alreadyExists);
|
||||
} catch {
|
||||
// Успешного HTTP достаточно.
|
||||
}
|
||||
|
||||
return {
|
||||
v: 1,
|
||||
name: String(file.name || 'file'),
|
||||
size: Number(file.size),
|
||||
sha256,
|
||||
ar: '',
|
||||
uploadedAtMs: Date.now(),
|
||||
alreadyExists,
|
||||
};
|
||||
/** Canonical public object URL; avatars do not have a special prefix. */
|
||||
export function avatarR2Url(pr) {
|
||||
return publicR2Url(pr);
|
||||
}
|
||||
|
||||
export async function resolvePublicFileUrl({
|
||||
|
||||
@@ -277,7 +277,7 @@ export async function loadUserProfileCard(login) {
|
||||
avatar: snapshot?.avatar?.source
|
||||
? {
|
||||
ar: String(snapshot.avatar.txId || snapshot.avatar.ar || '').trim(),
|
||||
sha256Hex: String(snapshot?.avatar?.sha256Hex || '').trim().toLowerCase(),
|
||||
pr: String(snapshot?.avatar?.pr || '').trim(),
|
||||
}
|
||||
: null,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { t } from '../i18n/index.js';
|
||||
import { authService, state } from '../state.js';
|
||||
import { validateArweaveTxId, validateSha256Hex } from './arweave-file-service.js';
|
||||
import { validateArweaveTxId } from './arweave-file-service.js';
|
||||
import { validatePublicPiecesRoot } from './public-pieces-root.js';
|
||||
import { buildAvatarReference, parseAvatarReference } from './avatar-reference.js';
|
||||
import { CITY_MAX_LENGTH, CITY_PARAM, CITY_SUGGESTIONS } from './cities.js';
|
||||
|
||||
@@ -146,7 +147,7 @@ export async function loadProfileSnapshot(login) {
|
||||
source: parsedAvatar.source,
|
||||
txId: parsedAvatar.txId || '',
|
||||
ar: parsedAvatar.ar || '',
|
||||
sha256Hex: parsedAvatar.sha256Hex || '',
|
||||
pr: parsedAvatar.pr || '',
|
||||
size: Number(parsedAvatar.size || 0),
|
||||
timeMs: latestAvatar?.timeMs || 0,
|
||||
}
|
||||
@@ -155,7 +156,7 @@ export async function loadProfileSnapshot(login) {
|
||||
source: '',
|
||||
txId: '',
|
||||
ar: '',
|
||||
sha256Hex: '',
|
||||
pr: '',
|
||||
size: 0,
|
||||
timeMs: latestAvatar?.timeMs || 0,
|
||||
};
|
||||
@@ -215,15 +216,10 @@ export async function saveProfileGender(login, gender) {
|
||||
}
|
||||
|
||||
export async function saveProfileAvatar(login, avatar) {
|
||||
const cleanAr = String(avatar?.ar || '').trim();
|
||||
const cleanSha = String(avatar?.sha256 || avatar?.sha256Hex || '').trim().toLowerCase();
|
||||
const ar = String(avatar?.ar || '').trim();
|
||||
const pr = String(avatar?.pr || '').trim();
|
||||
const size = Number(avatar?.size || 0);
|
||||
if (cleanAr && !validateArweaveTxId(cleanAr)) throw new Error(t('remaining.profile_errors.invalid_arweave_transaction_id'));
|
||||
if (!validateSha256Hex(cleanSha)) throw new Error(t('remaining.profile_errors.invalid_avatar_sha_256_hash'));
|
||||
await saveProfileParamBlock(login, 'ava', buildAvatarReference({ ar: cleanAr, sha256: cleanSha, size }));
|
||||
}
|
||||
|
||||
// Legacy export for old callers. New UI writes only <S:ava;...>.
|
||||
export async function saveProfileAvatarArweave(login, txId, sha256Hex, size = 1) {
|
||||
return saveProfileAvatar(login, { ar: txId, sha256: sha256Hex, size });
|
||||
if (ar && !validateArweaveTxId(ar)) throw new Error(t('remaining.profile_errors.invalid_arweave_transaction_id'));
|
||||
if (!validatePublicPiecesRoot(pr)) throw new Error('Некорректный Pieces Root аватара');
|
||||
await saveProfileParamBlock(login, 'ava', buildAvatarReference({ ar, pr, size }));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user