2021-07-19 19:26:06 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from '../state/ducks/conversations';
|
|
|
|
import type { ProfileRequestDataType } from '../textsecure/WebAPI';
|
2021-07-19 19:26:06 +00:00
|
|
|
import { assert } from './assert';
|
2021-09-24 00:49:05 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2021-07-19 19:26:06 +00:00
|
|
|
import {
|
2021-09-24 00:49:05 +00:00
|
|
|
PaddedLengths,
|
|
|
|
encryptProfile,
|
|
|
|
encryptProfileItemWithPadding,
|
2021-07-19 19:26:06 +00:00
|
|
|
} from '../Crypto';
|
|
|
|
import { deriveProfileKeyCommitment, deriveProfileKeyVersion } from './zkgroup';
|
|
|
|
|
|
|
|
export async function encryptProfileData(
|
|
|
|
conversation: ConversationType,
|
2021-09-24 00:49:05 +00:00
|
|
|
avatarBuffer?: Uint8Array
|
|
|
|
): Promise<[ProfileRequestDataType, Uint8Array | undefined]> {
|
2021-11-24 18:48:25 +00:00
|
|
|
const {
|
|
|
|
aboutEmoji,
|
|
|
|
aboutText,
|
|
|
|
badges,
|
|
|
|
familyName,
|
|
|
|
firstName,
|
|
|
|
profileKey,
|
|
|
|
uuid,
|
|
|
|
} = conversation;
|
2021-07-19 19:26:06 +00:00
|
|
|
|
|
|
|
assert(profileKey, 'profileKey');
|
|
|
|
assert(uuid, 'uuid');
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const keyBuffer = Bytes.fromBase64(profileKey);
|
2021-07-19 19:26:06 +00:00
|
|
|
|
|
|
|
const fullName = [firstName, familyName].filter(Boolean).join('\0');
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const bytesName = encryptProfileItemWithPadding(
|
|
|
|
Bytes.fromString(fullName),
|
|
|
|
keyBuffer,
|
|
|
|
PaddedLengths.Name
|
|
|
|
);
|
|
|
|
|
|
|
|
const bytesAbout = aboutText
|
|
|
|
? encryptProfileItemWithPadding(
|
|
|
|
Bytes.fromString(aboutText),
|
|
|
|
keyBuffer,
|
|
|
|
PaddedLengths.About
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const bytesAboutEmoji = aboutEmoji
|
|
|
|
? encryptProfileItemWithPadding(
|
|
|
|
Bytes.fromString(aboutEmoji),
|
|
|
|
keyBuffer,
|
|
|
|
PaddedLengths.AboutEmoji
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const encryptedAvatarData = avatarBuffer
|
|
|
|
? encryptProfile(avatarBuffer, keyBuffer)
|
|
|
|
: undefined;
|
2021-07-19 19:26:06 +00:00
|
|
|
|
|
|
|
const profileData = {
|
|
|
|
version: deriveProfileKeyVersion(profileKey, uuid),
|
2021-09-24 00:49:05 +00:00
|
|
|
name: Bytes.toBase64(bytesName),
|
|
|
|
about: bytesAbout ? Bytes.toBase64(bytesAbout) : null,
|
|
|
|
aboutEmoji: bytesAboutEmoji ? Bytes.toBase64(bytesAboutEmoji) : null,
|
2021-11-24 18:48:25 +00:00
|
|
|
badgeIds: (badges || []).map(({ id }) => id),
|
2021-07-19 19:26:06 +00:00
|
|
|
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];
|
|
|
|
}
|