UNPKG

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.

70 lines (69 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubAddr = void 0; const blsct_1 = require("./blsct"); const doublePublicKey_1 = require("./keys/doublePublicKey"); const managedObj_1 = require("./managedObj"); /** Represents a sub-address. * * Examples: * ```ts * const { SubAddr, PublicKey, DoublePublicKey, SubAddrId } = require('navio-blsct') * const seed = new Scalar() * const viewKey = new ChildKey(seed).toTxKey().toViewKey() * const spendingPubKey = new PublicKey() * const subAddrId = new SubAddrId(123, 456) * new SubAddr(viewKey, spendingPubKey, subAddrId) * const dpk = new DoublePublicKey() * const x = SubAddr.fromDoublePublicKey(dpk) * const ser = x.serialize() * const deser = SubAddr.deserialize(ser) * ser === deser.serialize() // true * ``` */ class SubAddr extends managedObj_1.ManagedObj { constructor(obj) { super(obj); } /** Generates a new `SubAddr` instance. * * @param viewKey - The view key used for deriving the sub-address. * @param spendingPubKey - The spending public key used for deriving the sub-address. * @param subAddrId - The sub-address ID used for deriving the sub-address. * @return A new `SubAddr` instance with the specified view key, spending public key, and sub-address ID. */ static generate(viewKey, spendingPubKey, subAddrId) { const obj = (0, blsct_1.deriveSubAddress)(viewKey.value(), spendingPubKey.value(), subAddrId.value()); return new SubAddr(obj); } /** Generates a `SubAddr` from a `DoublePublicKey`. * * @param dpk - The `DoublePublicKey` used to derive the sub-address. * @return A new `SubAddr` instance derived from the `DoublePublicKey`. */ static fromDoublePublicKey(dpk) { const rv = (0, blsct_1.dpkToSubAddr)(dpk.value()); const inst = new SubAddr(rv.value); (0, blsct_1.freeObj)(rv); return inst; } toDoublePublicKey() { const blsctDpk = (0, blsct_1.subAddrToDpk)(this.value()); return doublePublicKey_1.DoublePublicKey.fromObj(blsctDpk); } value() { return (0, blsct_1.castToSubAddr)(this.obj); } serialize() { return (0, blsct_1.serializeSubAddr)(this.value()); } /** Deserializes a `SubAddr` from its hexadecimal representation. * * @param hex - The hexadecimal string to deserialize. * @returns A new `SubAddr` instance. */ static deserialize(hex) { return SubAddr._deserialize(hex, blsct_1.deserializeSubAddr); } } exports.SubAddr = SubAddr;