Signal-Desktop/ts/util/encryptProfileData.ts

77 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-07-19 19:26:06 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import Crypto, { PaddedLengths } from '../textsecure/Crypto';
import { ConversationType } from '../state/ducks/conversations';
import { ProfileRequestDataType } from '../textsecure/WebAPI';
import { assert } from './assert';
import {
arrayBufferToBase64,
base64ToArrayBuffer,
bytesFromString,
} from '../Crypto';
import { deriveProfileKeyCommitment, deriveProfileKeyVersion } from './zkgroup';
const { encryptProfile, encryptProfileItemWithPadding } = Crypto;
export async function encryptProfileData(
conversation: ConversationType,
2021-08-06 00:17:05 +00:00
avatarBuffer?: ArrayBuffer
2021-07-19 19:26:06 +00:00
): Promise<[ProfileRequestDataType, ArrayBuffer | undefined]> {
const {
aboutEmoji,
aboutText,
familyName,
firstName,
profileKey,
uuid,
} = conversation;
assert(profileKey, 'profileKey');
assert(uuid, 'uuid');
const keyBuffer = base64ToArrayBuffer(profileKey);
const fullName = [firstName, familyName].filter(Boolean).join('\0');
const [
bytesName,
bytesAbout,
bytesAboutEmoji,
encryptedAvatarData,
] = await Promise.all([
encryptProfileItemWithPadding(
bytesFromString(fullName),
keyBuffer,
PaddedLengths.Name
),
aboutText
? encryptProfileItemWithPadding(
bytesFromString(aboutText),
keyBuffer,
PaddedLengths.About
)
: null,
aboutEmoji
? encryptProfileItemWithPadding(
bytesFromString(aboutEmoji),
keyBuffer,
PaddedLengths.AboutEmoji
)
: null,
2021-08-06 00:17:05 +00:00
avatarBuffer ? encryptProfile(avatarBuffer, keyBuffer) : undefined,
2021-07-19 19:26:06 +00:00
]);
const profileData = {
version: deriveProfileKeyVersion(profileKey, uuid),
name: arrayBufferToBase64(bytesName),
about: bytesAbout ? arrayBufferToBase64(bytesAbout) : null,
aboutEmoji: bytesAboutEmoji ? arrayBufferToBase64(bytesAboutEmoji) : null,
paymentAddress: window.storage.get('paymentAddress') || null,
2021-08-06 00:17:05 +00:00
avatar: Boolean(avatarBuffer),
2021-07-19 19:26:06 +00:00
commitment: deriveProfileKeyCommitment(profileKey, uuid),
};
return [profileData, encryptedAvatarData];
}