2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
/* eslint-disable guard-for-in */
|
|
|
|
/* eslint-disable no-restricted-syntax */
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Response } from 'node-fetch';
|
|
|
|
import fetch from 'node-fetch';
|
2023-05-30 23:57:16 +00:00
|
|
|
import type { Agent } from 'https';
|
2023-01-12 20:58:53 +00:00
|
|
|
import { escapeRegExp, isNumber, isString, isObject } from 'lodash';
|
2020-09-24 21:53:21 +00:00
|
|
|
import PQueue from 'p-queue';
|
|
|
|
import { v4 as getGuid } from 'uuid';
|
2021-05-25 22:40:04 +00:00
|
|
|
import { z } from 'zod';
|
2021-11-15 22:54:59 +00:00
|
|
|
import type { Readable } from 'stream';
|
2023-02-27 22:34:43 +00:00
|
|
|
import type { connection as WebSocket } from 'websocket';
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2022-09-15 19:17:15 +00:00
|
|
|
import { assertDev, strictAssert } from '../util/assert';
|
2021-11-30 16:29:57 +00:00
|
|
|
import { isRecord } from '../util/isRecord';
|
2021-08-26 14:10:58 +00:00
|
|
|
import * as durations from '../util/durations';
|
2021-12-17 21:26:50 +00:00
|
|
|
import type { ExplodePromiseResultType } from '../util/explodePromise';
|
|
|
|
import { explodePromise } from '../util/explodePromise';
|
2020-09-24 21:53:21 +00:00
|
|
|
import { getUserAgent } from '../util/getUserAgent';
|
2023-10-30 16:24:28 +00:00
|
|
|
import {
|
|
|
|
getTimeoutStream,
|
|
|
|
getStreamWithTimeout,
|
|
|
|
} from '../util/getStreamWithTimeout';
|
2021-11-02 23:01:13 +00:00
|
|
|
import { formatAcceptLanguageHeader } from '../util/userLanguages';
|
2023-02-08 17:14:59 +00:00
|
|
|
import { toWebSafeBase64, fromWebSafeBase64 } from '../util/webSafeBase64';
|
2022-03-09 19:28:40 +00:00
|
|
|
import { getBasicAuth } from '../util/getBasicAuth';
|
2023-05-30 23:57:16 +00:00
|
|
|
import { createHTTPSAgent } from '../util/createHTTPSAgent';
|
2023-08-29 23:58:48 +00:00
|
|
|
import { createProxyAgent } from '../util/createProxyAgent';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { SocketStatus } from '../types/SocketStatus';
|
2023-08-29 00:41:32 +00:00
|
|
|
import { VerificationTransport } from '../types/VerificationTransport';
|
2021-09-24 00:49:05 +00:00
|
|
|
import { toLogFormat } from '../types/errors';
|
2021-07-09 19:36:10 +00:00
|
|
|
import { isPackIdValid, redactPackId } from '../types/Stickers';
|
2023-08-16 20:54:39 +00:00
|
|
|
import type {
|
|
|
|
ServiceIdString,
|
|
|
|
AciString,
|
|
|
|
UntaggedPniString,
|
2023-08-10 16:43:33 +00:00
|
|
|
} from '../types/ServiceId';
|
2023-08-29 00:41:32 +00:00
|
|
|
import {
|
|
|
|
ServiceIdKind,
|
|
|
|
serviceIdSchema,
|
|
|
|
aciSchema,
|
|
|
|
untaggedPniSchema,
|
|
|
|
} from '../types/ServiceId';
|
2022-08-19 00:31:12 +00:00
|
|
|
import type { DirectoryConfigType } from '../types/RendererConfig';
|
2021-07-13 18:54:53 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2022-10-05 16:35:56 +00:00
|
|
|
import { randomInt } from '../Crypto';
|
2020-09-28 23:46:31 +00:00
|
|
|
import * as linkPreviewFetch from '../linkPreviews/linkPreviewFetch';
|
2021-11-02 23:01:13 +00:00
|
|
|
import { isBadgeImageFileUrlValid } from '../badges/isBadgeImageFileUrlValid';
|
2020-09-28 23:46:31 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
import { SocketManager } from './SocketManager';
|
2022-10-26 23:17:14 +00:00
|
|
|
import type { CDSAuthType, CDSResponseType } from './cds/Types.d';
|
2022-06-15 01:15:33 +00:00
|
|
|
import { CDSI } from './cds/CDSI';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type WebSocketResource from './WebsocketResources';
|
2021-06-22 14:46:42 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
2020-07-07 00:56:56 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
import { HTTPError } from './Errors';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type MessageSender from './SendMessage';
|
2022-06-13 21:39:35 +00:00
|
|
|
import type {
|
|
|
|
WebAPICredentials,
|
|
|
|
IRequestHandler,
|
|
|
|
StorageServiceCallOptionsType,
|
|
|
|
StorageServiceCredentials,
|
|
|
|
} from './Types.d';
|
2021-07-28 21:37:09 +00:00
|
|
|
import { handleStatusCode, translateError } from './Utils';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2023-01-19 19:53:04 +00:00
|
|
|
import { maybeParseUrl, urlPathFromComponents } from '../util/url';
|
2023-06-21 21:40:02 +00:00
|
|
|
import { SECOND } from '../util/durations';
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
// Note: this will break some code that expects to be able to use err.response when a
|
|
|
|
// web request fails, because it will force it to text. But it is very useful for
|
|
|
|
// debugging failed requests.
|
|
|
|
const DEBUG = false;
|
2023-06-21 21:40:02 +00:00
|
|
|
const DEFAULT_TIMEOUT = 30 * SECOND;
|
2021-01-29 22:16:48 +00:00
|
|
|
|
2020-08-20 22:15:50 +00:00
|
|
|
function _createRedactor(
|
|
|
|
...toReplace: ReadonlyArray<string | undefined>
|
|
|
|
): RedactUrl {
|
|
|
|
// NOTE: It would be nice to remove this cast, but TypeScript doesn't support
|
|
|
|
// it. However, there is [an issue][0] that discusses this in more detail.
|
|
|
|
// [0]: https://github.com/Microsoft/TypeScript/issues/16069
|
|
|
|
const stringsToReplace = toReplace.filter(Boolean) as Array<string>;
|
|
|
|
return href =>
|
|
|
|
stringsToReplace.reduce((result: string, stringToReplace: string) => {
|
|
|
|
const pattern = RegExp(escapeRegExp(stringToReplace), 'g');
|
|
|
|
const replacement = `[REDACTED]${stringToReplace.slice(-3)}`;
|
|
|
|
return result.replace(pattern, replacement);
|
|
|
|
}, href);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function _validateResponse(response: any, schema: any) {
|
2018-05-26 01:01:56 +00:00
|
|
|
try {
|
|
|
|
for (const i in schema) {
|
|
|
|
switch (schema[i]) {
|
|
|
|
case 'object':
|
|
|
|
case 'string':
|
|
|
|
case 'number':
|
|
|
|
if (typeof response[i] !== schema[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:10:58 +00:00
|
|
|
const FIVE_MINUTES = 5 * durations.MINUTE;
|
2021-11-15 22:54:59 +00:00
|
|
|
const GET_ATTACHMENT_CHUNK_TIMEOUT = 10 * durations.SECOND;
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
type AgentCacheType = {
|
|
|
|
[name: string]: {
|
|
|
|
timestamp: number;
|
2023-08-29 23:58:48 +00:00
|
|
|
agent: ReturnType<typeof createProxyAgent> | Agent;
|
2020-03-31 20:03:38 +00:00
|
|
|
};
|
2018-11-07 19:20:43 +00:00
|
|
|
};
|
2020-03-31 20:03:38 +00:00
|
|
|
const agents: AgentCacheType = {};
|
2018-11-07 19:20:43 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function getContentType(response: Response) {
|
2019-01-16 03:03:56 +00:00
|
|
|
if (response.headers && response.headers.get) {
|
|
|
|
return response.headers.get('content-type');
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
type FetchHeaderListType = { [name: string]: string };
|
2021-09-02 22:31:21 +00:00
|
|
|
export type HeaderListType = { [name: string]: string | ReadonlyArray<string> };
|
2021-11-30 19:33:51 +00:00
|
|
|
type HTTPCodeType = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2020-08-20 22:15:50 +00:00
|
|
|
type RedactUrl = (url: string) => string;
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type PromiseAjaxOptionsType = {
|
2021-07-28 21:37:09 +00:00
|
|
|
socketManager?: SocketManager;
|
2020-09-09 02:25:05 +00:00
|
|
|
basicAuth?: string;
|
2020-03-31 20:03:38 +00:00
|
|
|
certificateAuthority?: string;
|
|
|
|
contentType?: string;
|
2021-09-24 00:49:05 +00:00
|
|
|
data?: Uint8Array | string;
|
2023-06-14 20:51:49 +00:00
|
|
|
disableRetries?: boolean;
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption?: boolean;
|
2020-03-31 20:03:38 +00:00
|
|
|
headers?: HeaderListType;
|
|
|
|
host?: string;
|
|
|
|
password?: string;
|
|
|
|
path?: string;
|
|
|
|
proxyUrl?: string;
|
2020-08-20 22:15:50 +00:00
|
|
|
redactUrl?: RedactUrl;
|
2020-03-31 20:03:38 +00:00
|
|
|
redirect?: 'error' | 'follow' | 'manual';
|
2021-11-15 22:54:59 +00:00
|
|
|
responseType?:
|
|
|
|
| 'json'
|
|
|
|
| 'jsonwithdetails'
|
|
|
|
| 'bytes'
|
|
|
|
| 'byteswithdetails'
|
|
|
|
| 'stream';
|
2021-05-17 18:29:37 +00:00
|
|
|
serverUrl?: string;
|
2020-03-31 20:03:38 +00:00
|
|
|
stack?: string;
|
|
|
|
timeout?: number;
|
2020-07-07 00:56:56 +00:00
|
|
|
type: HTTPCodeType;
|
2020-03-31 20:03:38 +00:00
|
|
|
user?: string;
|
|
|
|
validateResponse?: any;
|
|
|
|
version: string;
|
2021-11-15 22:54:59 +00:00
|
|
|
abortSignal?: AbortSignal;
|
2021-11-30 16:29:57 +00:00
|
|
|
} & (
|
|
|
|
| {
|
|
|
|
unauthenticated?: false;
|
|
|
|
accessKey?: string;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
unauthenticated: true;
|
|
|
|
accessKey: undefined | string;
|
|
|
|
}
|
|
|
|
);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2022-06-15 01:15:33 +00:00
|
|
|
type JSONWithDetailsType<Data = unknown> = {
|
|
|
|
data: Data;
|
2020-09-09 02:25:05 +00:00
|
|
|
contentType: string | null;
|
|
|
|
response: Response;
|
|
|
|
};
|
2021-09-24 00:49:05 +00:00
|
|
|
type BytesWithDetailsType = {
|
|
|
|
data: Uint8Array;
|
2020-09-09 02:25:05 +00:00
|
|
|
contentType: string | null;
|
2020-09-04 01:25:19 +00:00
|
|
|
response: Response;
|
|
|
|
};
|
|
|
|
|
2022-10-18 17:12:02 +00:00
|
|
|
export const multiRecipient200ResponseSchema = z.object({
|
2023-08-10 16:43:33 +00:00
|
|
|
uuids404: z.array(serviceIdSchema).optional(),
|
2022-10-18 17:12:02 +00:00
|
|
|
needsSync: z.boolean().optional(),
|
|
|
|
});
|
2021-05-25 22:40:04 +00:00
|
|
|
export type MultiRecipient200ResponseType = z.infer<
|
|
|
|
typeof multiRecipient200ResponseSchema
|
|
|
|
>;
|
|
|
|
|
|
|
|
export const multiRecipient409ResponseSchema = z.array(
|
2022-10-18 17:12:02 +00:00
|
|
|
z.object({
|
2023-08-10 16:43:33 +00:00
|
|
|
uuid: serviceIdSchema,
|
2022-10-18 17:12:02 +00:00
|
|
|
devices: z.object({
|
|
|
|
missingDevices: z.array(z.number()).optional(),
|
|
|
|
extraDevices: z.array(z.number()).optional(),
|
|
|
|
}),
|
|
|
|
})
|
2021-05-25 22:40:04 +00:00
|
|
|
);
|
|
|
|
export type MultiRecipient409ResponseType = z.infer<
|
|
|
|
typeof multiRecipient409ResponseSchema
|
|
|
|
>;
|
|
|
|
|
|
|
|
export const multiRecipient410ResponseSchema = z.array(
|
2022-10-18 17:12:02 +00:00
|
|
|
z.object({
|
2023-08-10 16:43:33 +00:00
|
|
|
uuid: serviceIdSchema,
|
2022-10-18 17:12:02 +00:00
|
|
|
devices: z.object({
|
|
|
|
staleDevices: z.array(z.number()).optional(),
|
|
|
|
}),
|
|
|
|
})
|
2021-05-25 22:40:04 +00:00
|
|
|
);
|
|
|
|
export type MultiRecipient410ResponseType = z.infer<
|
|
|
|
typeof multiRecipient410ResponseSchema
|
|
|
|
>;
|
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
function isSuccess(status: number): boolean {
|
|
|
|
return status >= 0 && status < 400;
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:29:37 +00:00
|
|
|
function getHostname(url: string): string {
|
|
|
|
const urlObject = new URL(url);
|
|
|
|
return urlObject.hostname;
|
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
async function _promiseAjax(
|
|
|
|
providedUrl: string | null,
|
|
|
|
options: PromiseAjaxOptionsType
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<unknown> {
|
2021-09-28 20:55:50 +00:00
|
|
|
const { proxyUrl, socketManager } = options;
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const url = providedUrl || `${options.host}/${options.path}`;
|
2021-09-28 20:55:50 +00:00
|
|
|
const logType = socketManager ? '(WS)' : '(REST)';
|
|
|
|
const redactedURL = options.redactUrl ? options.redactUrl(url) : url;
|
2021-07-28 21:37:09 +00:00
|
|
|
|
|
|
|
const unauthLabel = options.unauthenticated ? ' (unauth)' : '';
|
2022-08-03 16:23:29 +00:00
|
|
|
const logId = `${options.type} ${logType} ${redactedURL}${unauthLabel}`;
|
|
|
|
log.info(logId);
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2023-06-21 21:40:02 +00:00
|
|
|
const timeout =
|
|
|
|
typeof options.timeout === 'number' ? options.timeout : DEFAULT_TIMEOUT;
|
2021-07-28 21:37:09 +00:00
|
|
|
|
|
|
|
const agentType = options.unauthenticated ? 'unauth' : 'auth';
|
|
|
|
const cacheKey = `${proxyUrl}-${agentType}`;
|
|
|
|
|
|
|
|
const { timestamp } = agents[cacheKey] || { timestamp: null };
|
|
|
|
if (!timestamp || timestamp + FIVE_MINUTES < Date.now()) {
|
|
|
|
if (timestamp) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Cycling agent for type ${cacheKey}`);
|
2021-07-28 21:37:09 +00:00
|
|
|
}
|
|
|
|
agents[cacheKey] = {
|
|
|
|
agent: proxyUrl
|
2023-08-29 23:58:48 +00:00
|
|
|
? createProxyAgent(proxyUrl)
|
2023-05-30 23:57:16 +00:00
|
|
|
: createHTTPSAgent({
|
2022-11-19 05:14:20 +00:00
|
|
|
keepAlive: !options.disableSessionResumption,
|
|
|
|
maxCachedSessions: options.disableSessionResumption ? 0 : undefined,
|
|
|
|
}),
|
2021-07-28 21:37:09 +00:00
|
|
|
timestamp: Date.now(),
|
|
|
|
};
|
2021-06-09 22:28:54 +00:00
|
|
|
}
|
2023-05-30 23:57:16 +00:00
|
|
|
const agentEntry = agents[cacheKey];
|
|
|
|
const agent = agentEntry?.agent ?? null;
|
2021-07-28 21:37:09 +00:00
|
|
|
|
|
|
|
const fetchOptions = {
|
|
|
|
method: options.type,
|
|
|
|
body: options.data,
|
|
|
|
headers: {
|
|
|
|
'User-Agent': getUserAgent(options.version),
|
|
|
|
'X-Signal-Agent': 'OWD',
|
|
|
|
...options.headers,
|
|
|
|
} as FetchHeaderListType,
|
|
|
|
redirect: options.redirect,
|
|
|
|
agent,
|
|
|
|
ca: options.certificateAuthority,
|
|
|
|
timeout,
|
2021-11-15 22:54:59 +00:00
|
|
|
abortSignal: options.abortSignal,
|
2021-07-28 21:37:09 +00:00
|
|
|
};
|
2021-06-09 22:28:54 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
if (fetchOptions.body instanceof Uint8Array) {
|
|
|
|
// node-fetch doesn't support Uint8Array, only node Buffer
|
2021-07-28 21:37:09 +00:00
|
|
|
const contentLength = fetchOptions.body.byteLength;
|
|
|
|
fetchOptions.body = Buffer.from(fetchOptions.body);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
// node-fetch doesn't set content-length like S3 requires
|
|
|
|
fetchOptions.headers['Content-Length'] = contentLength.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { accessKey, basicAuth, unauthenticated } = options;
|
|
|
|
if (basicAuth) {
|
|
|
|
fetchOptions.headers.Authorization = `Basic ${basicAuth}`;
|
|
|
|
} else if (unauthenticated) {
|
2021-11-30 16:29:57 +00:00
|
|
|
if (accessKey) {
|
|
|
|
// Access key is already a Base64 string
|
|
|
|
fetchOptions.headers['Unidentified-Access-Key'] = accessKey;
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
} else if (options.user && options.password) {
|
2022-03-09 19:28:40 +00:00
|
|
|
fetchOptions.headers.Authorization = getBasicAuth({
|
|
|
|
username: options.user,
|
|
|
|
password: options.password,
|
|
|
|
});
|
2021-07-28 21:37:09 +00:00
|
|
|
}
|
2019-02-20 22:40:32 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
if (options.contentType) {
|
|
|
|
fetchOptions.headers['Content-Type'] = options.contentType;
|
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
let response: Response;
|
2021-11-15 22:54:59 +00:00
|
|
|
let result: string | Uint8Array | Readable | unknown;
|
2021-07-28 21:37:09 +00:00
|
|
|
try {
|
|
|
|
response = socketManager
|
|
|
|
? await socketManager.fetch(url, fetchOptions)
|
|
|
|
: await fetch(url, fetchOptions);
|
|
|
|
|
|
|
|
if (
|
|
|
|
options.serverUrl &&
|
|
|
|
getHostname(options.serverUrl) === getHostname(url)
|
|
|
|
) {
|
|
|
|
await handleStatusCode(response.status);
|
2018-11-07 19:20:43 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
if (!unauthenticated && response.status === 401) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error('Got 401 from Signal Server. We might be unlinked.');
|
2021-07-28 21:37:09 +00:00
|
|
|
window.Whisper.events.trigger('mightBeUnlinked');
|
2018-11-07 19:20:43 +00:00
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
if (DEBUG && !isSuccess(response.status)) {
|
|
|
|
result = await response.text();
|
|
|
|
} else if (
|
|
|
|
(options.responseType === 'json' ||
|
|
|
|
options.responseType === 'jsonwithdetails') &&
|
|
|
|
/^application\/json(;.*)?$/.test(
|
|
|
|
response.headers.get('Content-Type') || ''
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
result = await response.json();
|
|
|
|
} else if (
|
2021-09-24 00:49:05 +00:00
|
|
|
options.responseType === 'bytes' ||
|
|
|
|
options.responseType === 'byteswithdetails'
|
2021-07-28 21:37:09 +00:00
|
|
|
) {
|
2021-09-24 00:49:05 +00:00
|
|
|
result = await response.buffer();
|
2021-11-15 22:54:59 +00:00
|
|
|
} else if (options.responseType === 'stream') {
|
|
|
|
result = response.body;
|
2021-07-28 21:37:09 +00:00
|
|
|
} else {
|
|
|
|
result = await response.textConverted();
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
} catch (e) {
|
2022-08-03 16:23:29 +00:00
|
|
|
log.error(logId, 0, 'Error');
|
2021-07-28 21:37:09 +00:00
|
|
|
const stack = `${e.stack}\nInitial stack:\n${options.stack}`;
|
|
|
|
throw makeHTTPError('promiseAjax catch', 0, {}, e.toString(), stack);
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
if (!isSuccess(response.status)) {
|
2022-08-03 16:23:29 +00:00
|
|
|
log.error(logId, response.status, 'Error');
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
throw makeHTTPError(
|
|
|
|
'promiseAjax: error response',
|
|
|
|
response.status,
|
|
|
|
response.headers.raw(),
|
|
|
|
result,
|
|
|
|
options.stack
|
|
|
|
);
|
|
|
|
}
|
2020-09-09 22:50:44 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
if (
|
|
|
|
options.responseType === 'json' ||
|
|
|
|
options.responseType === 'jsonwithdetails'
|
|
|
|
) {
|
|
|
|
if (options.validateResponse) {
|
|
|
|
if (!_validateResponse(result, options.validateResponse)) {
|
2022-08-03 16:23:29 +00:00
|
|
|
log.error(logId, response.status, 'Error');
|
2021-07-28 21:37:09 +00:00
|
|
|
throw makeHTTPError(
|
|
|
|
'promiseAjax: invalid response',
|
|
|
|
response.status,
|
|
|
|
response.headers.raw(),
|
|
|
|
result,
|
|
|
|
options.stack
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 03:03:56 +00:00
|
|
|
|
2022-08-03 16:23:29 +00:00
|
|
|
log.info(logId, response.status, 'Success');
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
if (options.responseType === 'byteswithdetails') {
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(result instanceof Uint8Array, 'Expected Uint8Array result');
|
2021-09-24 00:49:05 +00:00
|
|
|
const fullResult: BytesWithDetailsType = {
|
2021-07-28 21:37:09 +00:00
|
|
|
data: result,
|
|
|
|
contentType: getContentType(response),
|
|
|
|
response,
|
|
|
|
};
|
2020-09-09 02:25:05 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
return fullResult;
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
if (options.responseType === 'jsonwithdetails') {
|
|
|
|
const fullResult: JSONWithDetailsType = {
|
|
|
|
data: result,
|
|
|
|
contentType: getContentType(response),
|
|
|
|
response,
|
|
|
|
};
|
2019-01-16 03:03:56 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
return fullResult;
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
return result;
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function _retryAjax(
|
|
|
|
url: string | null,
|
|
|
|
options: PromiseAjaxOptionsType,
|
|
|
|
providedLimit?: number,
|
|
|
|
providedCount?: number
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<unknown> {
|
2018-05-26 01:01:56 +00:00
|
|
|
const count = (providedCount || 0) + 1;
|
|
|
|
const limit = providedLimit || 3;
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
try {
|
|
|
|
return await _promiseAjax(url, options);
|
|
|
|
} catch (e) {
|
2021-09-22 00:58:03 +00:00
|
|
|
if (e instanceof HTTPError && e.code === -1 && count < limit) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(_retryAjax(url, options, limit, count));
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
throw e;
|
2021-09-24 00:49:05 +00:00
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
function _outerAjax(
|
|
|
|
providedUrl: string | null,
|
|
|
|
options: PromiseAjaxOptionsType & { responseType: 'json' }
|
|
|
|
): Promise<unknown>;
|
|
|
|
function _outerAjax(
|
|
|
|
providedUrl: string | null,
|
|
|
|
options: PromiseAjaxOptionsType & { responseType: 'jsonwithdetails' }
|
|
|
|
): Promise<JSONWithDetailsType>;
|
|
|
|
function _outerAjax(
|
|
|
|
providedUrl: string | null,
|
|
|
|
options: PromiseAjaxOptionsType & { responseType?: 'bytes' }
|
|
|
|
): Promise<Uint8Array>;
|
|
|
|
function _outerAjax(
|
|
|
|
providedUrl: string | null,
|
|
|
|
options: PromiseAjaxOptionsType & { responseType: 'byteswithdetails' }
|
|
|
|
): Promise<BytesWithDetailsType>;
|
2021-11-15 22:54:59 +00:00
|
|
|
function _outerAjax(
|
|
|
|
providedUrl: string | null,
|
|
|
|
options: PromiseAjaxOptionsType & { responseType?: 'stream' }
|
|
|
|
): Promise<Readable>;
|
2021-09-24 00:49:05 +00:00
|
|
|
function _outerAjax(
|
|
|
|
providedUrl: string | null,
|
|
|
|
options: PromiseAjaxOptionsType
|
|
|
|
): Promise<unknown>;
|
|
|
|
|
|
|
|
async function _outerAjax(
|
|
|
|
url: string | null,
|
|
|
|
options: PromiseAjaxOptionsType
|
|
|
|
): Promise<unknown> {
|
2018-05-26 01:01:56 +00:00
|
|
|
options.stack = new Error().stack; // just in case, save stack here.
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2023-06-14 20:51:49 +00:00
|
|
|
if (options.disableRetries) {
|
|
|
|
return _promiseAjax(url, options);
|
|
|
|
}
|
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return _retryAjax(url, options);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function makeHTTPError(
|
|
|
|
message: string,
|
|
|
|
providedCode: number,
|
2021-05-06 00:09:29 +00:00
|
|
|
headers: HeaderListType,
|
2021-09-24 00:49:05 +00:00
|
|
|
response: unknown,
|
2020-03-31 20:03:38 +00:00
|
|
|
stack?: string
|
|
|
|
) {
|
2021-07-28 21:37:09 +00:00
|
|
|
return new HTTPError(message, {
|
|
|
|
code: providedCode,
|
|
|
|
headers,
|
|
|
|
response,
|
|
|
|
stack,
|
|
|
|
});
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const URL_CALLS = {
|
2021-11-30 19:33:51 +00:00
|
|
|
accountExistence: 'v1/accounts/account',
|
2023-05-04 20:58:53 +00:00
|
|
|
attachmentId: 'v3/attachments/form/upload',
|
2020-09-09 02:25:05 +00:00
|
|
|
attestation: 'v1/attestation',
|
2022-08-19 18:05:31 +00:00
|
|
|
batchIdentityCheck: 'v1/profile/identity_check/batch',
|
2021-11-01 19:13:35 +00:00
|
|
|
challenge: 'v1/challenge',
|
2020-09-09 02:25:05 +00:00
|
|
|
config: 'v1/config',
|
2018-10-18 01:01:21 +00:00
|
|
|
deliveryCert: 'v1/certificate/delivery',
|
2021-11-12 20:45:30 +00:00
|
|
|
directoryAuthV2: 'v2/directory/auth',
|
2020-09-09 02:25:05 +00:00
|
|
|
discovery: 'v1/discovery',
|
2020-11-20 17:30:45 +00:00
|
|
|
getGroupAvatarUpload: 'v1/groups/avatar/form',
|
2022-07-08 20:46:25 +00:00
|
|
|
getGroupCredentials: 'v1/certificate/auth/group',
|
2020-09-09 02:25:05 +00:00
|
|
|
getIceServers: 'v1/accounts/turn',
|
2022-11-09 02:38:19 +00:00
|
|
|
getOnboardingStoryManifest:
|
|
|
|
'dynamic/desktop/stories/onboarding/manifest.json',
|
2020-09-09 02:25:05 +00:00
|
|
|
getStickerPackUpload: 'v1/sticker/pack/form',
|
2023-02-27 22:34:43 +00:00
|
|
|
getArtAuth: 'v1/art/auth',
|
2020-09-09 02:25:05 +00:00
|
|
|
groupLog: 'v1/groups/logs',
|
2022-01-27 00:02:23 +00:00
|
|
|
groupJoinedAtVersion: 'v1/groups/joined_at_version',
|
2020-09-09 02:25:05 +00:00
|
|
|
groups: 'v1/groups',
|
2022-05-16 14:53:54 +00:00
|
|
|
groupsViaLink: 'v1/groups/join/',
|
2020-11-13 19:57:55 +00:00
|
|
|
groupToken: 'v1/groups/token',
|
2018-05-26 01:01:56 +00:00
|
|
|
keys: 'v2/keys',
|
2023-08-29 00:41:32 +00:00
|
|
|
linkDevice: 'v1/devices/link',
|
2018-05-26 01:01:56 +00:00
|
|
|
messages: 'v1/messages',
|
2021-05-25 22:40:04 +00:00
|
|
|
multiRecipient: 'v1/messages/multi_recipient',
|
2023-02-23 21:32:19 +00:00
|
|
|
phoneNumberDiscoverability: 'v2/accounts/phone_number_discoverability',
|
2018-05-26 01:01:56 +00:00
|
|
|
profile: 'v1/profile',
|
2023-08-29 00:41:32 +00:00
|
|
|
registration: 'v1/registration',
|
2020-07-07 00:56:56 +00:00
|
|
|
registerCapabilities: 'v1/devices/capabilities',
|
2021-05-27 20:17:05 +00:00
|
|
|
reportMessage: 'v1/messages/report',
|
2018-10-18 01:01:21 +00:00
|
|
|
signed: 'v2/keys/signed',
|
2020-07-07 00:56:56 +00:00
|
|
|
storageManifest: 'v1/storage/manifest',
|
|
|
|
storageModify: 'v1/storage/',
|
|
|
|
storageRead: 'v1/storage/read',
|
|
|
|
storageToken: 'v1/storage/auth',
|
2021-11-30 16:29:57 +00:00
|
|
|
subscriptions: 'v1/subscription',
|
2023-10-06 21:31:17 +00:00
|
|
|
subscriptionConfiguration: 'v1/subscription/configuration',
|
2020-07-07 00:56:56 +00:00
|
|
|
supportUnauthenticatedDelivery: 'v1/devices/unauthenticated_delivery',
|
2020-09-09 02:25:05 +00:00
|
|
|
updateDeviceName: 'v1/accounts/name',
|
2023-02-08 17:14:59 +00:00
|
|
|
username: 'v1/accounts/username_hash',
|
|
|
|
reserveUsername: 'v1/accounts/username_hash/reserve',
|
|
|
|
confirmUsername: 'v1/accounts/username_hash/confirm',
|
2023-07-20 03:14:08 +00:00
|
|
|
usernameLink: 'v1/accounts/username_link',
|
2023-08-29 00:41:32 +00:00
|
|
|
verificationSession: 'v1/verification/session',
|
2020-03-05 21:14:58 +00:00
|
|
|
whoami: 'v1/accounts/whoami',
|
2018-05-26 01:01:56 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
|
|
|
|
// MessageController
|
|
|
|
'messages',
|
2021-08-04 20:12:35 +00:00
|
|
|
'multiRecipient',
|
2021-07-28 21:37:09 +00:00
|
|
|
'reportMessage',
|
|
|
|
|
|
|
|
// ProfileController
|
|
|
|
'profile',
|
|
|
|
|
2023-08-16 17:44:33 +00:00
|
|
|
// AttachmentControllerV3
|
2021-07-28 21:37:09 +00:00
|
|
|
'attachmentId',
|
|
|
|
|
|
|
|
// RemoteConfigController
|
|
|
|
'config',
|
2021-08-04 00:37:17 +00:00
|
|
|
|
|
|
|
// Certificate
|
|
|
|
'deliveryCert',
|
|
|
|
'getGroupCredentials',
|
|
|
|
|
|
|
|
// Devices
|
2023-08-29 00:41:32 +00:00
|
|
|
'linkDevice',
|
2021-08-04 00:37:17 +00:00
|
|
|
'registerCapabilities',
|
|
|
|
'supportUnauthenticatedDelivery',
|
|
|
|
|
|
|
|
// Directory
|
2021-11-12 20:45:30 +00:00
|
|
|
'directoryAuthV2',
|
2021-08-04 00:37:17 +00:00
|
|
|
|
|
|
|
// Storage
|
|
|
|
'storageToken',
|
2023-02-23 21:32:19 +00:00
|
|
|
|
|
|
|
// Account V2
|
|
|
|
'phoneNumberDiscoverability',
|
2021-07-28 21:37:09 +00:00
|
|
|
]);
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type InitializeOptionsType = {
|
|
|
|
url: string;
|
2020-07-07 00:56:56 +00:00
|
|
|
storageUrl: string;
|
2021-11-02 23:01:13 +00:00
|
|
|
updatesUrl: string;
|
2022-11-09 02:38:19 +00:00
|
|
|
resourcesUrl: string;
|
2023-02-27 22:34:43 +00:00
|
|
|
artCreatorUrl: string;
|
2020-04-17 22:51:39 +00:00
|
|
|
cdnUrlObject: {
|
|
|
|
readonly '0': string;
|
|
|
|
readonly [propName: string]: string;
|
|
|
|
};
|
2020-03-31 20:03:38 +00:00
|
|
|
certificateAuthority: string;
|
|
|
|
contentProxyUrl: string;
|
2022-06-13 21:39:35 +00:00
|
|
|
proxyUrl: string | undefined;
|
2020-03-31 20:03:38 +00:00
|
|
|
version: string;
|
2022-08-19 00:31:12 +00:00
|
|
|
directoryConfig: DirectoryConfigType;
|
2020-03-31 20:03:38 +00:00
|
|
|
};
|
|
|
|
|
2021-09-28 23:38:55 +00:00
|
|
|
export type MessageType = Readonly<{
|
|
|
|
type: number;
|
|
|
|
destinationDeviceId: number;
|
|
|
|
destinationRegistrationId: number;
|
|
|
|
content: string;
|
|
|
|
}>;
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
type AjaxOptionsType = {
|
2020-09-09 02:25:05 +00:00
|
|
|
basicAuth?: string;
|
2020-03-31 20:03:38 +00:00
|
|
|
call: keyof typeof URL_CALLS;
|
2020-07-07 00:56:56 +00:00
|
|
|
contentType?: string;
|
2021-09-24 00:49:05 +00:00
|
|
|
data?: Uint8Array | Buffer | Uint8Array | string;
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption?: boolean;
|
2021-05-25 22:40:04 +00:00
|
|
|
headers?: HeaderListType;
|
2020-07-07 00:56:56 +00:00
|
|
|
host?: string;
|
|
|
|
httpType: HTTPCodeType;
|
2021-09-24 00:49:05 +00:00
|
|
|
jsonData?: unknown;
|
2020-07-07 00:56:56 +00:00
|
|
|
password?: string;
|
2020-08-20 22:15:50 +00:00
|
|
|
redactUrl?: RedactUrl;
|
2021-11-15 22:54:59 +00:00
|
|
|
responseType?: 'json' | 'bytes' | 'byteswithdetails' | 'stream';
|
2021-09-22 00:58:03 +00:00
|
|
|
schema?: unknown;
|
2020-03-31 20:03:38 +00:00
|
|
|
timeout?: number;
|
|
|
|
urlParameters?: string;
|
2020-07-07 00:56:56 +00:00
|
|
|
username?: string;
|
2020-03-31 20:03:38 +00:00
|
|
|
validateResponse?: any;
|
2021-12-17 21:26:50 +00:00
|
|
|
isRegistration?: true;
|
2022-10-18 17:12:02 +00:00
|
|
|
abortSignal?: AbortSignal;
|
2021-11-30 16:29:57 +00:00
|
|
|
} & (
|
|
|
|
| {
|
|
|
|
unauthenticated?: false;
|
|
|
|
accessKey?: string;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
unauthenticated: true;
|
|
|
|
accessKey: undefined | string;
|
|
|
|
}
|
|
|
|
);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2021-08-19 00:13:32 +00:00
|
|
|
export type WebAPIConnectOptionsType = WebAPICredentials & {
|
2021-09-15 18:44:27 +00:00
|
|
|
useWebSocket?: boolean;
|
2022-10-05 00:48:25 +00:00
|
|
|
hasStoriesDisabled: boolean;
|
2021-08-19 00:13:32 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
export type WebAPIConnectType = {
|
2021-08-19 00:13:32 +00:00
|
|
|
connect: (options: WebAPIConnectOptionsType) => WebAPIType;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
export type CapabilitiesType = {
|
2022-12-20 20:29:17 +00:00
|
|
|
pni: boolean;
|
2020-11-20 17:30:45 +00:00
|
|
|
};
|
|
|
|
export type CapabilitiesUploadType = {
|
2022-12-20 20:29:17 +00:00
|
|
|
// true in staging, false in production
|
|
|
|
pni: boolean;
|
2020-11-20 17:30:45 +00:00
|
|
|
};
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
type StickerPackManifestType = Uint8Array;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
export type GroupCredentialType = {
|
|
|
|
credential: string;
|
|
|
|
redemptionTime: number;
|
|
|
|
};
|
|
|
|
export type GroupCredentialsType = {
|
|
|
|
groupPublicParamsHex: string;
|
|
|
|
authCredentialPresentationHex: string;
|
|
|
|
};
|
2022-02-04 21:42:20 +00:00
|
|
|
export type GetGroupLogOptionsType = Readonly<{
|
|
|
|
startVersion: number | undefined;
|
|
|
|
includeFirstState: boolean;
|
|
|
|
includeLastState: boolean;
|
|
|
|
maxSupportedChangeEpoch: number;
|
|
|
|
}>;
|
2020-09-09 02:25:05 +00:00
|
|
|
export type GroupLogResponseType = {
|
|
|
|
currentRevision?: number;
|
|
|
|
start?: number;
|
|
|
|
end?: number;
|
2021-06-22 14:46:42 +00:00
|
|
|
changes: Proto.GroupChanges;
|
2020-09-09 02:25:05 +00:00
|
|
|
};
|
|
|
|
|
2021-07-19 19:26:06 +00:00
|
|
|
export type ProfileRequestDataType = {
|
|
|
|
about: string | null;
|
|
|
|
aboutEmoji: string | null;
|
|
|
|
avatar: boolean;
|
2022-03-16 00:14:20 +00:00
|
|
|
sameAvatar: boolean;
|
2021-07-19 19:26:06 +00:00
|
|
|
commitment: string;
|
|
|
|
name: string;
|
|
|
|
paymentAddress: string | null;
|
|
|
|
version: string;
|
|
|
|
};
|
|
|
|
|
2022-10-18 17:12:02 +00:00
|
|
|
const uploadAvatarHeadersZod = z.object({
|
|
|
|
acl: z.string(),
|
|
|
|
algorithm: z.string(),
|
|
|
|
credential: z.string(),
|
|
|
|
date: z.string(),
|
|
|
|
key: z.string(),
|
|
|
|
policy: z.string(),
|
|
|
|
signature: z.string(),
|
|
|
|
});
|
2021-07-19 19:26:06 +00:00
|
|
|
export type UploadAvatarHeadersType = z.infer<typeof uploadAvatarHeadersZod>;
|
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
const remoteConfigResponseZod = z.object({
|
|
|
|
config: z
|
|
|
|
.object({
|
|
|
|
name: z.string(),
|
|
|
|
enabled: z.boolean(),
|
|
|
|
value: z.string().or(z.null()).optional(),
|
|
|
|
})
|
|
|
|
.array(),
|
|
|
|
serverEpochTime: z.number(),
|
|
|
|
});
|
|
|
|
export type RemoteConfigResponseType = z.infer<typeof remoteConfigResponseZod>;
|
|
|
|
|
2021-09-22 00:58:03 +00:00
|
|
|
export type ProfileType = Readonly<{
|
|
|
|
identityKey?: string;
|
|
|
|
name?: string;
|
|
|
|
about?: string;
|
|
|
|
aboutEmoji?: string;
|
|
|
|
avatar?: string;
|
|
|
|
unidentifiedAccess?: string;
|
|
|
|
unrestrictedUnidentifiedAccess?: string;
|
|
|
|
uuid?: string;
|
|
|
|
credential?: string;
|
2022-07-08 20:46:25 +00:00
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
capabilities?: CapabilitiesType;
|
|
|
|
paymentAddress?: string;
|
2021-11-02 23:01:13 +00:00
|
|
|
badges?: unknown;
|
2021-09-22 00:58:03 +00:00
|
|
|
}>;
|
|
|
|
|
2023-02-08 17:14:59 +00:00
|
|
|
export type GetAccountForUsernameOptionsType = Readonly<{
|
|
|
|
hash: Uint8Array;
|
2022-10-19 20:41:47 +00:00
|
|
|
}>;
|
|
|
|
|
2023-02-08 17:14:59 +00:00
|
|
|
const getAccountForUsernameResultZod = z.object({
|
2023-08-10 16:43:33 +00:00
|
|
|
uuid: aciSchema,
|
2023-02-08 17:14:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export type GetAccountForUsernameResultType = z.infer<
|
|
|
|
typeof getAccountForUsernameResultZod
|
|
|
|
>;
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
export type GetIceServersResultType = Readonly<{
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
urls: ReadonlyArray<string>;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type GetDevicesResultType = ReadonlyArray<
|
|
|
|
Readonly<{
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
lastSeen: number;
|
|
|
|
created: number;
|
|
|
|
}>
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type GetSenderCertificateResultType = Readonly<{ certificate: string }>;
|
|
|
|
|
|
|
|
export type MakeProxiedRequestResultType =
|
|
|
|
| Uint8Array
|
|
|
|
| {
|
|
|
|
result: BytesWithDetailsType;
|
|
|
|
totalSize: number;
|
|
|
|
};
|
|
|
|
|
2022-10-18 17:12:02 +00:00
|
|
|
const whoamiResultZod = z.object({
|
|
|
|
uuid: z.string(),
|
|
|
|
pni: z.string(),
|
|
|
|
number: z.string(),
|
|
|
|
username: z.string().or(z.null()).optional(),
|
|
|
|
});
|
2022-07-18 22:32:00 +00:00
|
|
|
export type WhoamiResultType = z.infer<typeof whoamiResultZod>;
|
2021-09-24 00:49:05 +00:00
|
|
|
|
2022-08-18 20:44:53 +00:00
|
|
|
export type CdsLookupOptionsType = Readonly<{
|
2021-12-06 22:54:20 +00:00
|
|
|
e164s: ReadonlyArray<string>;
|
2023-10-31 19:00:25 +00:00
|
|
|
acisAndAccessKeys?: ReadonlyArray<{ aci: AciString; accessKey: string }>;
|
2022-08-19 00:31:12 +00:00
|
|
|
returnAcisWithoutUaks?: boolean;
|
2021-12-06 22:54:20 +00:00
|
|
|
}>;
|
|
|
|
|
2022-03-09 20:23:21 +00:00
|
|
|
type GetProfileCommonOptionsType = Readonly<
|
|
|
|
{
|
|
|
|
userLanguages: ReadonlyArray<string>;
|
|
|
|
} & (
|
|
|
|
| {
|
|
|
|
profileKeyVersion?: undefined;
|
|
|
|
profileKeyCredentialRequest?: undefined;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
profileKeyVersion: string;
|
|
|
|
profileKeyCredentialRequest?: string;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type GetProfileOptionsType = GetProfileCommonOptionsType &
|
|
|
|
Readonly<{
|
|
|
|
accessKey?: undefined;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type GetProfileUnauthOptionsType = GetProfileCommonOptionsType &
|
|
|
|
Readonly<{
|
|
|
|
accessKey: string;
|
|
|
|
}>;
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
export type GetGroupCredentialsOptionsType = Readonly<{
|
|
|
|
startDayInMs: number;
|
|
|
|
endDayInMs: number;
|
|
|
|
}>;
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
export type GetGroupCredentialsResultType = Readonly<{
|
2023-08-16 20:54:39 +00:00
|
|
|
pni?: UntaggedPniString | null;
|
2022-07-28 16:35:29 +00:00
|
|
|
credentials: ReadonlyArray<GroupCredentialType>;
|
|
|
|
}>;
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const verifyServiceIdResponse = z.object({
|
2022-10-18 17:12:02 +00:00
|
|
|
elements: z.array(
|
|
|
|
z.object({
|
2023-08-10 16:43:33 +00:00
|
|
|
uuid: serviceIdSchema,
|
2022-10-18 17:12:02 +00:00
|
|
|
identityKey: z.string(),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
});
|
2022-08-19 18:05:31 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
export type VerifyServiceIdRequestType = Array<{
|
|
|
|
uuid: ServiceIdString;
|
|
|
|
fingerprint: string;
|
|
|
|
}>;
|
|
|
|
export type VerifyServiceIdResponseType = z.infer<
|
|
|
|
typeof verifyServiceIdResponse
|
|
|
|
>;
|
2022-08-19 18:05:31 +00:00
|
|
|
|
2022-10-18 17:12:02 +00:00
|
|
|
export type ReserveUsernameOptionsType = Readonly<{
|
2023-02-08 17:14:59 +00:00
|
|
|
hashes: ReadonlyArray<Uint8Array>;
|
2022-10-18 17:12:02 +00:00
|
|
|
abortSignal?: AbortSignal;
|
|
|
|
}>;
|
|
|
|
|
2023-07-20 03:14:08 +00:00
|
|
|
export type ReplaceUsernameLinkOptionsType = Readonly<{
|
|
|
|
encryptedUsername: Uint8Array;
|
|
|
|
}>;
|
|
|
|
|
2022-10-18 17:12:02 +00:00
|
|
|
export type ConfirmUsernameOptionsType = Readonly<{
|
2023-02-08 17:14:59 +00:00
|
|
|
hash: Uint8Array;
|
|
|
|
proof: Uint8Array;
|
2023-07-20 23:19:32 +00:00
|
|
|
encryptedUsername: Uint8Array;
|
2022-10-18 17:12:02 +00:00
|
|
|
abortSignal?: AbortSignal;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
const reserveUsernameResultZod = z.object({
|
2023-02-08 17:14:59 +00:00
|
|
|
usernameHash: z
|
|
|
|
.string()
|
|
|
|
.transform(x => Bytes.fromBase64(fromWebSafeBase64(x))),
|
2022-10-18 17:12:02 +00:00
|
|
|
});
|
|
|
|
export type ReserveUsernameResultType = z.infer<
|
|
|
|
typeof reserveUsernameResultZod
|
|
|
|
>;
|
|
|
|
|
2023-07-20 23:19:32 +00:00
|
|
|
const confirmUsernameResultZod = z.object({
|
|
|
|
usernameLinkHandle: z.string(),
|
|
|
|
});
|
|
|
|
export type ConfirmUsernameResultType = z.infer<
|
|
|
|
typeof confirmUsernameResultZod
|
|
|
|
>;
|
|
|
|
|
2023-07-20 03:14:08 +00:00
|
|
|
const replaceUsernameLinkResultZod = z.object({
|
|
|
|
usernameLinkHandle: z.string(),
|
|
|
|
});
|
|
|
|
export type ReplaceUsernameLinkResultType = z.infer<
|
|
|
|
typeof replaceUsernameLinkResultZod
|
|
|
|
>;
|
|
|
|
|
|
|
|
const resolveUsernameLinkResultZod = z.object({
|
2023-07-20 23:19:32 +00:00
|
|
|
usernameLinkEncryptedValue: z
|
|
|
|
.string()
|
|
|
|
.transform(x => Bytes.fromBase64(fromWebSafeBase64(x))),
|
2023-07-20 03:14:08 +00:00
|
|
|
});
|
|
|
|
export type ResolveUsernameLinkResultType = z.infer<
|
|
|
|
typeof resolveUsernameLinkResultZod
|
|
|
|
>;
|
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
export type CreateAccountOptionsType = Readonly<{
|
|
|
|
sessionId: string;
|
2022-10-05 00:50:07 +00:00
|
|
|
number: string;
|
|
|
|
code: string;
|
|
|
|
newPassword: string;
|
|
|
|
registrationId: number;
|
|
|
|
pniRegistrationId: number;
|
2023-08-29 00:41:32 +00:00
|
|
|
accessKey: Uint8Array;
|
|
|
|
aciPublicKey: Uint8Array;
|
|
|
|
pniPublicKey: Uint8Array;
|
|
|
|
aciSignedPreKey: UploadSignedPreKeyType;
|
|
|
|
pniSignedPreKey: UploadSignedPreKeyType;
|
|
|
|
aciPqLastResortPreKey: UploadSignedPreKeyType;
|
|
|
|
pniPqLastResortPreKey: UploadSignedPreKeyType;
|
2022-10-05 00:50:07 +00:00
|
|
|
}>;
|
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
const linkDeviceResultZod = z.object({
|
|
|
|
uuid: aciSchema,
|
|
|
|
pni: untaggedPniSchema,
|
|
|
|
deviceId: z.number(),
|
|
|
|
});
|
|
|
|
export type LinkDeviceResultType = z.infer<typeof linkDeviceResultZod>;
|
|
|
|
|
2023-02-08 00:55:12 +00:00
|
|
|
export type ReportMessageOptionsType = Readonly<{
|
2023-08-16 20:54:39 +00:00
|
|
|
senderAci: AciString;
|
2023-02-08 00:55:12 +00:00
|
|
|
serverGuid: string;
|
|
|
|
token?: string;
|
|
|
|
}>;
|
|
|
|
|
2023-02-27 22:34:43 +00:00
|
|
|
const artAuthZod = z.object({
|
|
|
|
username: z.string(),
|
|
|
|
password: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type ArtAuthType = z.infer<typeof artAuthZod>;
|
|
|
|
|
2023-05-04 20:58:53 +00:00
|
|
|
const attachmentV3Response = z.object({
|
|
|
|
cdn: z.literal(2),
|
|
|
|
key: z.string(),
|
|
|
|
headers: z.record(z.string()),
|
|
|
|
signedUploadLocation: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type AttachmentV3ResponseType = z.infer<typeof attachmentV3Response>;
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
export type ServerKeyCountType = {
|
|
|
|
count: number;
|
|
|
|
pqCount: number;
|
|
|
|
};
|
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
export type LinkDeviceOptionsType = Readonly<{
|
|
|
|
number: string;
|
|
|
|
verificationCode: string;
|
|
|
|
encryptedDeviceName?: string;
|
|
|
|
newPassword: string;
|
|
|
|
registrationId: number;
|
|
|
|
pniRegistrationId: number;
|
|
|
|
aciSignedPreKey: UploadSignedPreKeyType;
|
|
|
|
pniSignedPreKey: UploadSignedPreKeyType;
|
|
|
|
aciPqLastResortPreKey: UploadSignedPreKeyType;
|
|
|
|
pniPqLastResortPreKey: UploadSignedPreKeyType;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
const createAccountResultZod = z.object({
|
|
|
|
uuid: aciSchema,
|
|
|
|
pni: untaggedPniSchema,
|
|
|
|
});
|
|
|
|
export type CreateAccountResultType = z.infer<typeof createAccountResultZod>;
|
|
|
|
|
|
|
|
const verificationSessionZod = z.object({
|
|
|
|
id: z.string(),
|
|
|
|
allowedToRequestCode: z.boolean(),
|
|
|
|
verified: z.boolean(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type RequestVerificationResultType = Readonly<{
|
|
|
|
sessionId: string;
|
|
|
|
}>;
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
export type WebAPIType = {
|
2021-12-17 21:26:50 +00:00
|
|
|
startRegistration(): unknown;
|
|
|
|
finishRegistration(baton: unknown): void;
|
2023-01-24 19:17:00 +00:00
|
|
|
cancelInflightRequests: (reason: string) => void;
|
2022-08-18 20:44:53 +00:00
|
|
|
cdsLookup: (options: CdsLookupOptionsType) => Promise<CDSResponseType>;
|
2023-08-29 00:41:32 +00:00
|
|
|
createAccount: (
|
|
|
|
options: CreateAccountOptionsType
|
|
|
|
) => Promise<CreateAccountResultType>;
|
2020-09-09 02:25:05 +00:00
|
|
|
createGroup: (
|
2021-06-22 14:46:42 +00:00
|
|
|
group: Proto.IGroup,
|
2020-09-09 02:25:05 +00:00
|
|
|
options: GroupCredentialsType
|
|
|
|
) => Promise<void>;
|
2022-10-18 17:12:02 +00:00
|
|
|
deleteUsername: (abortSignal?: AbortSignal) => Promise<void>;
|
2022-11-09 02:38:19 +00:00
|
|
|
downloadOnboardingStories: (
|
|
|
|
version: string,
|
|
|
|
imageFiles: Array<string>
|
|
|
|
) => Promise<Array<Uint8Array>>;
|
2023-02-27 22:34:43 +00:00
|
|
|
getArtAuth: () => Promise<ArtAuthType>;
|
2023-06-14 20:51:49 +00:00
|
|
|
getAttachment: (
|
|
|
|
cdnKey: string,
|
|
|
|
cdnNumber?: number,
|
|
|
|
options?: {
|
|
|
|
disableRetries?: boolean;
|
|
|
|
timeout?: number;
|
|
|
|
}
|
|
|
|
) => Promise<Uint8Array>;
|
2023-10-30 16:24:28 +00:00
|
|
|
getAttachmentV2: (
|
|
|
|
cdnKey: string,
|
|
|
|
cdnNumber?: number,
|
|
|
|
options?: {
|
|
|
|
disableRetries?: boolean;
|
|
|
|
timeout?: number;
|
|
|
|
}
|
|
|
|
) => Promise<Readable>;
|
2021-09-24 00:49:05 +00:00
|
|
|
getAvatar: (path: string) => Promise<Uint8Array>;
|
2021-11-30 16:29:57 +00:00
|
|
|
getHasSubscription: (subscriberId: Uint8Array) => Promise<boolean>;
|
2021-06-22 14:46:42 +00:00
|
|
|
getGroup: (options: GroupCredentialsType) => Promise<Proto.Group>;
|
2021-01-29 22:16:48 +00:00
|
|
|
getGroupFromLink: (
|
2022-05-16 14:53:54 +00:00
|
|
|
inviteLinkPassword: string | undefined,
|
2021-01-29 22:16:48 +00:00
|
|
|
auth: GroupCredentialsType
|
2021-06-22 14:46:42 +00:00
|
|
|
) => Promise<Proto.GroupJoinInfo>;
|
2021-09-24 00:49:05 +00:00
|
|
|
getGroupAvatar: (key: string) => Promise<Uint8Array>;
|
2020-09-09 02:25:05 +00:00
|
|
|
getGroupCredentials: (
|
2022-07-08 20:46:25 +00:00
|
|
|
options: GetGroupCredentialsOptionsType
|
2022-07-28 16:35:29 +00:00
|
|
|
) => Promise<GetGroupCredentialsResultType>;
|
2020-11-13 19:57:55 +00:00
|
|
|
getGroupExternalCredential: (
|
|
|
|
options: GroupCredentialsType
|
2021-06-22 14:46:42 +00:00
|
|
|
) => Promise<Proto.GroupExternalCredential>;
|
2020-09-09 02:25:05 +00:00
|
|
|
getGroupLog: (
|
2022-02-04 21:42:20 +00:00
|
|
|
options: GetGroupLogOptionsType,
|
|
|
|
credentials: GroupCredentialsType
|
2020-09-09 02:25:05 +00:00
|
|
|
) => Promise<GroupLogResponseType>;
|
2021-09-24 00:49:05 +00:00
|
|
|
getIceServers: () => Promise<GetIceServersResultType>;
|
2023-08-10 16:43:33 +00:00
|
|
|
getKeysForServiceId: (
|
|
|
|
serviceId: ServiceIdString,
|
2020-04-13 17:37:29 +00:00
|
|
|
deviceId?: number
|
|
|
|
) => Promise<ServerKeysType>;
|
2023-08-10 16:43:33 +00:00
|
|
|
getKeysForServiceIdUnauth: (
|
|
|
|
serviceId: ServiceIdString,
|
2020-04-13 17:37:29 +00:00
|
|
|
deviceId?: number,
|
|
|
|
options?: { accessKey?: string }
|
|
|
|
) => Promise<ServerKeysType>;
|
2023-08-10 16:43:33 +00:00
|
|
|
getMyKeyCounts: (serviceIdKind: ServiceIdKind) => Promise<ServerKeyCountType>;
|
2022-11-09 02:38:19 +00:00
|
|
|
getOnboardingStoryManifest: () => Promise<{
|
|
|
|
version: string;
|
|
|
|
languages: Record<string, Array<string>>;
|
|
|
|
}>;
|
2020-04-15 23:12:28 +00:00
|
|
|
getProfile: (
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString,
|
2022-03-09 20:23:21 +00:00
|
|
|
options: GetProfileOptionsType
|
2021-09-22 00:58:03 +00:00
|
|
|
) => Promise<ProfileType>;
|
2023-02-08 17:14:59 +00:00
|
|
|
getAccountForUsername: (
|
|
|
|
options: GetAccountForUsernameOptionsType
|
|
|
|
) => Promise<GetAccountForUsernameResultType>;
|
2020-04-13 17:37:29 +00:00
|
|
|
getProfileUnauth: (
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString,
|
2022-03-09 20:23:21 +00:00
|
|
|
options: GetProfileUnauthOptionsType
|
2021-09-22 00:58:03 +00:00
|
|
|
) => Promise<ProfileType>;
|
2021-11-02 23:01:13 +00:00
|
|
|
getBadgeImageFile: (imageUrl: string) => Promise<Uint8Array>;
|
2023-10-06 21:31:17 +00:00
|
|
|
getSubscriptionConfiguration: (
|
2022-05-11 20:59:58 +00:00
|
|
|
userLanguages: ReadonlyArray<string>
|
|
|
|
) => Promise<unknown>;
|
2021-07-28 21:37:09 +00:00
|
|
|
getProvisioningResource: (
|
|
|
|
handler: IRequestHandler
|
|
|
|
) => Promise<WebSocketResource>;
|
2023-02-27 22:34:43 +00:00
|
|
|
getArtProvisioningSocket: (token: string) => Promise<WebSocket>;
|
2021-04-08 16:24:21 +00:00
|
|
|
getSenderCertificate: (
|
|
|
|
withUuid?: boolean
|
2021-09-24 00:49:05 +00:00
|
|
|
) => Promise<GetSenderCertificateResultType>;
|
|
|
|
getSticker: (packId: string, stickerId: number) => Promise<Uint8Array>;
|
2020-04-13 17:37:29 +00:00
|
|
|
getStickerPackManifest: (packId: string) => Promise<StickerPackManifestType>;
|
2020-07-10 18:28:49 +00:00
|
|
|
getStorageCredentials: MessageSender['getStorageCredentials'];
|
|
|
|
getStorageManifest: MessageSender['getStorageManifest'];
|
|
|
|
getStorageRecords: MessageSender['getStorageRecords'];
|
2020-09-28 23:46:31 +00:00
|
|
|
fetchLinkPreviewMetadata: (
|
|
|
|
href: string,
|
|
|
|
abortSignal: AbortSignal
|
|
|
|
) => Promise<null | linkPreviewFetch.LinkPreviewMetadata>;
|
|
|
|
fetchLinkPreviewImage: (
|
|
|
|
href: string,
|
|
|
|
abortSignal: AbortSignal
|
|
|
|
) => Promise<null | linkPreviewFetch.LinkPreviewImage>;
|
2023-08-29 00:41:32 +00:00
|
|
|
linkDevice: (options: LinkDeviceOptionsType) => Promise<LinkDeviceResultType>;
|
2020-04-13 17:37:29 +00:00
|
|
|
makeProxiedRequest: (
|
|
|
|
targetUrl: string,
|
|
|
|
options?: ProxiedRequestOptionsType
|
2021-09-24 00:49:05 +00:00
|
|
|
) => Promise<MakeProxiedRequestResultType>;
|
2020-11-13 19:57:55 +00:00
|
|
|
makeSfuRequest: (
|
|
|
|
targetUrl: string,
|
|
|
|
type: HTTPCodeType,
|
|
|
|
headers: HeaderListType,
|
2021-09-24 00:49:05 +00:00
|
|
|
body: Uint8Array | undefined
|
|
|
|
) => Promise<BytesWithDetailsType>;
|
2020-09-09 02:25:05 +00:00
|
|
|
modifyGroup: (
|
2021-06-22 14:46:42 +00:00
|
|
|
changes: Proto.GroupChange.IActions,
|
2021-01-29 22:16:48 +00:00
|
|
|
options: GroupCredentialsType,
|
|
|
|
inviteLinkBase64?: string
|
2021-06-22 14:46:42 +00:00
|
|
|
) => Promise<Proto.IGroupChange>;
|
2020-09-09 00:56:23 +00:00
|
|
|
modifyStorageRecords: MessageSender['modifyStorageRecords'];
|
2022-08-19 18:05:31 +00:00
|
|
|
postBatchIdentityCheck: (
|
2023-08-10 16:43:33 +00:00
|
|
|
elements: VerifyServiceIdRequestType
|
|
|
|
) => Promise<VerifyServiceIdResponseType>;
|
2023-04-20 16:31:59 +00:00
|
|
|
putEncryptedAttachment: (encryptedBin: Uint8Array) => Promise<string>;
|
2021-07-19 19:26:06 +00:00
|
|
|
putProfile: (
|
|
|
|
jsonData: ProfileRequestDataType
|
|
|
|
) => Promise<UploadAvatarHeadersType | undefined>;
|
2020-04-13 17:37:29 +00:00
|
|
|
putStickers: (
|
2021-09-24 00:49:05 +00:00
|
|
|
encryptedManifest: Uint8Array,
|
|
|
|
encryptedStickers: Array<Uint8Array>,
|
2020-04-13 17:37:29 +00:00
|
|
|
onProgress?: () => void
|
|
|
|
) => Promise<string>;
|
2022-10-18 17:12:02 +00:00
|
|
|
reserveUsername: (
|
|
|
|
options: ReserveUsernameOptionsType
|
|
|
|
) => Promise<ReserveUsernameResultType>;
|
2023-07-20 23:19:32 +00:00
|
|
|
confirmUsername(
|
|
|
|
options: ConfirmUsernameOptionsType
|
|
|
|
): Promise<ConfirmUsernameResultType>;
|
2023-07-20 03:14:08 +00:00
|
|
|
replaceUsernameLink: (
|
|
|
|
options: ReplaceUsernameLinkOptionsType
|
|
|
|
) => Promise<ReplaceUsernameLinkResultType>;
|
|
|
|
deleteUsernameLink: () => Promise<void>;
|
|
|
|
resolveUsernameLink: (
|
|
|
|
serverId: string
|
|
|
|
) => Promise<ResolveUsernameLinkResultType>;
|
2021-11-01 19:13:35 +00:00
|
|
|
registerCapabilities: (capabilities: CapabilitiesUploadType) => Promise<void>;
|
2023-08-10 16:43:33 +00:00
|
|
|
registerKeys: (
|
|
|
|
genKeys: UploadKeysType,
|
|
|
|
serviceIdKind: ServiceIdKind
|
|
|
|
) => Promise<void>;
|
2021-09-22 00:58:03 +00:00
|
|
|
registerSupportForUnauthenticatedDelivery: () => Promise<void>;
|
2023-02-08 00:55:12 +00:00
|
|
|
reportMessage: (options: ReportMessageOptionsType) => Promise<void>;
|
2023-08-29 00:41:32 +00:00
|
|
|
requestVerification: (
|
|
|
|
number: string,
|
|
|
|
captcha: string,
|
|
|
|
transport: VerificationTransport
|
|
|
|
) => Promise<RequestVerificationResultType>;
|
2023-08-10 16:43:33 +00:00
|
|
|
checkAccountExistence: (serviceId: ServiceIdString) => Promise<boolean>;
|
2020-04-13 17:37:29 +00:00
|
|
|
sendMessages: (
|
2023-08-10 16:43:33 +00:00
|
|
|
destination: ServiceIdString,
|
2021-09-28 23:38:55 +00:00
|
|
|
messageArray: ReadonlyArray<MessageType>,
|
2020-04-13 17:37:29 +00:00
|
|
|
timestamp: number,
|
2022-09-30 16:59:36 +00:00
|
|
|
options: { online?: boolean; story?: boolean; urgent?: boolean }
|
2020-04-13 17:37:29 +00:00
|
|
|
) => Promise<void>;
|
|
|
|
sendMessagesUnauth: (
|
2023-08-10 16:43:33 +00:00
|
|
|
destination: ServiceIdString,
|
2021-09-28 23:38:55 +00:00
|
|
|
messageArray: ReadonlyArray<MessageType>,
|
2020-04-13 17:37:29 +00:00
|
|
|
timestamp: number,
|
2022-09-30 16:59:36 +00:00
|
|
|
options: {
|
|
|
|
accessKey?: string;
|
|
|
|
online?: boolean;
|
|
|
|
story?: boolean;
|
|
|
|
urgent?: boolean;
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
) => Promise<void>;
|
2021-05-25 22:40:04 +00:00
|
|
|
sendWithSenderKey: (
|
2021-09-24 00:49:05 +00:00
|
|
|
payload: Uint8Array,
|
|
|
|
accessKeys: Uint8Array,
|
2021-05-25 22:40:04 +00:00
|
|
|
timestamp: number,
|
2022-07-01 16:55:13 +00:00
|
|
|
options: {
|
|
|
|
online?: boolean;
|
2022-10-07 17:02:08 +00:00
|
|
|
story?: boolean;
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent?: boolean;
|
|
|
|
}
|
2021-05-25 22:40:04 +00:00
|
|
|
) => Promise<MultiRecipient200ResponseType>;
|
2023-02-23 21:32:19 +00:00
|
|
|
setPhoneNumberDiscoverability: (newValue: boolean) => Promise<void>;
|
2020-04-13 17:37:29 +00:00
|
|
|
updateDeviceName: (deviceName: string) => Promise<void>;
|
2021-07-19 19:26:06 +00:00
|
|
|
uploadAvatar: (
|
|
|
|
uploadAvatarRequestHeaders: UploadAvatarHeadersType,
|
2021-09-24 00:49:05 +00:00
|
|
|
avatarData: Uint8Array
|
2021-07-19 19:26:06 +00:00
|
|
|
) => Promise<string>;
|
2020-09-09 02:25:05 +00:00
|
|
|
uploadGroupAvatar: (
|
2021-06-22 14:46:42 +00:00
|
|
|
avatarData: Uint8Array,
|
2020-09-09 02:25:05 +00:00
|
|
|
options: GroupCredentialsType
|
|
|
|
) => Promise<string>;
|
2021-09-24 00:49:05 +00:00
|
|
|
whoami: () => Promise<WhoamiResultType>;
|
2021-09-22 00:58:03 +00:00
|
|
|
sendChallengeResponse: (challengeResponse: ChallengeType) => Promise<void>;
|
2023-07-13 19:06:42 +00:00
|
|
|
getConfig: () => Promise<RemoteConfigResponseType>;
|
2021-07-23 17:23:50 +00:00
|
|
|
authenticate: (credentials: WebAPICredentials) => Promise<void>;
|
2021-09-15 18:44:27 +00:00
|
|
|
logout: () => Promise<void>;
|
2021-07-28 21:37:09 +00:00
|
|
|
getSocketStatus: () => SocketStatus;
|
|
|
|
registerRequestHandler: (handler: IRequestHandler) => void;
|
|
|
|
unregisterRequestHandler: (handler: IRequestHandler) => void;
|
2022-10-05 00:48:25 +00:00
|
|
|
onHasStoriesDisabledChange: (newValue: boolean) => void;
|
2021-07-28 21:37:09 +00:00
|
|
|
checkSockets: () => void;
|
|
|
|
onOnline: () => Promise<void>;
|
2022-10-05 00:48:25 +00:00
|
|
|
onOffline: () => void;
|
|
|
|
reconnect: () => Promise<void>;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
export type UploadSignedPreKeyType = {
|
2020-04-13 17:37:29 +00:00
|
|
|
keyId: number;
|
2021-09-24 00:49:05 +00:00
|
|
|
publicKey: Uint8Array;
|
|
|
|
signature: Uint8Array;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
2023-07-14 16:53:20 +00:00
|
|
|
export type UploadPreKeyType = {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: Uint8Array;
|
|
|
|
};
|
|
|
|
export type UploadKyberPreKeyType = UploadSignedPreKeyType;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
type SerializedSignedPreKeyType = Readonly<{
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
signature: string;
|
|
|
|
}>;
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
export type UploadKeysType = {
|
2021-09-24 00:49:05 +00:00
|
|
|
identityKey: Uint8Array;
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
// If a field is not provided, the server won't update its data.
|
|
|
|
preKeys?: Array<UploadPreKeyType>;
|
|
|
|
pqPreKeys?: Array<UploadSignedPreKeyType>;
|
|
|
|
pqLastResortPreKey?: UploadSignedPreKeyType;
|
|
|
|
signedPreKey?: UploadSignedPreKeyType;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type ServerKeysType = {
|
|
|
|
devices: Array<{
|
|
|
|
deviceId: number;
|
|
|
|
registrationId: number;
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
// We'll get a 404 if none of these keys are provided; we'll have at least one
|
|
|
|
preKey?: {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: Uint8Array;
|
|
|
|
};
|
|
|
|
signedPreKey?: {
|
2020-04-13 17:37:29 +00:00
|
|
|
keyId: number;
|
2021-09-24 00:49:05 +00:00
|
|
|
publicKey: Uint8Array;
|
|
|
|
signature: Uint8Array;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
2023-07-14 16:53:20 +00:00
|
|
|
pqPreKey?: {
|
2020-04-13 17:37:29 +00:00
|
|
|
keyId: number;
|
2021-09-24 00:49:05 +00:00
|
|
|
publicKey: Uint8Array;
|
2023-07-14 16:53:20 +00:00
|
|
|
signature: Uint8Array;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
}>;
|
2021-09-24 00:49:05 +00:00
|
|
|
identityKey: Uint8Array;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
export type ChallengeType = {
|
|
|
|
readonly type: 'recaptcha';
|
|
|
|
readonly token: string;
|
|
|
|
readonly captcha: string;
|
|
|
|
};
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
export type ProxiedRequestOptionsType = {
|
2021-09-24 00:49:05 +00:00
|
|
|
returnUint8Array?: boolean;
|
2020-04-13 17:37:29 +00:00
|
|
|
start?: number;
|
|
|
|
end?: number;
|
|
|
|
};
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
export type TopLevelType = {
|
|
|
|
multiRecipient200ResponseSchema: typeof multiRecipient200ResponseSchema;
|
|
|
|
multiRecipient409ResponseSchema: typeof multiRecipient409ResponseSchema;
|
|
|
|
multiRecipient410ResponseSchema: typeof multiRecipient410ResponseSchema;
|
|
|
|
initialize: (options: InitializeOptionsType) => WebAPIConnectType;
|
|
|
|
};
|
|
|
|
|
2023-01-24 19:17:00 +00:00
|
|
|
type InflightCallback = (error: Error) => unknown;
|
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
// We first set up the data that won't change during this session of the app
|
2020-04-13 17:37:29 +00:00
|
|
|
export function initialize({
|
2019-01-16 03:03:56 +00:00
|
|
|
url,
|
2020-07-07 00:56:56 +00:00
|
|
|
storageUrl,
|
2021-11-02 23:01:13 +00:00
|
|
|
updatesUrl,
|
2022-11-09 02:38:19 +00:00
|
|
|
resourcesUrl,
|
2023-02-27 22:34:43 +00:00
|
|
|
artCreatorUrl,
|
2022-06-15 01:15:33 +00:00
|
|
|
directoryConfig,
|
2020-04-17 22:51:39 +00:00
|
|
|
cdnUrlObject,
|
2019-01-16 03:03:56 +00:00
|
|
|
certificateAuthority,
|
|
|
|
contentProxyUrl,
|
|
|
|
proxyUrl,
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2020-04-13 17:37:29 +00:00
|
|
|
}: InitializeOptionsType): WebAPIConnectType {
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isString(url)) {
|
2018-05-26 01:01:56 +00:00
|
|
|
throw new Error('WebAPI.initialize: Invalid server url');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isString(storageUrl)) {
|
2020-07-07 00:56:56 +00:00
|
|
|
throw new Error('WebAPI.initialize: Invalid storageUrl');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isString(updatesUrl)) {
|
2021-11-02 23:01:13 +00:00
|
|
|
throw new Error('WebAPI.initialize: Invalid updatesUrl');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isString(resourcesUrl)) {
|
2022-11-09 02:38:19 +00:00
|
|
|
throw new Error('WebAPI.initialize: Invalid updatesUrl (general)');
|
|
|
|
}
|
2023-02-27 22:34:43 +00:00
|
|
|
if (!isString(artCreatorUrl)) {
|
|
|
|
throw new Error('WebAPI.initialize: Invalid artCreatorUrl');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isObject(cdnUrlObject)) {
|
2020-04-17 22:51:39 +00:00
|
|
|
throw new Error('WebAPI.initialize: Invalid cdnUrlObject');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isString(cdnUrlObject['0'])) {
|
2020-04-17 22:51:39 +00:00
|
|
|
throw new Error('WebAPI.initialize: Missing CDN 0 configuration');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isString(cdnUrlObject['2'])) {
|
2020-04-17 22:51:39 +00:00
|
|
|
throw new Error('WebAPI.initialize: Missing CDN 2 configuration');
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
2023-07-26 22:15:05 +00:00
|
|
|
if (!isString(cdnUrlObject['3'])) {
|
|
|
|
throw new Error('WebAPI.initialize: Missing CDN 3 configuration');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isString(certificateAuthority)) {
|
2018-05-26 01:01:56 +00:00
|
|
|
throw new Error('WebAPI.initialize: Invalid certificateAuthority');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isString(contentProxyUrl)) {
|
2019-01-16 03:03:56 +00:00
|
|
|
throw new Error('WebAPI.initialize: Invalid contentProxyUrl');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (proxyUrl && !isString(proxyUrl)) {
|
2020-10-12 22:08:43 +00:00
|
|
|
throw new Error('WebAPI.initialize: Invalid proxyUrl');
|
|
|
|
}
|
2023-01-12 20:58:53 +00:00
|
|
|
if (!isString(version)) {
|
2020-01-17 00:53:35 +00:00
|
|
|
throw new Error('WebAPI.initialize: Invalid version');
|
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
// Thanks to function-hoisting, we can put this return statement before all of the
|
|
|
|
// below function definitions.
|
|
|
|
return {
|
|
|
|
connect,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Then we connect to the server with user-specific information. This is the only API
|
|
|
|
// exposed to the browser context, ensuring that it can't connect to arbitrary
|
|
|
|
// locations.
|
2020-03-31 20:03:38 +00:00
|
|
|
function connect({
|
|
|
|
username: initialUsername,
|
|
|
|
password: initialPassword,
|
2021-09-15 18:44:27 +00:00
|
|
|
useWebSocket = true,
|
2022-10-05 00:48:25 +00:00
|
|
|
hasStoriesDisabled,
|
2021-08-19 00:13:32 +00:00
|
|
|
}: WebAPIConnectOptionsType) {
|
2018-05-26 01:01:56 +00:00
|
|
|
let username = initialUsername;
|
|
|
|
let password = initialPassword;
|
2019-05-16 22:32:11 +00:00
|
|
|
const PARSE_RANGE_HEADER = /\/(\d+)$/;
|
2021-11-11 22:43:05 +00:00
|
|
|
const PARSE_GROUP_LOG_RANGE_HEADER =
|
2022-02-04 21:42:20 +00:00
|
|
|
/^versions\s+(\d{1,10})-(\d{1,10})\/(\d{1,10})/;
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2021-12-17 21:26:50 +00:00
|
|
|
let activeRegistration: ExplodePromiseResultType<void> | undefined;
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
const socketManager = new SocketManager({
|
|
|
|
url,
|
2023-02-27 22:34:43 +00:00
|
|
|
artCreatorUrl,
|
2021-07-28 21:37:09 +00:00
|
|
|
certificateAuthority,
|
|
|
|
version,
|
|
|
|
proxyUrl,
|
2022-10-05 00:48:25 +00:00
|
|
|
hasStoriesDisabled,
|
2021-07-28 21:37:09 +00:00
|
|
|
});
|
|
|
|
|
2021-09-16 20:18:42 +00:00
|
|
|
socketManager.on('statusChange', () => {
|
|
|
|
window.Whisper.events.trigger('socketStatusChange');
|
|
|
|
});
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
socketManager.on('authError', () => {
|
|
|
|
window.Whisper.events.trigger('unlinkAndDisconnect');
|
|
|
|
});
|
|
|
|
|
2021-09-15 18:44:27 +00:00
|
|
|
if (useWebSocket) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void socketManager.authenticate({ username, password });
|
2021-08-19 00:13:32 +00:00
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2022-10-26 23:17:14 +00:00
|
|
|
const { directoryUrl, directoryMRENCLAVE } = directoryConfig;
|
2022-08-19 00:31:12 +00:00
|
|
|
|
2022-10-26 23:17:14 +00:00
|
|
|
const cds = new CDSI({
|
|
|
|
logger: log,
|
|
|
|
proxyUrl,
|
2022-06-15 01:15:33 +00:00
|
|
|
|
2022-10-26 23:17:14 +00:00
|
|
|
url: directoryUrl,
|
|
|
|
mrenclave: directoryMRENCLAVE,
|
|
|
|
certificateAuthority,
|
|
|
|
version,
|
2022-06-15 01:15:33 +00:00
|
|
|
|
2022-10-26 23:17:14 +00:00
|
|
|
async getAuth() {
|
|
|
|
return (await _ajax({
|
|
|
|
call: 'directoryAuthV2',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
|
|
|
})) as CDSAuthType;
|
|
|
|
},
|
|
|
|
});
|
2021-11-08 23:32:31 +00:00
|
|
|
|
2023-01-24 19:17:00 +00:00
|
|
|
const inflightRequests = new Set<(error: Error) => unknown>();
|
|
|
|
function registerInflightRequest(request: InflightCallback) {
|
|
|
|
inflightRequests.add(request);
|
|
|
|
}
|
|
|
|
function unregisterInFlightRequest(request: InflightCallback) {
|
|
|
|
inflightRequests.delete(request);
|
|
|
|
}
|
|
|
|
function cancelInflightRequests(reason: string) {
|
|
|
|
const logId = `cancelInflightRequests/${reason}`;
|
|
|
|
log.warn(`${logId}: Cancelling ${inflightRequests.size} requests`);
|
|
|
|
for (const request of inflightRequests) {
|
|
|
|
try {
|
|
|
|
request(new Error(`${logId}: Cancelled!`));
|
|
|
|
} catch (error: unknown) {
|
|
|
|
log.error(
|
|
|
|
`${logId}: Failed to cancel request: ${toLogFormat(error)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inflightRequests.clear();
|
|
|
|
log.warn(`${logId}: Done`);
|
|
|
|
}
|
|
|
|
|
2023-12-12 22:57:09 +00:00
|
|
|
let fetchAgent: Agent;
|
2021-10-14 22:49:36 +00:00
|
|
|
if (proxyUrl) {
|
2023-12-12 22:57:09 +00:00
|
|
|
fetchAgent = createProxyAgent(proxyUrl);
|
2021-10-14 22:49:36 +00:00
|
|
|
} else {
|
2023-12-12 22:57:09 +00:00
|
|
|
fetchAgent = createHTTPSAgent({
|
|
|
|
keepAlive: false,
|
|
|
|
maxCachedSessions: 0,
|
|
|
|
});
|
2021-10-14 22:49:36 +00:00
|
|
|
}
|
2023-12-12 22:57:09 +00:00
|
|
|
const fetchForLinkPreviews: linkPreviewFetch.FetchFn = (href, init) =>
|
|
|
|
fetch(href, { ...init, agent: fetchAgent });
|
2021-10-14 22:49:36 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
// Thanks, function hoisting!
|
|
|
|
return {
|
2021-07-23 17:23:50 +00:00
|
|
|
authenticate,
|
2023-01-24 19:17:00 +00:00
|
|
|
cancelInflightRequests,
|
2022-08-18 20:44:53 +00:00
|
|
|
cdsLookup,
|
2021-11-30 19:33:51 +00:00
|
|
|
checkAccountExistence,
|
2022-11-09 02:38:19 +00:00
|
|
|
checkSockets,
|
2023-08-29 00:41:32 +00:00
|
|
|
createAccount,
|
2022-11-09 02:38:19 +00:00
|
|
|
confirmUsername,
|
2020-09-09 02:25:05 +00:00
|
|
|
createGroup,
|
2021-11-01 19:13:35 +00:00
|
|
|
deleteUsername,
|
2023-07-20 03:14:08 +00:00
|
|
|
deleteUsernameLink,
|
2022-11-09 02:38:19 +00:00
|
|
|
downloadOnboardingStories,
|
2021-01-29 22:16:48 +00:00
|
|
|
fetchLinkPreviewImage,
|
|
|
|
fetchLinkPreviewMetadata,
|
2022-11-09 02:38:19 +00:00
|
|
|
finishRegistration,
|
|
|
|
getAccountForUsername,
|
2023-02-27 22:34:43 +00:00
|
|
|
getArtAuth,
|
|
|
|
getArtProvisioningSocket,
|
2018-05-26 01:01:56 +00:00
|
|
|
getAttachment,
|
2023-10-30 16:24:28 +00:00
|
|
|
getAttachmentV2,
|
2018-05-26 01:01:56 +00:00
|
|
|
getAvatar,
|
2022-11-09 02:38:19 +00:00
|
|
|
getBadgeImageFile,
|
2020-09-09 02:25:05 +00:00
|
|
|
getConfig,
|
|
|
|
getGroup,
|
|
|
|
getGroupAvatar,
|
|
|
|
getGroupCredentials,
|
2020-11-13 19:57:55 +00:00
|
|
|
getGroupExternalCredential,
|
2021-01-29 22:16:48 +00:00
|
|
|
getGroupFromLink,
|
2020-09-09 02:25:05 +00:00
|
|
|
getGroupLog,
|
2021-11-30 16:29:57 +00:00
|
|
|
getHasSubscription,
|
2020-06-04 18:16:19 +00:00
|
|
|
getIceServers,
|
2023-08-10 16:43:33 +00:00
|
|
|
getKeysForServiceId,
|
|
|
|
getKeysForServiceIdUnauth,
|
2023-07-14 16:53:20 +00:00
|
|
|
getMyKeyCounts,
|
2022-11-09 02:38:19 +00:00
|
|
|
getOnboardingStoryManifest,
|
2018-05-26 01:01:56 +00:00
|
|
|
getProfile,
|
2018-10-18 01:01:21 +00:00
|
|
|
getProfileUnauth,
|
2021-07-28 21:37:09 +00:00
|
|
|
getProvisioningResource,
|
2019-01-16 03:03:56 +00:00
|
|
|
getSenderCertificate,
|
2022-11-09 02:38:19 +00:00
|
|
|
getSocketStatus,
|
2019-05-16 22:32:11 +00:00
|
|
|
getSticker,
|
|
|
|
getStickerPackManifest,
|
2020-07-07 00:56:56 +00:00
|
|
|
getStorageCredentials,
|
|
|
|
getStorageManifest,
|
|
|
|
getStorageRecords,
|
2023-10-06 21:31:17 +00:00
|
|
|
getSubscriptionConfiguration,
|
2023-08-29 00:41:32 +00:00
|
|
|
linkDevice,
|
2022-11-09 02:38:19 +00:00
|
|
|
logout,
|
2019-01-16 03:03:56 +00:00
|
|
|
makeProxiedRequest,
|
2020-11-13 19:57:55 +00:00
|
|
|
makeSfuRequest,
|
2020-09-09 02:25:05 +00:00
|
|
|
modifyGroup,
|
2020-09-09 00:56:23 +00:00
|
|
|
modifyStorageRecords,
|
2022-11-09 02:38:19 +00:00
|
|
|
onHasStoriesDisabledChange,
|
|
|
|
onOffline,
|
|
|
|
onOnline,
|
2022-08-19 18:05:31 +00:00
|
|
|
postBatchIdentityCheck,
|
2023-04-20 16:31:59 +00:00
|
|
|
putEncryptedAttachment,
|
2021-07-19 19:26:06 +00:00
|
|
|
putProfile,
|
2019-12-17 20:25:57 +00:00
|
|
|
putStickers,
|
2022-11-09 02:38:19 +00:00
|
|
|
reconnect,
|
2020-09-09 02:25:05 +00:00
|
|
|
registerCapabilities,
|
2018-05-26 01:01:56 +00:00
|
|
|
registerKeys,
|
2022-11-09 02:38:19 +00:00
|
|
|
registerRequestHandler,
|
2019-01-16 03:03:56 +00:00
|
|
|
registerSupportForUnauthenticatedDelivery,
|
2023-07-20 03:14:08 +00:00
|
|
|
resolveUsernameLink,
|
|
|
|
replaceUsernameLink,
|
2021-05-27 20:17:05 +00:00
|
|
|
reportMessage,
|
2023-08-29 00:41:32 +00:00
|
|
|
requestVerification,
|
2022-11-09 02:38:19 +00:00
|
|
|
reserveUsername,
|
|
|
|
sendChallengeResponse,
|
2018-05-26 01:01:56 +00:00
|
|
|
sendMessages,
|
2018-10-18 01:01:21 +00:00
|
|
|
sendMessagesUnauth,
|
2021-05-25 22:40:04 +00:00
|
|
|
sendWithSenderKey,
|
2023-02-23 21:32:19 +00:00
|
|
|
setPhoneNumberDiscoverability,
|
2021-12-17 21:26:50 +00:00
|
|
|
startRegistration,
|
2022-11-09 02:38:19 +00:00
|
|
|
unregisterRequestHandler,
|
2018-12-13 19:12:33 +00:00
|
|
|
updateDeviceName,
|
2021-07-19 19:26:06 +00:00
|
|
|
uploadAvatar,
|
2020-09-09 02:25:05 +00:00
|
|
|
uploadGroupAvatar,
|
2020-03-05 21:14:58 +00:00
|
|
|
whoami,
|
2018-05-26 01:01:56 +00:00
|
|
|
};
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
function _ajax(
|
|
|
|
param: AjaxOptionsType & { responseType?: 'bytes' }
|
|
|
|
): Promise<Uint8Array>;
|
|
|
|
function _ajax(
|
|
|
|
param: AjaxOptionsType & { responseType: 'byteswithdetails' }
|
|
|
|
): Promise<BytesWithDetailsType>;
|
2021-11-15 22:54:59 +00:00
|
|
|
function _ajax(
|
|
|
|
param: AjaxOptionsType & { responseType: 'stream' }
|
|
|
|
): Promise<Readable>;
|
2021-09-24 00:49:05 +00:00
|
|
|
function _ajax(
|
|
|
|
param: AjaxOptionsType & { responseType: 'json' }
|
|
|
|
): Promise<unknown>;
|
|
|
|
|
|
|
|
async function _ajax(param: AjaxOptionsType): Promise<unknown> {
|
2021-12-17 21:26:50 +00:00
|
|
|
if (
|
|
|
|
!param.unauthenticated &&
|
|
|
|
activeRegistration &&
|
|
|
|
!param.isRegistration
|
|
|
|
) {
|
|
|
|
log.info('WebAPI: request blocked by active registration');
|
|
|
|
const start = Date.now();
|
|
|
|
await activeRegistration.promise;
|
|
|
|
const duration = Date.now() - start;
|
|
|
|
log.info(`WebAPI: request unblocked after ${duration}ms`);
|
|
|
|
}
|
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
if (!param.urlParameters) {
|
|
|
|
param.urlParameters = '';
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2021-09-15 18:44:27 +00:00
|
|
|
const useWebSocketForEndpoint =
|
|
|
|
useWebSocket && WEBSOCKET_CALLS.has(param.call);
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const outerParams = {
|
2021-09-15 18:44:27 +00:00
|
|
|
socketManager: useWebSocketForEndpoint ? socketManager : undefined,
|
2020-09-09 02:25:05 +00:00
|
|
|
basicAuth: param.basicAuth,
|
2018-05-26 01:01:56 +00:00
|
|
|
certificateAuthority,
|
2020-07-07 00:56:56 +00:00
|
|
|
contentType: param.contentType || 'application/json; charset=utf-8',
|
2021-09-24 00:49:05 +00:00
|
|
|
data:
|
|
|
|
param.data ||
|
2021-09-28 23:38:55 +00:00
|
|
|
(param.jsonData ? JSON.stringify(param.jsonData) : undefined),
|
2021-05-25 22:40:04 +00:00
|
|
|
headers: param.headers,
|
2020-07-07 00:56:56 +00:00
|
|
|
host: param.host || url,
|
2021-11-30 19:33:51 +00:00
|
|
|
password: param.password ?? password,
|
2018-05-26 01:01:56 +00:00
|
|
|
path: URL_CALLS[param.call] + param.urlParameters,
|
|
|
|
proxyUrl,
|
|
|
|
responseType: param.responseType,
|
|
|
|
timeout: param.timeout,
|
|
|
|
type: param.httpType,
|
2021-11-30 19:33:51 +00:00
|
|
|
user: param.username ?? username,
|
2020-08-20 22:15:50 +00:00
|
|
|
redactUrl: param.redactUrl,
|
2021-05-17 18:29:37 +00:00
|
|
|
serverUrl: url,
|
2018-05-26 01:01:56 +00:00
|
|
|
validateResponse: param.validateResponse,
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2018-10-18 01:01:21 +00:00
|
|
|
unauthenticated: param.unauthenticated,
|
|
|
|
accessKey: param.accessKey,
|
2022-10-18 17:12:02 +00:00
|
|
|
abortSignal: param.abortSignal,
|
2021-09-24 00:49:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await _outerAjax(null, outerParams);
|
|
|
|
} catch (e) {
|
2021-09-22 00:58:03 +00:00
|
|
|
if (!(e instanceof HTTPError)) {
|
|
|
|
throw e;
|
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
const translatedError = translateError(e);
|
2021-06-09 22:28:54 +00:00
|
|
|
if (translatedError) {
|
|
|
|
throw translatedError;
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
function serializeSignedPreKey(
|
|
|
|
preKey?: UploadSignedPreKeyType
|
|
|
|
): SerializedSignedPreKeyType | undefined {
|
|
|
|
if (preKey == null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { keyId, publicKey, signature } = preKey;
|
|
|
|
|
|
|
|
return {
|
|
|
|
keyId,
|
|
|
|
publicKey: Bytes.toBase64(publicKey),
|
|
|
|
signature: Bytes.toBase64(signature),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
function serviceIdKindToQuery(kind: ServiceIdKind): string {
|
2021-11-30 19:33:51 +00:00
|
|
|
let value: string;
|
2023-08-10 16:43:33 +00:00
|
|
|
if (kind === ServiceIdKind.ACI) {
|
2021-11-30 19:33:51 +00:00
|
|
|
value = 'aci';
|
2023-08-10 16:43:33 +00:00
|
|
|
} else if (kind === ServiceIdKind.PNI) {
|
2021-11-30 19:33:51 +00:00
|
|
|
value = 'pni';
|
|
|
|
} else {
|
2023-08-10 16:43:33 +00:00
|
|
|
throw new Error(`Unsupported ServiceIdKind: ${kind}`);
|
2021-11-30 19:33:51 +00:00
|
|
|
}
|
|
|
|
return `identity=${value}`;
|
|
|
|
}
|
|
|
|
|
2022-01-13 21:25:20 +00:00
|
|
|
async function whoami(): Promise<WhoamiResultType> {
|
|
|
|
const response = await _ajax({
|
2020-03-05 21:14:58 +00:00
|
|
|
call: 'whoami',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
2022-01-13 21:25:20 +00:00
|
|
|
});
|
|
|
|
|
2022-07-18 22:32:00 +00:00
|
|
|
return whoamiResultZod.parse(response);
|
2020-03-05 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
async function sendChallengeResponse(challengeResponse: ChallengeType) {
|
2021-09-24 00:49:05 +00:00
|
|
|
await _ajax({
|
2021-05-06 00:09:29 +00:00
|
|
|
call: 'challenge',
|
|
|
|
httpType: 'PUT',
|
|
|
|
jsonData: challengeResponse,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-23 17:23:50 +00:00
|
|
|
async function authenticate({
|
|
|
|
username: newUsername,
|
|
|
|
password: newPassword,
|
|
|
|
}: WebAPICredentials) {
|
|
|
|
username = newUsername;
|
|
|
|
password = newPassword;
|
2021-07-28 21:37:09 +00:00
|
|
|
|
2021-09-15 18:44:27 +00:00
|
|
|
if (useWebSocket) {
|
2021-08-19 00:13:32 +00:00
|
|
|
await socketManager.authenticate({ username, password });
|
|
|
|
}
|
2021-07-28 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:44:27 +00:00
|
|
|
async function logout() {
|
|
|
|
username = '';
|
|
|
|
password = '';
|
|
|
|
|
|
|
|
if (useWebSocket) {
|
|
|
|
await socketManager.logout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
function getSocketStatus(): SocketStatus {
|
|
|
|
return socketManager.getStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkSockets(): void {
|
|
|
|
// Intentionally not awaiting
|
2022-12-21 18:41:48 +00:00
|
|
|
void socketManager.check();
|
2021-07-28 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function onOnline(): Promise<void> {
|
|
|
|
await socketManager.onOnline();
|
|
|
|
}
|
|
|
|
|
2022-10-05 00:48:25 +00:00
|
|
|
function onOffline(): void {
|
|
|
|
socketManager.onOffline();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function reconnect(): Promise<void> {
|
|
|
|
await socketManager.reconnect();
|
2021-07-28 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function registerRequestHandler(handler: IRequestHandler): void {
|
|
|
|
socketManager.registerRequestHandler(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
function unregisterRequestHandler(handler: IRequestHandler): void {
|
|
|
|
socketManager.unregisterRequestHandler(handler);
|
2021-07-23 17:23:50 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 00:48:25 +00:00
|
|
|
function onHasStoriesDisabledChange(newValue: boolean): void {
|
2022-12-21 18:41:48 +00:00
|
|
|
void socketManager.onHasStoriesDisabledChange(newValue);
|
2022-10-05 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
async function getConfig() {
|
2023-07-13 19:06:42 +00:00
|
|
|
const rawRes = await _ajax({
|
2020-05-27 21:37:06 +00:00
|
|
|
call: 'config',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
2023-07-13 19:06:42 +00:00
|
|
|
});
|
|
|
|
const res = remoteConfigResponseZod.parse(rawRes);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...res,
|
|
|
|
config: res.config.filter(
|
|
|
|
({ name }: { name: string }) =>
|
2023-07-19 00:03:39 +00:00
|
|
|
name.startsWith('desktop.') ||
|
|
|
|
name.startsWith('global.') ||
|
|
|
|
name.startsWith('cds.')
|
2023-07-13 19:06:42 +00:00
|
|
|
),
|
|
|
|
};
|
2020-05-27 21:37:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 22:26:37 +00:00
|
|
|
async function getSenderCertificate(omitE164?: boolean) {
|
2021-09-24 00:49:05 +00:00
|
|
|
return (await _ajax({
|
2018-10-18 01:01:21 +00:00
|
|
|
call: 'deliveryCert',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
2020-03-31 20:03:38 +00:00
|
|
|
validateResponse: { certificate: 'string' },
|
2021-01-27 17:10:00 +00:00
|
|
|
...(omitE164 ? { urlParameters: '?includeE164=false' } : {}),
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as GetSenderCertificateResultType;
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 00:56:56 +00:00
|
|
|
async function getStorageCredentials(): Promise<StorageServiceCredentials> {
|
2021-09-24 00:49:05 +00:00
|
|
|
return (await _ajax({
|
2020-07-07 00:56:56 +00:00
|
|
|
call: 'storageToken',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
|
|
|
schema: { username: 'string', password: 'string' },
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as StorageServiceCredentials;
|
2020-07-07 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 02:38:19 +00:00
|
|
|
async function getOnboardingStoryManifest() {
|
|
|
|
const res = await _ajax({
|
|
|
|
call: 'getOnboardingStoryManifest',
|
|
|
|
host: resourcesUrl,
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
|
|
|
return res as {
|
|
|
|
version: string;
|
|
|
|
languages: Record<string, Array<string>>;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-07 00:56:56 +00:00
|
|
|
async function getStorageManifest(
|
|
|
|
options: StorageServiceCallOptionsType = {}
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<Uint8Array> {
|
2020-07-07 00:56:56 +00:00
|
|
|
const { credentials, greaterThanVersion } = options;
|
|
|
|
|
2022-01-31 19:58:20 +00:00
|
|
|
const { data, response } = await _ajax({
|
2020-07-07 00:56:56 +00:00
|
|
|
call: 'storageManifest',
|
|
|
|
contentType: 'application/x-protobuf',
|
|
|
|
host: storageUrl,
|
|
|
|
httpType: 'GET',
|
2022-01-31 19:58:20 +00:00
|
|
|
responseType: 'byteswithdetails',
|
2020-07-07 00:56:56 +00:00
|
|
|
urlParameters: greaterThanVersion
|
|
|
|
? `/version/${greaterThanVersion}`
|
|
|
|
: '',
|
|
|
|
...credentials,
|
|
|
|
});
|
2022-01-31 19:58:20 +00:00
|
|
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
throw makeHTTPError(
|
|
|
|
'promiseAjax: error response',
|
|
|
|
response.status,
|
|
|
|
response.headers.raw(),
|
|
|
|
data,
|
|
|
|
new Error().stack
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2020-07-07 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getStorageRecords(
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Uint8Array,
|
2020-07-07 00:56:56 +00:00
|
|
|
options: StorageServiceCallOptionsType = {}
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<Uint8Array> {
|
2020-07-07 00:56:56 +00:00
|
|
|
const { credentials } = options;
|
|
|
|
|
|
|
|
return _ajax({
|
|
|
|
call: 'storageRead',
|
|
|
|
contentType: 'application/x-protobuf',
|
|
|
|
data,
|
|
|
|
host: storageUrl,
|
|
|
|
httpType: 'PUT',
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2020-07-07 00:56:56 +00:00
|
|
|
...credentials,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
async function modifyStorageRecords(
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Uint8Array,
|
2020-09-09 00:56:23 +00:00
|
|
|
options: StorageServiceCallOptionsType = {}
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<Uint8Array> {
|
2020-09-09 00:56:23 +00:00
|
|
|
const { credentials } = options;
|
|
|
|
|
|
|
|
return _ajax({
|
|
|
|
call: 'storageModify',
|
|
|
|
contentType: 'application/x-protobuf',
|
|
|
|
data,
|
|
|
|
host: storageUrl,
|
|
|
|
httpType: 'PUT',
|
|
|
|
// If we run into a conflict, the current manifest is returned -
|
2021-09-24 00:49:05 +00:00
|
|
|
// it will will be an Uint8Array at the response key on the Error
|
|
|
|
responseType: 'bytes',
|
2020-09-09 00:56:23 +00:00
|
|
|
...credentials,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function registerSupportForUnauthenticatedDelivery() {
|
2021-09-24 00:49:05 +00:00
|
|
|
await _ajax({
|
2018-10-18 01:01:21 +00:00
|
|
|
call: 'supportUnauthenticatedDelivery',
|
|
|
|
httpType: 'PUT',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
async function registerCapabilities(capabilities: CapabilitiesUploadType) {
|
2021-09-24 00:49:05 +00:00
|
|
|
await _ajax({
|
2020-03-05 21:14:58 +00:00
|
|
|
call: 'registerCapabilities',
|
|
|
|
httpType: 'PUT',
|
2020-09-10 22:03:17 +00:00
|
|
|
jsonData: capabilities,
|
2020-03-05 21:14:58 +00:00
|
|
|
});
|
|
|
|
}
|
2022-08-19 18:05:31 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
async function postBatchIdentityCheck(
|
|
|
|
elements: VerifyServiceIdRequestType
|
|
|
|
) {
|
2022-08-19 18:05:31 +00:00
|
|
|
const res = await _ajax({
|
|
|
|
data: JSON.stringify({ elements }),
|
|
|
|
call: 'batchIdentityCheck',
|
|
|
|
httpType: 'POST',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const result = verifyServiceIdResponse.safeParse(res);
|
2022-08-19 18:05:31 +00:00
|
|
|
|
|
|
|
if (result.success) {
|
|
|
|
return result.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.warn(
|
|
|
|
'WebAPI: invalid response from postBatchIdentityCheck',
|
|
|
|
toLogFormat(result.error)
|
|
|
|
);
|
|
|
|
|
|
|
|
throw result.error;
|
|
|
|
}
|
2020-03-05 21:14:58 +00:00
|
|
|
|
2020-04-15 23:12:28 +00:00
|
|
|
function getProfileUrl(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString,
|
2022-03-09 20:23:21 +00:00
|
|
|
{
|
|
|
|
profileKeyVersion,
|
|
|
|
profileKeyCredentialRequest,
|
|
|
|
}: GetProfileCommonOptionsType
|
2020-04-15 23:12:28 +00:00
|
|
|
) {
|
2023-08-10 16:43:33 +00:00
|
|
|
let profileUrl = `/${serviceId}`;
|
2022-03-09 20:23:21 +00:00
|
|
|
if (profileKeyVersion !== undefined) {
|
|
|
|
profileUrl += `/${profileKeyVersion}`;
|
|
|
|
if (profileKeyCredentialRequest !== undefined) {
|
|
|
|
profileUrl +=
|
|
|
|
`/${profileKeyCredentialRequest}` +
|
2022-09-21 16:18:48 +00:00
|
|
|
'?credentialType=expiringProfileKey';
|
2022-03-09 20:23:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strictAssert(
|
|
|
|
profileKeyCredentialRequest === undefined,
|
|
|
|
'getProfileUrl called without version, but with request'
|
|
|
|
);
|
2020-04-15 23:12:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 19:39:06 +00:00
|
|
|
return profileUrl;
|
2020-04-15 23:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getProfile(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString,
|
2022-03-09 20:23:21 +00:00
|
|
|
options: GetProfileOptionsType
|
2020-04-15 23:12:28 +00:00
|
|
|
) {
|
2022-03-09 20:23:21 +00:00
|
|
|
const { profileKeyVersion, profileKeyCredentialRequest, userLanguages } =
|
|
|
|
options;
|
2020-04-15 23:12:28 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return (await _ajax({
|
2018-05-26 01:01:56 +00:00
|
|
|
call: 'profile',
|
|
|
|
httpType: 'GET',
|
2023-08-10 16:43:33 +00:00
|
|
|
urlParameters: getProfileUrl(serviceId, options),
|
2021-11-02 23:01:13 +00:00
|
|
|
headers: {
|
|
|
|
'Accept-Language': formatAcceptLanguageHeader(userLanguages),
|
|
|
|
},
|
2018-05-26 01:01:56 +00:00
|
|
|
responseType: 'json',
|
2020-08-20 22:15:50 +00:00
|
|
|
redactUrl: _createRedactor(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2020-08-20 22:15:50 +00:00
|
|
|
profileKeyVersion,
|
|
|
|
profileKeyCredentialRequest
|
|
|
|
),
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as ProfileType;
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
2020-04-15 23:12:28 +00:00
|
|
|
|
2023-02-08 17:14:59 +00:00
|
|
|
async function getAccountForUsername({
|
|
|
|
hash,
|
|
|
|
}: GetAccountForUsernameOptionsType) {
|
|
|
|
const hashBase64 = toWebSafeBase64(Bytes.toBase64(hash));
|
|
|
|
return getAccountForUsernameResultZod.parse(
|
|
|
|
await _ajax({
|
|
|
|
call: 'username',
|
|
|
|
httpType: 'GET',
|
|
|
|
urlParameters: `/${hashBase64}`,
|
|
|
|
responseType: 'json',
|
|
|
|
redactUrl: _createRedactor(hashBase64),
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey: undefined,
|
|
|
|
})
|
|
|
|
);
|
2021-11-12 01:17:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 19:26:06 +00:00
|
|
|
async function putProfile(
|
|
|
|
jsonData: ProfileRequestDataType
|
|
|
|
): Promise<UploadAvatarHeadersType | undefined> {
|
|
|
|
const res = await _ajax({
|
|
|
|
call: 'profile',
|
|
|
|
httpType: 'PUT',
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'json',
|
2021-07-19 19:26:06 +00:00
|
|
|
jsonData,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return uploadAvatarHeadersZod.parse(res);
|
2021-07-19 19:26:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function getProfileUnauth(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString,
|
2022-03-09 20:23:21 +00:00
|
|
|
options: GetProfileUnauthOptionsType
|
2020-03-31 20:03:38 +00:00
|
|
|
) {
|
2020-04-15 23:12:28 +00:00
|
|
|
const {
|
|
|
|
accessKey,
|
|
|
|
profileKeyVersion,
|
|
|
|
profileKeyCredentialRequest,
|
2021-11-02 23:01:13 +00:00
|
|
|
userLanguages,
|
2020-04-15 23:12:28 +00:00
|
|
|
} = options;
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return (await _ajax({
|
2018-10-18 01:01:21 +00:00
|
|
|
call: 'profile',
|
|
|
|
httpType: 'GET',
|
2023-08-10 16:43:33 +00:00
|
|
|
urlParameters: getProfileUrl(serviceId, options),
|
2021-11-02 23:01:13 +00:00
|
|
|
headers: {
|
|
|
|
'Accept-Language': formatAcceptLanguageHeader(userLanguages),
|
|
|
|
},
|
2018-10-18 01:01:21 +00:00
|
|
|
responseType: 'json',
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey,
|
2020-08-20 22:15:50 +00:00
|
|
|
redactUrl: _createRedactor(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2020-08-20 22:15:50 +00:00
|
|
|
profileKeyVersion,
|
|
|
|
profileKeyCredentialRequest
|
|
|
|
),
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as ProfileType;
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2021-11-02 23:01:13 +00:00
|
|
|
async function getBadgeImageFile(
|
|
|
|
imageFileUrl: string
|
|
|
|
): Promise<Uint8Array> {
|
|
|
|
strictAssert(
|
|
|
|
isBadgeImageFileUrlValid(imageFileUrl, updatesUrl),
|
|
|
|
'getBadgeImageFile got an invalid URL. Was bad data saved?'
|
|
|
|
);
|
|
|
|
|
|
|
|
return _outerAjax(imageFileUrl, {
|
|
|
|
certificateAuthority,
|
|
|
|
contentType: 'application/octet-stream',
|
|
|
|
proxyUrl,
|
|
|
|
responseType: 'bytes',
|
|
|
|
timeout: 0,
|
|
|
|
type: 'GET',
|
|
|
|
redactUrl: (href: string) => {
|
|
|
|
const parsedUrl = maybeParseUrl(href);
|
|
|
|
if (!parsedUrl) {
|
|
|
|
return href;
|
|
|
|
}
|
|
|
|
const { pathname } = parsedUrl;
|
|
|
|
const pattern = RegExp(escapeRegExp(pathname), 'g');
|
|
|
|
return href.replace(pattern, `[REDACTED]${pathname.slice(-3)}`);
|
|
|
|
},
|
|
|
|
version,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-09 02:38:19 +00:00
|
|
|
async function downloadOnboardingStories(
|
|
|
|
manifestVersion: string,
|
|
|
|
imageFiles: Array<string>
|
|
|
|
): Promise<Array<Uint8Array>> {
|
|
|
|
return Promise.all(
|
|
|
|
imageFiles.map(fileName =>
|
|
|
|
_outerAjax(
|
|
|
|
`${resourcesUrl}/static/desktop/stories/onboarding/${manifestVersion}/${fileName}.jpg`,
|
|
|
|
{
|
|
|
|
certificateAuthority,
|
|
|
|
contentType: 'application/octet-stream',
|
|
|
|
proxyUrl,
|
|
|
|
responseType: 'bytes',
|
|
|
|
timeout: 0,
|
|
|
|
type: 'GET',
|
|
|
|
version,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-06 21:31:17 +00:00
|
|
|
async function getSubscriptionConfiguration(
|
2022-05-11 20:59:58 +00:00
|
|
|
userLanguages: ReadonlyArray<string>
|
|
|
|
): Promise<unknown> {
|
|
|
|
return _ajax({
|
2023-10-06 21:31:17 +00:00
|
|
|
call: 'subscriptionConfiguration',
|
2022-05-11 20:59:58 +00:00
|
|
|
httpType: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Accept-Language': formatAcceptLanguageHeader(userLanguages),
|
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function getAvatar(path: string) {
|
2018-05-26 01:01:56 +00:00
|
|
|
// Using _outerAJAX, since it's not hardcoded to the Signal Server. Unlike our
|
|
|
|
// attachment CDN, it uses our self-signed certificate, so we pass it in.
|
2021-09-24 00:49:05 +00:00
|
|
|
return _outerAjax(`${cdnUrlObject['0']}/${path}`, {
|
2018-05-26 01:01:56 +00:00
|
|
|
certificateAuthority,
|
|
|
|
contentType: 'application/octet-stream',
|
|
|
|
proxyUrl,
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2018-05-26 01:01:56 +00:00
|
|
|
timeout: 0,
|
|
|
|
type: 'GET',
|
2020-08-20 22:15:50 +00:00
|
|
|
redactUrl: (href: string) => {
|
|
|
|
const pattern = RegExp(escapeRegExp(path), 'g');
|
|
|
|
return href.replace(pattern, `[REDACTED]${path.slice(-3)}`);
|
|
|
|
},
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2021-09-24 00:49:05 +00:00
|
|
|
});
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 17:12:02 +00:00
|
|
|
async function deleteUsername(abortSignal?: AbortSignal) {
|
2021-11-01 19:13:35 +00:00
|
|
|
await _ajax({
|
|
|
|
call: 'username',
|
|
|
|
httpType: 'DELETE',
|
2022-10-18 17:12:02 +00:00
|
|
|
abortSignal,
|
2021-11-01 19:13:35 +00:00
|
|
|
});
|
|
|
|
}
|
2023-02-08 17:14:59 +00:00
|
|
|
|
2022-10-18 17:12:02 +00:00
|
|
|
async function reserveUsername({
|
2023-02-08 17:14:59 +00:00
|
|
|
hashes,
|
2022-10-18 17:12:02 +00:00
|
|
|
abortSignal,
|
|
|
|
}: ReserveUsernameOptionsType) {
|
|
|
|
const response = await _ajax({
|
2023-02-08 17:14:59 +00:00
|
|
|
call: 'reserveUsername',
|
2022-10-18 17:12:02 +00:00
|
|
|
httpType: 'PUT',
|
|
|
|
jsonData: {
|
2023-02-08 17:14:59 +00:00
|
|
|
usernameHashes: hashes.map(hash =>
|
|
|
|
toWebSafeBase64(Bytes.toBase64(hash))
|
|
|
|
),
|
2022-10-18 17:12:02 +00:00
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
abortSignal,
|
|
|
|
});
|
|
|
|
|
|
|
|
return reserveUsernameResultZod.parse(response);
|
|
|
|
}
|
|
|
|
async function confirmUsername({
|
2023-02-08 17:14:59 +00:00
|
|
|
hash,
|
|
|
|
proof,
|
2023-07-20 23:19:32 +00:00
|
|
|
encryptedUsername,
|
2022-10-18 17:12:02 +00:00
|
|
|
abortSignal,
|
|
|
|
}: ConfirmUsernameOptionsType) {
|
2023-07-20 23:19:32 +00:00
|
|
|
const response = await _ajax({
|
2022-10-18 17:12:02 +00:00
|
|
|
call: 'confirmUsername',
|
2021-11-01 19:13:35 +00:00
|
|
|
httpType: 'PUT',
|
2022-10-18 17:12:02 +00:00
|
|
|
jsonData: {
|
2023-02-08 17:14:59 +00:00
|
|
|
usernameHash: toWebSafeBase64(Bytes.toBase64(hash)),
|
|
|
|
zkProof: toWebSafeBase64(Bytes.toBase64(proof)),
|
2023-07-20 23:19:32 +00:00
|
|
|
encryptedUsername: toWebSafeBase64(Bytes.toBase64(encryptedUsername)),
|
2022-10-18 17:12:02 +00:00
|
|
|
},
|
2023-07-20 23:19:32 +00:00
|
|
|
responseType: 'json',
|
2022-10-18 17:12:02 +00:00
|
|
|
abortSignal,
|
2021-11-01 19:13:35 +00:00
|
|
|
});
|
2023-07-20 23:19:32 +00:00
|
|
|
return confirmUsernameResultZod.parse(response);
|
2021-11-01 19:13:35 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 03:14:08 +00:00
|
|
|
async function replaceUsernameLink({
|
|
|
|
encryptedUsername,
|
|
|
|
}: ReplaceUsernameLinkOptionsType): Promise<ReplaceUsernameLinkResultType> {
|
|
|
|
return replaceUsernameLinkResultZod.parse(
|
|
|
|
await _ajax({
|
|
|
|
call: 'usernameLink',
|
|
|
|
httpType: 'PUT',
|
|
|
|
responseType: 'json',
|
|
|
|
jsonData: {
|
2023-07-20 23:19:32 +00:00
|
|
|
usernameLinkEncryptedValue: toWebSafeBase64(
|
|
|
|
Bytes.toBase64(encryptedUsername)
|
|
|
|
),
|
2023-07-20 03:14:08 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteUsernameLink(): Promise<void> {
|
|
|
|
await _ajax({
|
|
|
|
call: 'usernameLink',
|
|
|
|
httpType: 'DELETE',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resolveUsernameLink(
|
|
|
|
serverId: string
|
|
|
|
): Promise<ResolveUsernameLinkResultType> {
|
|
|
|
return resolveUsernameLinkResultZod.parse(
|
|
|
|
await _ajax({
|
|
|
|
httpType: 'GET',
|
|
|
|
call: 'usernameLink',
|
|
|
|
urlParameters: `/${encodeURIComponent(serverId)}`,
|
|
|
|
responseType: 'json',
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey: undefined,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:55:12 +00:00
|
|
|
async function reportMessage({
|
2023-08-16 20:54:39 +00:00
|
|
|
senderAci,
|
2023-02-08 00:55:12 +00:00
|
|
|
serverGuid,
|
|
|
|
token,
|
|
|
|
}: ReportMessageOptionsType): Promise<void> {
|
|
|
|
const jsonData = { token };
|
|
|
|
|
2021-05-27 20:17:05 +00:00
|
|
|
await _ajax({
|
|
|
|
call: 'reportMessage',
|
|
|
|
httpType: 'POST',
|
2023-08-16 20:54:39 +00:00
|
|
|
urlParameters: urlPathFromComponents([senderAci, serverGuid]),
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2023-02-08 00:55:12 +00:00
|
|
|
jsonData,
|
2021-05-27 20:17:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
async function requestVerification(
|
|
|
|
number: string,
|
|
|
|
captcha: string,
|
|
|
|
transport: VerificationTransport
|
|
|
|
) {
|
|
|
|
// Create a new blank session using just a E164
|
|
|
|
let session = verificationSessionZod.parse(
|
|
|
|
await _ajax({
|
|
|
|
call: 'verificationSession',
|
|
|
|
httpType: 'POST',
|
|
|
|
responseType: 'json',
|
|
|
|
jsonData: {
|
|
|
|
number,
|
|
|
|
},
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey: undefined,
|
|
|
|
})
|
|
|
|
);
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
// Submit a captcha solution to the session
|
|
|
|
session = verificationSessionZod.parse(
|
|
|
|
await _ajax({
|
|
|
|
call: 'verificationSession',
|
|
|
|
httpType: 'PATCH',
|
|
|
|
urlParameters: `/${encodeURIComponent(session.id)}`,
|
|
|
|
responseType: 'json',
|
|
|
|
jsonData: {
|
|
|
|
captcha,
|
|
|
|
},
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey: undefined,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verify that captcha was accepted
|
|
|
|
if (!session.allowedToRequestCode) {
|
|
|
|
throw new Error('requestVerification: Not allowed to send code');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request an SMS or Voice confirmation
|
|
|
|
session = verificationSessionZod.parse(
|
|
|
|
await _ajax({
|
|
|
|
call: 'verificationSession',
|
|
|
|
httpType: 'POST',
|
|
|
|
urlParameters: `/${encodeURIComponent(session.id)}/code`,
|
|
|
|
responseType: 'json',
|
|
|
|
jsonData: {
|
|
|
|
client: 'ios',
|
|
|
|
transport:
|
|
|
|
transport === VerificationTransport.SMS ? 'sms' : 'voice',
|
|
|
|
},
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey: undefined,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Return sessionId to be used in `createAccount`
|
|
|
|
return { sessionId: session.id };
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
async function checkAccountExistence(serviceId: ServiceIdString) {
|
2021-11-30 19:33:51 +00:00
|
|
|
try {
|
|
|
|
await _ajax({
|
|
|
|
httpType: 'HEAD',
|
|
|
|
call: 'accountExistence',
|
2023-08-10 16:43:33 +00:00
|
|
|
urlParameters: `/${serviceId}`,
|
2021-11-30 19:33:51 +00:00
|
|
|
unauthenticated: true,
|
|
|
|
accessKey: undefined,
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof HTTPError && error.code === 404) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 21:26:50 +00:00
|
|
|
function startRegistration() {
|
|
|
|
strictAssert(
|
|
|
|
activeRegistration === undefined,
|
|
|
|
'Registration already in progress'
|
|
|
|
);
|
|
|
|
|
|
|
|
activeRegistration = explodePromise<void>();
|
|
|
|
log.info('WebAPI: starting registration');
|
|
|
|
|
|
|
|
return activeRegistration;
|
|
|
|
}
|
|
|
|
|
|
|
|
function finishRegistration(registration: unknown) {
|
|
|
|
strictAssert(activeRegistration !== undefined, 'No active registration');
|
|
|
|
strictAssert(
|
|
|
|
activeRegistration === registration,
|
|
|
|
'Invalid registration baton'
|
|
|
|
);
|
|
|
|
|
|
|
|
log.info('WebAPI: finishing registration');
|
|
|
|
const current = activeRegistration;
|
|
|
|
activeRegistration = undefined;
|
|
|
|
current.resolve();
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
async function _withNewCredentials<
|
|
|
|
Result extends { uuid: AciString; deviceId?: number }
|
|
|
|
>(
|
|
|
|
{ username: newUsername, password: newPassword }: WebAPICredentials,
|
|
|
|
callback: () => Promise<Result>
|
|
|
|
): Promise<Result> {
|
2021-08-04 20:12:35 +00:00
|
|
|
// Reset old websocket credentials and disconnect.
|
|
|
|
// AccountManager is our only caller and it will trigger
|
|
|
|
// `registration_done` which will update credentials.
|
2021-09-15 18:44:27 +00:00
|
|
|
await logout();
|
2021-08-04 20:12:35 +00:00
|
|
|
|
|
|
|
// Update REST credentials, though. We need them for the call below
|
2023-08-29 00:41:32 +00:00
|
|
|
username = newUsername;
|
2021-08-04 20:12:35 +00:00
|
|
|
password = newPassword;
|
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
const result = await callback();
|
|
|
|
|
|
|
|
const { uuid: aci = newUsername, deviceId = 1 } = result;
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2021-08-04 20:12:35 +00:00
|
|
|
// Set final REST credentials to let `registerKeys` succeed.
|
2023-08-29 00:41:32 +00:00
|
|
|
username = `${aci}.${deviceId}`;
|
2021-08-04 20:12:35 +00:00
|
|
|
password = newPassword;
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2023-08-29 00:41:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createAccount({
|
|
|
|
sessionId,
|
|
|
|
number,
|
|
|
|
code,
|
|
|
|
newPassword,
|
|
|
|
registrationId,
|
|
|
|
pniRegistrationId,
|
|
|
|
accessKey,
|
|
|
|
aciPublicKey,
|
|
|
|
pniPublicKey,
|
|
|
|
aciSignedPreKey,
|
|
|
|
pniSignedPreKey,
|
|
|
|
aciPqLastResortPreKey,
|
|
|
|
pniPqLastResortPreKey,
|
|
|
|
}: CreateAccountOptionsType) {
|
|
|
|
const session = verificationSessionZod.parse(
|
|
|
|
await _ajax({
|
|
|
|
isRegistration: true,
|
|
|
|
call: 'verificationSession',
|
|
|
|
httpType: 'PUT',
|
|
|
|
urlParameters: `/${encodeURIComponent(sessionId)}/code`,
|
|
|
|
responseType: 'json',
|
|
|
|
jsonData: {
|
|
|
|
code,
|
|
|
|
},
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey: undefined,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!session.verified) {
|
|
|
|
throw new Error('createAccount: invalid code');
|
|
|
|
}
|
|
|
|
|
|
|
|
const jsonData = {
|
|
|
|
sessionId: session.id,
|
|
|
|
accountAttributes: {
|
|
|
|
fetchesMessages: true,
|
|
|
|
registrationId,
|
|
|
|
pniRegistrationId,
|
|
|
|
capabilities: {
|
2023-10-31 23:21:39 +00:00
|
|
|
pni: true,
|
2023-08-29 00:41:32 +00:00
|
|
|
},
|
|
|
|
unidentifiedAccessKey: Bytes.toBase64(accessKey),
|
|
|
|
},
|
|
|
|
requireAtomic: true,
|
|
|
|
skipDeviceTransfer: true,
|
|
|
|
aciIdentityKey: Bytes.toBase64(aciPublicKey),
|
|
|
|
pniIdentityKey: Bytes.toBase64(pniPublicKey),
|
|
|
|
aciSignedPreKey: serializeSignedPreKey(aciSignedPreKey),
|
|
|
|
pniSignedPreKey: serializeSignedPreKey(pniSignedPreKey),
|
|
|
|
aciPqLastResortPreKey: serializeSignedPreKey(aciPqLastResortPreKey),
|
|
|
|
pniPqLastResortPreKey: serializeSignedPreKey(pniPqLastResortPreKey),
|
|
|
|
};
|
|
|
|
|
|
|
|
return _withNewCredentials(
|
|
|
|
{
|
|
|
|
username: number,
|
|
|
|
password: newPassword,
|
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
const responseJson = await _ajax({
|
|
|
|
isRegistration: true,
|
|
|
|
call: 'registration',
|
|
|
|
httpType: 'POST',
|
|
|
|
responseType: 'json',
|
|
|
|
jsonData,
|
|
|
|
});
|
|
|
|
|
|
|
|
return createAccountResultZod.parse(responseJson);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function linkDevice({
|
|
|
|
number,
|
|
|
|
verificationCode,
|
|
|
|
encryptedDeviceName,
|
|
|
|
newPassword,
|
|
|
|
registrationId,
|
|
|
|
pniRegistrationId,
|
|
|
|
aciSignedPreKey,
|
|
|
|
pniSignedPreKey,
|
|
|
|
aciPqLastResortPreKey,
|
|
|
|
pniPqLastResortPreKey,
|
|
|
|
}: LinkDeviceOptionsType) {
|
|
|
|
const jsonData = {
|
|
|
|
verificationCode,
|
|
|
|
accountAttributes: {
|
|
|
|
fetchesMessages: true,
|
|
|
|
name: encryptedDeviceName,
|
|
|
|
registrationId,
|
|
|
|
pniRegistrationId,
|
|
|
|
capabilities: {
|
2023-10-31 23:21:39 +00:00
|
|
|
pni: true,
|
2023-08-29 00:41:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
aciSignedPreKey: serializeSignedPreKey(aciSignedPreKey),
|
|
|
|
pniSignedPreKey: serializeSignedPreKey(pniSignedPreKey),
|
|
|
|
aciPqLastResortPreKey: serializeSignedPreKey(aciPqLastResortPreKey),
|
|
|
|
pniPqLastResortPreKey: serializeSignedPreKey(pniPqLastResortPreKey),
|
|
|
|
};
|
|
|
|
return _withNewCredentials(
|
|
|
|
{
|
|
|
|
username: number,
|
|
|
|
password: newPassword,
|
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
const responseJson = await _ajax({
|
|
|
|
isRegistration: true,
|
|
|
|
call: 'linkDevice',
|
|
|
|
httpType: 'PUT',
|
|
|
|
responseType: 'json',
|
|
|
|
jsonData,
|
|
|
|
});
|
|
|
|
|
|
|
|
return linkDeviceResultZod.parse(responseJson);
|
|
|
|
}
|
|
|
|
);
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function updateDeviceName(deviceName: string) {
|
2021-09-24 00:49:05 +00:00
|
|
|
await _ajax({
|
2018-12-13 19:12:33 +00:00
|
|
|
call: 'updateDeviceName',
|
|
|
|
httpType: 'PUT',
|
|
|
|
jsonData: {
|
|
|
|
deviceName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
async function getIceServers() {
|
2021-09-24 00:49:05 +00:00
|
|
|
return (await _ajax({
|
2020-06-04 18:16:19 +00:00
|
|
|
call: 'getIceServers',
|
|
|
|
httpType: 'GET',
|
2021-09-22 00:58:03 +00:00
|
|
|
responseType: 'json',
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as GetIceServersResultType;
|
2020-06-04 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type JSONSignedPreKeyType = {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
signature: string;
|
|
|
|
};
|
2023-07-14 16:53:20 +00:00
|
|
|
type JSONPreKeyType = {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
};
|
|
|
|
type JSONKyberPreKeyType = {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
signature: string;
|
|
|
|
};
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
type JSONKeysType = {
|
|
|
|
identityKey: string;
|
2023-07-14 16:53:20 +00:00
|
|
|
preKeys?: Array<JSONPreKeyType>;
|
|
|
|
pqPreKeys?: Array<JSONKyberPreKeyType>;
|
|
|
|
pqLastResortPreKey?: JSONKyberPreKeyType;
|
|
|
|
signedPreKey?: JSONSignedPreKeyType;
|
2020-03-31 20:03:38 +00:00
|
|
|
};
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
async function registerKeys(
|
|
|
|
genKeys: UploadKeysType,
|
|
|
|
serviceIdKind: ServiceIdKind
|
|
|
|
) {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = genKeys.preKeys?.map(key => ({
|
|
|
|
keyId: key.keyId,
|
|
|
|
publicKey: Bytes.toBase64(key.publicKey),
|
|
|
|
}));
|
|
|
|
const pqPreKeys = genKeys.pqPreKeys?.map(key => ({
|
2020-03-31 20:03:38 +00:00
|
|
|
keyId: key.keyId,
|
2021-09-24 00:49:05 +00:00
|
|
|
publicKey: Bytes.toBase64(key.publicKey),
|
2023-07-14 16:53:20 +00:00
|
|
|
signature: Bytes.toBase64(key.signature),
|
2020-03-31 20:03:38 +00:00
|
|
|
}));
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
if (
|
|
|
|
!preKeys?.length &&
|
|
|
|
!pqPreKeys?.length &&
|
|
|
|
!genKeys.pqLastResortPreKey &&
|
|
|
|
!genKeys.signedPreKey
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'registerKeys: None of the four potential key types were provided!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (preKeys && preKeys.length === 0) {
|
|
|
|
throw new Error('registerKeys: Attempting to upload zero preKeys!');
|
|
|
|
}
|
|
|
|
if (pqPreKeys && pqPreKeys.length === 0) {
|
|
|
|
throw new Error('registerKeys: Attempting to upload zero pqPreKeys!');
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
const keys: JSONKeysType = {
|
2021-09-24 00:49:05 +00:00
|
|
|
identityKey: Bytes.toBase64(genKeys.identityKey),
|
2020-03-31 20:03:38 +00:00
|
|
|
preKeys,
|
2023-07-14 16:53:20 +00:00
|
|
|
pqPreKeys,
|
2023-08-29 00:41:32 +00:00
|
|
|
pqLastResortPreKey: serializeSignedPreKey(genKeys.pqLastResortPreKey),
|
|
|
|
signedPreKey: serializeSignedPreKey(genKeys.signedPreKey),
|
2020-03-31 20:03:38 +00:00
|
|
|
};
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
await _ajax({
|
2021-12-17 21:26:50 +00:00
|
|
|
isRegistration: true,
|
2018-05-26 01:01:56 +00:00
|
|
|
call: 'keys',
|
2023-08-10 16:43:33 +00:00
|
|
|
urlParameters: `?${serviceIdKindToQuery(serviceIdKind)}`,
|
2018-05-26 01:01:56 +00:00
|
|
|
httpType: 'PUT',
|
|
|
|
jsonData: keys,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-23 21:32:19 +00:00
|
|
|
async function setPhoneNumberDiscoverability(newValue: boolean) {
|
|
|
|
await _ajax({
|
|
|
|
call: 'phoneNumberDiscoverability',
|
|
|
|
httpType: 'PUT',
|
|
|
|
jsonData: {
|
|
|
|
discoverableByPhoneNumber: newValue,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
async function getMyKeyCounts(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceIdKind: ServiceIdKind
|
2023-07-14 16:53:20 +00:00
|
|
|
): Promise<ServerKeyCountType> {
|
2021-09-24 00:49:05 +00:00
|
|
|
const result = (await _ajax({
|
2018-05-26 01:01:56 +00:00
|
|
|
call: 'keys',
|
2023-08-10 16:43:33 +00:00
|
|
|
urlParameters: `?${serviceIdKindToQuery(serviceIdKind)}`,
|
2018-05-26 01:01:56 +00:00
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
2023-07-14 16:53:20 +00:00
|
|
|
validateResponse: { count: 'number', pqCount: 'number' },
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as ServerKeyCountType;
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
return result;
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type ServerKeyResponseType = {
|
|
|
|
devices: Array<{
|
|
|
|
deviceId: number;
|
|
|
|
registrationId: number;
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
// We'll get a 404 if none of these keys are provided; we'll have at least one
|
|
|
|
preKey?: {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
};
|
|
|
|
signedPreKey?: {
|
2020-03-31 20:03:38 +00:00
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
signature: string;
|
|
|
|
};
|
2023-07-14 16:53:20 +00:00
|
|
|
pqPreKey?: {
|
2020-03-31 20:03:38 +00:00
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
2023-07-14 16:53:20 +00:00
|
|
|
signature: string;
|
2020-03-31 20:03:38 +00:00
|
|
|
};
|
|
|
|
}>;
|
|
|
|
identityKey: string;
|
|
|
|
};
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
function handleKeys(res: ServerKeyResponseType): ServerKeysType {
|
2018-10-18 01:01:21 +00:00
|
|
|
if (!Array.isArray(res.devices)) {
|
|
|
|
throw new Error('Invalid response');
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
const devices = res.devices.map(device => {
|
2018-10-18 01:01:21 +00:00
|
|
|
if (
|
|
|
|
!_validateResponse(device, { signedPreKey: 'object' }) ||
|
|
|
|
!_validateResponse(device.signedPreKey, {
|
|
|
|
publicKey: 'string',
|
|
|
|
signature: 'string',
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
throw new Error('Invalid signedPreKey');
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
let preKey;
|
2018-10-18 01:01:21 +00:00
|
|
|
if (device.preKey) {
|
|
|
|
if (
|
|
|
|
!_validateResponse(device, { preKey: 'object' }) ||
|
|
|
|
!_validateResponse(device.preKey, { publicKey: 'string' })
|
|
|
|
) {
|
|
|
|
throw new Error('Invalid preKey');
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
preKey = {
|
|
|
|
keyId: device.preKey.keyId,
|
2021-09-24 00:49:05 +00:00
|
|
|
publicKey: Bytes.fromBase64(device.preKey.publicKey),
|
2020-03-31 20:03:38 +00:00
|
|
|
};
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
deviceId: device.deviceId,
|
|
|
|
registrationId: device.registrationId,
|
2023-07-14 16:53:20 +00:00
|
|
|
...(preKey ? { preKey } : null),
|
|
|
|
...(device.signedPreKey
|
|
|
|
? {
|
|
|
|
signedPreKey: {
|
|
|
|
keyId: device.signedPreKey.keyId,
|
|
|
|
publicKey: Bytes.fromBase64(device.signedPreKey.publicKey),
|
|
|
|
signature: Bytes.fromBase64(device.signedPreKey.signature),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: null),
|
|
|
|
...(device.pqPreKey
|
|
|
|
? {
|
|
|
|
pqPreKey: {
|
|
|
|
keyId: device.pqPreKey.keyId,
|
|
|
|
publicKey: Bytes.fromBase64(device.pqPreKey.publicKey),
|
|
|
|
signature: Bytes.fromBase64(device.pqPreKey.signature),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: null),
|
2020-03-31 20:03:38 +00:00
|
|
|
};
|
2018-10-18 01:01:21 +00:00
|
|
|
});
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
devices,
|
2021-09-24 00:49:05 +00:00
|
|
|
identityKey: Bytes.fromBase64(res.identityKey),
|
2020-03-31 20:03:38 +00:00
|
|
|
};
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
async function getKeysForServiceId(
|
|
|
|
serviceId: ServiceIdString,
|
|
|
|
deviceId?: number
|
|
|
|
) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const keys = (await _ajax({
|
2018-05-26 01:01:56 +00:00
|
|
|
call: 'keys',
|
|
|
|
httpType: 'GET',
|
2023-08-10 16:43:33 +00:00
|
|
|
urlParameters: `/${serviceId}/${deviceId || '*'}?pq=true`,
|
2018-05-26 01:01:56 +00:00
|
|
|
responseType: 'json',
|
|
|
|
validateResponse: { identityKey: 'string', devices: 'object' },
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as ServerKeyResponseType;
|
|
|
|
return handleKeys(keys);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
async function getKeysForServiceIdUnauth(
|
|
|
|
serviceId: ServiceIdString,
|
2020-04-13 17:37:29 +00:00
|
|
|
deviceId?: number,
|
2020-03-31 20:03:38 +00:00
|
|
|
{ accessKey }: { accessKey?: string } = {}
|
2018-10-18 01:01:21 +00:00
|
|
|
) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const keys = (await _ajax({
|
2018-10-18 01:01:21 +00:00
|
|
|
call: 'keys',
|
|
|
|
httpType: 'GET',
|
2023-08-10 16:43:33 +00:00
|
|
|
urlParameters: `/${serviceId}/${deviceId || '*'}?pq=true`,
|
2018-10-18 01:01:21 +00:00
|
|
|
responseType: 'json',
|
|
|
|
validateResponse: { identityKey: 'string', devices: 'object' },
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey,
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as ServerKeyResponseType;
|
|
|
|
return handleKeys(keys);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function sendMessagesUnauth(
|
2023-08-10 16:43:33 +00:00
|
|
|
destination: ServiceIdString,
|
2021-09-28 23:38:55 +00:00
|
|
|
messages: ReadonlyArray<MessageType>,
|
2020-03-31 20:03:38 +00:00
|
|
|
timestamp: number,
|
2022-07-01 16:55:13 +00:00
|
|
|
{
|
|
|
|
accessKey,
|
|
|
|
online,
|
|
|
|
urgent = true,
|
2022-09-30 16:59:36 +00:00
|
|
|
story = false,
|
|
|
|
}: {
|
|
|
|
accessKey?: string;
|
|
|
|
online?: boolean;
|
|
|
|
story?: boolean;
|
|
|
|
urgent?: boolean;
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
) {
|
2022-07-01 16:55:13 +00:00
|
|
|
const jsonData = {
|
|
|
|
messages,
|
|
|
|
timestamp,
|
|
|
|
online: Boolean(online),
|
|
|
|
urgent,
|
|
|
|
};
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
await _ajax({
|
2018-10-18 01:01:21 +00:00
|
|
|
call: 'messages',
|
|
|
|
httpType: 'PUT',
|
2022-10-07 17:02:08 +00:00
|
|
|
urlParameters: `/${destination}?story=${booleanToString(story)}`,
|
2018-10-18 01:01:21 +00:00
|
|
|
jsonData,
|
|
|
|
responseType: 'json',
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey,
|
2018-05-26 01:01:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function sendMessages(
|
2023-08-10 16:43:33 +00:00
|
|
|
destination: ServiceIdString,
|
2021-09-28 23:38:55 +00:00
|
|
|
messages: ReadonlyArray<MessageType>,
|
2020-03-31 20:03:38 +00:00
|
|
|
timestamp: number,
|
2022-09-30 16:59:36 +00:00
|
|
|
{
|
|
|
|
online,
|
|
|
|
urgent = true,
|
|
|
|
story = false,
|
|
|
|
}: { online?: boolean; story?: boolean; urgent?: boolean }
|
2018-11-14 19:10:32 +00:00
|
|
|
) {
|
2022-07-01 16:55:13 +00:00
|
|
|
const jsonData = {
|
|
|
|
messages,
|
|
|
|
timestamp,
|
|
|
|
online: Boolean(online),
|
|
|
|
urgent,
|
|
|
|
};
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
await _ajax({
|
2018-05-26 01:01:56 +00:00
|
|
|
call: 'messages',
|
|
|
|
httpType: 'PUT',
|
2022-10-07 17:02:08 +00:00
|
|
|
urlParameters: `/${destination}?story=${booleanToString(story)}`,
|
2018-05-26 01:01:56 +00:00
|
|
|
jsonData,
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:55:13 +00:00
|
|
|
function booleanToString(value: boolean | undefined): string {
|
|
|
|
return value ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
async function sendWithSenderKey(
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Uint8Array,
|
|
|
|
accessKeys: Uint8Array,
|
2021-05-25 22:40:04 +00:00
|
|
|
timestamp: number,
|
2022-07-01 16:55:13 +00:00
|
|
|
{
|
|
|
|
online,
|
|
|
|
urgent = true,
|
2022-10-07 17:02:08 +00:00
|
|
|
story = false,
|
2022-07-01 16:55:13 +00:00
|
|
|
}: {
|
|
|
|
online?: boolean;
|
2022-10-07 17:02:08 +00:00
|
|
|
story?: boolean;
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent?: boolean;
|
|
|
|
}
|
2021-05-25 22:40:04 +00:00
|
|
|
): Promise<MultiRecipient200ResponseType> {
|
2022-07-01 16:55:13 +00:00
|
|
|
const onlineParam = `&online=${booleanToString(online)}`;
|
|
|
|
const urgentParam = `&urgent=${booleanToString(urgent)}`;
|
2022-10-07 17:02:08 +00:00
|
|
|
const storyParam = `&story=${booleanToString(story)}`;
|
2022-07-01 16:55:13 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const response = await _ajax({
|
2021-05-25 22:40:04 +00:00
|
|
|
call: 'multiRecipient',
|
|
|
|
httpType: 'PUT',
|
|
|
|
contentType: 'application/vnd.signal-messenger.mrm',
|
|
|
|
data,
|
2022-10-07 17:02:08 +00:00
|
|
|
urlParameters: `?ts=${timestamp}${onlineParam}${urgentParam}${storyParam}`,
|
2021-05-25 22:40:04 +00:00
|
|
|
responseType: 'json',
|
2021-08-04 20:12:35 +00:00
|
|
|
unauthenticated: true,
|
2021-09-24 00:49:05 +00:00
|
|
|
accessKey: Bytes.toBase64(accessKeys),
|
2021-05-25 22:40:04 +00:00
|
|
|
});
|
2021-09-24 00:49:05 +00:00
|
|
|
const parseResult = multiRecipient200ResponseSchema.safeParse(response);
|
|
|
|
if (parseResult.success) {
|
|
|
|
return parseResult.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.warn(
|
|
|
|
'WebAPI: invalid response from sendWithSenderKey',
|
|
|
|
toLogFormat(parseResult.error)
|
|
|
|
);
|
|
|
|
return response as MultiRecipient200ResponseType;
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function redactStickerUrl(stickerUrl: string) {
|
2019-05-16 22:32:11 +00:00
|
|
|
return stickerUrl.replace(
|
|
|
|
/(\/stickers\/)([^/]+)(\/)/,
|
2020-03-31 20:03:38 +00:00
|
|
|
(_, begin: string, packId: string, end: string) =>
|
|
|
|
`${begin}${redactPackId(packId)}${end}`
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async function getSticker(packId: string, stickerId: number) {
|
2020-08-31 16:29:22 +00:00
|
|
|
if (!isPackIdValid(packId)) {
|
|
|
|
throw new Error('getSticker: pack ID was invalid');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
return _outerAjax(
|
2020-04-17 22:51:39 +00:00
|
|
|
`${cdnUrlObject['0']}/stickers/${packId}/full/${stickerId}`,
|
|
|
|
{
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2020-04-17 22:51:39 +00:00
|
|
|
type: 'GET',
|
|
|
|
redactUrl: redactStickerUrl,
|
|
|
|
version,
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function getStickerPackManifest(packId: string) {
|
2020-08-31 16:29:22 +00:00
|
|
|
if (!isPackIdValid(packId)) {
|
|
|
|
throw new Error('getStickerPackManifest: pack ID was invalid');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
return _outerAjax(
|
2020-04-17 22:51:39 +00:00
|
|
|
`${cdnUrlObject['0']}/stickers/${packId}/manifest.proto`,
|
|
|
|
{
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2020-04-17 22:51:39 +00:00
|
|
|
type: 'GET',
|
|
|
|
redactUrl: redactStickerUrl,
|
|
|
|
version,
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 20:58:53 +00:00
|
|
|
type ServerV2AttachmentType = {
|
2020-03-31 20:03:38 +00:00
|
|
|
key: string;
|
|
|
|
credential: string;
|
|
|
|
acl: string;
|
|
|
|
algorithm: string;
|
|
|
|
date: string;
|
|
|
|
policy: string;
|
|
|
|
signature: string;
|
|
|
|
};
|
|
|
|
|
2019-12-17 20:25:57 +00:00
|
|
|
function makePutParams(
|
2020-03-31 20:03:38 +00:00
|
|
|
{
|
|
|
|
key,
|
|
|
|
credential,
|
|
|
|
acl,
|
|
|
|
algorithm,
|
|
|
|
date,
|
|
|
|
policy,
|
|
|
|
signature,
|
2023-05-04 20:58:53 +00:00
|
|
|
}: ServerV2AttachmentType,
|
2021-09-24 00:49:05 +00:00
|
|
|
encryptedBin: Uint8Array
|
2019-12-17 20:25:57 +00:00
|
|
|
) {
|
2019-05-08 20:14:52 +00:00
|
|
|
// Note: when using the boundary string in the POST body, it needs to be prefixed by
|
|
|
|
// an extra --, and the final boundary string at the end gets a -- prefix and a --
|
|
|
|
// suffix.
|
|
|
|
const boundaryString = `----------------${getGuid().replace(/-/g, '')}`;
|
|
|
|
const CRLF = '\r\n';
|
2020-03-31 20:03:38 +00:00
|
|
|
const getSection = (name: string, value: string) =>
|
2019-05-08 20:14:52 +00:00
|
|
|
[
|
|
|
|
`--${boundaryString}`,
|
|
|
|
`Content-Disposition: form-data; name="${name}"${CRLF}`,
|
|
|
|
value,
|
|
|
|
].join(CRLF);
|
|
|
|
|
|
|
|
const start = [
|
|
|
|
getSection('key', key),
|
|
|
|
getSection('x-amz-credential', credential),
|
|
|
|
getSection('acl', acl),
|
|
|
|
getSection('x-amz-algorithm', algorithm),
|
|
|
|
getSection('x-amz-date', date),
|
|
|
|
getSection('policy', policy),
|
|
|
|
getSection('x-amz-signature', signature),
|
|
|
|
getSection('Content-Type', 'application/octet-stream'),
|
|
|
|
`--${boundaryString}`,
|
|
|
|
'Content-Disposition: form-data; name="file"',
|
|
|
|
`Content-Type: application/octet-stream${CRLF}${CRLF}`,
|
|
|
|
].join(CRLF);
|
|
|
|
const end = `${CRLF}--${boundaryString}--${CRLF}`;
|
|
|
|
|
|
|
|
const startBuffer = Buffer.from(start, 'utf8');
|
|
|
|
const attachmentBuffer = Buffer.from(encryptedBin);
|
|
|
|
const endBuffer = Buffer.from(end, 'utf8');
|
|
|
|
|
|
|
|
const contentLength =
|
|
|
|
startBuffer.length + attachmentBuffer.length + endBuffer.length;
|
|
|
|
const data = Buffer.concat(
|
|
|
|
[startBuffer, attachmentBuffer, endBuffer],
|
|
|
|
contentLength
|
2018-05-26 01:01:56 +00:00
|
|
|
);
|
2019-05-08 20:14:52 +00:00
|
|
|
|
2019-12-17 20:25:57 +00:00
|
|
|
return {
|
|
|
|
data,
|
|
|
|
contentType: `multipart/form-data; boundary=${boundaryString}`,
|
|
|
|
headers: {
|
2020-03-31 20:03:38 +00:00
|
|
|
'Content-Length': contentLength.toString(),
|
2019-12-17 20:25:57 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function putStickers(
|
2021-09-24 00:49:05 +00:00
|
|
|
encryptedManifest: Uint8Array,
|
|
|
|
encryptedStickers: Array<Uint8Array>,
|
2020-03-31 20:03:38 +00:00
|
|
|
onProgress?: () => void
|
2019-12-17 20:25:57 +00:00
|
|
|
) {
|
|
|
|
// Get manifest and sticker upload parameters
|
2021-09-24 00:49:05 +00:00
|
|
|
const { packId, manifest, stickers } = (await _ajax({
|
2019-12-17 20:25:57 +00:00
|
|
|
call: 'getStickerPackUpload',
|
|
|
|
responseType: 'json',
|
2020-03-31 20:03:38 +00:00
|
|
|
httpType: 'GET',
|
2019-12-17 20:25:57 +00:00
|
|
|
urlParameters: `/${encryptedStickers.length}`,
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as {
|
|
|
|
packId: string;
|
2023-05-04 20:58:53 +00:00
|
|
|
manifest: ServerV2AttachmentType;
|
|
|
|
stickers: ReadonlyArray<ServerV2AttachmentType>;
|
2021-09-24 00:49:05 +00:00
|
|
|
};
|
2019-12-17 20:25:57 +00:00
|
|
|
|
|
|
|
// Upload manifest
|
|
|
|
const manifestParams = makePutParams(manifest, encryptedManifest);
|
|
|
|
// This is going to the CDN, not the service, so we use _outerAjax
|
2020-04-17 22:51:39 +00:00
|
|
|
await _outerAjax(`${cdnUrlObject['0']}/`, {
|
2019-12-17 20:25:57 +00:00
|
|
|
...manifestParams,
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
timeout: 0,
|
|
|
|
type: 'POST',
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2019-12-17 20:25:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Upload stickers
|
2021-11-23 22:01:03 +00:00
|
|
|
const queue = new PQueue({
|
|
|
|
concurrency: 3,
|
2022-06-27 16:46:43 +00:00
|
|
|
timeout: durations.MINUTE * 30,
|
2021-11-23 22:01:03 +00:00
|
|
|
throwOnTimeout: true,
|
|
|
|
});
|
2019-12-17 20:25:57 +00:00
|
|
|
await Promise.all(
|
2023-05-04 20:58:53 +00:00
|
|
|
stickers.map(async (sticker: ServerV2AttachmentType, index: number) => {
|
2020-03-31 20:03:38 +00:00
|
|
|
const stickerParams = makePutParams(
|
|
|
|
sticker,
|
|
|
|
encryptedStickers[index]
|
|
|
|
);
|
2019-12-19 23:27:02 +00:00
|
|
|
await queue.add(async () =>
|
2020-04-17 22:51:39 +00:00
|
|
|
_outerAjax(`${cdnUrlObject['0']}/`, {
|
2019-12-19 23:27:02 +00:00
|
|
|
...stickerParams,
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
timeout: 0,
|
|
|
|
type: 'POST',
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2019-12-19 23:27:02 +00:00
|
|
|
})
|
|
|
|
);
|
2019-12-17 20:25:57 +00:00
|
|
|
if (onProgress) {
|
|
|
|
onProgress();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Done!
|
|
|
|
return packId;
|
|
|
|
}
|
|
|
|
|
2023-06-14 20:51:49 +00:00
|
|
|
async function getAttachment(
|
|
|
|
cdnKey: string,
|
|
|
|
cdnNumber?: number,
|
|
|
|
options?: {
|
|
|
|
disableRetries?: boolean;
|
|
|
|
timeout?: number;
|
|
|
|
}
|
|
|
|
) {
|
2021-11-15 22:54:59 +00:00
|
|
|
const abortController = new AbortController();
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
const cdnUrl = isNumber(cdnNumber)
|
2023-07-26 22:15:05 +00:00
|
|
|
? cdnUrlObject[cdnNumber] ?? cdnUrlObject['0']
|
2021-06-17 17:15:10 +00:00
|
|
|
: cdnUrlObject['0'];
|
2019-12-17 20:25:57 +00:00
|
|
|
// This is going to the CDN, not the service, so we use _outerAjax
|
2021-11-15 22:54:59 +00:00
|
|
|
const stream = await _outerAjax(`${cdnUrl}/attachments/${cdnKey}`, {
|
2019-12-17 20:25:57 +00:00
|
|
|
certificateAuthority,
|
2023-06-14 20:51:49 +00:00
|
|
|
disableRetries: options?.disableRetries,
|
2019-12-17 20:25:57 +00:00
|
|
|
proxyUrl,
|
2021-11-15 22:54:59 +00:00
|
|
|
responseType: 'stream',
|
2023-06-14 20:51:49 +00:00
|
|
|
timeout: options?.timeout || 0,
|
2019-12-17 20:25:57 +00:00
|
|
|
type: 'GET',
|
2020-08-20 22:15:50 +00:00
|
|
|
redactUrl: _createRedactor(cdnKey),
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2021-11-15 22:54:59 +00:00
|
|
|
abortSignal: abortController.signal,
|
|
|
|
});
|
|
|
|
|
2023-01-24 19:17:00 +00:00
|
|
|
const streamPromise = getStreamWithTimeout(stream, {
|
2021-11-15 22:54:59 +00:00
|
|
|
name: `getAttachment(${cdnKey})`,
|
|
|
|
timeout: GET_ATTACHMENT_CHUNK_TIMEOUT,
|
|
|
|
abortController,
|
2021-09-24 00:49:05 +00:00
|
|
|
});
|
2023-01-24 19:17:00 +00:00
|
|
|
|
|
|
|
// Add callback to central store that would reject a promise
|
|
|
|
const { promise: cancelPromise, reject } = explodePromise<Uint8Array>();
|
|
|
|
const inflightRequest = (error: Error) => {
|
|
|
|
reject(error);
|
|
|
|
abortController.abort();
|
|
|
|
};
|
|
|
|
registerInflightRequest(inflightRequest);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return Promise.race([streamPromise, cancelPromise]);
|
|
|
|
} finally {
|
|
|
|
unregisterInFlightRequest(inflightRequest);
|
|
|
|
}
|
2019-12-17 20:25:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
async function getAttachmentV2(
|
|
|
|
cdnKey: string,
|
|
|
|
cdnNumber?: number,
|
|
|
|
options?: {
|
|
|
|
disableRetries?: boolean;
|
|
|
|
timeout?: number;
|
|
|
|
}
|
|
|
|
): Promise<Readable> {
|
|
|
|
const abortController = new AbortController();
|
|
|
|
|
|
|
|
const cdnUrl = isNumber(cdnNumber)
|
|
|
|
? cdnUrlObject[cdnNumber] ?? cdnUrlObject['0']
|
|
|
|
: cdnUrlObject['0'];
|
|
|
|
// This is going to the CDN, not the service, so we use _outerAjax
|
|
|
|
const downloadStream = await _outerAjax(
|
|
|
|
`${cdnUrl}/attachments/${cdnKey}`,
|
|
|
|
{
|
|
|
|
certificateAuthority,
|
|
|
|
disableRetries: options?.disableRetries,
|
|
|
|
proxyUrl,
|
|
|
|
responseType: 'stream',
|
|
|
|
timeout: options?.timeout || 0,
|
|
|
|
type: 'GET',
|
|
|
|
redactUrl: _createRedactor(cdnKey),
|
|
|
|
version,
|
|
|
|
abortSignal: abortController.signal,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const timeoutStream = getTimeoutStream({
|
|
|
|
name: `getAttachment(${cdnKey})`,
|
|
|
|
timeout: GET_ATTACHMENT_CHUNK_TIMEOUT,
|
|
|
|
abortController,
|
|
|
|
});
|
|
|
|
|
|
|
|
const combinedStream = downloadStream
|
|
|
|
// We do this manually; pipe() doesn't flow errors through the streams for us
|
|
|
|
.on('error', (error: Error) => {
|
|
|
|
timeoutStream.emit('error', error);
|
|
|
|
})
|
|
|
|
.pipe(timeoutStream);
|
|
|
|
|
|
|
|
const cancelRequest = (error: Error) => {
|
|
|
|
combinedStream.emit('error', error);
|
|
|
|
abortController.abort();
|
|
|
|
};
|
|
|
|
registerInflightRequest(cancelRequest);
|
|
|
|
|
|
|
|
combinedStream.on('done', () => {
|
|
|
|
unregisterInFlightRequest(cancelRequest);
|
|
|
|
});
|
|
|
|
|
|
|
|
return combinedStream;
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
async function putEncryptedAttachment(encryptedBin: Uint8Array) {
|
2023-05-04 20:58:53 +00:00
|
|
|
const response = attachmentV3Response.parse(
|
|
|
|
await _ajax({
|
|
|
|
call: 'attachmentId',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const { signedUploadLocation, key: cdnKey, headers } = response;
|
2019-12-17 20:25:57 +00:00
|
|
|
|
2023-05-04 20:58:53 +00:00
|
|
|
// This is going to the CDN, not the service, so we use _outerAjax
|
|
|
|
const { response: uploadResponse } = await _outerAjax(
|
|
|
|
signedUploadLocation,
|
|
|
|
{
|
|
|
|
responseType: 'byteswithdetails',
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
timeout: 0,
|
|
|
|
type: 'POST',
|
|
|
|
version,
|
|
|
|
headers,
|
2023-06-01 18:28:16 +00:00
|
|
|
redactUrl: () => {
|
|
|
|
const tmp = new URL(signedUploadLocation);
|
|
|
|
tmp.search = '';
|
|
|
|
tmp.pathname = '';
|
|
|
|
return `${tmp}[REDACTED]`;
|
|
|
|
},
|
2023-05-04 20:58:53 +00:00
|
|
|
}
|
|
|
|
);
|
2019-12-17 20:25:57 +00:00
|
|
|
|
2023-05-04 20:58:53 +00:00
|
|
|
const uploadLocation = uploadResponse.headers.get('location');
|
|
|
|
strictAssert(
|
|
|
|
uploadLocation,
|
|
|
|
'attachment v3 response header has no location'
|
|
|
|
);
|
2019-12-17 20:25:57 +00:00
|
|
|
|
2019-05-08 20:14:52 +00:00
|
|
|
// This is going to the CDN, not the service, so we use _outerAjax
|
2023-05-04 20:58:53 +00:00
|
|
|
await _outerAjax(uploadLocation, {
|
2019-05-08 20:14:52 +00:00
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
timeout: 0,
|
2023-05-04 20:58:53 +00:00
|
|
|
type: 'PUT',
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2023-05-04 20:58:53 +00:00
|
|
|
data: encryptedBin,
|
2023-08-16 17:44:33 +00:00
|
|
|
redactUrl: () => {
|
|
|
|
const tmp = new URL(uploadLocation);
|
|
|
|
tmp.search = '';
|
|
|
|
tmp.pathname = '';
|
|
|
|
return `${tmp}[REDACTED]`;
|
|
|
|
},
|
2019-05-08 20:14:52 +00:00
|
|
|
});
|
|
|
|
|
2023-05-04 20:58:53 +00:00
|
|
|
return cdnKey;
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
function getHeaderPadding() {
|
2022-10-05 16:35:56 +00:00
|
|
|
const max = randomInt(1, 64);
|
2019-05-16 22:32:11 +00:00
|
|
|
let characters = '';
|
2019-01-16 03:03:56 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
for (let i = 0; i < max; i += 1) {
|
2022-10-05 16:35:56 +00:00
|
|
|
characters += String.fromCharCode(randomInt(65, 122));
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return characters;
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 23:46:31 +00:00
|
|
|
async function fetchLinkPreviewMetadata(
|
|
|
|
href: string,
|
|
|
|
abortSignal: AbortSignal
|
|
|
|
) {
|
|
|
|
return linkPreviewFetch.fetchLinkPreviewMetadata(
|
2021-10-14 22:49:36 +00:00
|
|
|
fetchForLinkPreviews,
|
2020-09-28 23:46:31 +00:00
|
|
|
href,
|
|
|
|
abortSignal
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchLinkPreviewImage(
|
|
|
|
href: string,
|
|
|
|
abortSignal: AbortSignal
|
|
|
|
) {
|
2021-10-14 22:49:36 +00:00
|
|
|
return linkPreviewFetch.fetchLinkPreviewImage(
|
|
|
|
fetchForLinkPreviews,
|
|
|
|
href,
|
|
|
|
abortSignal
|
|
|
|
);
|
2020-09-28 23:46:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function makeProxiedRequest(
|
|
|
|
targetUrl: string,
|
|
|
|
options: ProxiedRequestOptionsType = {}
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<MakeProxiedRequestResultType> {
|
|
|
|
const { returnUint8Array, start, end } = options;
|
2020-03-31 20:03:38 +00:00
|
|
|
const headers: HeaderListType = {
|
2019-05-16 22:32:11 +00:00
|
|
|
'X-SignalPadding': getHeaderPadding(),
|
|
|
|
};
|
2019-01-16 03:03:56 +00:00
|
|
|
|
2023-01-12 20:58:53 +00:00
|
|
|
if (isNumber(start) && isNumber(end)) {
|
2019-05-16 22:32:11 +00:00
|
|
|
headers.Range = `bytes=${start}-${end}`;
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const result = await _outerAjax(targetUrl, {
|
|
|
|
responseType: returnUint8Array ? 'byteswithdetails' : undefined,
|
2019-01-16 03:03:56 +00:00
|
|
|
proxyUrl: contentProxyUrl,
|
|
|
|
type: 'GET',
|
|
|
|
redirect: 'follow',
|
2019-05-16 22:32:11 +00:00
|
|
|
redactUrl: () => '[REDACTED_URL]',
|
2019-01-16 03:03:56 +00:00
|
|
|
headers,
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2021-09-24 00:49:05 +00:00
|
|
|
});
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
if (!returnUint8Array) {
|
|
|
|
return result as Uint8Array;
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const { response } = result as BytesWithDetailsType;
|
2019-05-16 22:32:11 +00:00
|
|
|
if (!response.headers || !response.headers.get) {
|
|
|
|
throw new Error('makeProxiedRequest: Problem retrieving header value');
|
|
|
|
}
|
|
|
|
|
|
|
|
const range = response.headers.get('content-range');
|
2020-09-09 02:25:05 +00:00
|
|
|
const match = PARSE_RANGE_HEADER.exec(range || '');
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
if (!match || !match[1]) {
|
|
|
|
throw new Error(
|
|
|
|
`makeProxiedRequest: Unable to parse total size from ${range}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const totalSize = parseInt(match[1], 10);
|
|
|
|
|
|
|
|
return {
|
|
|
|
totalSize,
|
2021-09-24 00:49:05 +00:00
|
|
|
result: result as BytesWithDetailsType,
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 19:57:55 +00:00
|
|
|
async function makeSfuRequest(
|
|
|
|
targetUrl: string,
|
|
|
|
type: HTTPCodeType,
|
|
|
|
headers: HeaderListType,
|
2021-09-24 00:49:05 +00:00
|
|
|
body: Uint8Array | undefined
|
|
|
|
): Promise<BytesWithDetailsType> {
|
2020-11-13 19:57:55 +00:00
|
|
|
return _outerAjax(targetUrl, {
|
|
|
|
certificateAuthority,
|
|
|
|
data: body,
|
|
|
|
headers,
|
|
|
|
proxyUrl,
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'byteswithdetails',
|
2020-11-13 19:57:55 +00:00
|
|
|
timeout: 0,
|
|
|
|
type,
|
|
|
|
version,
|
2021-09-24 00:49:05 +00:00
|
|
|
});
|
2020-11-13 19:57:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
// Groups
|
|
|
|
|
|
|
|
function generateGroupAuth(
|
|
|
|
groupPublicParamsHex: string,
|
|
|
|
authCredentialPresentationHex: string
|
|
|
|
) {
|
2021-09-24 00:49:05 +00:00
|
|
|
return Bytes.toBase64(
|
|
|
|
Bytes.fromString(
|
|
|
|
`${groupPublicParamsHex}:${authCredentialPresentationHex}`
|
|
|
|
)
|
|
|
|
);
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CredentialResponseType = {
|
|
|
|
credentials: Array<GroupCredentialType>;
|
|
|
|
};
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
async function getGroupCredentials({
|
|
|
|
startDayInMs,
|
|
|
|
endDayInMs,
|
2022-07-28 16:35:29 +00:00
|
|
|
}: GetGroupCredentialsOptionsType): Promise<GetGroupCredentialsResultType> {
|
2022-07-08 20:46:25 +00:00
|
|
|
const startDayInSeconds = startDayInMs / durations.SECOND;
|
|
|
|
const endDayInSeconds = endDayInMs / durations.SECOND;
|
2021-09-24 00:49:05 +00:00
|
|
|
const response = (await _ajax({
|
2020-09-09 02:25:05 +00:00
|
|
|
call: 'getGroupCredentials',
|
2022-07-08 20:46:25 +00:00
|
|
|
urlParameters:
|
|
|
|
`?redemptionStartSeconds=${startDayInSeconds}&` +
|
2023-08-16 20:54:39 +00:00
|
|
|
`redemptionEndSeconds=${endDayInSeconds}&` +
|
|
|
|
'pniAsServiceId=true',
|
2020-09-09 02:25:05 +00:00
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
2021-09-24 00:49:05 +00:00
|
|
|
})) as CredentialResponseType;
|
2020-09-09 02:25:05 +00:00
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
return response;
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 19:57:55 +00:00
|
|
|
async function getGroupExternalCredential(
|
|
|
|
options: GroupCredentialsType
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.GroupExternalCredential> {
|
2020-11-13 19:57:55 +00:00
|
|
|
const basicAuth = generateGroupAuth(
|
|
|
|
options.groupPublicParamsHex,
|
|
|
|
options.authCredentialPresentationHex
|
|
|
|
);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const response = await _ajax({
|
2020-11-13 19:57:55 +00:00
|
|
|
basicAuth,
|
|
|
|
call: 'groupToken',
|
|
|
|
httpType: 'GET',
|
|
|
|
contentType: 'application/x-protobuf',
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2020-11-13 19:57:55 +00:00
|
|
|
host: storageUrl,
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption: true,
|
2020-11-13 19:57:55 +00:00
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return Proto.GroupExternalCredential.decode(response);
|
2020-11-13 19:57:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
function verifyAttributes(attributes: Proto.IAvatarUploadAttributes) {
|
2021-11-11 22:43:05 +00:00
|
|
|
const { key, credential, acl, algorithm, date, policy, signature } =
|
|
|
|
attributes;
|
2020-09-09 02:25:05 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
!key ||
|
|
|
|
!credential ||
|
|
|
|
!acl ||
|
|
|
|
!algorithm ||
|
|
|
|
!date ||
|
|
|
|
!policy ||
|
|
|
|
!signature
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'verifyAttributes: Missing value from AvatarUploadAttributes'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
key,
|
|
|
|
credential,
|
|
|
|
acl,
|
|
|
|
algorithm,
|
|
|
|
date,
|
|
|
|
policy,
|
|
|
|
signature,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:26:06 +00:00
|
|
|
async function uploadAvatar(
|
|
|
|
uploadAvatarRequestHeaders: UploadAvatarHeadersType,
|
2021-09-24 00:49:05 +00:00
|
|
|
avatarData: Uint8Array
|
2021-07-19 19:26:06 +00:00
|
|
|
): Promise<string> {
|
|
|
|
const verified = verifyAttributes(uploadAvatarRequestHeaders);
|
|
|
|
const { key } = verified;
|
|
|
|
|
|
|
|
const manifestParams = makePutParams(verified, avatarData);
|
|
|
|
|
|
|
|
await _outerAjax(`${cdnUrlObject['0']}/`, {
|
|
|
|
...manifestParams,
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
timeout: 0,
|
|
|
|
type: 'POST',
|
|
|
|
version,
|
|
|
|
});
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
async function uploadGroupAvatar(
|
2021-06-22 14:46:42 +00:00
|
|
|
avatarData: Uint8Array,
|
2020-09-09 02:25:05 +00:00
|
|
|
options: GroupCredentialsType
|
|
|
|
): Promise<string> {
|
|
|
|
const basicAuth = generateGroupAuth(
|
|
|
|
options.groupPublicParamsHex,
|
|
|
|
options.authCredentialPresentationHex
|
|
|
|
);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const response = await _ajax({
|
2020-09-09 02:25:05 +00:00
|
|
|
basicAuth,
|
|
|
|
call: 'getGroupAvatarUpload',
|
|
|
|
httpType: 'GET',
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2020-09-09 02:25:05 +00:00
|
|
|
host: storageUrl,
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption: true,
|
2020-09-09 02:25:05 +00:00
|
|
|
});
|
2021-09-24 00:49:05 +00:00
|
|
|
const attributes = Proto.AvatarUploadAttributes.decode(response);
|
2020-09-09 02:25:05 +00:00
|
|
|
|
|
|
|
const verified = verifyAttributes(attributes);
|
|
|
|
const { key } = verified;
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const manifestParams = makePutParams(verified, avatarData);
|
2020-09-09 02:25:05 +00:00
|
|
|
|
|
|
|
await _outerAjax(`${cdnUrlObject['0']}/`, {
|
|
|
|
...manifestParams,
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
timeout: 0,
|
|
|
|
type: 'POST',
|
|
|
|
version,
|
|
|
|
});
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
async function getGroupAvatar(key: string): Promise<Uint8Array> {
|
2020-09-09 02:25:05 +00:00
|
|
|
return _outerAjax(`${cdnUrlObject['0']}/${key}`, {
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2020-09-09 02:25:05 +00:00
|
|
|
timeout: 0,
|
|
|
|
type: 'GET',
|
|
|
|
version,
|
2021-10-20 21:32:38 +00:00
|
|
|
redactUrl: _createRedactor(key),
|
2021-09-24 00:49:05 +00:00
|
|
|
});
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function createGroup(
|
2021-06-22 14:46:42 +00:00
|
|
|
group: Proto.IGroup,
|
2020-09-09 02:25:05 +00:00
|
|
|
options: GroupCredentialsType
|
|
|
|
): Promise<void> {
|
|
|
|
const basicAuth = generateGroupAuth(
|
|
|
|
options.groupPublicParamsHex,
|
|
|
|
options.authCredentialPresentationHex
|
|
|
|
);
|
2021-06-22 14:46:42 +00:00
|
|
|
const data = Proto.Group.encode(group).finish();
|
2020-09-09 02:25:05 +00:00
|
|
|
|
|
|
|
await _ajax({
|
|
|
|
basicAuth,
|
|
|
|
call: 'groups',
|
2020-11-20 17:30:45 +00:00
|
|
|
contentType: 'application/x-protobuf',
|
2020-09-09 02:25:05 +00:00
|
|
|
data,
|
|
|
|
host: storageUrl,
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption: true,
|
2020-11-20 17:30:45 +00:00
|
|
|
httpType: 'PUT',
|
2020-09-09 02:25:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getGroup(
|
|
|
|
options: GroupCredentialsType
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.Group> {
|
2020-09-09 02:25:05 +00:00
|
|
|
const basicAuth = generateGroupAuth(
|
|
|
|
options.groupPublicParamsHex,
|
|
|
|
options.authCredentialPresentationHex
|
|
|
|
);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const response = await _ajax({
|
2020-09-09 02:25:05 +00:00
|
|
|
basicAuth,
|
|
|
|
call: 'groups',
|
|
|
|
contentType: 'application/x-protobuf',
|
|
|
|
host: storageUrl,
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption: true,
|
2020-11-20 17:30:45 +00:00
|
|
|
httpType: 'GET',
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2020-09-09 02:25:05 +00:00
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return Proto.Group.decode(response);
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
async function getGroupFromLink(
|
2022-05-16 14:53:54 +00:00
|
|
|
inviteLinkPassword: string | undefined,
|
2021-01-29 22:16:48 +00:00
|
|
|
auth: GroupCredentialsType
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.GroupJoinInfo> {
|
2021-01-29 22:16:48 +00:00
|
|
|
const basicAuth = generateGroupAuth(
|
|
|
|
auth.groupPublicParamsHex,
|
|
|
|
auth.authCredentialPresentationHex
|
|
|
|
);
|
2022-05-16 14:53:54 +00:00
|
|
|
const safeInviteLinkPassword = inviteLinkPassword
|
|
|
|
? toWebSafeBase64(inviteLinkPassword)
|
|
|
|
: undefined;
|
2021-01-29 22:16:48 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const response = await _ajax({
|
2021-01-29 22:16:48 +00:00
|
|
|
basicAuth,
|
|
|
|
call: 'groupsViaLink',
|
|
|
|
contentType: 'application/x-protobuf',
|
|
|
|
host: storageUrl,
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption: true,
|
2021-01-29 22:16:48 +00:00
|
|
|
httpType: 'GET',
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2022-05-16 14:53:54 +00:00
|
|
|
urlParameters: safeInviteLinkPassword
|
|
|
|
? `${safeInviteLinkPassword}`
|
|
|
|
: undefined,
|
2021-05-06 19:06:20 +00:00
|
|
|
redactUrl: _createRedactor(safeInviteLinkPassword),
|
2021-01-29 22:16:48 +00:00
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return Proto.GroupJoinInfo.decode(response);
|
2021-01-29 22:16:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
async function modifyGroup(
|
2021-06-22 14:46:42 +00:00
|
|
|
changes: Proto.GroupChange.IActions,
|
2021-01-29 22:16:48 +00:00
|
|
|
options: GroupCredentialsType,
|
|
|
|
inviteLinkBase64?: string
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.IGroupChange> {
|
2020-09-09 02:25:05 +00:00
|
|
|
const basicAuth = generateGroupAuth(
|
|
|
|
options.groupPublicParamsHex,
|
|
|
|
options.authCredentialPresentationHex
|
|
|
|
);
|
2021-06-22 14:46:42 +00:00
|
|
|
const data = Proto.GroupChange.Actions.encode(changes).finish();
|
2021-05-06 19:06:20 +00:00
|
|
|
const safeInviteLinkPassword = inviteLinkBase64
|
|
|
|
? toWebSafeBase64(inviteLinkBase64)
|
|
|
|
: undefined;
|
2020-09-09 02:25:05 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const response = await _ajax({
|
2020-09-09 02:25:05 +00:00
|
|
|
basicAuth,
|
|
|
|
call: 'groups',
|
|
|
|
contentType: 'application/x-protobuf',
|
2020-11-20 17:30:45 +00:00
|
|
|
data,
|
2020-09-09 02:25:05 +00:00
|
|
|
host: storageUrl,
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption: true,
|
2020-11-20 17:30:45 +00:00
|
|
|
httpType: 'PATCH',
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'bytes',
|
2021-05-06 19:06:20 +00:00
|
|
|
urlParameters: safeInviteLinkPassword
|
|
|
|
? `?inviteLinkPassword=${safeInviteLinkPassword}`
|
|
|
|
: undefined,
|
|
|
|
redactUrl: safeInviteLinkPassword
|
|
|
|
? _createRedactor(safeInviteLinkPassword)
|
2021-01-29 22:16:48 +00:00
|
|
|
: undefined,
|
2020-09-09 02:25:05 +00:00
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return Proto.GroupChange.decode(response);
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getGroupLog(
|
2022-02-04 21:42:20 +00:00
|
|
|
options: GetGroupLogOptionsType,
|
|
|
|
credentials: GroupCredentialsType
|
2020-09-09 02:25:05 +00:00
|
|
|
): Promise<GroupLogResponseType> {
|
|
|
|
const basicAuth = generateGroupAuth(
|
2022-02-04 21:42:20 +00:00
|
|
|
credentials.groupPublicParamsHex,
|
|
|
|
credentials.authCredentialPresentationHex
|
2020-09-09 02:25:05 +00:00
|
|
|
);
|
|
|
|
|
2022-02-04 21:42:20 +00:00
|
|
|
const {
|
|
|
|
startVersion,
|
|
|
|
includeFirstState,
|
|
|
|
includeLastState,
|
|
|
|
maxSupportedChangeEpoch,
|
|
|
|
} = options;
|
|
|
|
|
2022-01-27 00:02:23 +00:00
|
|
|
// If we don't know starting revision - fetch it from the server
|
|
|
|
if (startVersion === undefined) {
|
|
|
|
const { data: joinedData } = await _ajax({
|
|
|
|
basicAuth,
|
|
|
|
call: 'groupJoinedAtVersion',
|
|
|
|
contentType: 'application/x-protobuf',
|
|
|
|
host: storageUrl,
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption: true,
|
2022-01-27 00:02:23 +00:00
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'byteswithdetails',
|
|
|
|
});
|
|
|
|
|
|
|
|
const { joinedAtVersion } = Proto.Member.decode(joinedData);
|
|
|
|
|
2022-02-04 21:42:20 +00:00
|
|
|
return getGroupLog(
|
|
|
|
{
|
|
|
|
...options,
|
2023-11-07 21:31:59 +00:00
|
|
|
startVersion: joinedAtVersion ?? 0,
|
2022-02-04 21:42:20 +00:00
|
|
|
},
|
|
|
|
credentials
|
|
|
|
);
|
2022-01-27 00:02:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const withDetails = await _ajax({
|
2020-09-09 02:25:05 +00:00
|
|
|
basicAuth,
|
|
|
|
call: 'groupLog',
|
|
|
|
contentType: 'application/x-protobuf',
|
|
|
|
host: storageUrl,
|
2022-11-19 05:14:20 +00:00
|
|
|
disableSessionResumption: true,
|
2020-11-20 17:30:45 +00:00
|
|
|
httpType: 'GET',
|
2021-09-24 00:49:05 +00:00
|
|
|
responseType: 'byteswithdetails',
|
2022-02-04 21:42:20 +00:00
|
|
|
urlParameters:
|
|
|
|
`/${startVersion}?` +
|
|
|
|
`includeFirstState=${Boolean(includeFirstState)}&` +
|
|
|
|
`includeLastState=${Boolean(includeLastState)}&` +
|
|
|
|
`maxSupportedChangeEpoch=${Number(maxSupportedChangeEpoch)}`,
|
2020-09-09 02:25:05 +00:00
|
|
|
});
|
|
|
|
const { data, response } = withDetails;
|
2021-09-24 00:49:05 +00:00
|
|
|
const changes = Proto.GroupChanges.decode(data);
|
2020-09-09 02:25:05 +00:00
|
|
|
|
|
|
|
if (response && response.status === 206) {
|
|
|
|
const range = response.headers.get('Content-Range');
|
|
|
|
const match = PARSE_GROUP_LOG_RANGE_HEADER.exec(range || '');
|
|
|
|
|
2022-02-04 21:42:20 +00:00
|
|
|
const start = match ? parseInt(match[1], 10) : undefined;
|
|
|
|
const end = match ? parseInt(match[2], 10) : undefined;
|
|
|
|
const currentRevision = match ? parseInt(match[3], 10) : undefined;
|
2020-09-09 02:25:05 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
match &&
|
2023-01-12 20:58:53 +00:00
|
|
|
isNumber(start) &&
|
|
|
|
isNumber(end) &&
|
|
|
|
isNumber(currentRevision)
|
2020-09-09 02:25:05 +00:00
|
|
|
) {
|
|
|
|
return {
|
|
|
|
changes,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
currentRevision,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
changes,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-30 16:29:57 +00:00
|
|
|
async function getHasSubscription(
|
|
|
|
subscriberId: Uint8Array
|
|
|
|
): Promise<boolean> {
|
|
|
|
const formattedId = toWebSafeBase64(Bytes.toBase64(subscriberId));
|
|
|
|
const data = await _ajax({
|
|
|
|
call: 'subscriptions',
|
|
|
|
httpType: 'GET',
|
|
|
|
urlParameters: `/${formattedId}`,
|
|
|
|
responseType: 'json',
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey: undefined,
|
|
|
|
redactUrl: _createRedactor(formattedId),
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
isRecord(data) &&
|
|
|
|
isRecord(data.subscription) &&
|
|
|
|
Boolean(data.subscription.active)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
function getProvisioningResource(
|
|
|
|
handler: IRequestHandler
|
|
|
|
): Promise<WebSocketResource> {
|
|
|
|
return socketManager.getProvisioningResource(handler);
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
2020-09-04 01:25:19 +00:00
|
|
|
|
2023-02-27 22:34:43 +00:00
|
|
|
function getArtProvisioningSocket(token: string): Promise<WebSocket> {
|
|
|
|
return socketManager.connectExternalSocket({
|
|
|
|
url: `${artCreatorUrl}/api/socket?token=${token}`,
|
|
|
|
extraHeaders: {
|
|
|
|
origin: artCreatorUrl,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-18 20:44:53 +00:00
|
|
|
async function cdsLookup({
|
2021-12-06 22:54:20 +00:00
|
|
|
e164s,
|
2023-10-31 19:00:25 +00:00
|
|
|
acisAndAccessKeys = [],
|
2022-08-19 00:31:12 +00:00
|
|
|
returnAcisWithoutUaks,
|
2022-08-18 20:44:53 +00:00
|
|
|
}: CdsLookupOptionsType): Promise<CDSResponseType> {
|
2022-10-26 23:17:14 +00:00
|
|
|
return cds.request({
|
2021-11-12 20:45:30 +00:00
|
|
|
e164s,
|
2023-10-31 19:00:25 +00:00
|
|
|
acisAndAccessKeys,
|
2022-08-19 00:31:12 +00:00
|
|
|
returnAcisWithoutUaks,
|
2022-10-26 23:17:14 +00:00
|
|
|
});
|
2021-11-08 23:32:31 +00:00
|
|
|
}
|
2023-02-27 22:34:43 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Art
|
|
|
|
//
|
|
|
|
|
|
|
|
async function getArtAuth(): Promise<ArtAuthType> {
|
|
|
|
const response = await _ajax({
|
|
|
|
call: 'getArtAuth',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
|
|
|
return artAuthZod.parse(response);
|
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
}
|