navio-blsct
Version:
TypeScript bindings for the `libblsct` library used by the [Navio](https://nav.io/) blockchain to construct confidential transactions based on the BLS12-381 curve.
106 lines (105 loc) • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PublicKey = void 0;
const blsct_1 = require("../blsct");
const managedObj_1 = require("../managedObj");
const point_1 = require("../point");
/** Represents an element in the BLS12-381 G1 curve group that is used as a public key.
*
* Examples:
* ```ts
* const { PublicKey, Point, Scalar } = require('navio-blsct')
* const s123 = new Scalar(123)
* const pk123 = PublicKey.fromScalar(s123)
* const s234 = new Scalar(234)
* const pk234 = PublicKey.fromScalar(s234)
* pk123.equals(pk234) // false
* pk123.equals(pk123) // true
* const p = Point.random()
* const pk = PublicKey.fromPoint(p)
* pk.getPoint()
* const vk = Scalar.random()
* pk.generateNonce(vk)
* const ser = pk.serialize()
* const deser = PublicKey.deserialize(ser)
* ser == deser.serialize()
* ```
*/
class PublicKey extends managedObj_1.ManagedObj {
/** Constructs a new `PublicKey` instance.
* - If no parameter is provided, a random public key is generated.
*/
constructor(obj) {
if (typeof obj === 'object') {
super(obj);
}
else {
const rv = (0, blsct_1.genRandomPublicKey)();
if (rv.result !== 0) {
throw new Error('Failed to generate random PublicKey');
}
super(rv.value);
}
}
/** Return the underlying point of the public key
* @return {Point} The underlying point of the public key.
*/
getPoint() {
const blsctPoint = (0, blsct_1.getPublicKeyPoint)(this.value());
return point_1.Point.fromObj(blsctPoint);
}
/** Returns a random public key.
* @returns A new random `PublicKey`.
*/
static random() {
const rv = (0, blsct_1.genRandomPublicKey)();
const pk = PublicKey.fromObj(rv.value);
(0, blsct_1.freeObj)(rv);
return pk;
}
/** Creates a `PublicKey` from a `Point` object.
* @param point - The `Point` object to convert.
*/
static fromPoint(point) {
const blsctPubKey = (0, blsct_1.pointToPublicKey)(point.value());
return PublicKey.fromObj(blsctPubKey);
}
/** Creates a `PublicKey` from a `Scalar` object.
* @param scalar - The `Scalar` object to convert.
*/
static fromScalar(scalar) {
const blsctPubKey = (0, blsct_1.scalarToPubKey)(scalar.value());
return PublicKey.fromObj(blsctPubKey);
}
/** Generates a nonce from this `PublicKey` using a `Scalar` view key.
*
* @param viewKey - The view key.
* @returns A new `PublicKey` that represents the nonce.
*/
generateNonce(viewKey) {
const blsctNonce = (0, blsct_1.calcNonce)(this.value(), viewKey.value());
return PublicKey.fromObj(blsctNonce);
}
/** Returns if the provied public key is equal to this public key.
* @param other - The public key to compare with.
* @returns `true` if the public keys are equal, `false` otherwise.
*/
equals(other) {
return this.getPoint().equals(other.getPoint());
}
value() {
return (0, blsct_1.castToPubKey)(this.obj);
}
serialize() {
return this.getPoint().serialize();
}
/** Deserializes a hexadecimal string into a `PublicKey` instance.
* @param hex - The hexadecimal string to convert.
* @returns The `PublicKey` instance represented by the input string.
*/
static deserialize(hex) {
const p = point_1.Point.deserialize(hex);
return PublicKey.fromPoint(p);
}
}
exports.PublicKey = PublicKey;