Signal-Desktop/ts/util/safetyNumber.ts

81 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { PublicKey, Fingerprint } from '@signalapp/libsignal-client';
import type { ConversationType } from '../state/ducks/conversations';
import { UUID } from '../types/UUID';
import { assertDev } from './assert';
import * as log from '../logging/log';
export async function generateSecurityNumber(
ourNumber: string,
2021-09-24 00:49:05 +00:00
ourKey: Uint8Array,
theirNumber: string,
2021-09-24 00:49:05 +00:00
theirKey: Uint8Array
): Promise<string> {
2021-02-23 23:34:23 +00:00
const ourNumberBuf = Buffer.from(ourNumber);
const ourKeyObj = PublicKey.deserialize(Buffer.from(ourKey));
const theirNumberBuf = Buffer.from(theirNumber);
const theirKeyObj = PublicKey.deserialize(Buffer.from(theirKey));
const fingerprint = Fingerprint.new(
5200,
2,
ourNumberBuf,
ourKeyObj,
theirNumberBuf,
theirKeyObj
);
2021-02-23 23:34:23 +00:00
const fingerprintString = fingerprint.displayableFingerprint().toString();
return Promise.resolve(fingerprintString);
}
export async function generateSecurityNumberBlock(
contact: ConversationType
): Promise<Array<string>> {
const { storage } = window.textsecure;
const ourNumber = storage.user.getNumber();
const ourUuid = storage.user.getCheckedUuid();
const us = storage.protocol.getIdentityRecord(ourUuid);
const ourKey = us ? us.publicKey : null;
const theirUuid = UUID.lookup(contact.id);
const them = theirUuid
? await storage.protocol.getOrMigrateIdentityRecord(theirUuid)
: undefined;
const theirKey = them?.publicKey;
if (!ourKey) {
throw new Error('Could not load our key');
}
if (!theirKey) {
throw new Error('Could not load their key');
}
2020-07-24 01:35:32 +00:00
if (!contact.e164) {
log.error(
2020-07-24 01:35:32 +00:00
'generateSecurityNumberBlock: Attempted to generate security number for contact with no e164'
);
return [];
}
assertDev(ourNumber, 'Should have our number');
const securityNumber = await generateSecurityNumber(
ourNumber,
ourKey,
contact.e164,
theirKey
);
const chunks = [];
for (let i = 0; i < securityNumber.length; i += 5) {
chunks.push(securityNumber.substring(i, i + 5));
}
return chunks;
}