@chainsafe/bls
Version:
Implementation of bls signature verification for ethereum 2.0
50 lines (49 loc) • 1.65 kB
JavaScript
import blst from "@chainsafe/blst";
import { EmptyAggregateError } from "../errors.js";
import { bytesToHex, hexToBytes } from "../helpers/index.js";
import { PointFormat } from "../types.js";
export class PublicKey {
constructor(value) {
this.value = value;
}
/** @param type Defaults to `CoordType.jacobian` */
static fromBytes(bytes, type, validate = true) {
// need to hack the CoordType so @chainsafe/blst is not a required dep
const pk = blst.PublicKey.fromBytes(bytes, validate);
return new PublicKey(pk);
}
static fromHex(hex) {
return this.fromBytes(hexToBytes(hex));
}
static aggregate(publicKeys) {
if (publicKeys.length === 0) {
throw new EmptyAggregateError();
}
const pk = blst.aggregatePublicKeys(publicKeys.map(PublicKey.convertToBlstPublicKeyArg));
return new PublicKey(pk);
}
static convertToBlstPublicKeyArg(publicKey) {
// need to cast to blst-native key instead of IPublicKey
return publicKey instanceof Uint8Array ? blst.PublicKey.fromBytes(publicKey) : publicKey.value;
}
/**
* Implemented for SecretKey to be able to call .toPublicKey()
*/
static friendBuild(key) {
return new PublicKey(key);
}
toBytes(format) {
if (format === PointFormat.uncompressed) {
return this.value.toBytes(false);
}
else {
return this.value.toBytes(true);
}
}
toHex(format) {
return bytesToHex(this.toBytes(format));
}
multiplyBy(_bytes) {
throw new Error("Not implemented");
}
}