Move refresh_sender_certificate.js to TypeScript

This commit is contained in:
Evan Hahn 2021-04-01 17:06:55 -05:00 committed by Josh Perez
parent 130dac527f
commit 99928ee831
4 changed files with 41 additions and 30 deletions

View file

@ -21,7 +21,6 @@ const Stickers = require('./stickers');
const Settings = require('./settings'); const Settings = require('./settings');
const RemoteConfig = require('../../ts/RemoteConfig'); const RemoteConfig = require('../../ts/RemoteConfig');
const Util = require('../../ts/util'); const Util = require('../../ts/util');
const RefreshSenderCertificate = require('./refresh_sender_certificate');
const LinkPreviews = require('./link_previews'); const LinkPreviews = require('./link_previews');
const AttachmentDownloads = require('./attachment_downloads'); const AttachmentDownloads = require('./attachment_downloads');
@ -434,7 +433,6 @@ exports.setup = (options = {}) => {
Migrations, Migrations,
Notifications, Notifications,
OS, OS,
RefreshSenderCertificate,
RemoteConfig, RemoteConfig,
Settings, Settings,
Services, Services,

View file

@ -7,6 +7,7 @@ import { WhatIsThis } from './window.d';
import { getTitleBarVisibility, TitleBarVisibility } from './types/Settings'; import { getTitleBarVisibility, TitleBarVisibility } from './types/Settings';
import { isWindowDragElement } from './util/isWindowDragElement'; import { isWindowDragElement } from './util/isWindowDragElement';
import { assert } from './util/assert'; import { assert } from './util/assert';
import * as refreshSenderCertificate from './refreshSenderCertificate';
import { routineProfileRefresh } from './routineProfileRefresh'; import { routineProfileRefresh } from './routineProfileRefresh';
import { isMoreRecentThan, isOlderThan } from './util/timestamp'; import { isMoreRecentThan, isOlderThan } from './util/timestamp';
@ -2068,11 +2069,10 @@ export async function startApp(): Promise<void> {
window.Whisper.events, window.Whisper.events,
newVersion newVersion
); );
window.Signal.RefreshSenderCertificate.initialize({ refreshSenderCertificate.initialize({
events: window.Whisper.events, events: window.Whisper.events,
storage: window.storage, storage: window.storage,
navigator, navigator,
logger: window.log,
}); });
window.Whisper.deliveryReceiptQueue.start(); window.Whisper.deliveryReceiptQueue.start();

View file

