2021-01-12 06:11:05 +01:00
|
|
|
import { fromHexToArray } from '../utils/String';
|
|
|
|
|
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;
|
2020-09-17 11:36:46 +02:00
|
|
|
private static readonly HEX = '[0-9a-fA-F]';
|
|
|
|
|
2020-07-15 03:12:52 +02:00
|
|
|
// 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)
|
2020-09-17 11:36:46 +02:00
|
|
|
// tslint:disable: member-ordering
|
|
|
|
public static readonly regexForPubkeys = `((05)?${PubKey.HEX}{64})`;
|
|
|
|
public static readonly PREFIX_GROUP_TEXTSECURE = '__textsecure_group__!';
|
2020-07-14 04:33:07 +02:00
|
|
|
// prettier-ignore
|
2020-06-12 06:03:50 +02:00
|
|
|
private static readonly regex: RegExp = new RegExp(
|
2020-09-17 11:36:46 +02:00
|
|
|
`^(${PubKey.PREFIX_GROUP_TEXTSECURE})?(05)?(${PubKey.HEX}{64}|${PubKey.HEX}{32})$`
|
2020-06-12 06:03:50 +02:00
|
|
|
);
|
2020-09-17 11:36:46 +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;
|
2020-07-29 06:40:49 +02:00
|
|
|
|
2020-06-24 05:03:38 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
2020-06-24 05:03:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-24 05:03:38 +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 {
|
2020-07-14 04:33:07 +02:00
|
|
|
return this.regex.test(pubkeyString);
|
2020-06-04 07:38:21 +02:00
|
|
|
}
|
2020-06-15 01:31:56 +02:00
|
|
|
|
2020-12-15 07:15:00 +01: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
|
|
|
}
|
2021-01-12 06:11:05 +01:00
|
|
|
|
|
|
|
public withoutPrefix(): string {
|
|
|
|
return PubKey.remove05PrefixIfNeeded(this.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public toArray(): Uint8Array {
|
|
|
|
return fromHexToArray(this.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public withoutPrefixToArray(): Uint8Array {
|
|
|
|
return fromHexToArray(PubKey.remove05PrefixIfNeeded(this.key));
|
|
|
|
}
|
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 {}
|