Introduce Service Id Types

Co-authored-by: Scott Nonnenberg <scott@signal.org>
This commit is contained in:
Fedor Indutny 2023-08-10 18:43:33 +02:00 committed by Jamie Kyle
parent 414c0a58d3
commit 366b875fd2
269 changed files with 5832 additions and 5550 deletions

View file

@ -27,37 +27,38 @@ import {
} from '@signalapp/libsignal-client';
import { Address } from './types/Address';
import { QualifiedAddress } from './types/QualifiedAddress';
import type { UUID } from './types/UUID';
import type { ServiceIdString } from './types/ServiceId';
import { normalizeServiceId } from './types/ServiceId';
import type { Zone } from './util/Zone';
function encodeAddress(address: ProtocolAddress): Address {
const name = address.name();
const deviceId = address.deviceId();
return Address.create(name, deviceId);
return Address.create(normalizeServiceId(name, 'encodeAddress'), deviceId);
}
function toQualifiedAddress(
ourUuid: UUID,
ourServiceId: ServiceIdString,
address: ProtocolAddress
): QualifiedAddress {
return new QualifiedAddress(ourUuid, encodeAddress(address));
return new QualifiedAddress(ourServiceId, encodeAddress(address));
}
export type SessionsOptions = Readonly<{
ourUuid: UUID;
ourServiceId: ServiceIdString;
zone?: Zone;
}>;
export class Sessions extends SessionStore {
private readonly ourUuid: UUID;
private readonly ourServiceId: ServiceIdString;
private readonly zone: Zone | undefined;
constructor({ ourUuid, zone }: SessionsOptions) {
constructor({ ourServiceId, zone }: SessionsOptions) {
super();
this.ourUuid = ourUuid;
this.ourServiceId = ourServiceId;
this.zone = zone;
}
@ -66,14 +67,14 @@ export class Sessions extends SessionStore {
record: SessionRecord
): Promise<void> {
await window.textsecure.storage.protocol.storeSession(
toQualifiedAddress(this.ourUuid, address),
toQualifiedAddress(this.ourServiceId, address),
record,
{ zone: this.zone }
);
}
async getSession(name: ProtocolAddress): Promise<SessionRecord | null> {
const encodedAddress = toQualifiedAddress(this.ourUuid, name);
const encodedAddress = toQualifiedAddress(this.ourServiceId, name);
const record = await window.textsecure.storage.protocol.loadSession(
encodedAddress,
{ zone: this.zone }
@ -86,7 +87,7 @@ export class Sessions extends SessionStore {
addresses: Array<ProtocolAddress>
): Promise<Array<SessionRecord>> {
const encodedAddresses = addresses.map(addr =>
toQualifiedAddress(this.ourUuid, addr)
toQualifiedAddress(this.ourServiceId, addr)
);
return window.textsecure.storage.protocol.loadSessions(encodedAddresses, {
zone: this.zone,
@ -95,25 +96,25 @@ export class Sessions extends SessionStore {
}
export type IdentityKeysOptions = Readonly<{
ourUuid: UUID;
ourServiceId: ServiceIdString;
zone?: Zone;
}>;
export class IdentityKeys extends IdentityKeyStore {
private readonly ourUuid: UUID;
private readonly ourServiceId: ServiceIdString;
private readonly zone: Zone | undefined;
constructor({ ourUuid, zone }: IdentityKeysOptions) {
constructor({ ourServiceId, zone }: IdentityKeysOptions) {
super();
this.ourUuid = ourUuid;
this.ourServiceId = ourServiceId;
this.zone = zone;
}
async getIdentityKey(): Promise<PrivateKey> {
const keyPair = window.textsecure.storage.protocol.getIdentityKeyPair(
this.ourUuid
this.ourServiceId
);
if (!keyPair) {
throw new Error('IdentityKeyStore/getIdentityKey: No identity key!');
@ -123,7 +124,7 @@ export class IdentityKeys extends IdentityKeyStore {
async getLocalRegistrationId(): Promise<number> {
const id = await window.textsecure.storage.protocol.getLocalRegistrationId(
this.ourUuid
this.ourServiceId
);
if (!isNumber(id)) {
throw new Error(
@ -136,7 +137,7 @@ export class IdentityKeys extends IdentityKeyStore {
async getIdentity(address: ProtocolAddress): Promise<PublicKey | null> {
const encodedAddress = encodeAddress(address);
const key = await window.textsecure.storage.protocol.loadIdentityKey(
encodedAddress.uuid
encodedAddress.serviceId
);
if (!key) {
@ -177,15 +178,15 @@ export class IdentityKeys extends IdentityKeyStore {
}
export type PreKeysOptions = Readonly<{
ourUuid: UUID;
ourServiceId: ServiceIdString;
}>;
export class PreKeys extends PreKeyStore {
private readonly ourUuid: UUID;
private readonly ourServiceId: ServiceIdString;
constructor({ ourUuid }: PreKeysOptions) {
constructor({ ourServiceId }: PreKeysOptions) {
super();
this.ourUuid = ourUuid;
this.ourServiceId = ourServiceId;
}
async savePreKey(): Promise<void> {
@ -194,7 +195,7 @@ export class PreKeys extends PreKeyStore {
async getPreKey(id: number): Promise<PreKeyRecord> {
const preKey = await window.textsecure.storage.protocol.loadPreKey(
this.ourUuid,
this.ourServiceId,
id
);
@ -206,16 +207,18 @@ export class PreKeys extends PreKeyStore {
}
async removePreKey(id: number): Promise<void> {
await window.textsecure.storage.protocol.removePreKeys(this.ourUuid, [id]);
await window.textsecure.storage.protocol.removePreKeys(this.ourServiceId, [
id,
]);
}
}
export class KyberPreKeys extends KyberPreKeyStore {
private readonly ourUuid: UUID;
private readonly ourServiceId: ServiceIdString;
constructor({ ourUuid }: PreKeysOptions) {
constructor({ ourServiceId }: PreKeysOptions) {
super();
this.ourUuid = ourUuid;
this.ourServiceId = ourServiceId;
}
async saveKyberPreKey(): Promise<void> {
@ -225,7 +228,7 @@ export class KyberPreKeys extends KyberPreKeyStore {
async getKyberPreKey(id: number): Promise<KyberPreKeyRecord> {
const kyberPreKey =
await window.textsecure.storage.protocol.loadKyberPreKey(
this.ourUuid,
this.ourServiceId,
id
);
@ -238,25 +241,25 @@ export class KyberPreKeys extends KyberPreKeyStore {
async markKyberPreKeyUsed(id: number): Promise<void> {
await window.textsecure.storage.protocol.maybeRemoveKyberPreKey(
this.ourUuid,
this.ourServiceId,
id
);
}
}
export type SenderKeysOptions = Readonly<{
readonly ourUuid: UUID;
readonly ourServiceId: ServiceIdString;
readonly zone: Zone | undefined;
}>;
export class SenderKeys extends SenderKeyStore {
private readonly ourUuid: UUID;
private readonly ourServiceId: ServiceIdString;
readonly zone: Zone | undefined;
constructor({ ourUuid, zone }: SenderKeysOptions) {
constructor({ ourServiceId, zone }: SenderKeysOptions) {
super();
this.ourUuid = ourUuid;
this.ourServiceId = ourServiceId;
this.zone = zone;
}
@ -265,7 +268,7 @@ export class SenderKeys extends SenderKeyStore {
distributionId: Uuid,
record: SenderKeyRecord
): Promise<void> {
const encodedAddress = toQualifiedAddress(this.ourUuid, sender);
const encodedAddress = toQualifiedAddress(this.ourServiceId, sender);
await window.textsecure.storage.protocol.saveSenderKey(
encodedAddress,
@ -279,7 +282,7 @@ export class SenderKeys extends SenderKeyStore {
sender: ProtocolAddress,
distributionId: Uuid
): Promise<SenderKeyRecord | null> {
const encodedAddress = toQualifiedAddress(this.ourUuid, sender);
const encodedAddress = toQualifiedAddress(this.ourServiceId, sender);
const senderKey = await window.textsecure.storage.protocol.getSenderKey(
encodedAddress,
@ -292,15 +295,15 @@ export class SenderKeys extends SenderKeyStore {
}
export type SignedPreKeysOptions = Readonly<{
ourUuid: UUID;
ourServiceId: ServiceIdString;
}>;
export class SignedPreKeys extends SignedPreKeyStore {
private readonly ourUuid: UUID;
private readonly ourServiceId: ServiceIdString;
constructor({ ourUuid }: SignedPreKeysOptions) {
constructor({ ourServiceId }: SignedPreKeysOptions) {
super();
this.ourUuid = ourUuid;
this.ourServiceId = ourServiceId;
}
async saveSignedPreKey(): Promise<void> {
@ -310,7 +313,7 @@ export class SignedPreKeys extends SignedPreKeyStore {
async getSignedPreKey(id: number): Promise<SignedPreKeyRecord> {
const signedPreKey =
await window.textsecure.storage.protocol.loadSignedPreKey(
this.ourUuid,
this.ourServiceId,
id
);