2020-11-19 13:22:41 +00:00
|
|
|
import * as blst from "@chainsafe/blst";
|
2022-04-11 15:08:15 +00:00
|
|
|
import {EmptyAggregateError} from "../errors.js";
|
|
|
|
import {bytesToHex, hexToBytes} from "../helpers/index.js";
|
|
|
|
import {PointFormat, PublicKey as IPublicKey} from "../interface.js";
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2021-04-04 23:44:55 +00:00
|
|
|
export class PublicKey extends blst.PublicKey implements IPublicKey {
|
|
|
|
constructor(value: ConstructorParameters<typeof blst.PublicKey>[0]) {
|
|
|
|
super(value);
|
2019-08-05 15:48:26 +00:00
|
|
|
}
|
2020-02-04 22:30:10 +00:00
|
|
|
|
2021-04-05 17:47:25 +00:00
|
|
|
/** @param type Defaults to `CoordType.jacobian` */
|
2021-04-05 20:38:12 +00:00
|
|
|
static fromBytes(bytes: Uint8Array, type?: blst.CoordType, validate?: boolean): PublicKey {
|
2021-04-04 23:44:55 +00:00
|
|
|
const pk = blst.PublicKey.fromBytes(bytes, type);
|
2021-04-05 20:38:12 +00:00
|
|
|
if (validate) pk.keyValidate();
|
2021-04-04 23:44:55 +00:00
|
|
|
return new PublicKey(pk.value);
|
2019-11-27 20:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 00:23:34 +00:00
|
|
|
static fromHex(hex: string): PublicKey {
|
|
|
|
return this.fromBytes(hexToBytes(hex));
|
2019-08-05 15:48:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:01:13 +00:00
|
|
|
static aggregate(publicKeys: PublicKey[]): PublicKey {
|
|
|
|
if (publicKeys.length === 0) {
|
2020-11-30 00:20:52 +00:00
|
|
|
throw new EmptyAggregateError();
|
|
|
|
}
|
|
|
|
|
2021-04-04 23:44:55 +00:00
|
|
|
const pk = blst.aggregatePubkeys(publicKeys);
|
|
|
|
return new PublicKey(pk.value);
|
2019-08-28 15:18:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 23:44:55 +00:00
|
|
|
toBytes(format?: PointFormat): Uint8Array {
|
|
|
|
if (format === PointFormat.uncompressed) {
|
|
|
|
return this.value.serialize();
|
|
|
|
} else {
|
|
|
|
return this.value.compress();
|
|
|
|
}
|
2019-08-28 15:18:51 +00:00
|
|
|
}
|
2019-11-27 20:58:41 +00:00
|
|
|
|
2021-04-04 23:44:55 +00:00
|
|
|
toHex(format?: PointFormat): string {
|
|
|
|
return bytesToHex(this.toBytes(format));
|
2019-11-27 20:58:41 +00:00
|
|
|
}
|
2019-08-05 15:48:26 +00:00
|
|
|
}
|