This repository has been archived on 2023-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
chainsafe-bls/src/blst-native/publicKey.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-11-19 13:22:41 +00:00
import * as blst from "@chainsafe/blst";
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
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
/** @param type Defaults to `CoordType.jacobian` */
static fromBytes(bytes: Uint8Array, type?: blst.CoordType, validate?: boolean): PublicKey {
const pk = blst.PublicKey.fromBytes(bytes, type);
if (validate) pk.keyValidate();
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();
}
const pk = blst.aggregatePubkeys(publicKeys);
return new PublicKey(pk.value);
2019-08-28 15:18:51 +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
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
}