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.
67 lines (66 loc) • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HashId = void 0;
const blsct_1 = require("./blsct");
const managedObj_1 = require("./managedObj");
const publicKey_1 = require("./keys/publicKey");
const scalar_1 = require("./scalar");
/** Represents a hash ID consisting of a blinding public key, a spending public key, and a view key. Also known as `CKeyId` which is an alias for `uint160` on the C++ side.
*
* Examples:
* ```ts
* const { HashId, PublicKey, ChildKey, Scalar } = require('navio-blsct')
* const blindingPubKey = PublicKey.random()
* const spendingPubKey = PublicKey.random()
* const seed = Scalar.random()
* const viewKey = Scalar.random()
* const hashId = HashId.generate(blindingPubKey, spendingPubKey, viewKey)
* const ser = hashId.serialize()
* const deser = HashId.deserialize(ser)
* ser === deser.serialize() // true
* ```
*/
class HashId extends managedObj_1.ManagedObj {
/** Constructs a new `HashId` instance.
* - If no parameter is provided, a random `HashId` is generated.
*/
constructor(obj) {
if (typeof obj === 'object') {
super(obj);
}
else {
const obj = (0, blsct_1.calcKeyId)(publicKey_1.PublicKey.random().value(), publicKey_1.PublicKey.random().value(), scalar_1.Scalar.random().value());
super(obj);
}
}
/** Generates a `HashId` from the provided blinding public key, spending public key, and view key.
* @param blindingPubKey - The public key used for blinding.
* @param spendingPubKey - The spending public key.
* @param viewKey - The view key.
* @returns A new `HashId` instance.
*/
static generate(blindingPubKey, spendingPubKey, viewKey) {
const obj = (0, blsct_1.calcKeyId)(blindingPubKey.value(), spendingPubKey.value(), viewKey.value());
return HashId.fromObj(obj);
}
/** Generates a random `HashId`.
* @returns A new `HashId` instance with two random `PublicKey`s and a random `ViewKey`.
*/
static random() {
return HashId.generate(publicKey_1.PublicKey.random(), publicKey_1.PublicKey.random(), scalar_1.Scalar.random());
}
value() {
return (0, blsct_1.castToKeyId)(this.obj);
}
serialize() {
return (0, blsct_1.serializeKeyId)(this.value());
}
/** Deserializes a hexadecimal string into a `HashId` instance.
* @param hex - The hexadecimal string to deserialize.
* @returns A new `HashId` instance.
*/
static deserialize(hex) {
return HashId._deserialize(hex, blsct_1.deserializeKeyId);
}
}
exports.HashId = HashId;