Signal-Desktop/ts/util/encryptProfileData.ts

73 lines
1.9 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 { ConversationType } from '../state/ducks/conversations';
import { ProfileRequestDataType } from '../textsecure/WebAPI';
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-07-19 19:26:06 +00:00
const {
aboutEmoji,
aboutText,
familyName,
firstName,
profileKey,
uuid,
} = conversation;
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-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];
}