2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
import { PublicKey, Fingerprint } from '@signalapp/signal-client';
|
2020-06-26 00:08:58 +00:00
|
|
|
import { ConversationType } from '../state/ducks/conversations';
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
import { assert } from './assert';
|
|
|
|
|
2020-06-26 00:08:58 +00:00
|
|
|
export async function generateSecurityNumber(
|
|
|
|
ourNumber: string,
|
|
|
|
ourKey: ArrayBuffer,
|
|
|
|
theirNumber: string,
|
|
|
|
theirKey: ArrayBuffer
|
|
|
|
): 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
|
2020-06-26 00:08:58 +00:00
|
|
|
);
|
2021-02-23 23:34:23 +00:00
|
|
|
|
|
|
|
const fingerprintString = fingerprint.displayableFingerprint().toString();
|
|
|
|
return Promise.resolve(fingerprintString);
|
2020-06-26 00:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function generateSecurityNumberBlock(
|
|
|
|
contact: ConversationType
|
|
|
|
): Promise<Array<string>> {
|
|
|
|
const ourNumber = window.textsecure.storage.user.getNumber();
|
|
|
|
const ourUuid = window.textsecure.storage.user.getUuid();
|
|
|
|
|
|
|
|
const us = window.textsecure.storage.protocol.getIdentityRecord(
|
2021-06-15 00:09:37 +00:00
|
|
|
ourUuid || ourNumber || ''
|
2020-06-26 00:08:58 +00:00
|
|
|
);
|
|
|
|
const ourKey = us ? us.publicKey : null;
|
|
|
|
|
|
|
|
const them = window.textsecure.storage.protocol.getIdentityRecord(contact.id);
|
|
|
|
const theirKey = them ? them.publicKey : null;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
window.log.error(
|
|
|
|
'generateSecurityNumberBlock: Attempted to generate security number for contact with no e164'
|
|
|
|
);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
assert(ourNumber, 'Should have our number');
|
2020-06-26 00:08:58 +00:00
|
|
|
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;
|
|
|
|
}
|