signal-desktop/ts/util/checkOurPniIdentityKey.ts
2023-08-21 09:30:33 -07:00

30 lines
997 B
TypeScript

// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as log from '../logging/log';
import { constantTimeEqual } from '../Crypto';
import { strictAssert } from './assert';
export async function checkOurPniIdentityKey(): Promise<void> {
const { server } = window.textsecure;
strictAssert(server, 'WebAPI not ready');
const ourPni = window.storage.user.getCheckedPni();
const localKeyPair = await window.storage.protocol.getIdentityKeyPair(ourPni);
if (!localKeyPair) {
log.warn(
`checkOurPniIdentityKey: no local key pair for ${ourPni}, unlinking`
);
window.Whisper.events.trigger('unlinkAndDisconnect');
return;
}
const { identityKey: remoteKey } = await server.getKeysForServiceId(ourPni);
if (!constantTimeEqual(localKeyPair.pubKey, remoteKey)) {
log.warn(
`checkOurPniIdentityKey: local/remote key mismatch for ${ourPni}, unlinking`
);
window.Whisper.events.trigger('unlinkAndDisconnect');
}
}