38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
import * as blst from "@chainsafe/blst";
|
|
import { EmptyAggregateError } from "../errors.js";
|
|
import { bytesToHex, hexToBytes } from "../helpers/index.js";
|
|
import { PointFormat } from "../types.js";
|
|
export class PublicKey extends blst.PublicKey {
|
|
constructor(value) {
|
|
super(value);
|
|
}
|
|
/** @param type Defaults to `CoordType.jacobian` */
|
|
static fromBytes(bytes, type, validate) {
|
|
const pk = blst.PublicKey.fromBytes(bytes, type);
|
|
if (validate)
|
|
pk.keyValidate();
|
|
return new PublicKey(pk.value);
|
|
}
|
|
static fromHex(hex) {
|
|
return this.fromBytes(hexToBytes(hex));
|
|
}
|
|
static aggregate(publicKeys) {
|
|
if (publicKeys.length === 0) {
|
|
throw new EmptyAggregateError();
|
|
}
|
|
const pk = blst.aggregatePubkeys(publicKeys);
|
|
return new PublicKey(pk.value);
|
|
}
|
|
toBytes(format) {
|
|
if (format === PointFormat.uncompressed) {
|
|
return this.value.serialize();
|
|
}
|
|
else {
|
|
return this.value.compress();
|
|
}
|
|
}
|
|
toHex(format) {
|
|
return bytesToHex(this.toBytes(format));
|
|
}
|
|
}
|