session-desktop/ts/session/types/PubKey.ts

87 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-06-04 07:38:21 +02:00
export class PubKey {
2020-06-05 08:56:47 +02:00
public static readonly PUBKEY_LEN = 66;
private static readonly HEX = '[0-9a-fA-F]';
// This is a temporary fix to allow groupPubkeys created from mobile to be handled correctly
// They have a different regex to match
// FIXME move this to a new class which validates group ids and use it in all places where we have group ids (message sending included)
// tslint:disable: member-ordering
public static readonly regexForPubkeys = `((05)?${PubKey.HEX}{64})`;
public static readonly PREFIX_GROUP_TEXTSECURE = '__textsecure_group__!';
// prettier-ignore
2020-06-12 06:03:50 +02:00
private static readonly regex: RegExp = new RegExp(
`^(${PubKey.PREFIX_GROUP_TEXTSECURE})?(05)?(${PubKey.HEX}{64}|${PubKey.HEX}{32})$`
2020-06-12 06:03:50 +02:00
);
/**
* If you want to update this regex. Be sure that those are matches ;
* __textsecure_group__!05010203040506070809a0b0c0d0e0f0ff010203040506070809a0b0c0d0e0f0ff
* __textsecure_group__!010203040506070809a0b0c0d0e0f0ff010203040506070809a0b0c0d0e0f0ff
* __textsecure_group__!05010203040506070809a0b0c0d0e0f0ff
* __textsecure_group__!010203040506070809a0b0c0d0e0f0ff
* 05010203040506070809a0b0c0d0e0f0ff010203040506070809a0b0c0d0e0f0ff
* 010203040506070809a0b0c0d0e0f0ff010203040506070809a0B0c0d0e0f0FF
* 05010203040506070809a0b0c0d0e0f0ff
* 010203040506070809a0b0c0d0e0f0ff
*/
2020-06-04 07:38:21 +02:00
public readonly key: string;
/**
* A PubKey object.
* If `pubKeyString` is not valid then this will throw an `Error`.
*
* @param pubkeyString The public key string.
*/
2020-06-04 08:11:50 +02:00
constructor(pubkeyString: string) {
if (!PubKey.validate(pubkeyString)) {
throw new Error(`Invalid pubkey string passed: ${pubkeyString}`);
}
2020-06-25 04:13:53 +02:00
this.key = pubkeyString.toLowerCase();
2020-06-04 07:38:21 +02:00
}
/**
* Cast a `value` to a `PubKey`.
* If `value` is not valid then this will throw.
*
* @param value The value to cast.
*/
public static cast(value: string | PubKey): PubKey {
return typeof value === 'string' ? new PubKey(value) : value;
}
/**
* Try convert `pubKeyString` to `PubKey`.
*
* @param pubkeyString The public key string.
* @returns `PubKey` if valid otherwise returns `undefined`.
*/
2020-06-04 07:38:21 +02:00
public static from(pubkeyString: string): PubKey | undefined {
// Returns a new instance if the pubkey is valid
if (PubKey.validate(pubkeyString)) {
return new PubKey(pubkeyString);
}
return undefined;
}
public static validate(pubkeyString: string): boolean {
return this.regex.test(pubkeyString);
2020-06-04 07:38:21 +02:00
}
2020-06-15 01:31:56 +02:00
public static remove05PrefixIfNeeded(recipient: string): string {
if (recipient.length === 66 && recipient.startsWith('05')) {
return recipient.substr(2);
}
return recipient;
}
2020-06-18 02:27:08 +02:00
public isEqual(comparator: PubKey | string) {
return comparator instanceof PubKey
? this.key === comparator.key
2020-06-25 04:13:53 +02:00
: this.key === comparator.toLowerCase();
2020-06-15 01:31:56 +02:00
}
2020-06-04 07:38:21 +02:00
}
2020-06-12 05:53:54 +02:00
export class PrimaryPubKey extends PubKey {}
export class SecondaryPubKey extends PubKey {}