import { buildAvatarReference } from './avatar-reference.js'; import { WsJsonClient } from './ws-client.js'; import { invalidateConnectionsGraphCache } from './connections-graph-cache.js'; import { base64ToBytes, bytesToBase64, deriveEd25519FromMasterSecret, deriveMasterSecretFromPassword, ed25519PublicToX25519Public, ed25519SeedToX25519Private, exportEd25519PublicKeyB64, exportPkcs8B64, generateEd25519Pair, extractEd25519SeedFromPkcs8B64, hkdfSha256, importPkcs8Ed25519, publicKeyB64FromPkcs8Ed25519, randomBase64, randomBytes, sha256Bytes, signBytes, signBase64, utf8Bytes, x25519PublicFromPrivate, x25519RandomPrivateKey, x25519SharedSecret, encryptBytesAesGcm, decryptBytesAesGcm, } from './crypto-utils.js'; import { channelNameErrorText, normalizeChannelDisplayName, toCanonicalChannelSlug, validateChannelDisplayName, } from './channel-name-rules.js'; import { loadEncryptedUserSecrets, loadSessionMaterial, saveEncryptedUserSecrets, saveSessionMaterial, } from './key-vault.js'; import { coreHashBytes, coreHex, signCoreContentPair, splitEventBody } from './core-content.js'; import { defaultServerWs } from '../deploy-config.js'; import { SHINE_CORE_LINE_PROFILE, SHINE_CORE_LINE_CONNECTION, SHINE_CORE_LINE_TECH } from './protocol-tags.js'; const BCH_SUFFIX = '001'; const ZERO64 = '0'.repeat(64); const ZERO_HASH_HEX = ZERO64; const MSG_TYPE_TECH = 0; const MSG_TYPE_TEXT = 1; const MSG_TYPE_REACTION = 2; const MSG_TYPE_CONNECTION = 3; const MSG_TYPE_USER_PROFILE = 4; const MSG_TYPE_STATUS_ACTION = 5; const MSG_SUBTYPE_TECH_HEADER = 0; const MSG_SUBTYPE_TECH_CREATE_CHANNEL = 1; const MSG_SUBTYPE_TECH_FORK = 2; const MSG_SUBTYPE_TEXT_POST = 10; const MSG_SUBTYPE_TEXT_EDIT_POST = 11; const MSG_SUBTYPE_TEXT_REPLY = 20; const MSG_SUBTYPE_TEXT_EDIT_REPLY = 21; const MSG_SUBTYPE_TEXT_RATING = 30; const MSG_SUBTYPE_TEXT_REPOST = 50; const MSG_SUBTYPE_TEXT_CHANNEL_META = 90; const MSG_SUBTYPE_TEXT_CHANNEL_DELETE = 92; const MSG_SUBTYPE_TEXT_ENTRYPOINT = 100; const MSG_SUBTYPE_TEXT_EXERCISE = 110; const MSG_SUBTYPE_TEXT_SERVICE = 120; const MSG_SUBTYPE_TEXT_COURSE = 130; const MSG_SUBTYPE_STATUS_DONE_ONCE = 10; const MSG_SUBTYPE_STATUS_LEARNED = 20; const MSG_SUBTYPE_STATUS_SERVICE_PASSED = 30; const MSG_SUBTYPE_STATUS_CONFIRMED = 100; const MSG_SUBTYPE_STATUS_INTERESTED = 110; const MSG_SUBTYPE_STATUS_STARTED = 120; const MSG_SUBTYPE_STATUS_IN_STUDY = 130; const MSG_SUBTYPE_STATUS_ABANDONED = 140; const MSG_SUBTYPE_STATUS_COMPLETED = 150; const MSG_SUBTYPE_REACTION_LIKE = 1; const MSG_SUBTYPE_REACTION_UNLIKE = 2; const MSG_SUBTYPE_CONNECTION_FOLLOW = 30; const MSG_SUBTYPE_CONNECTION_UNFOLLOW = 31; const CREATE_CHANNEL_BODY_VERSION = 1; const CHANNEL_TYPE_STORIES = 0; const CHANNEL_TYPE_PUBLIC = 1; const CHANNEL_TYPE_PERSONAL = 100; const CHANNEL_TYPE_GROUP = 200; const CHANNEL_TYPE_VERSION_DEFAULT = 1; const CREATE_CHANNEL_DESCRIPTION_MAX_BYTES = 2048; const SESSION_TYPE_CLIENT = 1; const SESSION_TYPE_WALLET = 50; const SESSION_TYPE_HOMESERVER = 100; const SIGNAL_TARGET_SINGLE = 'single_session'; const SIGNAL_TARGET_ALL = 'all_sessions'; const CONNECTION_SUBTYPES = Object.freeze({ close_friend: { on: 10, off: 11 }, friend: { on: 14, off: 15 }, contact: { on: 20, off: 21 }, // Reserved: пока не используется UI. follow: { on: 30, off: 31 }, // Reserved: семейные связи пока скрыты из UI. spouse: { on: 40, off: 41 }, parent: { on: 50, off: 51 }, child: { on: 52, off: 53 }, sibling: { on: 54, off: 55 }, // Legacy 60/61 intentionally absent: старые блоки сервер принимает, новый UI их не создаёт. shine_confirmed: { on: 70, off: 71 }, // Reserved: пока не используется UI. shine_seen: { on: 74, off: 75 }, official_account: { on: 80, off: 81 }, }); function normalizeServerUrl(url) { const value = (url || '').trim(); if (!value) return defaultServerWs; if (value.startsWith('ws://') || value.startsWith('wss://')) { try { const parsed = new URL(value); if (!parsed.pathname || parsed.pathname === '/') parsed.pathname = '/ws'; return parsed.toString(); } catch { return value; } } if (value.startsWith('https://') || value.startsWith('http://')) { try { const parsed = new URL(value); parsed.protocol = parsed.protocol === 'https:' ? 'wss:' : 'ws:'; if (!parsed.pathname || parsed.pathname === '/') parsed.pathname = '/ws'; return parsed.toString(); } catch { return `${value.replace(/^http/, 'ws').replace(/\/$/, '')}/ws`; } } return value; } function opError(op, response) { const payload = response?.payload || {}; const message = payload?.message || response?.message || payload?.error || response?.error || 'Unknown server error'; const code = String(payload?.code || response?.code || payload?.error || response?.error || 'UNKNOWN').toUpperCase(); const error = new Error(`${op}: ${message} (${code})`); error.op = op; error.code = code; error.status = response?.status || 0; return error; } function makeClientInfo() { const ua = navigator.userAgent || 'unknown'; return ua.slice(0, 50); } function makeClientPlatform() { return 'Web'; } function escapeUserSettingPart(value = '') { return String(value ?? '').replace(/\\/g, '\\\\').replace(/\|/g, '\\|'); } function hexToBytes(hex) { const clean = String(hex || '').trim().toLowerCase(); if (!clean || clean.length % 2 !== 0) throw new Error('Некорректный hex'); const out = new Uint8Array(clean.length / 2); for (let i = 0; i < out.length; i += 1) { const byte = Number.parseInt(clean.slice(i * 2, i * 2 + 2), 16); if (Number.isNaN(byte)) throw new Error('Некорректный hex'); out[i] = byte; } return out; } function normalizeHex32(value, fallback = ZERO64) { const raw = String(value || '').trim().toLowerCase(); if (!raw) return fallback; if (/^0+$/.test(raw)) return ZERO64; if (!/^[0-9a-f]{64}$/.test(raw)) throw new Error('Bad hash32 format'); return raw; } async function sha256Base64FromText(text) { const digest = await sha256Bytes(utf8Bytes(String(text || ''))); return bytesToBase64(digest).replace(/=+$/g, ''); } function concatBytes(...chunks) { const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0); const out = new Uint8Array(total); let offset = 0; chunks.forEach((chunk) => { out.set(chunk, offset); offset += chunk.length; }); return out; } function int32Bytes(value) { const bytes = new Uint8Array(4); const view = new DataView(bytes.buffer); view.setInt32(0, Number(value), false); return bytes; } function int16Bytes(value) { const bytes = new Uint8Array(2); const view = new DataView(bytes.buffer); view.setUint16(0, Number(value), false); return bytes; } function int8Byte(value) { const n = Number(value); if (!Number.isFinite(n) || n < 0 || n > 255) throw new Error('Bad uint8 value'); return new Uint8Array([n & 0xff]); } function uint16Bytes(value) { const bytes = new Uint8Array(2); const view = new DataView(bytes.buffer); view.setUint16(0, Number(value), false); return bytes; } function uint32Bytes(value) { const bytes = new Uint8Array(4); const view = new DataView(bytes.buffer); view.setUint32(0, Number(value), false); return bytes; } function uint64Bytes(value) { const bytes = new Uint8Array(8); const view = new DataView(bytes.buffer); view.setBigUint64(0, BigInt(value), false); return bytes; } function uint8Bytes(value) { return new Uint8Array([Number(value) & 0xff]); } const DM_PREFIX_V1 = utf8Bytes('SHiNE_DM'); const NTF_PREFIX_V1 = utf8Bytes('SHiNE_NTF'); const NTF_FORMAT_VERSION_MAJOR = 1; const NTF_FORMAT_VERSION_MINOR = 0; const NTF_STATE_SEEN_WATERMARK = 1; const NTF_CATEGORY = { replies: 1, connections: 2, events: 3 }; const DM_TYPE_INCOMING = 1; const DM_TYPE_OUTGOING_COPY = 2; const DM_TYPE_READ_INCOMING = 3; const DM_TYPE_READ_OUTGOING_COPY = 4; const DM_TYPE_MESSAGE_DELETED_BY_SENDER = 5; const DM_TYPE_MESSAGE_DELETED_BY_RECIPIENT = 6; const DM_TYPE_CONVERSATION_DELETED_BY_SENDER = 7; const DM_TYPE_CONVERSATION_DELETED_BY_RECIPIENT = 8; const DM_FORMAT_VERSION_MAJOR = 1; const DM_FORMAT_VERSION_MINOR = 0; const DM_MAX_ENCRYPTED_BODY_BYTES = 16384; const DM_CRYPTO_METHOD_X25519_HKDF_AES_GCM = 1; const DM_CRYPTO_VERSION_1_0 = 0; const DM_HKDF_INFO = utf8Bytes('SHiNE_DM|1|0|X25519+HKDF-SHA256+AES-256-GCM'); const DM_DECRYPT_ERROR_UNSUPPORTED_FORMAT = 'unsupported_format'; const DM_DECRYPT_ERROR_DECRYPT_FAILED = 'decrypt_failed'; const DM_DECRYPT_ERROR_CORRUPTED = 'corrupted'; function ensureAsciiBytes(value, field, min = 1, max = 60) { const text = String(value || '').trim(); const bytes = utf8Bytes(text); if (bytes.length < min || bytes.length > max) { throw new Error(`${field} должен быть ${min}..${max} ASCII-символов`); } for (let i = 0; i < bytes.length; i += 1) { const code = bytes[i]; if (code < 0x20 || code > 0x7e) throw new Error(`${field} должен быть ASCII`); } return bytes; } function dmBaseKey({ toLogin, fromLogin, timeMs, nonce }) { return `${fromLogin}|${toLogin}|${Number(timeMs)}|${Number(nonce)}`; } function dmMessageKey({ toLogin, fromLogin, timeMs, nonce, messageType }) { return `${dmBaseKey({ toLogin, fromLogin, timeMs, nonce })}|${Number(messageType)}`; } function buildReadReceiptPayloadBytes({ refToLogin, refFromLogin, refTimeMs, refNonce }) { const toBytes = ensureAsciiBytes(refToLogin, 'receipt.refToLogin'); const fromBytes = ensureAsciiBytes(refFromLogin, 'receipt.refFromLogin'); return concatBytes( uint8Bytes(toBytes.length), toBytes, uint8Bytes(fromBytes.length), fromBytes, uint64Bytes(refTimeMs), uint32Bytes(refNonce), ); } function buildDmEncryptedBodyBytes({ plainBytes, recipientClientKeyB64 }) { return buildDmEncryptedBodyBytesAsync({ plainBytes, recipientClientKeyB64 }); } async function buildDmEncryptedBodyBytesAsync({ plainBytes, recipientClientKeyB64 }) { const recipientEd25519Pub = base64ToBytes(String(recipientClientKeyB64 || '').trim()); if (recipientEd25519Pub.length !== 32) { throw new Error('Некорректный clientKey получателя'); } const recipientX25519Pub = ed25519PublicToX25519Public(recipientEd25519Pub); const ephemeralPriv = x25519RandomPrivateKey(); const ephemeralPub = x25519PublicFromPrivate(ephemeralPriv); const sharedSecret = x25519SharedSecret(ephemeralPriv, recipientX25519Pub); const salt = concatBytes(ephemeralPub, recipientX25519Pub); const aesKeyBytes = await hkdfSha256(sharedSecret, salt, DM_HKDF_INFO, 32); const iv = randomBytes(12); const cipherText = await encryptBytesAesGcm(plainBytes, aesKeyBytes, iv); return concatBytes( uint8Bytes(DM_CRYPTO_METHOD_X25519_HKDF_AES_GCM), uint8Bytes(DM_CRYPTO_VERSION_1_0), uint8Bytes(ephemeralPub.length), ephemeralPub, uint8Bytes(iv.length), iv, uint32Bytes(cipherText.length), cipherText, ); } function parseEncryptedDmBodyBytes(payloadBytes) { if (!(payloadBytes instanceof Uint8Array)) throw new Error('BAD_ENCRYPTED_BODY'); let o = 0; const read = (n) => { if (o + n > payloadBytes.length) throw new Error('BAD_ENCRYPTED_BODY'); const out = payloadBytes.slice(o, o + n); o += n; return out; }; const readU8 = () => read(1)[0]; const cryptoMethod = readU8(); const cryptoVersion = readU8(); const ephemeralPubKeyLen = readU8(); const ephemeralPubKey = read(ephemeralPubKeyLen); const ivLen = readU8(); const iv = read(ivLen); const cipherTextLenBytes = read(4); const cipherTextLen = new DataView(cipherTextLenBytes.buffer, cipherTextLenBytes.byteOffset, 4).getUint32(0, false); const cipherText = read(cipherTextLen); if (o !== payloadBytes.length) throw new Error('BAD_ENCRYPTED_BODY'); return { cryptoMethod, cryptoVersion, ephemeralPubKey, iv, cipherText, }; } function classifyDmDecryptFailure(error) { const code = String(error?.message || '').trim(); if (code === 'BAD_ENCRYPTED_BODY') return DM_DECRYPT_ERROR_CORRUPTED; if (code === 'Неподдерживаемый метод шифрования DM') return DM_DECRYPT_ERROR_UNSUPPORTED_FORMAT; if (code === 'OperationError') return DM_DECRYPT_ERROR_DECRYPT_FAILED; return DM_DECRYPT_ERROR_DECRYPT_FAILED; } function parseReadReceiptBodyBytes(payloadBytes) { if (!(payloadBytes instanceof Uint8Array)) throw new Error('BAD_RECEIPT_PAYLOAD_LEN'); let o = 0; const read = (n) => { if (o + n > payloadBytes.length) throw new Error('BAD_RECEIPT_PAYLOAD_LEN'); const out = payloadBytes.slice(o, o + n); o += n; return out; }; const readU8 = () => read(1)[0]; const readU32 = () => { const part = read(4); return new DataView(part.buffer, part.byteOffset, 4).getUint32(0, false); }; const readU64 = () => { const part = read(8); return Number(new DataView(part.buffer, part.byteOffset, 8).getBigUint64(0, false)); }; const readAscii = (code) => { const len = readU8(); const part = read(len); for (let i = 0; i < part.length; i += 1) { const c = part[i]; if (c < 0x20 || c > 0x7e) throw new Error(code); } return new TextDecoder().decode(part); }; const refToLogin = readAscii('BAD_RECEIPT_TO_LOGIN'); const refFromLogin = readAscii('BAD_RECEIPT_FROM_LOGIN'); const refTimeMs = readU64(); const refNonce = readU32(); if (o !== payloadBytes.length) throw new Error('BAD_RECEIPT_PAYLOAD_LEN'); return { refToLogin, refFromLogin, refTimeMs, refNonce }; } function parseSignedMessageBlockBytes(bytes) { if (!(bytes instanceof Uint8Array)) throw new Error('Expected Uint8Array'); let o = 0; const read = (n) => { if (o + n > bytes.length) throw new Error('BAD_LEN'); const out = bytes.slice(o, o + n); o += n; return out; }; const readU8 = () => read(1)[0]; const readU32 = () => { const part = read(4); const view = new DataView(part.buffer, part.byteOffset, 4); return view.getUint32(0, false); }; const readU64 = () => { const part = read(8); const view = new DataView(part.buffer, part.byteOffset, 8); return Number(view.getBigUint64(0, false)); }; const readAscii = () => { const len = readU8(); const part = read(len); const text = new TextDecoder().decode(part); for (let i = 0; i < part.length; i += 1) { const c = part[i]; if (c < 0x20 || c > 0x7e) throw new Error('BAD_ASCII'); } return text; }; const startsWith = (prefix) => { if (bytes.length < prefix.length) return false; for (let i = 0; i < prefix.length; i += 1) { if (bytes[i] !== prefix[i]) return false; } return true; }; if (!startsWith(DM_PREFIX_V1)) throw new Error('BAD_PREFIX'); read(DM_PREFIX_V1.length); const formatVersionMajor = readU8(); const formatVersionMinor = readU8(); if (formatVersionMajor !== DM_FORMAT_VERSION_MAJOR || formatVersionMinor !== DM_FORMAT_VERSION_MINOR) { throw new Error('BAD_FORMAT_VERSION'); } const toLogin = readAscii(); const fromLogin = readAscii(); const timeMs = readU64(); const nonce = readU32(); const messageType = readU8(); if (messageType < DM_TYPE_INCOMING || messageType > DM_TYPE_CONVERSATION_DELETED_BY_RECIPIENT) { throw new Error('BAD_MESSAGE_TYPE'); } const revisionTimeMs = readU64(); const reencryptedAtMs = readU64(); const bodyLen = readU32(); const bodyBytes = read(bodyLen); const signatureBytes = read(64); if (o !== bytes.length) throw new Error('BAD_LEN'); const signedBody = bytes.slice(0, bytes.length - 64); const baseKey = dmBaseKey({ toLogin, fromLogin, timeMs, nonce }); const messageKey = dmMessageKey({ toLogin, fromLogin, timeMs, nonce, messageType }); let encryptedBodyPayload = null; let readReceiptPayload = null; if (messageType === DM_TYPE_INCOMING || messageType === DM_TYPE_OUTGOING_COPY) { if (bodyBytes.length === 0) throw new Error('BAD_MESSAGE_LEN'); } else if (messageType === DM_TYPE_READ_INCOMING || messageType === DM_TYPE_READ_OUTGOING_COPY) { if (bodyBytes.length === 0) throw new Error('BAD_RECEIPT_PAYLOAD_LEN'); if (revisionTimeMs !== 0 || reencryptedAtMs !== 0) throw new Error('BAD_RECEIPT_REVISION'); readReceiptPayload = parseReadReceiptBodyBytes(bodyBytes); } else if (messageType === DM_TYPE_MESSAGE_DELETED_BY_SENDER || messageType === DM_TYPE_MESSAGE_DELETED_BY_RECIPIENT) { if (bodyBytes.length !== 0) throw new Error('BAD_DELETE_BODY'); if (revisionTimeMs <= 0) throw new Error('BAD_REVISION_TIME'); if (reencryptedAtMs !== 0) throw new Error('BAD_REENCRYPTED_TIME'); } else if (messageType === DM_TYPE_CONVERSATION_DELETED_BY_SENDER || messageType === DM_TYPE_CONVERSATION_DELETED_BY_RECIPIENT) { if (bodyBytes.length !== 0) throw new Error('BAD_DELETE_BODY'); if (revisionTimeMs !== 0) throw new Error('BAD_REVISION_TIME'); if (reencryptedAtMs !== 0) throw new Error('BAD_REENCRYPTED_TIME'); } return { toLogin, fromLogin, timeMs, nonce, messageType, revisionTimeMs, reencryptedAtMs, formatVersionMajor, formatVersionMinor, encryptedBodyBytes: bodyBytes, encryptedBodyPayload, text: '', bodyAttachments: [], payloadBytes: bodyBytes, signatureBytes, signedBody, rawBytes: bytes, baseKey, messageKey, legacyFormat: false, deleted: messageType >= DM_TYPE_MESSAGE_DELETED_BY_SENDER, isMessageDelete: messageType === DM_TYPE_MESSAGE_DELETED_BY_SENDER || messageType === DM_TYPE_MESSAGE_DELETED_BY_RECIPIENT, isConversationDelete: messageType === DM_TYPE_CONVERSATION_DELETED_BY_SENDER || messageType === DM_TYPE_CONVERSATION_DELETED_BY_RECIPIENT, readReceiptPayload, }; } function extractContactsFromDialogs(dialogs) { const seen = new Set(); const out = []; (Array.isArray(dialogs) ? dialogs : []).forEach((dialog) => { const relationFlag = String(dialog?.relationFlag || '').trim().toLowerCase(); if (relationFlag !== 'contact' && relationFlag !== 'close_friend') return; const login = String(dialog?.peerLogin || '').trim(); if (!login) return; const key = login.toLowerCase(); if (seen.has(key)) return; seen.add(key); out.push(login); }); return out; } export const HEADER_CONTENT_VERSION = 1; function makeHeaderBodyBytes({ login, initialBlockchainKey32 }) { const cleanLogin = String(login || '').trim(); const loginBytes = utf8Bytes(cleanLogin); const keyBytes = initialBlockchainKey32 instanceof Uint8Array ? initialBlockchainKey32 : new Uint8Array(initialBlockchainKey32 || []); if (loginBytes.length < 1 || loginBytes.length > 255) throw new Error('HEADER login must be 1..255 UTF-8 bytes'); if (keyBytes.length !== 32 || keyBytes.every((value) => value === 0)) { throw new Error('HEADER initialBlockchainKey32 must be a non-zero 32-byte key'); } return concatBytes( utf8Bytes('SHiNE'), int8Byte(loginBytes.length), loginBytes, keyBytes, ); } function makeUserParamBodyBytes({ prevProfileBlockNumber, prevProfileBlockHashHex, profileSequence, key, value }) { const keyBytes = utf8Bytes(String(key || '')); const valueBytes = utf8Bytes(String(value || '')); const prevHashBytes = hexToBytes(prevProfileBlockHashHex); if (!keyBytes.length || !valueBytes.length) throw new Error('Пустые key/value для блока профиля'); if (!Number.isInteger(Number(prevProfileBlockNumber)) || Number(prevProfileBlockNumber) < 0) throw new Error('Некорректный previous profile block number'); if (!Number.isInteger(Number(profileSequence)) || Number(profileSequence) < 1) throw new Error('Некорректный profile sequence'); if (prevHashBytes.length !== 32) throw new Error('prevProfileBlockHash должен быть 32 байта'); return concatBytes( int32Bytes(prevProfileBlockNumber), prevHashBytes, int32Bytes(profileSequence), int16Bytes(keyBytes.length), keyBytes, int16Bytes(valueBytes.length), valueBytes, ); } function makeReactionLikeBodyBytes({ toLogin, toForkNumber, toBlockNumber, toBlockHashHex }) { const cleanLogin = String(toLogin || '').trim(); const forkNumber = normalizeTargetForkNumber(toForkNumber, 'like'); const blockNumber = normalizeTargetBlockNumber(toBlockNumber, 'like'); const loginBytes = targetLoginBytes(cleanLogin); const hashBytes = nonZeroHash32Bytes(toBlockHashHex, 'like'); return concatBytes(int8Byte(loginBytes.length), loginBytes, int16Bytes(forkNumber), int32Bytes(blockNumber), hashBytes); } function makeTextReplyBodyBytes({ toLogin, toForkNumber, toBlockNumber, toBlockHashHex, text }) { const loginBytes = targetLoginBytes(toLogin); const forkNumber = normalizeTargetForkNumber(toForkNumber, 'reply'); const blockNumber = normalizeTargetBlockNumber(toBlockNumber, 'reply'); const hashBytes = nonZeroHash32Bytes(toBlockHashHex, 'reply'); const textBytes = utf8Bytes(String(text || '').trim()); if (textBytes.length < 1 || textBytes.length > 65535) throw new Error('Reply text must be 1..65535 UTF-8 bytes'); return concatBytes(int8Byte(loginBytes.length), loginBytes, int16Bytes(forkNumber), int32Bytes(blockNumber), hashBytes, int16Bytes(textBytes.length), textBytes); } function makeTextRatingBodyBytes({ toLogin, toForkNumber, toBlockNumber, toBlockHashHex, text }) { const loginBytes = targetLoginBytes(toLogin); const forkNumber = normalizeTargetForkNumber(toForkNumber, 'rating'); const blockNumber = normalizeTargetBlockNumber(toBlockNumber, 'rating'); const hashBytes = nonZeroHash32Bytes(toBlockHashHex, 'rating'); const textBytes = utf8Bytes(String(text || '').trim()); if (textBytes.length < 1 || textBytes.length > 65535) throw new Error('Rating text must be 1..65535 UTF-8 bytes'); return concatBytes(int8Byte(loginBytes.length), loginBytes, int16Bytes(forkNumber), int32Bytes(blockNumber), hashBytes, int16Bytes(textBytes.length), textBytes); } function makeStatusActionBodyBytes({ toLogin, toForkNumber, toBlockNumber, toBlockHashHex, text = '' }) { const loginBytes = targetLoginBytes(toLogin); const forkNumber = normalizeTargetForkNumber(toForkNumber, 'status action'); const blockNumber = normalizeTargetBlockNumber(toBlockNumber, 'status action'); const hashBytes = nonZeroHash32Bytes(toBlockHashHex, 'status action'); const textBytes = utf8Bytes(String(text || '').trim()); if (textBytes.length > 65535) throw new Error('Status action text must be 0..65535 UTF-8 bytes'); return concatBytes(int8Byte(loginBytes.length), loginBytes, int16Bytes(forkNumber), int32Bytes(blockNumber), hashBytes, int16Bytes(textBytes.length), textBytes); } function makeTextRepostBodyBytes({ lineCode, prevLineNumber, prevLineHashHex, thisLineNumber, toLogin, toForkNumber, toBlockNumber, toBlockHashHex, text, }) { const message = String(text || '').trim(); if (!message) throw new Error('Комментарий к репосту обязателен'); const loginBytes = targetLoginBytes(toLogin); const forkNumber = normalizeTargetForkNumber(toForkNumber, 'repost'); const blockNumber = normalizeTargetBlockNumber(toBlockNumber, 'repost'); const hashBytes = nonZeroHash32Bytes(toBlockHashHex, 'repost'); const textBytes = utf8Bytes(message); if (textBytes.length > 65535) throw new Error('Repost comment too long'); return concatBytes( int32Bytes(lineCode), int32Bytes(prevLineNumber), hexToBytes(normalizeHex32(prevLineHashHex)), int32Bytes(thisLineNumber), int8Byte(loginBytes.length), loginBytes, int16Bytes(forkNumber), int32Bytes(blockNumber), hashBytes, int16Bytes(textBytes.length), textBytes, ); } function makeTextEditPostBodyBytes({ lineCode, prevLineNumber, prevLineHashHex, thisLineNumber, toBlockNumber, toBlockHashHex, text, }) { const targetBlockNumber = normalizeTargetBlockNumber(toBlockNumber, 'edit post'); const hashBytes = nonZeroHash32Bytes(toBlockHashHex, 'edit post'); const textBytes = utf8Bytes(String(text || '').trim()); if (textBytes.length > 65535) throw new Error('Message text must be 0..65535 UTF-8 bytes'); return concatBytes( int32Bytes(lineCode), int32Bytes(prevLineNumber), hexToBytes(normalizeHex32(prevLineHashHex)), int32Bytes(thisLineNumber), int32Bytes(targetBlockNumber), hashBytes, int16Bytes(textBytes.length), textBytes, ); } function makeTextEditReplyBodyBytes({ toBlockNumber, toBlockHashHex, text }) { const targetBlockNumber = normalizeTargetBlockNumber(toBlockNumber, 'edit reply'); const hashBytes = nonZeroHash32Bytes(toBlockHashHex, 'edit reply'); const textBytes = utf8Bytes(String(text || '').trim()); if (textBytes.length > 65535) throw new Error('Message text must be 0..65535 UTF-8 bytes'); return concatBytes(int32Bytes(targetBlockNumber), hashBytes, int16Bytes(textBytes.length), textBytes); } function makeConnectionBodyBytes({ prevConnectionBlockNumber, prevConnectionBlockHashHex, connectionSequence, toLogin, toForkNumber, toBlockNumber, toBlockHashHex, }) { const loginBytes = targetLoginBytes(toLogin); const forkNumber = normalizeTargetForkNumber(toForkNumber, 'connection'); const blockNumber = normalizeTargetBlockNumber(toBlockNumber, 'connection'); const hashBytes = nonZeroHash32Bytes(toBlockHashHex, 'connection'); if (!Number.isInteger(Number(prevConnectionBlockNumber)) || Number(prevConnectionBlockNumber) < 0) throw new Error('Некорректный previous connection block number'); if (!Number.isInteger(Number(connectionSequence)) || Number(connectionSequence) < 1) throw new Error('Некорректный connection sequence'); return concatBytes( int32Bytes(prevConnectionBlockNumber), hexToBytes(normalizeHex32(prevConnectionBlockHashHex)), int32Bytes(connectionSequence), int8Byte(loginBytes.length), loginBytes, int16Bytes(forkNumber), int32Bytes(blockNumber), hashBytes, ); } function targetLoginBytes(value) { const bytes = utf8Bytes(String(value || '').trim()); if (bytes.length < 1 || bytes.length > 255) throw new Error('toLogin must be 1..255 UTF-8 bytes'); return bytes; } function normalizeTargetForkNumber(value, actionName = 'target') { const n = Number(value); if (!Number.isInteger(n) || n < 0 || n > 999) throw new Error(`Invalid toForkNumber for ${actionName}`); return n; } function normalizeTargetBlockNumber(value, actionName = 'target') { const n = Number(value); if (!Number.isInteger(n) || n < 0) throw new Error(`Invalid toBlockNumber for ${actionName}`); return n; } function nonZeroHash32Bytes(value, actionName = 'target') { const hex = normalizeHex32(value, ZERO64); if (hex === ZERO64) throw new Error(`Zero target hash is forbidden for ${actionName}`); return hexToBytes(hex); } function normalizeChannelDescription(value) { const text = String(value == null ? '' : value).trim().replace(/\r\n/g, '\n').replace(/\r/g, '\n'); const bytes = utf8Bytes(text); if (bytes.length > CREATE_CHANNEL_DESCRIPTION_MAX_BYTES) { throw new Error('Описание канала слишком длинное: максимум 2048 байт.'); } return text; } function validateChannelMetaTitle(value) { const text = String(value || '').trim(); if (Array.from(text).length > 50) throw new Error('Название канала слишком длинное: максимум 50 символов.'); if (/[<>;\t\n\r\u0000]/u.test(text)) { throw new Error('В названии канала нельзя использовать < > ; табуляцию и перевод строки.'); } return text; } function normalizeChannelMetaDescription(value) { const text = String(value == null ? '' : value).trim(); if (Array.from(text).length > 250) throw new Error('Описание канала слишком длинное: максимум 250 символов.'); return text; } function composeChannelMetaText({ title = '', description = '', avatar = null } = {}) { const cleanTitle = validateChannelMetaTitle(title); const cleanDescription = normalizeChannelMetaDescription(description); const rows = []; if (cleanTitle) rows.push(``); if (avatar?.pr) { rows.push(buildAvatarReference({ size: avatar.size, pr: avatar.pr, ar: avatar.ar })); } if (cleanDescription) rows.push(cleanDescription); return rows.join('\n'); } function validatePersonalChannelName(value) { const normalized = normalizeChannelDisplayName(value); if (!normalized) return { ok: false, error: 'Введите логин пользователя.' }; const length = Array.from(normalized).length; if (length < 4 || length > 20) { return { ok: false, error: 'Логин для нового персонального канала: 4–20 символов.' }; } if (!/^[A-Za-z0-9_]+$/.test(normalized)) { return { ok: false, error: 'Логин: разрешены только латиница, цифры и _.' }; } return { ok: true, normalized }; } function makeCreateChannelBodyBytes({ lineCode, prevLineNumber, prevLineHashHex, thisLineNumber, channelName, channelDescription = '', channelType = CHANNEL_TYPE_PUBLIC, channelTypeVersion = CHANNEL_TYPE_VERSION_DEFAULT, }) { const typeCode = Number(channelType); const nameCheck = typeCode === CHANNEL_TYPE_PERSONAL ? validatePersonalChannelName(channelName) : validateChannelDisplayName(channelName); if (!nameCheck.ok) { throw new Error(typeCode === CHANNEL_TYPE_PERSONAL ? nameCheck.error : channelNameErrorText(nameCheck.code)); } const cleanName = nameCheck.normalized; const cleanDescription = normalizeChannelDescription(channelDescription); const nameBytes = utf8Bytes(cleanName); if (nameBytes.length < 1 || nameBytes.length > 255) { throw new Error('Channel name must be 1..255 bytes'); } const descriptionBytes = utf8Bytes(cleanDescription); if (descriptionBytes.length > CREATE_CHANNEL_DESCRIPTION_MAX_BYTES) { throw new Error('Описание канала слишком длинное: максимум 2048 байт.'); } const typeVer = Number(channelTypeVersion); if (!Number.isFinite(typeCode) || typeCode < 0 || typeCode > 65535) { throw new Error('Некорректный тип канала.'); } if (!Number.isFinite(typeVer) || typeVer < 0 || typeVer > 65535) { throw new Error('Некорректная версия типа канала.'); } return concatBytes( int32Bytes(lineCode), int32Bytes(prevLineNumber), hexToBytes(normalizeHex32(prevLineHashHex)), int32Bytes(thisLineNumber), int8Byte(nameBytes.length), nameBytes, int16Bytes(descriptionBytes.length), descriptionBytes, uint16Bytes(typeCode), uint16Bytes(typeVer), ); } function makeTextPostBodyBytes({ lineCode, prevLineNumber, prevLineHashHex, thisLineNumber, text }) { const message = String(text || '').trim(); if (!message) throw new Error('Message text is required'); const textBytes = utf8Bytes(message); if (textBytes.length < 1 || textBytes.length > 65535) { throw new Error('Message text must be 1..65535 UTF-8 bytes'); } return concatBytes( int32Bytes(lineCode), int32Bytes(prevLineNumber), hexToBytes(normalizeHex32(prevLineHashHex)), int32Bytes(thisLineNumber), int16Bytes(textBytes.length), textBytes ); } function makeTextLineBodyBytesAllowEmpty({ lineCode, prevLineNumber, prevLineHashHex, thisLineNumber, text }) { const message = String(text || '').trim(); const textBytes = utf8Bytes(message); if (textBytes.length > 65535) { throw new Error('Message text must be 0..65535 UTF-8 bytes'); } return concatBytes( int32Bytes(lineCode), int32Bytes(prevLineNumber), hexToBytes(normalizeHex32(prevLineHashHex)), int32Bytes(thisLineNumber), int16Bytes(textBytes.length), textBytes ); } function loginFromBlockchainNameValue(value) { const name = String(value || '').trim(); const match = name.match(/^(.*)-(\d{3})$/u); return match?.[1] ? match[1] : ''; } function forkNumberFromBlockchainNameValue(value) { const name = String(value || '').trim(); const match = name.match(/^(.*)-(\d{3})$/u); if (!match) return 0; const forkNumber = Number(match[2]); return Number.isInteger(forkNumber) && forkNumber >= 1 && forkNumber <= 999 ? forkNumber : 0; } function normalizeMessageRefTarget(target, actionName = 'action') { const cleanBch = String(target?.blockchainName || target?.authorBlockchainName || '').trim(); const cleanLogin = String(target?.login || target?.authorLogin || target?.ownerLogin || loginFromBlockchainNameValue(cleanBch)).trim(); const cleanBlockNumber = Number(target?.blockNumber ?? target?.messageRef?.blockNumber); const cleanBlockHash = String(target?.blockHash ?? target?.messageRef?.blockHash ?? '').trim().toLowerCase(); if (!cleanLogin) { throw new Error(`Missing message target login for ${actionName}`); } if (!Number.isFinite(cleanBlockNumber) || cleanBlockNumber < 0) { throw new Error(`Invalid message target block number for ${actionName}`); } if (!/^[0-9a-f]{64}$/.test(cleanBlockHash) || /^0+$/.test(cleanBlockHash)) { throw new Error(`Invalid message target hash for ${actionName}`); } const forkNumber = Number(target?.forkNumber ?? target?.toForkNumber ?? forkNumberFromBlockchainNameValue(cleanBch)); if (!Number.isInteger(forkNumber) || forkNumber < 1 || forkNumber > 999) { throw new Error(`Missing/invalid message target fork for ${actionName}`); } return { login: cleanLogin, blockchainName: cleanBch, forkNumber, blockNumber: cleanBlockNumber, blockHash: cleanBlockHash, }; } function resolveLatestLineStep(message) { const lineStep = Number(message?.lineStep); if (Number.isFinite(lineStep) && lineStep >= 0) return lineStep; const fallbackByVersions = Number(message?.versionsTotal); if (Number.isFinite(fallbackByVersions) && fallbackByVersions > 0) return Math.max(0, fallbackByVersions - 1); return null; } function isLineConflictError(error) { return new Set([ 'BAD_PREV_LINE_NUMBER', 'BAD_PREV_LINE_HASH', 'BAD_LINE_SEQUENCE', 'BAD_LINE_STATE', 'LINE_HEAD_CONFLICT', 'GLOBAL_HEAD_CONFLICT', ]).has(String(error?.code || '').toUpperCase()); } export class AuthService { constructor(serverUrl) { this.serverUrl = normalizeServerUrl(serverUrl); this.ws = new WsJsonClient(this.serverUrl); this.eventListeners = new Map(); this.wsEventUnsubscribers = new Map(); this.headerHashCache = new Map(); this.lineWriteQueues = new Map(); this.writeLocks = new Map(); this.passwordKeyBundleCache = new Map(); this.passwordKeyBundleInFlight = new Map(); this.currentLogin = ''; this.currentSessionId = ''; this.remoteAddBlockSessionId = ''; } bindRegisteredEventsToCurrentWs() { this.wsEventUnsubscribers.forEach((unsubscribe) => { try { unsubscribe?.(); } catch {} }); this.wsEventUnsubscribers.clear(); this.eventListeners.forEach((_handlers, op) => { const unsubscribe = this.ws.onEvent(op, (data) => { const handlers = this.eventListeners.get(op); if (!handlers) return; handlers.forEach((handler) => { try { handler(data); } catch {} }); }); this.wsEventUnsubscribers.set(op, unsubscribe); }); } resetConnection(serverUrl = this.serverUrl, { clearSessionContext = true } = {}) { const normalized = normalizeServerUrl(serverUrl); const requestGuard = this.ws?.requestGuard; try { this.ws?.close(); } catch {} this.serverUrl = normalized; this.ws = new WsJsonClient(this.serverUrl); this.ws.setRequestGuard(requestGuard); this.headerHashCache = new Map(); this.lineWriteQueues = new Map(); this.writeLocks.clear(); this.bindRegisteredEventsToCurrentWs(); if (clearSessionContext) this.clearActiveSessionContext(); } async reconnect(serverUrl) { const normalized = normalizeServerUrl(serverUrl); if (normalized === this.serverUrl) return; this.resetConnection(normalized, { clearSessionContext: false }); } setActiveSessionContext({ login = '', sessionId = '' } = {}) { this.currentLogin = String(login || '').trim(); this.currentSessionId = String(sessionId || '').trim(); } clearActiveSessionContext() { this.currentLogin = ''; this.currentSessionId = ''; } setRemoteAddBlockSessionId(sessionId = '') { this.remoteAddBlockSessionId = String(sessionId || '').trim(); } runWriteLocked(lockKey, runAction) { const key = String(lockKey || '').trim() || 'write'; if (this.writeLocks.has(key)) return this.writeLocks.get(key); const task = (async () => runAction())().finally(() => { this.writeLocks.delete(key); }); this.writeLocks.set(key, task); return task; } async getUser(login) { const response = await this.ws.request('GetUser', { login }); if (response.status !== 200) throw opError('GetUser', response); return response.payload || {}; } async resolveLoginForAuth(login) { const cleanLogin = String(login || '').trim(); if (!cleanLogin) throw new Error('Введите логин'); const response = await this.ws.request('ResolveLoginForAuth', { login: cleanLogin }); if (response.status !== 200) throw opError('ResolveLoginForAuth', response); return response.payload || {}; } async resolveCanonicalDisplayLogin(login) { const cleanLogin = String(login || '').trim(); if (!cleanLogin) return ''; try { const user = await this.getUser(cleanLogin); const canonicalLogin = String(user?.login || '').trim(); return canonicalLogin || cleanLogin; } catch { return cleanLogin; } } // При ЗАПИСИ в блокчейн нельзя использовать fallback из имени цепочки, // URL или непроверенного сообщения: нужен именно регистр регистрационной записи. async requireRegisteredDisplayLogin(login) { const wanted = String(login || '').trim(); if (!wanted) throw new Error('Не указан логин адресата'); const user = await this.getUser(wanted); const registered = String(user?.login || '').trim(); if (!user?.exists || !registered || registered.toLowerCase() !== wanted.toLowerCase()) { throw new Error(`Не удалось подтвердить зарегистрированный логин: ${wanted}`); } return registered; } async ensureLoginFree(login) { const payload = await this.getUser(login); return payload.exists !== true; } async derivePasswordKeyBundle(login, password, options = {}) { const normalizedLogin = String(login ?? ''); const normalizedPassword = String(password ?? ''); const cacheKey = `${normalizedLogin}\n${normalizedPassword}`; const onProgress = typeof options?.onProgress === 'function' ? options.onProgress : null; const isCancelled = typeof options?.isCancelled === 'function' ? options.isCancelled : null; if (this.passwordKeyBundleCache.has(cacheKey)) { if (onProgress) onProgress({ percent: 100, stage: 'cached', message: 'Ключи уже сгенерированы в памяти.' }); return this.passwordKeyBundleCache.get(cacheKey); } if (this.passwordKeyBundleInFlight.has(cacheKey)) { return this.passwordKeyBundleInFlight.get(cacheKey); } const task = (async () => { if (onProgress) onProgress({ percent: 3, stage: 'prepare', message: 'Подготовка параметров генерации...' }); if (isCancelled && isCancelled()) throw new Error('DERIVE_CANCELLED'); const masterSecret = await deriveMasterSecretFromPassword(normalizedPassword, { login: normalizedLogin, onProgress: (value01) => { if (!onProgress) return; const v = Math.max(0, Math.min(1, Number(value01) || 0)); const percent = Math.round(5 + (v * 88)); onProgress({ percent, stage: 'secret', message: 'Генерация секрета из пароля...' }); }, }); if (isCancelled && isCancelled()) throw new Error('DERIVE_CANCELLED'); if (onProgress) onProgress({ percent: 94, stage: 'derive', message: 'Вычисление root key...' }); const rootPair = await deriveEd25519FromMasterSecret(masterSecret, 'root.key'); if (isCancelled && isCancelled()) throw new Error('DERIVE_CANCELLED'); if (onProgress) onProgress({ percent: 97, stage: 'derive', message: 'Вычисление blockchain key...' }); const blockchainPair = await deriveEd25519FromMasterSecret(masterSecret, 'blockchain.key'); if (isCancelled && isCancelled()) throw new Error('DERIVE_CANCELLED'); if (onProgress) onProgress({ percent: 99, stage: 'derive', message: 'Вычисление client key...' }); const clientPair = await deriveEd25519FromMasterSecret(masterSecret, 'client.key'); const result = { masterSecretB64: bytesToBase64(masterSecret), rootPair, blockchainPair, clientPair, }; this.passwordKeyBundleCache.set(cacheKey, result); if (onProgress) onProgress({ percent: 100, stage: 'done', message: 'Ключи сгенерированы.' }); return result; })().finally(() => { this.passwordKeyBundleInFlight.delete(cacheKey); }); this.passwordKeyBundleInFlight.set(cacheKey, task); return task; } async createAuthSession(login, keyBundle) { const cleanLogin = (login || '').trim(); if (!cleanLogin) throw new Error('Введите логин'); const clientPair = keyBundle?.clientPair; if (!clientPair) throw new Error('createAuthSession: не передан clientPair'); // Новый вход всегда начинается на анонимном транспорте. Это важно после // частично успешной попытки: сервер хранит авторизацию в ConnectionContext // конкретного WebSocket, поэтому повторный AuthChallenge на нём получает // ALREADY_AUTHED даже для другого логина. if (this.ws.isSessionReady()) { this.resetConnection(this.serverUrl, { clearSessionContext: true }); } const sessionPair = await generateEd25519Pair(); const sessionKeyPub = await exportEd25519PublicKeyB64(sessionPair.publicKey); const sessionKey = `ed25519/${sessionKeyPub}`; const storagePwd = randomBase64(32); let challengeResp; for (let attempt = 0; attempt < 2; attempt += 1) { challengeResp = await this.ws.request('AuthChallenge', { login: cleanLogin }); if (challengeResp.status === 200) break; const error = opError('AuthChallenge', challengeResp); if (attempt === 0 && error.code === 'ALREADY_AUTHED') { // Сбрасываем только транспорт. Ключи и данные регистрации остаются // в памяти, после чего повторяем только challenge на новом сокете. this.resetConnection(this.serverUrl, { clearSessionContext: true }); continue; } throw error; } const authNonce = challengeResp?.payload?.authNonce; if (!authNonce) throw new Error('AuthChallenge: сервер не вернул authNonce'); const timeMs = Date.now(); const preimage = `AUTH_CREATE_SESSION:${cleanLogin}:${sessionKey}:${storagePwd}:${timeMs}:${authNonce}`; const signatureB64 = await signBase64(clientPair.privateKey, preimage); const createResp = await this.ws.request('CreateAuthSession', { login: cleanLogin, storagePwd, sessionKey, timeMs, authNonce, clientKey: clientPair.publicKeyB64, signatureB64, sessionType: SESSION_TYPE_CLIENT, clientPlatform: makeClientPlatform(), clientInfo: makeClientInfo(), }); if (createResp.status !== 200) throw opError('CreateAuthSession', createResp); const sessionId = createResp?.payload?.sessionId; if (!sessionId) throw new Error('CreateAuthSession: не вернулся sessionId'); this.ws.markSessionReady(); const connectionScope = String(createResp?.payload?.connectionScope || '').trim().toUpperCase(); const canonicalLogin = await this.resolveCanonicalDisplayLogin(cleanLogin); return { login: canonicalLogin, sessionId, storagePwd, connectionScope, sessionMaterial: { sessionId, sessionKey, sessionPrivPkcs8: await exportPkcs8B64(sessionPair.privateKey), }, }; } async createSessionForExistingUser(login, password) { const cleanLogin = (login || '').trim(); if (!cleanLogin) throw new Error('Введите логин'); const keyBundle = await this.derivePasswordKeyBundle(cleanLogin, password); const session = await this.createAuthSession(cleanLogin, keyBundle); return { ...session, keyBundle }; } async createSessionFromImportedSecrets(login, secrets) { const cleanLogin = (login || '').trim(); if (!cleanLogin) throw new Error('В QR-коде нет логина'); const clientKey = String(secrets?.clientKey || secrets?.clientKey || '').trim(); if (!clientKey) throw new Error('В QR-коде нет client key для входа'); const privateKey = await importPkcs8Ed25519(clientKey); const publicKeyB64 = await publicKeyB64FromPkcs8Ed25519(clientKey); const session = await this.createAuthSession(cleanLogin, { clientPair: { privateKey, publicKeyB64, }, }); return session; } async createDelegatedSessionWithClientKey({ login, clientPrivPkcs8, sessionKey, sessionType = SESSION_TYPE_WALLET, clientPlatform = 'Delegated session', clientInfo = 'Delegated session via pairing', }) { const cleanLogin = String(login || '').trim(); const cleanSessionKey = String(sessionKey || '').trim(); const cleanClientPriv = String(clientPrivPkcs8 || '').trim(); if (!cleanLogin) throw new Error('createDelegatedSessionWithClientKey: пустой login'); if (!cleanSessionKey) throw new Error('createDelegatedSessionWithClientKey: пустой sessionKey'); if (!cleanClientPriv) throw new Error('createDelegatedSessionWithClientKey: пустой client private key'); const clientPrivateKey = await importPkcs8Ed25519(cleanClientPriv); const clientPublicKeyB64 = await publicKeyB64FromPkcs8Ed25519(cleanClientPriv); const storagePwd = randomBase64(32); const tempAuth = new AuthService(this.serverUrl); try { const challengeResp = await tempAuth.ws.request('AuthChallenge', { login: cleanLogin }); if (challengeResp.status !== 200) throw opError('AuthChallenge', challengeResp); const authNonce = challengeResp?.payload?.authNonce; if (!authNonce) throw new Error('AuthChallenge: сервер не вернул authNonce'); const timeMs = Date.now(); const preimage = `AUTH_CREATE_SESSION:${cleanLogin}:${cleanSessionKey}:${storagePwd}:${timeMs}:${authNonce}`; const signatureB64 = await signBase64(clientPrivateKey, preimage); const createResp = await tempAuth.ws.request('CreateAuthSession', { login: cleanLogin, storagePwd, sessionKey: cleanSessionKey, timeMs, authNonce, clientKey: clientPublicKeyB64, signatureB64, sessionType: Number(sessionType) || SESSION_TYPE_WALLET, clientPlatform: String(clientPlatform || '').trim() || 'Delegated session', clientInfo: String(clientInfo || '').trim() || 'Delegated session via pairing', }); if (createResp.status !== 200) throw opError('CreateAuthSession', createResp); const sessionId = createResp?.payload?.sessionId; if (!sessionId) throw new Error('CreateAuthSession: не вернулся sessionId'); tempAuth.ws.markSessionReady(); const canonicalLogin = await tempAuth.resolveCanonicalDisplayLogin(cleanLogin); return { login: canonicalLogin, sessionId, storagePwd, sessionKey: cleanSessionKey, sessionType: Number(sessionType) || SESSION_TYPE_WALLET, clientPlatform: String(clientPlatform || '').trim() || 'Delegated session', }; } finally { tempAuth.ws.close(); } } async persistSelectedKeys(login, storagePwd, keyBundle, saveOptions = { saveRoot: true, saveBlockchain: true }) { let currentSecrets = {}; try { const loaded = await loadEncryptedUserSecrets(login, storagePwd); if (loaded && typeof loaded === 'object') { currentSecrets = loaded; } } catch { // Если контейнера ещё нет или пароль новый для этого логина — создадим новый ниже. } const secrets = { ...currentSecrets, clientKey: keyBundle.clientPair.privatePkcs8B64, }; if (saveOptions.saveRoot) secrets.rootKey = keyBundle.rootPair.privatePkcs8B64; if (saveOptions.saveBlockchain) secrets.blockchainKey = keyBundle.blockchainPair.privatePkcs8B64; await saveEncryptedUserSecrets(login, storagePwd, secrets); } async persistSessionMaterial(login, sessionMaterial) { await saveSessionMaterial(login, sessionMaterial); } async resumeSession(login, preferredSessionId = '') { const cleanLogin = (login || '').trim(); if (!cleanLogin) throw new Error('Нет login для авто-входа'); const sessionMaterial = await loadSessionMaterial(cleanLogin); if (!sessionMaterial?.sessionId || !sessionMaterial?.sessionKey || !sessionMaterial?.sessionPrivPkcs8) { throw new Error('На устройстве нет сохраненного ключа сессии'); } const targetSessionId = preferredSessionId || sessionMaterial.sessionId; const privateKey = await importPkcs8Ed25519(sessionMaterial.sessionPrivPkcs8); const challengeResp = await this.ws.request('SessionChallenge', { sessionId: targetSessionId }); if (challengeResp.status !== 200) throw opError('SessionChallenge', challengeResp); const nonce = challengeResp?.payload?.nonce; if (!nonce) throw new Error('SessionChallenge: не вернулся nonce'); const timeMs = Date.now(); const preimage = `SESSION_LOGIN:${targetSessionId}:${timeMs}:${nonce}`; const signatureB64 = await signBase64(privateKey, preimage); const loginResp = await this.ws.request('SessionLogin', { sessionId: targetSessionId, sessionKey: sessionMaterial.sessionKey, timeMs, signatureB64, sessionType: SESSION_TYPE_CLIENT, clientPlatform: makeClientPlatform(), clientInfo: makeClientInfo(), }); if (loginResp.status !== 200) throw opError('SessionLogin', loginResp); const storagePwd = loginResp?.payload?.storagePwd; if (!storagePwd) throw new Error('SessionLogin: не вернулся storagePwd'); this.ws.markSessionReady(); const connectionScope = String(loginResp?.payload?.connectionScope || '').trim().toUpperCase(); const canonicalLogin = await this.resolveCanonicalDisplayLogin(cleanLogin); return { login: canonicalLogin, sessionId: targetSessionId, storagePwd, connectionScope, }; } async listSessions() { const response = await this.ws.request('ListSessions', {}); if (response.status !== 200) throw opError('ListSessions', response); return response?.payload?.sessions || []; } async waitForSignal({ signalType, signalRequestId, timeoutMs = 15000 }) { const cleanSignalType = String(signalType || '').trim(); const cleanSignalRequestId = String(signalRequestId || '').trim(); if (!cleanSignalType || !cleanSignalRequestId) { throw new Error('waitForSignal: не переданы signalType/signalRequestId'); } return new Promise((resolve, reject) => { let unsubscribe = () => {}; const timer = window.setTimeout(() => { unsubscribe(); reject(new Error(`Таймаут ожидания сигнала ${cleanSignalType}`)); }, timeoutMs); unsubscribe = this.onEvent('IncomingSignal', (evt) => { const payload = evt?.payload || {}; if (String(payload?.signalType || '').trim() !== cleanSignalType) return; if (String(payload?.signalRequestId || '').trim() !== cleanSignalRequestId) return; window.clearTimeout(timer); unsubscribe(); resolve(payload); }); }); } async sendSignal({ toLogin, targetMode = SIGNAL_TARGET_SINGLE, targetSessionId = '', signalType, signalRequestId, data = '', storagePwd = '', includeClientSignature = true, timeMs = Date.now(), } = {}) { const cleanToLogin = String(toLogin || '').trim(); const cleanTargetMode = String(targetMode || '').trim(); const cleanTargetSessionId = String(targetSessionId || '').trim(); const cleanSignalType = String(signalType || '').trim(); const cleanSignalRequestId = String(signalRequestId || '').trim(); const cleanLogin = String(this.currentLogin || '').trim(); const cleanSessionId = String(this.currentSessionId || '').trim(); if (!cleanLogin || !cleanSessionId) throw new Error('SendSignal: нет активного login/sessionId'); if (!cleanToLogin || !cleanSignalType || !cleanSignalRequestId) { throw new Error('SendSignal: не переданы toLogin/signalType/signalRequestId'); } if (cleanTargetMode !== SIGNAL_TARGET_SINGLE && cleanTargetMode !== SIGNAL_TARGET_ALL) { throw new Error('SendSignal: bad targetMode'); } if (cleanTargetMode === SIGNAL_TARGET_SINGLE && !cleanTargetSessionId) { throw new Error('SendSignal: targetSessionId обязателен для single_session'); } const sessionMaterial = await loadSessionMaterial(cleanLogin); if (!sessionMaterial?.sessionPrivPkcs8) { throw new Error('На устройстве нет сохранённого session key для SendSignal'); } const sessionPrivateKey = await importPkcs8Ed25519(sessionMaterial.sessionPrivPkcs8); const dataText = typeof data === 'string' ? data : JSON.stringify(data || {}); const dataSha256B64 = await sha256Base64FromText(dataText); const sessionPreimage = `SEND_SIGNAL_SESSION:${cleanLogin}:${cleanSessionId}:${cleanToLogin}:${cleanTargetMode}:${cleanTargetSessionId}:${cleanSignalType}:${cleanSignalRequestId}:${Number(timeMs)}:${dataSha256B64}`; const sessionSignatureB64 = await signBase64(sessionPrivateKey, sessionPreimage); let clientSignatureB64 = ''; if (includeClientSignature) { if (!storagePwd) throw new Error('SendSignal: нужен storagePwd для подписи client key'); const secrets = await loadEncryptedUserSecrets(cleanLogin, storagePwd); const clientPrivatePkcs8 = String(secrets?.clientKey || '').trim(); if (!clientPrivatePkcs8) { throw new Error('На устройстве нет сохранённого client key для SendSignal'); } const clientPrivateKey = await importPkcs8Ed25519(clientPrivatePkcs8); const clientPreimage = `SEND_SIGNAL_CLIENT:${cleanLogin}:${cleanSessionId}:${cleanToLogin}:${cleanTargetMode}:${cleanTargetSessionId}:${cleanSignalType}:${cleanSignalRequestId}:${Number(timeMs)}:${dataSha256B64}`; clientSignatureB64 = await signBase64(clientPrivateKey, clientPreimage); } const response = await this.ws.request('SendSignal', { toLogin: cleanToLogin, targetMode: cleanTargetMode, targetSessionId: cleanTargetMode === SIGNAL_TARGET_SINGLE ? cleanTargetSessionId : '', signalType: cleanSignalType, signalRequestId: cleanSignalRequestId, data: dataText, timeMs: Number(timeMs), sessionSignatureB64, clientSignatureB64, }); if (response.status !== 200) throw opError('SendSignal', response); return response.payload || {}; } async closeSession(sessionId) { const response = await this.ws.request('CloseActiveSession', { sessionId }); if (response.status !== 200) throw opError('CloseActiveSession', response); } async closeOtherSessions() { const response = await this.ws.request('CloseOtherSessions', {}); if (response.status !== 200) throw opError('CloseOtherSessions', response); return response.payload || {}; } async getTrustedDeviceLoginSettings() { const response = await this.ws.request('GetTrustedDeviceLoginSettings', {}); if (response.status !== 200) throw opError('GetTrustedDeviceLoginSettings', response); return response.payload || {}; } async upsertTrustedDeviceLoginSettings({ enabled, passwordHash = '' }) { const response = await this.ws.request('UpsertTrustedDeviceLoginSettings', { enabled: !!enabled, passwordHash: String(passwordHash || '').trim(), }); if (response.status !== 200) throw opError('UpsertTrustedDeviceLoginSettings', response); return response.payload || {}; } async startTrustedDeviceLogin({ login, passwordHash, requesterSessionKey, requesterSessionType = SESSION_TYPE_CLIENT, requesterClientPlatform = makeClientPlatform(), payloadType = 3, }) { const response = await this.ws.request('StartTrustedDeviceLogin', { login: String(login || '').trim(), passwordHash: String(passwordHash || '').trim(), requesterSessionKey: String(requesterSessionKey || '').trim(), requesterSessionType: Number(requesterSessionType) || SESSION_TYPE_CLIENT, requesterClientPlatform: String(requesterClientPlatform || '').trim() || makeClientPlatform(), payloadType: Number(payloadType) || 3, }); if (response.status !== 200) throw opError('StartTrustedDeviceLogin', response); return response.payload || {}; } async listTrustedDeviceLoginRequests() { const response = await this.ws.request('ListTrustedDeviceLoginRequests', {}); if (response.status !== 200) throw opError('ListTrustedDeviceLoginRequests', response); return Array.isArray(response?.payload?.requests) ? response.payload.requests : []; } async approveTrustedDeviceLogin(pairingId, encryptedPayload) { const response = await this.ws.request('ApproveTrustedDeviceLogin', { pairingId: String(pairingId || '').trim(), encryptedPayload: String(encryptedPayload || '').trim(), }); if (response.status !== 200) throw opError('ApproveTrustedDeviceLogin', response); return response.payload || {}; } async rejectTrustedDeviceLogin(pairingId, reason = '') { const response = await this.ws.request('RejectTrustedDeviceLogin', { pairingId: String(pairingId || '').trim(), reason: String(reason || '').trim(), }); if (response.status !== 200) throw opError('RejectTrustedDeviceLogin', response); return response.payload || {}; } async cancelTrustedDeviceLogin(pairingId, requesterSessionKey) { const response = await this.ws.request('CancelTrustedDeviceLogin', { pairingId: String(pairingId || '').trim(), requesterSessionKey: String(requesterSessionKey || '').trim(), }); if (response.status !== 200) throw opError('CancelTrustedDeviceLogin', response); return response.payload || {}; } async getTrustedDeviceLoginStatus(pairingId) { const response = await this.ws.request('GetTrustedDeviceLoginStatus', { pairingId: String(pairingId || '').trim(), }); if (response.status !== 200) throw opError('GetTrustedDeviceLoginStatus', response); return response.payload || {}; } async upsertEspPairingSettings(args) { return this.upsertTrustedDeviceLoginSettings(args); } async startEspPairing(args) { return this.startTrustedDeviceLogin(args); } async listEspPairingRequests() { return this.listTrustedDeviceLoginRequests(); } async approveEspPairing(pairingId, encryptedPayload) { return this.approveTrustedDeviceLogin(pairingId, encryptedPayload); } async rejectEspPairing(pairingId, reason = '') { return this.rejectTrustedDeviceLogin(pairingId, reason); } async cancelEspPairing(pairingId, requesterSessionKey) { return this.cancelTrustedDeviceLogin(pairingId, requesterSessionKey); } async getEspPairingStatus(pairingId) { return this.getTrustedDeviceLoginStatus(pairingId); } async listSubscriptionsFeed(login, limit = 200) { const response = await this.ws.request('ListSubscriptionsFeed', { login, limit }); if (response.status !== 200) throw opError('ListSubscriptionsFeed', response); return response.payload || {}; } // Public channel lookup is not a request for another user's private feed. async listOwnedChannelSummaries(login, limit = 500) { const result = await this.listUserProfileChannels(login, 'owned', limit, 0, false); return (Array.isArray(result?.channels) ? result.channels : []).map(row => ({ channel: { ownerLogin: row.ownerLogin, ownerBlockchainName: row.ownerBlockchainName, channelName: row.slug, displayName: row.displayName, avaAr: row.avatarAr, avaPr: row.avatarPr, deleted: !!row.deleted, channelRoot: { blockNumber: row.rootBlockNumber, blockHash: row.rootBlockHashHex }, } })); } async getChannelMessages(channel, limit = null, sort = 'asc', login = '') { const normalizedChannel = { ownerBlockchainName: String(channel?.ownerBlockchainName || '').trim(), channelRootBlockNumber: Number(channel?.channelRootBlockNumber), channelRootBlockHash: String(channel?.channelRootBlockHash || '').trim(), }; const payload = { channel: normalizedChannel, sort }; const cleanLimit = Number(limit); if (Number.isFinite(cleanLimit) && cleanLimit > 0) payload.limit = Math.trunc(cleanLimit); const cleanLogin = String(login || '').trim(); if (cleanLogin) payload.login = cleanLogin; const response = await this.ws.request('GetChannelMessages', payload); if (response.status !== 200) throw opError('GetChannelMessages', response); return response.payload || {}; } async getPersonalDiary(login, limit = 200, sort = 'asc') { const payload = { login: String(login || '').trim(), limit, sort }; const response = await this.ws.request('GetPersonalDiary', payload); if (response.status !== 200) throw opError('GetPersonalDiary', response); return response.payload || {}; } async getMessageLikes(message) { const normalizedMessage = { blockchainName: String(message?.blockchainName || '').trim(), blockNumber: Number(message?.blockNumber), blockHash: String(message?.blockHash || '').trim(), }; const response = await this.ws.request('GetMessageLikes', { message: normalizedMessage }); if (response.status !== 200) throw opError('GetMessageLikes', response); return response.payload || {}; } async getSupportStats(login) { const response = await this.ws.request('GetSupportStats', { login: String(login || '').trim() }); if (response.status !== 200) throw opError('GetSupportStats', response); return response.payload || {}; } async getSupportHistory(login, limit = 20, offset = 0) { const response = await this.ws.request('GetSupportHistory', { login: String(login || '').trim(), limit, offset, }); if (response.status !== 200) throw opError('GetSupportHistory', response); return response.payload || {}; } async getSupportTotals(channel) { const payload = { channelRootBlockNumber: Number(channel?.channelRootBlockNumber), channelRootBlockHash: String(channel?.channelRootBlockHash || '').trim(), }; const response = await this.ws.request('GetSupportTotals', payload); if (response.status !== 200) throw opError('GetSupportTotals', response); return response.payload || {}; } async getMessageThread(message, depthUp = 20, depthDown = 2, limitChildrenPerNode = 50, login = '') { const normalizedMessage = { blockchainName: String(message?.blockchainName || '').trim(), blockNumber: Number(message?.blockNumber), blockHash: String(message?.blockHash || '').trim(), }; const payload = { message: normalizedMessage, depthUp, depthDown, limitChildrenPerNode }; const cleanLogin = String(login || '').trim(); if (cleanLogin) payload.login = cleanLogin; const response = await this.ws.request('GetMessageThread', payload); if (response.status !== 200) throw opError('GetMessageThread', response); return response.payload || {}; } async getCoreHeads(lineCode = null) { const response = await this.ws.request('GetMyCoreHeads', lineCode === null ? {} : { lineCode }); if (response.status !== 200) throw opError('GetMyCoreHeads', response); const heads = response.payload?.result; if (!heads || typeof heads.blockchainName !== 'string' || !heads.blockchainName) throw new Error('Invalid CORE heads response'); for (const head of [heads.global, heads.line]) { if (head != null) { if (!Number.isInteger(head.blockNumber) || head.blockNumber < 0) throw new Error('Invalid CORE head number'); coreHashBytes(head.coreHash); } } return heads; } async addBlockSigned({ login, storagePwd, msgType, msgSubType, msgVersion = 1, bodyBytes, channelSlug = '', _skipHeaderEnsure = false }) { const cleanLogin = (login || '').trim(); if (!cleanLogin || !storagePwd) throw new Error('Missing login/storagePwd for AddBlock'); const requestServer = this.serverUrl; const requestLogin = this.currentLogin; const requestSession = this.currentSessionId; const sameContext = () => this.serverUrl === requestServer && this.currentLogin === requestLogin && this.currentSessionId === requestSession; const isHeader = Number(msgType) === MSG_TYPE_TECH && Number(msgSubType) === MSG_SUBTYPE_TECH_HEADER; const keyBundle = await loadEncryptedUserSecrets(cleanLogin, storagePwd); const blockchainPrivatePkcs8 = String(keyBundle?.blockchainKey || '').trim(); // Old homeserver/ESP32 signs Frame. Never send it a CORE write or silently fall back. if (!blockchainPrivatePkcs8) throw new Error('CORE AddBlock requires a local blockchain key; remote signing is not migrated yet'); if (!_skipHeaderEnsure && !isHeader) { await this.ensureBlockchainHeader(cleanLogin, storagePwd); } const { content, line } = splitEventBody({ msgType: Number(msgType), msgSubType: Number(msgSubType), msgVersion: Number(msgVersion), bodyBytes }); const privateKey = await importPkcs8Ed25519(blockchainPrivatePkcs8); const owner32 = base64ToBytes(await publicKeyB64FromPkcs8Ed25519(blockchainPrivatePkcs8)); const heads = await this.getCoreHeads(line?.lineCode ?? null); const blockchainName = heads.blockchainName; if (loginFromBlockchainNameValue(blockchainName).toLowerCase() !== cleanLogin.toLowerCase()) throw new Error('CORE heads belong to another user'); if (!sameContext()) throw new Error('CORE write context changed'); if (isHeader && heads.global != null) return { serverLastGlobalNumber: heads.global.blockNumber, serverLastGlobalHash: heads.global.coreHash }; if (!isHeader && heads.global == null) throw new Error('CORE HEADER missing'); const op = isHeader ? 'CreateGenesis' : 'AddBlock'; const send = async current => { if (current.blockchainName.toLowerCase() !== blockchainName.toLowerCase()) throw new Error('CORE fork changed; refresh before writing'); if (line && (!current.line || current.line.blockNumber !== line.prevLineBlockNumber || current.line.coreHash.toLowerCase() !== coreHex(line.prevLineCoreHash32))) { const error = new Error('CORE line changed; rebuild event'); error.code = 'LINE_HEAD_CONFLICT'; throw error; } const blockNumber = current.global == null ? 0 : current.global.blockNumber + 1; if (isHeader !== (blockNumber === 0)) throw new Error('CORE genesis state changed'); const prevBlockHash = current.global?.coreHash.toLowerCase() ?? ZERO64; const pair = await signCoreContentPair({ owner32, privateKey, content, line, blockNumber, prevCoreHash32: coreHashBytes(prevBlockHash), timestamp: Math.floor(Date.now() / 1000) }); const payload = { blockchainName, blockNumber, prevBlockHash: isHeader ? '' : prevBlockHash, coreDataItemB64: bytesToBase64(pair.coreDataItem), contentDataItemB64: bytesToBase64(pair.contentDataItem) }; const blockHash = coreHex(pair.coreHash32); const accepted = response => ({ ...response, payload: { ...response.payload, submittedBlockNumber: blockNumber, submittedBlockHash: blockHash } }); const request = () => { if (!sameContext()) throw new Error('CORE write context changed'); return this.ws.request(op, payload); }; let response, firstError; try { response = await request(); } catch (error) { firstError = error; } if (!firstError && response && Number(response.status) >= 200 && Number(response.status) < 500) { return response.status === 200 ? accepted(response) : response; } // A lost response (or server 5xx) is NOT an explicit head conflict. The // first attempt may have committed. Retry exactly the same signed items. try { response = await request(); if (response?.status === 200) return accepted(response); } catch {} // If exact retry did not confirm success, check the actual signed bytes // in the currently active fork, not just the latest global head. if (sameContext()) { try { const active = await this.getCoreHeads(); if (sameContext() && active.blockchainName.toLowerCase() === blockchainName.toLowerCase()) { const found = await this.ws.request('GetBlockchainBlock', { blockchainName, blockNumber }); const row = found?.payload; if (sameContext() && found?.status === 200 && row?.blockNumber === blockNumber && row?.coreHash === blockHash && row?.contentStatus === 'VALID' && row.coreDataItemB64 === payload.coreDataItemB64 && row.contentDataItemB64 === payload.contentDataItemB64) { return accepted({ status: 200, payload: { serverLastGlobalNumber: active.global.blockNumber, serverLastGlobalHash: active.global.coreHash } }); } } } catch {} } // Never let the existing line retry rebuild an event after an uncertain // outcome. The caller must inspect history before manually submitting again. const error = new Error('Результат записи неизвестен. Проверьте историю блокчейна перед повторной отправкой (CORE_WRITE_OUTCOME_UNKNOWN)', { cause: firstError }); error.code = 'CORE_WRITE_OUTCOME_UNKNOWN'; error.blockchainName = blockchainName; error.blockNumber = blockNumber; error.coreHash = blockHash; throw error; }; let response = await send(heads); // A fresh signature is allowed only after a definite rejection, never after an ambiguous attempt. if (!isHeader && response.status === 409 && opError('AddBlock', response).code === 'GLOBAL_HEAD_CONFLICT') { response = await send(await this.getCoreHeads(line?.lineCode ?? null)); } if (response.status !== 200) throw opError(op, response); const payload = response.payload || {}; if (payload.submittedBlockNumber === 0 && payload.submittedBlockHash) this.headerHashCache.set(blockchainName, payload.submittedBlockHash); return payload; } async ensureBlockchainHeader(login, storagePwd) { const cleanLogin = String(login || '').trim(); if (!cleanLogin) throw new Error('Missing login for HEADER bootstrap'); const current = await this.getUser(cleanLogin); const lastNum = Number(current?.serverLastGlobalNumber); if (Number.isFinite(lastNum) && lastNum >= 0) return current; if (!(Number.isFinite(lastNum) && lastNum === -1)) return current; const keyBundle = await loadEncryptedUserSecrets(cleanLogin, storagePwd); const privatePkcs8 = String(keyBundle?.blockchainKey || '').trim(); let publicKeyB64 = ''; if (privatePkcs8) { publicKeyB64 = await publicKeyB64FromPkcs8Ed25519(privatePkcs8); } else { publicKeyB64 = String(current?.blockchainKey || '').trim(); } const initialBlockchainKey32 = base64ToBytes(publicKeyB64); if (initialBlockchainKey32.length !== 32 || initialBlockchainKey32.every((value) => value === 0)) { throw new Error('Не удалось определить initial blockchain key для HEADER'); } const registeredLogin = await this.requireRegisteredDisplayLogin(cleanLogin); await this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TECH, msgSubType: MSG_SUBTYPE_TECH_HEADER, msgVersion: HEADER_CONTENT_VERSION, // HEADER хранит исходный регистр неизменяемого имени из Solana/Arweave. // Нормализация нужна только для уникальности и имени самой цепочки. bodyBytes: makeHeaderBodyBytes({ login: registeredLogin, initialBlockchainKey32 }), _skipHeaderEnsure: true, }); return this.getUser(cleanLogin); } async ensureChainInitializedForLineOps(login, storagePwd) { return this.ensureBlockchainHeader(login, storagePwd); } async addBlockLike({ login, message, storagePwd }) { const cleanLogin = String(login || '').trim(); const target = normalizeMessageRefTarget(message, 'like'); const key = `like:${cleanLogin}:${target.blockchainName}:${target.blockNumber}:${target.blockHash}`; return this.runWriteLocked(key, async () => { const bodyBytes = makeReactionLikeBodyBytes({ toLogin: await this.requireRegisteredDisplayLogin(target.login), toForkNumber: target.forkNumber, toBlockNumber: target.blockNumber, toBlockHashHex: target.blockHash, }); return this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_REACTION, msgSubType: MSG_SUBTYPE_REACTION_LIKE, msgVersion: 1, bodyBytes, }); }); } async addBlockUnlike({ login, message, storagePwd }) { const cleanLogin = String(login || '').trim(); const target = normalizeMessageRefTarget(message, 'unlike'); const key = `unlike:${cleanLogin}:${target.blockchainName}:${target.blockNumber}:${target.blockHash}`; return this.runWriteLocked(key, async () => { const bodyBytes = makeReactionLikeBodyBytes({ toLogin: await this.requireRegisteredDisplayLogin(target.login), toForkNumber: target.forkNumber, toBlockNumber: target.blockNumber, toBlockHashHex: target.blockHash, }); return this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_REACTION, msgSubType: MSG_SUBTYPE_REACTION_UNLIKE, msgVersion: 1, bodyBytes, }); }); } async addBlockReply({ login, message, text, storagePwd }) { const cleanLogin = String(login || '').trim(); const cleanText = String(text || '').trim(); const target = normalizeMessageRefTarget(message, 'reply'); const key = `reply:${cleanLogin}:${target.blockchainName}:${target.blockNumber}:${target.blockHash}:${cleanText}`; return this.runWriteLocked(key, async () => { const bodyBytes = makeTextReplyBodyBytes({ toLogin: await this.requireRegisteredDisplayLogin(target.login), toForkNumber: target.forkNumber, toBlockNumber: target.blockNumber, toBlockHashHex: target.blockHash, text: cleanText, }); return this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TEXT, msgSubType: MSG_SUBTYPE_TEXT_REPLY, msgVersion: 1, bodyBytes, }); }); } async addBlockRating({ login, message, text, storagePwd }) { const cleanLogin = String(login || '').trim(); const cleanText = String(text || '').trim(); const target = normalizeMessageRefTarget(message, 'rating'); const key = `rating:${cleanLogin}:${target.blockchainName}:${target.blockNumber}:${target.blockHash}:${cleanText}`; return this.runWriteLocked(key, async () => { const bodyBytes = makeTextRatingBodyBytes({ toLogin: await this.requireRegisteredDisplayLogin(target.login), toForkNumber: target.forkNumber, toBlockNumber: target.blockNumber, toBlockHashHex: target.blockHash, text: cleanText, }); return this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TEXT, msgSubType: MSG_SUBTYPE_TEXT_RATING, msgVersion: 1, bodyBytes, }); }); } async addBlockStatusAction({ login, message, text = '', statusSubType, storagePwd }) { const cleanLogin = String(login || '').trim(); const cleanText = String(text || '').trim(); const cleanSubType = Number(statusSubType); const target = normalizeMessageRefTarget(message, 'status action'); const allowedStatusSubTypes = new Set([ MSG_SUBTYPE_STATUS_DONE_ONCE, MSG_SUBTYPE_STATUS_LEARNED, MSG_SUBTYPE_STATUS_SERVICE_PASSED, MSG_SUBTYPE_STATUS_CONFIRMED, MSG_SUBTYPE_STATUS_INTERESTED, MSG_SUBTYPE_STATUS_STARTED, MSG_SUBTYPE_STATUS_IN_STUDY, MSG_SUBTYPE_STATUS_ABANDONED, MSG_SUBTYPE_STATUS_COMPLETED, ]); if (!allowedStatusSubTypes.has(cleanSubType)) { throw new Error('Unsupported status action subtype'); } const key = `status-action:${cleanLogin}:${cleanSubType}:${target.blockchainName}:${target.blockNumber}:${target.blockHash}:${cleanText}`; return this.runWriteLocked(key, async () => { const bodyBytes = makeStatusActionBodyBytes({ toLogin: await this.requireRegisteredDisplayLogin(target.login), toForkNumber: target.forkNumber, toBlockNumber: target.blockNumber, toBlockHashHex: target.blockHash, text: cleanText, }); return this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_STATUS_ACTION, msgSubType: cleanSubType, msgVersion: 1, bodyBytes, }); }); } async addBlockRepost({ login, channel, message, text, storagePwd }) { const cleanLogin = String(login || '').trim(); if (!cleanLogin) throw new Error('Missing login'); const cleanText = String(text || '').trim(); if (!cleanText) throw new Error('Комментарий к репосту обязателен'); const target = normalizeMessageRefTarget(message, 'repost'); const selector = channel || {}; const owner = String(selector?.ownerBlockchainName || '').trim(); const root = Number(selector?.channelRootBlockNumber); const key = `channel-line:${cleanLogin}:${owner}:${root}`; return this.runLineWriteLocked(key, async () => { const user = await this.ensureChainInitializedForLineOps(cleanLogin, storagePwd); const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim(); const tail = await this.resolveChannelLineTail({ login: cleanLogin, blockchainName, channel: selector }); const bodyBytes = makeTextRepostBodyBytes({ lineCode: tail.lineCode, prevLineNumber: tail.prevLineNumber, prevLineHashHex: tail.prevLineHashHex, thisLineNumber: tail.thisLineNumber, toLogin: await this.requireRegisteredDisplayLogin(target.login), toForkNumber: target.forkNumber, toBlockNumber: target.blockNumber, toBlockHashHex: target.blockHash, text: cleanText, }); const payload = await this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TEXT, msgSubType: MSG_SUBTYPE_TEXT_REPOST, msgVersion: 1, bodyBytes, channelSlug: tail.channelSlug, }); return payload; }); } async addBlockEditMessage({ login, message, text, storagePwd, isChannelPost = false, channel = null, }) { const cleanLogin = String(login || '').trim(); const cleanText = String(text || '').trim(); const target = normalizeMessageRefTarget(message, 'edit'); if (isChannelPost) { const selector = channel || {}; const ownerBlockchainName = String(selector?.ownerBlockchainName || target.blockchainName || '').trim(); const lineCode = Number(selector?.channelRootBlockNumber); if (!ownerBlockchainName || !Number.isInteger(lineCode) || lineCode < 0) { throw new Error('Invalid channel selector for edit'); } const lockKey = `channel-line:${cleanLogin}:${ownerBlockchainName}:${lineCode}`; return this.runLineWriteLocked(lockKey, async () => { const user = await this.ensureChainInitializedForLineOps(cleanLogin, storagePwd); const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim(); const tail = await this.resolveChannelLineTail({ login: cleanLogin, blockchainName, channel: { ...selector, ownerBlockchainName, channelRootBlockNumber: lineCode, }, }); const bodyBytes = makeTextEditPostBodyBytes({ lineCode: tail.lineCode, prevLineNumber: tail.prevLineNumber, prevLineHashHex: tail.prevLineHashHex, thisLineNumber: tail.thisLineNumber, toBlockNumber: target.blockNumber, toBlockHashHex: target.blockHash, text: cleanText, }); const payload = await this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TEXT, msgSubType: MSG_SUBTYPE_TEXT_EDIT_POST, msgVersion: 1, bodyBytes, channelSlug: tail.channelSlug, }); return payload; }); } const lockKey = `edit-reply:${cleanLogin}:${target.blockchainName}:${target.blockNumber}:${target.blockHash}`; return this.runWriteLocked(lockKey, async () => { const bodyBytes = makeTextEditReplyBodyBytes({ toBlockNumber: target.blockNumber, toBlockHashHex: target.blockHash, text: cleanText, }); return this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TEXT, msgSubType: MSG_SUBTYPE_TEXT_EDIT_REPLY, msgVersion: 1, bodyBytes, }); }); } async addBlockFollowUser({ login, targetLogin, storagePwd, unfollow = false }) { const cleanTargetLogin = String(targetLogin || '').trim().replace(/^@+/, ''); if (!cleanTargetLogin) throw new Error('Target login is required'); const cleanLogin = String(login || '').trim(); const key = `${unfollow ? 'unfollow-user' : 'follow-user'}:${cleanLogin}:${cleanTargetLogin.toLowerCase()}`; return this.runWriteLocked(key, async () => { const targetUser = await this.getUser(cleanTargetLogin); if (!targetUser?.exists) throw new Error('Target user not found'); const targetHeaderHash = await this.resolveHeaderHashForBlockchain(targetUser.blockchainName); return this.addBlockFollowChannel({ login: cleanLogin, storagePwd, targetLogin: cleanTargetLogin, targetBlockchainName: targetUser.blockchainName, targetBlockNumber: 0, targetBlockHashHex: targetHeaderHash, unfollow, }); }); } async addBlockFollowChannel({ login, storagePwd, targetLogin = '', targetBlockchainName, targetBlockNumber, targetBlockHashHex, unfollow = false, }) { const cleanLogin = String(login || '').trim(); const cleanTargetBch = String(targetBlockchainName || '').trim(); const cleanTargetLogin = String(targetLogin || loginFromBlockchainNameValue(cleanTargetBch)).trim(); const cleanTargetBlockNumber = Number(targetBlockNumber); if (!cleanTargetBch) throw new Error('Target blockchain is required'); if (!cleanTargetLogin) throw new Error('Target login is required'); if (!Number.isFinite(cleanTargetBlockNumber) || cleanTargetBlockNumber < 0) { throw new Error('Invalid target block number'); } const seedHash = normalizeHex32(targetBlockHashHex, ZERO64); let targetHashHex = seedHash; if (targetHashHex === ZERO64) { targetHashHex = cleanTargetBlockNumber === 0 ? await this.resolveHeaderHashForBlockchain(cleanTargetBch) : await this.getBlockHashByNumber(cleanTargetBch, cleanTargetBlockNumber); } return this.addBlockConnectionTarget({ login: cleanLogin, storagePwd, subType: unfollow ? MSG_SUBTYPE_CONNECTION_UNFOLLOW : MSG_SUBTYPE_CONNECTION_FOLLOW, toLogin: cleanTargetLogin, toForkNumber: forkNumberFromBlockchainNameValue(cleanTargetBch), toBlockNumber: cleanTargetBlockNumber, toBlockHashHex: targetHashHex, }); } async getBlockHashByNumber(blockchainName, blockNumber) { const cleanBlockchainName = String(blockchainName || '').trim(); const cleanBlockNumber = Number(blockNumber); if (!cleanBlockchainName || !Number.isInteger(cleanBlockNumber) || cleanBlockNumber < 0) { throw new Error('Некорректный blockchainName/blockNumber для GetBlockchainBlock'); } const response = await this.ws.request('GetBlockchainBlock', { blockchainName: cleanBlockchainName, blockNumber: cleanBlockNumber, }); if (response.status === 200) { return normalizeHex32(response?.payload?.blockHash, ZERO64); } if (cleanBlockNumber === 0 && Number(response?.status) === 404) return ZERO64; throw opError('GetBlockchainBlock', response); } async resolveHeaderHashForBlockchain(blockchainName) { const cleanBch = String(blockchainName || '').trim(); if (!cleanBch) throw new Error('Missing blockchainName'); if (this.headerHashCache.has(cleanBch)) { const cached = normalizeHex32(this.headerHashCache.get(cleanBch), ZERO64); if (cached !== ZERO64) return cached; this.headerHashCache.delete(cleanBch); } const headerHash = await this.getBlockHashByNumber(cleanBch, 0); if (headerHash !== ZERO64) { this.headerHashCache.set(cleanBch, headerHash); } else { this.headerHashCache.delete(cleanBch); } return headerHash; } async getMyBlockchainPage({ beforeBlock = null, limit = 100, includeBlockBytes = true } = {}) { const response = await this.ws.request('GetMyBlockchain', { beforeBlock, limit, includeBlockBytes }); if (response.status !== 200) throw opError('GetMyBlockchain', response); return response.payload || {}; } async resolveFixedLineTail({ blockchainName, kind }) { const code = { profile: SHINE_CORE_LINE_PROFILE, connections: SHINE_CORE_LINE_CONNECTION, tech: SHINE_CORE_LINE_TECH }[String(kind || '').toLowerCase()]; if (code === undefined) throw new Error(`Unknown fixed line kind: ${kind}`); const heads = await this.getCoreHeads(code); if (heads.blockchainName.toLowerCase() !== blockchainName.toLowerCase() || !heads.line) throw new Error('CORE fixed line not found'); // Sequence is only an in-memory compatibility argument to existing body factories. // It is not part of signed CORE or CONTENT and is never sent. return { prevLineNumber: heads.line.blockNumber, prevLineHashHex: heads.line.coreHash, thisLineNumber: 1 }; } async runLineWriteLocked(lockKey, buildAndSend) { const key = String(lockKey || '').trim() || 'line-write'; const previous = this.lineWriteQueues.get(key) || Promise.resolve(); const task = previous.catch(() => {}).then(async () => { try { return await buildAndSend(); } catch (error) { if (!isLineConflictError(error)) throw error; return buildAndSend(); } }); this.lineWriteQueues.set(key, task); return task.finally(() => { if (this.lineWriteQueues.get(key) === task) this.lineWriteQueues.delete(key); }); } async listOwnChannelsForBlockchain(login, blockchainName) { const feed = await this.listSubscriptionsFeed(login, 500); const own = feed?.ownedChannels || []; const ownerLogin = String(login || '').trim().toLowerCase(); return own .filter((item) => String(item?.channel?.ownerLogin || '').trim().toLowerCase() === ownerLogin) .map((item) => ({ ownerBlockchainName: String(item?.channel?.ownerBlockchainName || '').trim(), rootBlockNumber: Number(item?.channel?.channelRoot?.blockNumber), rootBlockHash: normalizeHex32(item?.channel?.channelRoot?.blockHash, ZERO64), channelName: String(item?.channel?.channelName || ''), channelTypeCode: Number(item?.channel?.channelTypeCode ?? CHANNEL_TYPE_PUBLIC), })) .filter((item) => Number.isFinite(item.rootBlockNumber) && item.rootBlockNumber >= 0); } async addBlockCreateChannel({ login, channelName, channelDescription = '', channelProfileTitle = '', channelAvatar = null, channelType = CHANNEL_TYPE_PUBLIC, channelTypeVersion = CHANNEL_TYPE_VERSION_DEFAULT, storagePwd, }) { const cleanLogin = (login || '').trim(); if (!cleanLogin) throw new Error('Missing login'); const typeCode = Number(channelType); const nameCheck = typeCode === CHANNEL_TYPE_PERSONAL ? validatePersonalChannelName(channelName) : validateChannelDisplayName(channelName); if (!nameCheck.ok) { throw new Error(typeCode === CHANNEL_TYPE_PERSONAL ? nameCheck.error : channelNameErrorText(nameCheck.code)); } const cleanChannelName = normalizeChannelDisplayName(nameCheck.normalized); const cleanChannelDescription = typeCode === CHANNEL_TYPE_PUBLIC ? composeChannelMetaText({ title: channelProfileTitle, description: channelDescription, avatar: channelAvatar, }) : normalizeChannelDescription(channelDescription); const channelSlug = toCanonicalChannelSlug(cleanChannelName); const typeVersion = Number(channelTypeVersion); const key = `tech-line:${cleanLogin}`; return this.runLineWriteLocked(key, async () => { const user = await this.ensureChainInitializedForLineOps(cleanLogin, storagePwd); const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim(); const tail = await this.resolveFixedLineTail({ blockchainName, kind: 'tech' }); const payload = await this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TECH, msgSubType: MSG_SUBTYPE_TECH_CREATE_CHANNEL, msgVersion: CREATE_CHANNEL_BODY_VERSION, channelSlug, bodyBytes: makeCreateChannelBodyBytes({ lineCode: 0, prevLineNumber: tail.prevLineNumber, prevLineHashHex: tail.prevLineHashHex, thisLineNumber: tail.thisLineNumber, channelName: cleanChannelName, channelDescription: cleanChannelDescription, channelType: typeCode, channelTypeVersion: typeVersion, }), }); const selector = { ownerBlockchainName: blockchainName, channelRootBlockNumber: Number(payload?.submittedBlockNumber ?? payload?.serverLastGlobalNumber), channelRootBlockHash: normalizeHex32(payload?.submittedBlockHash ?? payload?.serverLastGlobalHash, ZERO64), }; return { ...payload, channel: { ...selector, }, }; }); } async resolveChannelLineTail({ login, blockchainName, channel }) { const selector = channel || {}; const ownerBlockchainName = String(selector?.ownerBlockchainName || '').trim(); const lineCode = Number(selector?.channelRootBlockNumber); if (!ownerBlockchainName || !Number.isInteger(lineCode) || lineCode < 0) { throw new Error('Invalid channel selector'); } const currentOwnerLogin = loginFromBlockchainNameValue(blockchainName).toLowerCase(); const selectorOwnerLogin = loginFromBlockchainNameValue(ownerBlockchainName).toLowerCase(); if (!currentOwnerLogin || !selectorOwnerLogin || currentOwnerLogin !== selectorOwnerLogin) { throw new Error('Posting is allowed only to your own channels'); } let channelSlug = toCanonicalChannelSlug(String(selector?.channelName || selector?.slug || '')); let rootHashHex = normalizeHex32(selector?.channelRootBlockHash, ZERO64); if (rootHashHex === ZERO64 || !channelSlug) { const ownChannels = await this.listOwnChannelsForBlockchain(login, blockchainName); const rootChannel = ownChannels.find((item) => item.rootBlockNumber === lineCode); if (!rootChannel) throw new Error('Channel root not found'); if (rootHashHex === ZERO64) rootHashHex = normalizeHex32(rootChannel.rootBlockHash, ZERO64); if (!channelSlug) channelSlug = toCanonicalChannelSlug(rootChannel.channelName); } if (!channelSlug) throw new Error('Cannot resolve canonical channel slug'); if (rootHashHex === ZERO64) throw new Error('Cannot resolve channel root hash'); const heads = await this.getCoreHeads(lineCode); if (heads.blockchainName.toLowerCase() !== blockchainName.toLowerCase() || !heads.line) throw new Error('CORE channel line not found'); const canonicalRootHash = await this.getBlockHashByNumber(blockchainName, lineCode); if (canonicalRootHash !== rootHashHex) throw new Error('Channel root changed; refresh channel'); return { ownerBlockchainName, lineCode, rootHashHex, channelSlug, prevLineNumber: heads.line.blockNumber, prevLineHashHex: heads.line.coreHash, thisLineNumber: 0 }; } async addBlockTextPost({ login, channel, text, storagePwd, msgSubType = MSG_SUBTYPE_TEXT_POST }) { const cleanLogin = (login || '').trim(); if (!cleanLogin) throw new Error('Missing login'); const cleanText = String(text || '').trim(); const cleanSubType = Number(msgSubType || MSG_SUBTYPE_TEXT_POST); const allowedTextSubTypes = new Set([ MSG_SUBTYPE_TEXT_POST, MSG_SUBTYPE_TEXT_ENTRYPOINT, MSG_SUBTYPE_TEXT_EXERCISE, MSG_SUBTYPE_TEXT_SERVICE, MSG_SUBTYPE_TEXT_COURSE, ]); if (!allowedTextSubTypes.has(cleanSubType)) { throw new Error('Unsupported channel text subtype'); } const selector = channel || {}; const owner = String(selector?.ownerBlockchainName || '').trim(); const root = Number(selector?.channelRootBlockNumber); const key = `channel-line:${cleanLogin}:${owner}:${root}`; return this.runLineWriteLocked(key, async () => { const user = await this.ensureChainInitializedForLineOps(cleanLogin, storagePwd); const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim(); const tail = await this.resolveChannelLineTail({ login: cleanLogin, blockchainName, channel: selector }); const bodyBytes = makeTextPostBodyBytes({ lineCode: tail.lineCode, prevLineNumber: tail.prevLineNumber, prevLineHashHex: tail.prevLineHashHex, thisLineNumber: tail.thisLineNumber, text: cleanText, }); const payload = await this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TEXT, msgSubType: cleanSubType, msgVersion: 1, bodyBytes, channelSlug: tail.channelSlug, }); return { ...payload, channel: { ownerBlockchainName: tail.ownerBlockchainName, channelRootBlockNumber: tail.lineCode, channelRootBlockHash: tail.rootHashHex, }, }; }); } async addBlockChannelMeta({ login, channel, title = '', description = '', avatar = null, storagePwd }) { const cleanLogin = (login || '').trim(); if (!cleanLogin) throw new Error('Missing login'); const selector = channel || {}; const owner = String(selector?.ownerBlockchainName || '').trim(); const root = Number(selector?.channelRootBlockNumber); const metaText = composeChannelMetaText({ title, description, avatar }); const key = `channel-line:${cleanLogin}:${owner}:${root}`; return this.runLineWriteLocked(key, async () => { const user = await this.ensureChainInitializedForLineOps(cleanLogin, storagePwd); const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim(); const tail = await this.resolveChannelLineTail({ login: cleanLogin, blockchainName, channel: selector }); const bodyBytes = makeTextLineBodyBytesAllowEmpty({ lineCode: tail.lineCode, prevLineNumber: tail.prevLineNumber, prevLineHashHex: tail.prevLineHashHex, thisLineNumber: tail.thisLineNumber, text: metaText, }); const payload = await this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TEXT, msgSubType: MSG_SUBTYPE_TEXT_CHANNEL_META, msgVersion: 1, bodyBytes, channelSlug: tail.channelSlug, }); return { ...payload, channel: { ownerBlockchainName: tail.ownerBlockchainName, channelRootBlockNumber: tail.lineCode, channelRootBlockHash: tail.rootHashHex, }, }; }); } async addBlockChannelDelete({ login, channel, storagePwd }) { const cleanLogin = (login || '').trim(); if (!cleanLogin) throw new Error('Missing login'); const selector = channel || {}; const owner = String(selector?.ownerBlockchainName || '').trim(); const root = Number(selector?.channelRootBlockNumber); const key = `channel-line:${cleanLogin}:${owner}:${root}`; return this.runLineWriteLocked(key, async () => { const user = await this.ensureChainInitializedForLineOps(cleanLogin, storagePwd); const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim(); const tail = await this.resolveChannelLineTail({ login: cleanLogin, blockchainName, channel: selector }); const bodyBytes = makeTextLineBodyBytesAllowEmpty({ lineCode: tail.lineCode, prevLineNumber: tail.prevLineNumber, prevLineHashHex: tail.prevLineHashHex, thisLineNumber: tail.thisLineNumber, text: '', }); const payload = await this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_TEXT, msgSubType: MSG_SUBTYPE_TEXT_CHANNEL_DELETE, msgVersion: 1, bodyBytes, channelSlug: tail.channelSlug, }); return { ...payload, channel: { ownerBlockchainName: tail.ownerBlockchainName, channelRootBlockNumber: tail.lineCode, channelRootBlockHash: tail.rootHashHex } }; }); } onEvent(op, handler) { if (!op || typeof handler !== 'function') return () => {}; if (!this.eventListeners.has(op)) { this.eventListeners.set(op, new Set()); const unsubscribe = this.ws.onEvent(op, (data) => { const handlers = this.eventListeners.get(op); if (!handlers) return; handlers.forEach((callback) => { try { callback(data); } catch {} }); }); this.wsEventUnsubscribers.set(op, unsubscribe); } const handlers = this.eventListeners.get(op); handlers.add(handler); return () => { handlers.delete(handler); if (handlers.size) return; this.eventListeners.delete(op); const unsubscribe = this.wsEventUnsubscribers.get(op); try { unsubscribe?.(); } catch {} this.wsEventUnsubscribers.delete(op); }; } async upsertPushToken({ endpoint, p256dhKey, authKey, sessionId, platform = 'web', userAgent = navigator.userAgent || '' }) { const response = await this.ws.request('UpsertPushToken', { endpoint, p256dhKey, authKey, sessionId, platform, userAgent }); if (response.status !== 200) throw opError('UpsertPushToken', response); return response.payload || {}; } async sendTestWebPush({ login = '', sessionId = '', title = '', text = '' } = {}) { const payload = {}; if (String(login || '').trim()) payload.login = String(login || '').trim(); if (String(sessionId || '').trim()) payload.sessionId = String(sessionId || '').trim(); if (String(title || '').trim()) payload.title = String(title || '').trim(); if (String(text || '').trim()) payload.text = String(text || '').trim(); const response = await this.ws.request('SendTestWebPush', payload); if (response.status !== 200) throw opError('SendTestWebPush', response); return response.payload || {}; } normalizeDmLogin(login) { return String(login || '').trim().toLowerCase(); } async buildSignedDmBlock({ signerLogin, fromLogin, toLogin, storagePwd, timeMs, nonce, messageType, revisionTimeMs = 0, reencryptedAtMs = 0, bodyBytes = new Uint8Array(0), signerPrivatePkcs8B64 = '', }) { const cleanSignerLogin = this.normalizeDmLogin(signerLogin); const cleanFromLogin = this.normalizeDmLogin(fromLogin); const cleanToLogin = this.normalizeDmLogin(toLogin); if (!cleanSignerLogin || !cleanFromLogin || !cleanToLogin) throw new Error('Не передан signerLogin/fromLogin/toLogin'); if (!storagePwd && !String(signerPrivatePkcs8B64 || '').trim()) throw new Error('Не передан ключ для подписи'); if (!(bodyBytes instanceof Uint8Array) || bodyBytes.length > DM_MAX_ENCRYPTED_BODY_BYTES) { throw new Error(`body должен быть 0..${DM_MAX_ENCRYPTED_BODY_BYTES} байт`); } let clientPriv = String(signerPrivatePkcs8B64 || '').trim(); if (!clientPriv) { const secrets = await loadEncryptedUserSecrets(cleanSignerLogin, storagePwd); clientPriv = String(secrets?.clientKey || '').trim(); } if (!clientPriv) throw new Error('Не найден приватный clientKey'); const privateKey = await importPkcs8Ed25519(clientPriv); const toBytes = ensureAsciiBytes(cleanToLogin, 'toLogin'); const fromBytes = ensureAsciiBytes(cleanFromLogin, 'fromLogin'); const preimage = concatBytes( DM_PREFIX_V1, uint8Bytes(DM_FORMAT_VERSION_MAJOR), uint8Bytes(DM_FORMAT_VERSION_MINOR), uint8Bytes(toBytes.length), toBytes, uint8Bytes(fromBytes.length), fromBytes, uint64Bytes(timeMs), uint32Bytes(nonce), uint8Bytes(messageType), uint64Bytes(revisionTimeMs), uint64Bytes(reencryptedAtMs), uint32Bytes(bodyBytes.length), bodyBytes, ); const signature = await signBytes(privateKey, preimage); return concatBytes(preimage, signature); } parseSignedMessageBlob(blobB64) { const bytes = base64ToBytes(String(blobB64 || '').trim()); return parseSignedMessageBlockBytes(bytes); } parseReadReceiptPayload(payloadBytes) { return parseReadReceiptBodyBytes(payloadBytes); } classifyDmDecryptFailure(error) { return classifyDmDecryptFailure(error); } async decryptSignedMessageContent({ parsed, blobB64 = '', login, storagePwd, clientPrivatePkcs8B64 = '' }) { const parsedBlock = parsed || this.parseSignedMessageBlob(blobB64); const messageType = Number(parsedBlock?.messageType || 0); if (messageType !== DM_TYPE_INCOMING && messageType !== DM_TYPE_OUTGOING_COPY) { return { text: '', plainBytes: new Uint8Array(0) }; } if (!storagePwd && !String(clientPrivatePkcs8B64 || '').trim()) throw new Error('Не передан ключ для расшифровки'); const cleanLogin = String(login || '').trim(); if (!cleanLogin) throw new Error('Не передан login для расшифровки'); const payload = parsedBlock.encryptedBodyPayload || parseEncryptedDmBodyBytes(parsedBlock.payloadBytes); if (payload.cryptoMethod !== DM_CRYPTO_METHOD_X25519_HKDF_AES_GCM || payload.cryptoVersion !== DM_CRYPTO_VERSION_1_0) { throw new Error('Неподдерживаемый метод шифрования DM'); } let clientPrivPkcs8 = String(clientPrivatePkcs8B64 || '').trim(); if (!clientPrivPkcs8) { const secrets = await loadEncryptedUserSecrets(cleanLogin, storagePwd); clientPrivPkcs8 = String(secrets?.clientKey || '').trim(); } if (!clientPrivPkcs8) throw new Error('Не найден приватный clientKey'); const recipientSeed = extractEd25519SeedFromPkcs8B64(clientPrivPkcs8); const recipientX25519Priv = ed25519SeedToX25519Private(recipientSeed); const recipientEd25519PubB64 = await publicKeyB64FromPkcs8Ed25519(clientPrivPkcs8); const recipientX25519Pub = ed25519PublicToX25519Public(base64ToBytes(recipientEd25519PubB64)); const sharedSecret = x25519SharedSecret(recipientX25519Priv, payload.ephemeralPubKey); const salt = concatBytes(payload.ephemeralPubKey, recipientX25519Pub); const aesKeyBytes = await hkdfSha256(sharedSecret, salt, DM_HKDF_INFO, 32); const plainBytes = await decryptBytesAesGcm(payload.cipherText, aesKeyBytes, payload.iv); return { text: new TextDecoder().decode(plainBytes), plainBytes, }; } async sendMessagePair({ incomingBlobB64, outgoingBlobB64 }) { const body = { incomingBlobB64, outgoingBlobB64 }; const response = await this.ws.request('SendMessagePair', body); if (response.status !== 200) throw opError('SendMessagePair', response); return response.payload || {}; } async sendDirectMessageRevision({ login, toLogin, text = '', storagePwd, timeMs, nonce, revisionTimeMs = 0, reencryptedAtMs = 0, }) { const cleanFromLogin = this.normalizeDmLogin(login); const cleanToLogin = this.normalizeDmLogin(toLogin); const cleanText = String(text || ''); if (!cleanFromLogin || !cleanToLogin) throw new Error('Не передан login/toLogin'); const normalizedTimeMs = Number(timeMs); const normalizedNonce = Number(nonce); const normalizedRevisionTimeMs = Number(revisionTimeMs || 0); const normalizedReencryptedAtMs = Number(reencryptedAtMs || 0); if (!Number.isFinite(normalizedTimeMs) || normalizedTimeMs <= 0) throw new Error('Некорректный timeMs'); if (!Number.isFinite(normalizedNonce) || normalizedNonce < 0) throw new Error('Некорректный nonce'); if (!Number.isFinite(normalizedRevisionTimeMs) || normalizedRevisionTimeMs < 0) throw new Error('Некорректный revisionTimeMs'); if (!Number.isFinite(normalizedReencryptedAtMs) || normalizedReencryptedAtMs < 0) throw new Error('Некорректный reencryptedAtMs'); const plainBytes = utf8Bytes(cleanText); const targetUser = await this.getUser(cleanToLogin); if (!targetUser?.exists || !String(targetUser?.clientKey || '').trim()) { throw new Error('Не найден clientKey получателя'); } const secrets = await loadEncryptedUserSecrets(cleanFromLogin, storagePwd); const senderPrivPkcs8 = String(secrets?.clientKey || '').trim(); if (!senderPrivPkcs8) throw new Error('Не найден приватный clientKey отправителя'); const senderPublicKeyB64 = await publicKeyB64FromPkcs8Ed25519(senderPrivPkcs8); const incomingEncryptedBodyBytes = await buildDmEncryptedBodyBytes({ plainBytes, recipientClientKeyB64: String(targetUser.clientKey || '').trim(), }); const outgoingEncryptedBodyBytes = await buildDmEncryptedBodyBytes({ plainBytes, recipientClientKeyB64: senderPublicKeyB64, }); const incomingBlock = await this.buildSignedDmBlock({ signerLogin: cleanFromLogin, fromLogin: cleanFromLogin, toLogin: cleanToLogin, storagePwd, timeMs: normalizedTimeMs, nonce: normalizedNonce, messageType: DM_TYPE_INCOMING, revisionTimeMs: normalizedRevisionTimeMs, reencryptedAtMs: normalizedReencryptedAtMs, bodyBytes: incomingEncryptedBodyBytes, }); const outgoingBlock = await this.buildSignedDmBlock({ signerLogin: cleanFromLogin, fromLogin: cleanFromLogin, toLogin: cleanToLogin, storagePwd, timeMs: normalizedTimeMs, nonce: normalizedNonce, messageType: DM_TYPE_OUTGOING_COPY, revisionTimeMs: normalizedRevisionTimeMs, reencryptedAtMs: normalizedReencryptedAtMs, bodyBytes: outgoingEncryptedBodyBytes, }); const payload = await this.sendMessagePair({ incomingBlobB64: bytesToBase64(incomingBlock), outgoingBlobB64: bytesToBase64(outgoingBlock), }); return { ...payload, localIncomingBlobB64: bytesToBase64(incomingBlock), localOutgoingBlobB64: bytesToBase64(outgoingBlock), localBaseKey: dmBaseKey({ toLogin: cleanToLogin, fromLogin: cleanFromLogin, timeMs: normalizedTimeMs, nonce: normalizedNonce }), }; } async sendDirectMessage({ login, toLogin, text, storagePwd }) { const cleanText = String(text || ''); if (!cleanText) throw new Error('Пустое сообщение'); return this.sendDirectMessageRevision({ login, toLogin, text: cleanText, storagePwd, timeMs: Date.now(), nonce: Math.floor(Math.random() * 0x100000000), revisionTimeMs: 0, }); } async deleteDirectMessage({ login, toLogin, storagePwd, timeMs, nonce, revisionTimeMs, deleteByRecipient = false }) { const cleanLogin = this.normalizeDmLogin(login); const cleanPeerLogin = this.normalizeDmLogin(toLogin); if (!cleanLogin || !cleanPeerLogin) throw new Error('Не передан login/toLogin'); const normalizedTimeMs = Number(timeMs); const normalizedNonce = Number(nonce); const normalizedRevisionTimeMs = Number(revisionTimeMs || 0); if (!Number.isFinite(normalizedTimeMs) || normalizedTimeMs <= 0) throw new Error('Некорректный timeMs'); if (!Number.isFinite(normalizedNonce) || normalizedNonce < 0) throw new Error('Некорректный nonce'); if (!Number.isFinite(normalizedRevisionTimeMs) || normalizedRevisionTimeMs <= 0) throw new Error('Некорректный revisionTimeMs'); const block = await this.buildSignedDmBlock({ signerLogin: cleanLogin, fromLogin: deleteByRecipient ? cleanPeerLogin : cleanLogin, toLogin: deleteByRecipient ? cleanLogin : cleanPeerLogin, storagePwd, timeMs: normalizedTimeMs, nonce: normalizedNonce, messageType: deleteByRecipient ? DM_TYPE_MESSAGE_DELETED_BY_RECIPIENT : DM_TYPE_MESSAGE_DELETED_BY_SENDER, revisionTimeMs: normalizedRevisionTimeMs, reencryptedAtMs: 0, bodyBytes: new Uint8Array(0), }); const blobB64 = bytesToBase64(block); const response = await this.ws.request('DeleteMessage', { blobB64 }); if (response.status !== 200) throw opError('DeleteMessage', response); return { ...(response.payload || {}), localBlobB64: blobB64, }; } async sendReadReceipt({ login, toLogin, storagePwd, refToLogin, refFromLogin, refTimeMs, refNonce }) { const cleanLogin = this.normalizeDmLogin(login); const cleanToLogin = this.normalizeDmLogin(toLogin); const cleanRefToLogin = this.normalizeDmLogin(refToLogin); const cleanRefFromLogin = this.normalizeDmLogin(refFromLogin); const timeMs = Date.now(); const nonce = Math.floor(Math.random() * 0x100000000); const payload = buildReadReceiptPayloadBytes({ refToLogin: cleanRefToLogin, refFromLogin: cleanRefFromLogin, refTimeMs, refNonce, }); const type3 = await this.buildSignedDmBlock({ signerLogin: cleanLogin, fromLogin: cleanLogin, toLogin: cleanToLogin, storagePwd, timeMs, nonce, messageType: DM_TYPE_READ_INCOMING, bodyBytes: payload, }); const type4 = await this.buildSignedDmBlock({ signerLogin: cleanLogin, fromLogin: cleanLogin, toLogin: cleanToLogin, storagePwd, timeMs, nonce, messageType: DM_TYPE_READ_OUTGOING_COPY, bodyBytes: payload, }); return this.sendMessagePair({ incomingBlobB64: bytesToBase64(type3), outgoingBlobB64: bytesToBase64(type4), }); } async deleteConversation({ login, toLogin, storagePwd, deleteByRecipient = false, timeMs = Date.now(), nonce = Math.floor(Math.random() * 0x100000000) }) { const cleanLogin = this.normalizeDmLogin(login); const cleanPeerLogin = this.normalizeDmLogin(toLogin); if (!cleanLogin || !cleanPeerLogin) throw new Error('Не передан login/toLogin'); const normalizedTimeMs = Number(timeMs); const normalizedNonce = Number(nonce); if (!Number.isFinite(normalizedTimeMs) || normalizedTimeMs <= 0) throw new Error('Некорректный timeMs'); if (!Number.isFinite(normalizedNonce) || normalizedNonce < 0) throw new Error('Некорректный nonce'); const block = await this.buildSignedDmBlock({ signerLogin: cleanLogin, fromLogin: deleteByRecipient ? cleanPeerLogin : cleanLogin, toLogin: deleteByRecipient ? cleanLogin : cleanPeerLogin, storagePwd, timeMs: normalizedTimeMs, nonce: normalizedNonce, messageType: deleteByRecipient ? DM_TYPE_CONVERSATION_DELETED_BY_RECIPIENT : DM_TYPE_CONVERSATION_DELETED_BY_SENDER, revisionTimeMs: 0, reencryptedAtMs: 0, bodyBytes: new Uint8Array(0), }); const blobB64 = bytesToBase64(block); const response = await this.ws.request('DeleteConversation', { blobB64 }); if (response.status !== 200) throw opError('DeleteConversation', response); return { ...(response.payload || {}), localBlobB64: blobB64, }; } async getDirectMessages({ peerLogin, limit = 50, beforeTimeMs = 0, beforeMessageKey = '', reencryptedBeforeMs = 0 } = {}) { const payload = { peerLogin: String(peerLogin || '').trim(), limit: Number(limit || 0), }; if (!payload.peerLogin) throw new Error('Не передан peerLogin'); if (Number.isFinite(Number(beforeTimeMs)) && Number(beforeTimeMs) > 0) { payload.beforeTimeMs = Math.trunc(Number(beforeTimeMs)); } if (String(beforeMessageKey || '').trim()) { payload.beforeMessageKey = String(beforeMessageKey || '').trim(); } if (Number.isFinite(Number(reencryptedBeforeMs)) && Number(reencryptedBeforeMs) > 0) { payload.reencryptedBeforeMs = Math.trunc(Number(reencryptedBeforeMs)); } const response = await this.ws.request('GetDirectMessages', payload); if (response.status !== 200) throw opError('GetDirectMessages', response); return response.payload || {}; } async reencryptDirectMessage({ blobB64 }) { const response = await this.ws.request('ReencryptDirectMessage', { blobB64: String(blobB64 || '').trim() }); if (response.status !== 200) throw opError('ReencryptDirectMessage', response); return response.payload || {}; } async buildReencryptedLocalDm({ blobB64, login, oldClientPrivatePkcs8B64, newClientPrivatePkcs8B64, reencryptedAtMs }) { const parsed = this.parseSignedMessageBlob(blobB64); const messageType = Number(parsed?.messageType || 0); if (messageType !== DM_TYPE_INCOMING && messageType !== DM_TYPE_OUTGOING_COPY) throw new Error('Можно перешифровать только type=1/2'); const cleanLogin = this.normalizeDmLogin(login); const owned = messageType === DM_TYPE_INCOMING ? this.normalizeDmLogin(parsed.toLogin) === cleanLogin : this.normalizeDmLogin(parsed.fromLogin) === cleanLogin; if (!owned) throw new Error('Сообщение не принадлежит текущему локальному архиву'); let decrypted; try { decrypted = await this.decryptSignedMessageContent({ parsed, login: cleanLogin, clientPrivatePkcs8B64: oldClientPrivatePkcs8B64, }); } catch (oldKeyError) { // Сообщение могло прийти уже после смены PDA и быть зашифровано новым client key. // В этом случае нормализуем его в текущую эпоху миграции тем же новым ключом. try { decrypted = await this.decryptSignedMessageContent({ parsed, login: cleanLogin, clientPrivatePkcs8B64: newClientPrivatePkcs8B64, }); } catch (newKeyError) { const error = new Error('Не удалось расшифровать сообщение ни старым, ни новым client key'); error.cause = newKeyError; throw error; } } const newPublicKeyB64 = await publicKeyB64FromPkcs8Ed25519(newClientPrivatePkcs8B64); const encryptedBody = await buildDmEncryptedBodyBytes({ plainBytes: decrypted.plainBytes, recipientClientKeyB64: newPublicKeyB64, }); const block = await this.buildSignedDmBlock({ signerLogin: cleanLogin, fromLogin: parsed.fromLogin, toLogin: parsed.toLogin, timeMs: parsed.timeMs, nonce: parsed.nonce, messageType, revisionTimeMs: parsed.revisionTimeMs, reencryptedAtMs, bodyBytes: encryptedBody, signerPrivatePkcs8B64: newClientPrivatePkcs8B64, }); return bytesToBase64(block); } async ackSessionDelivery(messageKey) { const response = await this.ws.request('AckSessionDelivery', { messageKey }); if (response.status !== 200) throw opError('AckSessionDelivery', response); return response.payload || {}; } async callInviteBroadcast({ toLogin, callId, type = 100, data = '' }) { const response = await this.ws.request('CallInviteBroadcast', { toLogin, callId, type, data }); if (response.status !== 200) throw opError('CallInviteBroadcast', response); return response.payload || {}; } async callSignalToSession({ toLogin, targetSessionId, callId, type, data = '' }) { const response = await this.ws.request('CallSignalToSession', { toLogin, targetSessionId, callId, type, data }); if (response.status !== 200) throw opError('CallSignalToSession', response); return response.payload || {}; } async getCallIceConfig() { const response = await this.ws.request('GetCallIceConfig', {}); if (response.status !== 200) throw opError('GetCallIceConfig', response); return response.payload || {}; } async sendCallDeliveryReport(payload = {}) { const response = await this.ws.request('CallDeliveryReport', payload); if (response.status !== 200) throw opError('CallDeliveryReport', response); return response.payload || {}; } async listContacts() { const response = await this.ws.request('ListContacts', {}); if (response.status !== 200) throw opError('ListContacts', response); const payload = response.payload || {}; if (Array.isArray(payload.dialogs) && !Array.isArray(payload.contacts)) { payload.contacts = extractContactsFromDialogs(payload.dialogs); } return payload; } async addCloseFriend(toLogin) { const response = await this.ws.request('AddCloseFriend', { toLogin }); if (response.status !== 200) throw opError('AddCloseFriend', response); return response.payload || {}; } async getUserCounters() { const response = await this.ws.request('GetUserCounters', {}); if (response.status !== 200) throw opError('GetUserCounters', response); return response.payload || {}; } async getNotifications(countsOnly = false) { const response = await this.ws.request('GetNotifications', countsOnly ? { countsOnly: true } : {}); if (response.status !== 200) throw opError('GetNotifications', response); return response.payload || {}; } async setNotificationSeen({ login, category, seenAtMs, storagePwd }) { const cleanLogin = this.normalizeDmLogin(login); const cleanCategory = String(category || '').trim().toLowerCase(); const categoryCode = NTF_CATEGORY[cleanCategory]; if (!cleanLogin || !categoryCode) throw new Error('Некорректный login/category уведомлений'); if (!storagePwd) throw new Error('Не передан storagePwd для подписи состояния уведомлений'); const normalizedSeenAtMs = Math.max(0, Math.trunc(Number(seenAtMs || 0))); const timeMs = Date.now(); const nonce = Math.floor(Math.random() * 0x100000000); const secrets = await loadEncryptedUserSecrets(cleanLogin, storagePwd); const clientPriv = secrets?.clientKey; if (!clientPriv) throw new Error('Не найден приватный clientKey'); const privateKey = await importPkcs8Ed25519(clientPriv); const loginBytes = ensureAsciiBytes(cleanLogin, 'login'); const preimage = concatBytes( NTF_PREFIX_V1, uint8Bytes(NTF_FORMAT_VERSION_MAJOR), uint8Bytes(NTF_FORMAT_VERSION_MINOR), uint8Bytes(loginBytes.length), loginBytes, uint64Bytes(timeMs), uint32Bytes(nonce), uint8Bytes(NTF_STATE_SEEN_WATERMARK), uint8Bytes(categoryCode), uint64Bytes(normalizedSeenAtMs), ); const signature = await signBytes(privateKey, preimage); const response = await this.ws.request('SetNotificationState', { blobB64: bytesToBase64(concatBytes(preimage, signature)) }); if (response.status !== 200) throw opError('SetNotificationState', response); return response.payload || {}; } async getUserConnectionsGraph(login) { const response = await this.ws.request('GetUserConnectionsGraph', { login }); if (response.status !== 200) throw opError('GetUserConnectionsGraph', response); return response.payload || {}; } async listUserProfileRelations(login, listType, limit = 100, offset = 0) { const response = await this.ws.request('ListUserProfileRelations', { login, listType, limit, offset }); if (response.status !== 200) throw opError('ListUserProfileRelations', response); return response.payload || {}; } async listUserProfileChannels(login, mode, limit = 100, offset = 0, includeDeleted = false) { const response = await this.ws.request('ListUserProfileChannels', { login, mode, limit, offset, includeDeleted: !!includeDeleted }); if (response.status !== 200) throw opError('ListUserProfileChannels', response); return response.payload || {}; } async searchChannels(query, { limit = 10, ownerLogin = '' } = {}) { const payload = { query, limit }; if (ownerLogin) payload.ownerLogin = ownerLogin; const response = await this.ws.request('SearchChannels', payload); if (response.status !== 200) throw opError('SearchChannels', response); return Array.isArray(response.payload?.channels) ? response.payload.channels : []; } async searchPeople(query, { limit = 10 } = {}) { const response = await this.ws.request('SearchUsers', { prefix: query, limit }); if (response.status !== 200) throw opError('SearchUsers', response); return Array.isArray(response.payload?.users) ? response.payload.users : []; } async searchUsers(prefix, options = {}) { const payload = { prefix }; if (Number.isInteger(options?.limit)) payload.limit = options.limit; if (typeof options?.isServer === 'boolean') { payload.isServer = options.isServer; } const response = await this.ws.request('SearchUsers', payload); if (response.status !== 200) throw opError('SearchUsers', response); return response.payload?.logins || []; } async getUserParam(login, param) { const cleanLogin = (login || '').trim(); const cleanParam = (param || '').trim(); if (!cleanLogin || !cleanParam) throw new Error('Не переданы login/param'); const response = await this.ws.request('GetUserParam', { login: cleanLogin, param: cleanParam }); if (response.status === 200) return response.payload || {}; if (response.status === 404 || response.status === 204) return {}; throw opError('GetUserParam', response); } async listUserParams(login) { const cleanLogin = (login || '').trim(); if (!cleanLogin) throw new Error('Не передан login'); // Profile writes are CORE events now. Do not read the Frame-era // user_params projection: it is intentionally not updated by CORE. const params = []; let afterKey = ''; for (let page = 0; page < 100; page += 1) { const response = await this.ws.request('GetCoreProfile', { login: cleanLogin, afterKey, limit: 20 }); if (response.status !== 200) throw opError('GetCoreProfile', response); const result = response.payload?.result || {}; for (const row of Array.isArray(result.values) ? result.values : []) { const param = String(row?.key || '').trim(); if (!param) continue; const timestampSeconds = Number(row?.timestampSeconds); params.push({ param, value: String(row?.value || ''), time_ms: Number.isSafeInteger(timestampSeconds) && timestampSeconds >= 0 ? timestampSeconds * 1000 : 0, }); } const nextAfterKey = String(result.nextAfterKey || '').trim(); if (!nextAfterKey) return { params }; if (nextAfterKey <= afterKey) throw new Error('Некорректная пагинация CORE-профиля'); afterKey = nextAfterKey; } throw new Error('Превышен лимит страниц CORE-профиля'); } async listUserSettings(login) { const cleanLogin = String(login || '').trim(); if (!cleanLogin) throw new Error('Не передан login'); const response = await this.ws.request('ListUserSettings', { login: cleanLogin }); if (response.status !== 200) throw opError('ListUserSettings', response); return response.payload || {}; } async setChannelReadState({ login, owner, root, rootHash, followHash, anchor, anchorHash, timeMs, storagePwd }) { const viewer = String(login || '').trim(); const validLogin = value => typeof value === 'string' && value.length > 0 && value.length <= 128 && value === value.trim() && !/[|\x00-\x1f\x7f-\x9f]/u.test(value); const validHash = value => typeof value === 'string' && /^[0-9a-f]{64}$/.test(value); if (!validLogin(viewer) || !validLogin(owner) || viewer.toLowerCase() === owner.toLowerCase() || !Number.isInteger(root) || root < 0 || !Number.isInteger(anchor) || anchor < root || anchor > 2147482623 || ![rootHash, followHash, anchorHash].every(validHash) || !Number.isSafeInteger(timeMs) || timeMs < 0 || !storagePwd) throw new Error('BAD_READ_STATE'); const secrets = await loadEncryptedUserSecrets(viewer, storagePwd); const clientPrivPkcs8 = String(secrets?.clientKey || '').trim(); if (!clientPrivPkcs8) throw new Error('Не найден приватный clientKey'); const privateKey = await importPkcs8Ed25519(clientPrivPkcs8); const preimage = ['SHiNE_CORE_READ_V1', 'test5593', viewer, owner, root, rootHash, followHash, anchor, anchorHash, timeMs].join('|'); const signatureB64 = await signBase64(privateKey, preimage); const response = await this.ws.request('SetChannelReadState', { owner, root, rootHash, followHash, anchor, anchorHash, timeMs, signatureB64 }); if (response.status !== 200) throw opError('SetChannelReadState', response); return response.payload?.result || {}; } async upsertUserSetting({ login, settingType, settingKey, timeMs, valueText = '', valueNum = 0, storagePwd, }) { const cleanLogin = String(login || '').trim(); const cleanSettingKey = String(settingKey || '').trim(); const cleanValueText = String(valueText ?? ''); const cleanTimeMs = Number(timeMs); const cleanSettingType = Number(settingType); const cleanValueNum = Number(valueNum ?? 0); if (!cleanLogin || !cleanSettingKey) throw new Error('Не переданы login/settingKey'); if (!Number.isFinite(cleanTimeMs) || cleanTimeMs <= 0) throw new Error('Не передан корректный timeMs'); if (!Number.isFinite(cleanSettingType)) throw new Error('Не передан корректный settingType'); if (!storagePwd) throw new Error('Не передан storagePwd для подписи UpsertUserSetting.'); const secrets = await loadEncryptedUserSecrets(cleanLogin, storagePwd); const clientPrivPkcs8 = String(secrets?.clientKey || '').trim(); if (!clientPrivPkcs8) throw new Error('Не найден приватный clientKey'); const privateKey = await importPkcs8Ed25519(clientPrivPkcs8); const clientKey = await publicKeyB64FromPkcs8Ed25519(clientPrivPkcs8); const preimage = 'SHiNe/UserSettings:' + [ escapeUserSettingPart(cleanLogin), String(cleanSettingType), escapeUserSettingPart(cleanSettingKey), String(Math.trunc(cleanTimeMs)), escapeUserSettingPart(cleanValueText), String(Math.trunc(cleanValueNum)), ].join('|'); const signature = await signBase64(privateKey, preimage); const response = await this.ws.request('UpsertUserSetting', { login: cleanLogin, setting_type: Math.trunc(cleanSettingType), setting_key: cleanSettingKey, time_ms: Math.trunc(cleanTimeMs), value_text: cleanValueText, value_num: Math.trunc(cleanValueNum), client_key: clientKey, signature, }); if (response.status !== 200) throw opError('UpsertUserSetting', response); return response.payload || {}; } async setUserRelation({ login, toLogin, kind, enabled, storagePwd }) { const cleanKind = String(kind || '').trim().toLowerCase(); const kinds = CONNECTION_SUBTYPES[cleanKind]; if (!kinds) throw new Error(`Неподдерживаемый тип связи: ${kind}`); const subType = enabled ? kinds.on : kinds.off; const result = await this.addBlockConnection({ login, toLogin, subType, storagePwd }); invalidateConnectionsGraphCache(); return result; } async addBlockUserParam({ login, param, value, storagePwd }) { const cleanLogin = (login || '').trim(); const cleanParam = (param || '').trim(); const cleanValue = String(value ?? '').trim(); if (!cleanLogin || !cleanParam) throw new Error('Не переданы login/param.'); if (!cleanValue) throw new Error('Значение параметра не может быть пустым.'); if (!storagePwd) throw new Error('Не передан storagePwd для подписи AddBlock.'); return this.runLineWriteLocked(`profile-line:${cleanLogin}`, async () => { const user = await this.ensureChainInitializedForLineOps(cleanLogin, storagePwd); const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim(); const tail = await this.resolveFixedLineTail({ blockchainName, kind: 'profile' }); const bodyBytes = makeUserParamBodyBytes({ prevProfileBlockNumber: tail.prevLineNumber, prevProfileBlockHashHex: tail.prevLineHashHex, profileSequence: tail.thisLineNumber, key: cleanParam, value: cleanValue, }); const payload = await this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_USER_PROFILE, msgSubType: 1, msgVersion: 1, bodyBytes, }); return payload; }); } async addBlockConnectionTarget({ login, storagePwd, subType, toLogin, toForkNumber, toBlockNumber, toBlockHashHex, }) { const cleanLogin = String(login || '').trim(); if (!cleanLogin) throw new Error('Missing login for CONNECTION'); return this.runLineWriteLocked(`connections-line:${cleanLogin}`, async () => { const user = await this.ensureChainInitializedForLineOps(cleanLogin, storagePwd); const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim(); const tail = await this.resolveFixedLineTail({ blockchainName, kind: 'connections' }); const bodyBytes = makeConnectionBodyBytes({ prevConnectionBlockNumber: tail.prevLineNumber, prevConnectionBlockHashHex: tail.prevLineHashHex, connectionSequence: tail.thisLineNumber, toLogin: await this.requireRegisteredDisplayLogin(toLogin), toForkNumber, toBlockNumber, toBlockHashHex, }); const payload = await this.addBlockSigned({ login: cleanLogin, storagePwd, msgType: MSG_TYPE_CONNECTION, msgSubType: Number(subType), msgVersion: 1, bodyBytes, }); return payload; }); } async addBlockConnection({ login, toLogin, subType, storagePwd }) { const cleanLogin = (login || '').trim(); const cleanToLogin = (toLogin || '').trim(); const cleanSubType = Number(subType); if (!cleanLogin || !cleanToLogin) throw new Error('Не переданы login/toLogin для CONNECTION.'); if (!Number.isFinite(cleanSubType)) throw new Error('Не передан subType для CONNECTION.'); if (!storagePwd) throw new Error('Не передан storagePwd для подписи AddBlock.'); if (cleanLogin.toLowerCase() === cleanToLogin.toLowerCase()) { throw new Error('Нельзя создать связь на самого себя.'); } const targetUser = await this.getUser(cleanToLogin); if (!targetUser?.exists) throw new Error('Пользователь цели не найден.'); const targetBlockchainName = String(targetUser.blockchainName || '').trim(); const targetForkNumber = forkNumberFromBlockchainNameValue(targetBlockchainName); if (!targetForkNumber) throw new Error('Не удалось определить fork пользователя цели.'); const targetHeaderHash = await this.resolveHeaderHashForBlockchain(targetBlockchainName); if (targetHeaderHash === ZERO64) throw new Error('HEADER пользователя цели не найден.'); return this.addBlockConnectionTarget({ login: cleanLogin, storagePwd, subType: cleanSubType, toLogin: cleanToLogin, toForkNumber: targetForkNumber, toBlockNumber: 0, toBlockHashHex: targetHeaderHash, }); } async reportClientDebug({ runId = '', level = 'info', message = '', details = '' } = {}) { try { const response = await this.ws.request('ClientDebugLog', { runId, level, message, details }, 3000); return response?.status === 200; } catch { return false; } } async reportClientError(details) { // Ошибки не запускают переподключение: без открытого WS отчёт не отправляем. if (this.ws?.ws?.readyState !== WebSocket.OPEN) return false; try { const response = await this.ws.request('ClientErrorLog', details || {}, 3000); return response?.status === 200 && response?.payload?.accepted === true; } catch { return false; } } close() { this.ws.close(); } }