2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-03-24 21:47:21 +00:00
|
|
|
import { PublicKey, Fingerprint } from '@signalapp/libsignal-client';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from '../state/ducks/conversations';
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2022-09-15 19:17:15 +00:00
|
|
|
import { assertDev } from './assert';
|
2023-07-13 19:06:42 +00:00
|
|
|
import { uuidToBytes } from './uuidToBytes';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2023-07-13 19:06:42 +00:00
|
|
|
import type { SafetyNumberType } from '../types/safetyNumber';
|
2023-09-14 17:04:48 +00:00
|
|
|
import { isAciString } from './isAciString';
|
2021-06-15 00:09:37 +00:00
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
const ITERATION_COUNT = 5200;
|
2023-08-16 20:54:39 +00:00
|
|
|
const SERVICE_ID_VERSION = 2;
|
2023-04-07 17:46:00 +00:00
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
// Number of digits in a safety number block
|
|
|
|
const BLOCK_SIZE = 5;
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-11-01 22:55:30 +00:00
|
|
|
export async function generateSafetyNumber(
|
2023-11-01 20:35:55 +00:00
|
|
|
contact: ConversationType
|
2023-11-01 22:55:30 +00:00
|
|
|
): Promise<SafetyNumberType> {
|
2023-11-01 20:35:55 +00:00
|
|
|
const logId = `generateSafetyNumbers(${contact.id})`;
|
2023-04-07 17:46:00 +00:00
|
|
|
log.info(`${logId}: starting`);
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const { storage } = window.textsecure;
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = storage.user.getCheckedAci();
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
const us = storage.protocol.getIdentityRecord(ourAci);
|
|
|
|
const ourKeyBuffer = us ? us.publicKey : null;
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-08-30 03:15:28 +00:00
|
|
|
const theirAci = isAciString(contact.serviceId)
|
|
|
|
? contact.serviceId
|
|
|
|
: undefined;
|
2023-07-13 19:06:42 +00:00
|
|
|
const them = theirAci
|
2023-08-10 16:43:33 +00:00
|
|
|
? await storage.protocol.getOrMigrateIdentityRecord(theirAci)
|
2021-09-10 02:38:11 +00:00
|
|
|
: undefined;
|
2023-07-13 19:06:42 +00:00
|
|
|
const theirKeyBuffer = them?.publicKey;
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
if (!ourKeyBuffer) {
|
2020-06-26 00:08:58 +00:00
|
|
|
throw new Error('Could not load our key');
|
|
|
|
}
|
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
if (!theirKeyBuffer) {
|
2020-06-26 00:08:58 +00:00
|
|
|
throw new Error('Could not load their key');
|
|
|
|
}
|
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
const ourKey = PublicKey.deserialize(Buffer.from(ourKeyBuffer));
|
|
|
|
const theirKey = PublicKey.deserialize(Buffer.from(theirKeyBuffer));
|
|
|
|
|
2023-11-01 22:55:30 +00:00
|
|
|
assertDev(theirAci, 'Should have their serviceId');
|
|
|
|
const fingerprint = Fingerprint.new(
|
|
|
|
ITERATION_COUNT,
|
|
|
|
SERVICE_ID_VERSION,
|
|
|
|
Buffer.from(uuidToBytes(ourAci)),
|
|
|
|
ourKey,
|
|
|
|
Buffer.from(uuidToBytes(theirAci)),
|
|
|
|
theirKey
|
|
|
|
);
|
|
|
|
|
|
|
|
const securityNumber = fingerprint.displayableFingerprint().toString();
|
|
|
|
|
|
|
|
const numberBlocks = [];
|
|
|
|
for (let i = 0; i < securityNumber.length; i += BLOCK_SIZE) {
|
|
|
|
numberBlocks.push(securityNumber.substring(i, i + BLOCK_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
const qrData = fingerprint.scannableFingerprint().toBuffer();
|
|
|
|
|
|
|
|
return { numberBlocks, qrData };
|
2020-06-26 00:08:58 +00:00
|
|
|
}
|