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

34 lines
775 B
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;
2020-06-12 06:03:50 +02:00
private static readonly regex: RegExp = new RegExp(
`^05[0-9a-fA-F]{${PubKey.PUBKEY_LEN - 2}}$`
);
2020-06-04 07:38:21 +02:00
public readonly key: string;
2020-06-04 08:11:50 +02:00
constructor(pubkeyString: string) {
2020-06-04 07:38:21 +02:00
PubKey.validate(pubkeyString);
this.key = pubkeyString;
}
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-06-12 03:40:29 +02:00
if (this.regex.test(pubkeyString)) {
2020-06-04 07:38:21 +02:00
return true;
}
return false;
}
2020-06-15 01:31:56 +02:00
2020-06-16 08:53:10 +02:00
public isEqual(comparator: PubKey) {
return this.key === comparator.key;
2020-06-15 01:31:56 +02:00
}
2020-06-04 07:38:21 +02:00
}