@ -1,29 +1,35 @@
// Copyright 2018-2020 Signal Messenger, LLC // Copyright 2018-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
/* global window, setTimeout, clearTimeout, textsecure, WebAPI, ConversationController */ import * as log from './logging/log';
module.exports = {
initialize,
};
const ONE_DAY = 24 * 60 * 60 * 1000; // one day const ONE_DAY = 24 * 60 * 60 * 1000; // one day
const MINIMUM_TIME_LEFT = 2 * 60 * 60 * 1000; // two hours const MINIMUM_TIME_LEFT = 2 * 60 * 60 * 1000; // two hours
let timeout = null; let timeout: null | ReturnType<typeof setTimeout> = null;
let scheduledTime = null; let scheduledTime: null | number = null;
let scheduleNext = null; let scheduleNext: null | (() => void) = null;
// We need to refresh our own profile regularly to account for newly-added devices which // We need to refresh our own profile regularly to account for newly-added devices which
// do not support unidentified delivery. // do not support unidentified delivery.
function refreshOurProfile() { function refreshOurProfile() {
window.log.info('refreshOurProfile'); window.log.info('refreshOurProfile');
const ourId = ConversationController.getOurConversationId(); const ourId = window.ConversationController.getOurConversationIdOrThrow();
const conversation = ConversationController.get(ourId); const conversation = window.ConversationController.get(ourId);
conversation.getProfiles(); conversation?.getProfiles();
} }
function initialize({ events, storage, navigator, logger }) { export function initialize({
events,
storage,
navigator,
}: Readonly<{
events: {
on: (name: string, callback: () => void) => void;
};
storage: typeof window.storage;
navigator: Navigator;
}>): void {
// We don't want to set up all of the below functions, but we do want to ensure that our // We don't want to set up all of the below functions, but we do want to ensure that our
// refresh timer is up-to-date. // refresh timer is up-to-date.
if (scheduleNext) { if (scheduleNext) {
@ -65,12 +71,18 @@ function initialize({ events, storage, navigator, logger }) {
// Keeping this entrypoint around so more inialize() calls just kick the timing // Keeping this entrypoint around so more inialize() calls just kick the timing
scheduleNext = scheduleNextRotation; scheduleNext = scheduleNextRotation;
async function saveCert({ certificate, key }) { async function saveCert({
certificate,
key,
}: {
certificate: string;
key: string;
}): Promise<void> {
const arrayBuffer = window.Signal.Crypto.base64ToArrayBuffer(certificate); const arrayBuffer = window.Signal.Crypto.base64ToArrayBuffer(certificate);
const decodedContainer = textsecure.protobuf.SenderCertificate.decode( const decodedContainer = window.textsecure.protobuf.SenderCertificate.decode(
arrayBuffer arrayBuffer
); );
const decodedCert = textsecure.protobuf.SenderCertificate.Certificate.decode( const decodedCert = window.textsecure.protobuf.SenderCertificate.Certificate.decode(
decodedContainer.certificate decodedContainer.certificate
); );
@ -83,7 +95,7 @@ function initialize({ events, storage, navigator, logger }) {
await storage.put(key, toSave); await storage.put(key, toSave);
} }
async function removeOldKey() { async function removeOldKey(): Promise<void> {
const oldCertKey = 'senderCertificateWithUuid'; const oldCertKey = 'senderCertificateWithUuid';
const oldUuidCert = storage.get(oldCertKey); const oldUuidCert = storage.get(oldCertKey);
if (oldUuidCert) { if (oldUuidCert) {
@ -91,13 +103,13 @@ function initialize({ events, storage, navigator, logger }) {
} }
} }
async function run() { async function run(): Promise<void> {
logger.info('refreshSenderCertificate: Getting new certificate...'); log.info('refreshSenderCertificate: Getting new certificate...');
try { try {
const OLD_USERNAME = storage.get('number_id'); const OLD_USERNAME = storage.get('number_id');
const USERNAME = storage.get('uuid_id'); const USERNAME = storage.get('uuid_id');
const PASSWORD = storage.get('password'); const PASSWORD = storage.get('password');
const server = WebAPI.connect({ const server = window.WebAPI.connect({
username: USERNAME || OLD_USERNAME, username: USERNAME || OLD_USERNAME,
password: PASSWORD, password: PASSWORD,
}); });
@ -123,7 +135,7 @@ function initialize({ events, storage, navigator, logger }) {
scheduledTime = null; scheduledTime = null;
scheduleNextRotation(); scheduleNextRotation();
} catch (error) { } catch (error) {
logger.error( log.error(
'refreshSenderCertificate: Get failed. Trying again in five minutes...', 'refreshSenderCertificate: Get failed. Trying again in five minutes...',
error && error.stack ? error.stack : error error && error.stack ? error.stack : error
); );
@ -140,11 +152,11 @@ function initialize({ events, storage, navigator, logger }) {
if (navigator.onLine) { if (navigator.onLine) {
run(); run();
} else { } else {
logger.info( log.info(
'refreshSenderCertificate: Offline. Will update certificate when online...' 'refreshSenderCertificate: Offline. Will update certificate when online...'
); );
const listener = () => { const listener = () => {
logger.info( log.info(
'refreshSenderCertificate: Online. Now updating certificate...' 'refreshSenderCertificate: Online. Now updating certificate...'
); );
window.removeEventListener('online', listener); window.removeEventListener('online', listener);
@ -158,7 +170,7 @@ function initialize({ events, storage, navigator, logger }) {
const now = Date.now(); const now = Date.now();
if (scheduledTime !== time || !timeout) { if (scheduledTime !== time || !timeout) {
logger.info( log.info(
'Next sender certificate refresh scheduled for', 'Next sender certificate refresh scheduled for',
new Date(time).toISOString() new Date(time).toISOString()
); );
@ -167,7 +179,9 @@ function initialize({ events, storage, navigator, logger }) {
scheduledTime = time; scheduledTime = time;
const waitTime = Math.max(0, time - now); const waitTime = Math.max(0, time - now);
clearTimeout(timeout); if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(runWhenOnline, waitTime); timeout = setTimeout(runWhenOnline, waitTime);
} }
} }

1
ts/window.d.ts vendored
View file

@ -526,7 +526,6 @@ declare global {
getInitialState: () => WhatIsThis; getInitialState: () => WhatIsThis;
load: () => void; load: () => void;
}; };
RefreshSenderCertificate: WhatIsThis;
}; };
ConversationController: ConversationController; ConversationController: ConversationController;