Introduce Service Id Types
Co-authored-by: Scott Nonnenberg <scott@signal.org>
This commit is contained in:
parent
414c0a58d3
commit
366b875fd2
269 changed files with 5832 additions and 5550 deletions
|
@ -5,14 +5,19 @@ import type { WebAPICredentials } from '../Types.d';
|
|||
|
||||
import { strictAssert } from '../../util/assert';
|
||||
import type { StorageInterface } from '../../types/Storage.d';
|
||||
import { UUID, UUIDKind } from '../../types/UUID';
|
||||
import type {
|
||||
AciString,
|
||||
PniString,
|
||||
ServiceIdString,
|
||||
} from '../../types/ServiceId';
|
||||
import { ServiceIdKind, isAciString, isPniString } from '../../types/ServiceId';
|
||||
import * as log from '../../logging/log';
|
||||
|
||||
import Helpers from '../Helpers';
|
||||
|
||||
export type SetCredentialsOptions = {
|
||||
uuid: string;
|
||||
pni: string;
|
||||
aci: AciString;
|
||||
pni: PniString;
|
||||
number: string;
|
||||
deviceId: number;
|
||||
deviceName?: string;
|
||||
|
@ -23,12 +28,12 @@ export class User {
|
|||
constructor(private readonly storage: StorageInterface) {}
|
||||
|
||||
public async setUuidAndDeviceId(
|
||||
uuid: string,
|
||||
aci: AciString,
|
||||
deviceId: number
|
||||
): Promise<void> {
|
||||
await this.storage.put('uuid_id', `${uuid}.${deviceId}`);
|
||||
await this.storage.put('uuid_id', `${aci}.${deviceId}`);
|
||||
|
||||
log.info('storage.user: uuid and device id changed');
|
||||
log.info('storage.user: aci and device id changed');
|
||||
}
|
||||
|
||||
public async setNumber(number: string): Promise<void> {
|
||||
|
@ -61,53 +66,78 @@ export class User {
|
|||
return Helpers.unencodeNumber(numberId)[0];
|
||||
}
|
||||
|
||||
public getUuid(uuidKind = UUIDKind.ACI): UUID | undefined {
|
||||
if (uuidKind === UUIDKind.PNI) {
|
||||
const pni = this.storage.get('pni');
|
||||
if (pni === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return new UUID(pni);
|
||||
public getPni(): PniString | undefined {
|
||||
const pni = this.storage.get('pni');
|
||||
if (pni === undefined || !isPniString(pni)) {
|
||||
return undefined;
|
||||
}
|
||||
return pni;
|
||||
}
|
||||
|
||||
public getAci(): AciString | undefined {
|
||||
const uuidId = this.storage.get('uuid_id');
|
||||
if (!uuidId) {
|
||||
return undefined;
|
||||
}
|
||||
const aci = Helpers.unencodeNumber(uuidId.toLowerCase())[0];
|
||||
if (!isAciString(aci)) {
|
||||
return undefined;
|
||||
}
|
||||
return aci;
|
||||
}
|
||||
|
||||
public getServiceId(
|
||||
serviceIdKind: ServiceIdKind
|
||||
): ServiceIdString | undefined {
|
||||
if (serviceIdKind === ServiceIdKind.PNI) {
|
||||
return this.getPni();
|
||||
}
|
||||
|
||||
strictAssert(
|
||||
uuidKind === UUIDKind.ACI,
|
||||
`Unsupported uuid kind: ${uuidKind}`
|
||||
serviceIdKind === ServiceIdKind.ACI,
|
||||
`Unsupported uuid kind: ${serviceIdKind}`
|
||||
);
|
||||
const uuid = this.storage.get('uuid_id');
|
||||
if (!uuid) {
|
||||
return undefined;
|
||||
}
|
||||
return new UUID(Helpers.unencodeNumber(uuid.toLowerCase())[0]);
|
||||
return this.getAci();
|
||||
}
|
||||
|
||||
public getCheckedUuid(uuidKind?: UUIDKind): UUID {
|
||||
const uuid = this.getUuid(uuidKind);
|
||||
public getCheckedAci(): AciString {
|
||||
const aci = this.getAci();
|
||||
strictAssert(aci !== undefined, 'Must have our own ACI');
|
||||
return aci;
|
||||
}
|
||||
|
||||
public getCheckedPni(): PniString {
|
||||
const pni = this.getPni();
|
||||
strictAssert(pni !== undefined, 'Must have our own PNI');
|
||||
return pni;
|
||||
}
|
||||
|
||||
public getCheckedServiceId(serviceIdKind: ServiceIdKind): ServiceIdString {
|
||||
const uuid = this.getServiceId(serviceIdKind);
|
||||
strictAssert(uuid !== undefined, 'Must have our own uuid');
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public async setPni(pni: string): Promise<void> {
|
||||
await this.storage.put('pni', UUID.cast(pni));
|
||||
public async setPni(pni: PniString): Promise<void> {
|
||||
await this.storage.put('pni', pni);
|
||||
}
|
||||
|
||||
public getOurUuidKind(uuid: UUID): UUIDKind {
|
||||
const ourUuid = this.getUuid();
|
||||
|
||||
if (ourUuid?.toString() === uuid.toString()) {
|
||||
return UUIDKind.ACI;
|
||||
public getOurServiceIdKind(serviceId: ServiceIdString): ServiceIdKind {
|
||||
const ourAci = this.getAci();
|
||||
if (ourAci === serviceId) {
|
||||
return ServiceIdKind.ACI;
|
||||
}
|
||||
|
||||
const pni = this.getUuid(UUIDKind.PNI);
|
||||
if (pni?.toString() === uuid.toString()) {
|
||||
return UUIDKind.PNI;
|
||||
const pni = this.getPni();
|
||||
if (pni === serviceId) {
|
||||
return ServiceIdKind.PNI;
|
||||
}
|
||||
|
||||
return UUIDKind.Unknown;
|
||||
return ServiceIdKind.Unknown;
|
||||
}
|
||||
|
||||
public isOurUuid(uuid: UUID): boolean {
|
||||
return this.getOurUuidKind(uuid) !== UUIDKind.Unknown;
|
||||
public isOurServiceId(serviceId: ServiceIdString): boolean {
|
||||
return this.getOurServiceIdKind(serviceId) !== ServiceIdKind.Unknown;
|
||||
}
|
||||
|
||||
public getDeviceId(): number | undefined {
|
||||
|
@ -137,11 +167,11 @@ export class User {
|
|||
public async setCredentials(
|
||||
credentials: SetCredentialsOptions
|
||||
): Promise<void> {
|
||||
const { uuid, pni, number, deviceId, deviceName, password } = credentials;
|
||||
const { aci, pni, number, deviceId, deviceName, password } = credentials;
|
||||
|
||||
await Promise.all([
|
||||
this.storage.put('number_id', `${number}.${deviceId}`),
|
||||
this.storage.put('uuid_id', `${uuid}.${deviceId}`),
|
||||
this.storage.put('uuid_id', `${aci}.${deviceId}`),
|
||||
this.storage.put('password', password),
|
||||
this.setPni(pni),
|
||||
deviceName
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue