Files
libsignal/node/ts/zkgroup/profiles/ClientZkProfileOperations.ts
Jordan Rose 85686caa01 node: Combine Native.js and .d.ts into Native.ts
This allows the file to be checked by tsc, which would have caught
some of the missing type aliases sooner (now added to Native.ts.in).
Strictly speaking the behavior is slightly different: we have returned
to exporting many items individually instead of collecting them on a
single object.

Co-authored-by: Alex Bakon <akonradi@signal.org>
2025-10-15 17:50:37 -07:00

100 lines
3.2 KiB
TypeScript

//
// Copyright 2020-2022 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//
import { randomBytes } from 'node:crypto';
import { RANDOM_LENGTH } from '../internal/Constants.js';
import * as Native from '../../Native.js';
import ServerPublicParams from '../ServerPublicParams.js';
import GroupSecretParams from '../groups/GroupSecretParams.js';
import ExpiringProfileKeyCredential from './ExpiringProfileKeyCredential.js';
import ExpiringProfileKeyCredentialResponse from './ExpiringProfileKeyCredentialResponse.js';
import ProfileKey from './ProfileKey.js';
import ProfileKeyCredentialPresentation from './ProfileKeyCredentialPresentation.js';
import ProfileKeyCredentialRequestContext from './ProfileKeyCredentialRequestContext.js';
import { Aci } from '../../Address.js';
export default class ClientZkProfileOperations {
serverPublicParams: ServerPublicParams;
constructor(serverPublicParams: ServerPublicParams) {
this.serverPublicParams = serverPublicParams;
}
createProfileKeyCredentialRequestContext(
userId: Aci,
profileKey: ProfileKey
): ProfileKeyCredentialRequestContext {
const random = randomBytes(RANDOM_LENGTH);
return this.createProfileKeyCredentialRequestContextWithRandom(
random,
userId,
profileKey
);
}
createProfileKeyCredentialRequestContextWithRandom(
random: Uint8Array,
userId: Aci,
profileKey: ProfileKey
): ProfileKeyCredentialRequestContext {
return new ProfileKeyCredentialRequestContext(
Native.ServerPublicParams_CreateProfileKeyCredentialRequestContextDeterministic(
this.serverPublicParams,
random,
userId.getServiceIdFixedWidthBinary(),
profileKey.getContents()
)
);
}
receiveExpiringProfileKeyCredential(
profileKeyCredentialRequestContext: ProfileKeyCredentialRequestContext,
profileKeyCredentialResponse: ExpiringProfileKeyCredentialResponse,
now: Date = new Date()
): ExpiringProfileKeyCredential {
return new ExpiringProfileKeyCredential(
Native.ServerPublicParams_ReceiveExpiringProfileKeyCredential(
this.serverPublicParams,
profileKeyCredentialRequestContext.getContents(),
profileKeyCredentialResponse.getContents(),
Math.floor(now.getTime() / 1000)
)
);
}
createExpiringProfileKeyCredentialPresentation(
groupSecretParams: GroupSecretParams,
profileKeyCredential: ExpiringProfileKeyCredential
): ProfileKeyCredentialPresentation {
const random = randomBytes(RANDOM_LENGTH);
return this.createExpiringProfileKeyCredentialPresentationWithRandom(
random,
groupSecretParams,
profileKeyCredential
);
}
createExpiringProfileKeyCredentialPresentationWithRandom(
random: Uint8Array,
groupSecretParams: GroupSecretParams,
profileKeyCredential: ExpiringProfileKeyCredential
): ProfileKeyCredentialPresentation {
return new ProfileKeyCredentialPresentation(
Native.ServerPublicParams_CreateExpiringProfileKeyCredentialPresentationDeterministic(
this.serverPublicParams,
random,
groupSecretParams.getContents(),
profileKeyCredential.getContents()
)
);
}
}