2020-06-04 07:38:21 +02:00
|
|
|
import * as crypto from 'crypto';
|
|
|
|
|
|
|
|
export class PubKey {
|
|
|
|
private static readonly PUBKEY_LEN = 66;
|
2020-06-05 04:13:23 +02:00
|
|
|
private static readonly regex: string = `^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 {
|
|
|
|
if (pubkeyString.match(PubKey.regex)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static generateFake(): PubKey {
|
|
|
|
// Generates a mock pubkey for testing
|
2020-06-05 04:13:23 +02:00
|
|
|
const numBytes = PubKey.PUBKEY_LEN / 2 - 1;
|
2020-06-04 07:38:21 +02:00
|
|
|
const hexBuffer = crypto.randomBytes(numBytes).toString('hex');
|
|
|
|
const pubkeyString = `05${hexBuffer}`;
|
|
|
|
|
|
|
|
return new PubKey(pubkeyString);
|
|
|
|
}
|
|
|
|
}
|