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.

122 lines (121 loc) 4.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TxOut = void 0; const blsct_1 = require("./blsct"); const managedObj_1 = require("./managedObj"); const subAddr_1 = require("./subAddr"); const tokenId_1 = require("./tokenId"); /** Represents a transaction output used to construct a CTxOut in a confidential transaction. * * Examples: * ```ts * const { SubAddr, DoublePublicKey, TxOut, TokenId, TxOutputType } = require('navio-blsct') * const dpk = new DoublePublicKey() * const subAddr = SubAddr.fromDoublePublicKey(dpk) * const amount = 789 * const memo = "apple" * const txOut = TxOut.generate(subAddr, amount, memo) * txOut.getDestination() * txOut.getAmount() // 789 * txOut.getMemo() // "apple" * txOut.getTokenId() * txOut.getMinStake() // 0 * const ser = txOut.serialize() * const deser = TxOut.deserialize(ser) * ser === deser.serialize() // true * ``` */ class TxOut extends managedObj_1.ManagedObj { constructor(obj) { super(obj); } /** Constructs a new `TxOut` instance. * @param subAddr - The destination SubAddr of the output. * @param amount - The amount of the output. * @param memo - The memo of the output. * @param tokenId - The token ID associated with the output (optional). * @param outputType - The type of the output (default is `TxOutputType.Normal`). * @param minStake - The minimum stake for the output (default is 0). * @returns A new `TxOut` instance. */ static generate(subAddr, amount, memo, tokenId, outputType = blsct_1.TxOutputType.Normal, minStake = 0) { tokenId = tokenId === undefined ? tokenId_1.TokenId.default() : tokenId; const rv = (0, blsct_1.buildTxOut)(subAddr.value(), amount, memo, tokenId.value(), outputType, minStake); if (rv.result !== 0) { const msg = `Failed to build TxOut. Error code = ${rv.result}`; (0, blsct_1.freeObj)(rv); throw new Error(msg); } const x = new TxOut(rv.value); x.objSize = rv.value_size; (0, blsct_1.freeObj)(rv); return x; } value() { return (0, blsct_1.castToTxOut)(this.obj); } /** Returns the destination SubAddr of the transaction output. * @returns The destination SubAddr of the transaction output. */ getDestination() { const obj = (0, blsct_1.getTxOutDestination)(this.value()); return subAddr_1.SubAddr.fromObj(obj); } /** Returns the amount of the transaction output. * @returns The amount of the transaction output. */ getAmount() { return (0, blsct_1.getTxOutAmount)(this.value()); } /** Returns the memo of the transaction output. * @returns The memo of the transaction output. */ getMemo() { return (0, blsct_1.getTxOutMemo)(this.value()); } /** Returns the token ID associated with the transaction output. * @returns The token ID associated with the transaction output. */ getTokenId() { const obj = (0, blsct_1.getTxOutTokenId)(this.value()); return tokenId_1.TokenId.fromObj(obj); } /** Returns the type of the transaction output. * @returns The type of the transaction output. */ getOutputType() { return (0, blsct_1.getTxOutOutputType)(this.value()); } /** Returns the minimum stake required for the transaction output. * @returns The minimum stake required for the transaction output. */ getMinStake() { return (0, blsct_1.getTxOutMinStake)(this.value()); } /** Returns a deep copy of the instance. * @returns A new `TxOut` instance that is a deep copy of this instance. */ clone() { const ser = this.serialize(); return TxOut.deserialize(ser); } serialize() { const buf = (0, blsct_1.castToUint8_tPtr)(this.value()); return (0, blsct_1.toHex)(buf, this.size()); } /** Deserializes a `TxOut` from its hexadecimal representation. * @param hex - The hexadecimal string to deserialize. * @returns A new `TxOut` instance. */ static deserialize(hex) { if (hex.length % 2 !== 0) { hex = `0${hex}`; } const obj = (0, blsct_1.hexToMallocedBuf)(hex); const x = new TxOut(obj); x.objSize = hex.length / 2; return x; } } exports.TxOut = TxOut;