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.

138 lines (137 loc) 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TxIn = void 0; const blsct_1 = require("./blsct"); const managedObj_1 = require("./managedObj"); const outPoint_1 = require("./outPoint"); const scalar_1 = require("./scalar"); const tokenId_1 = require("./tokenId"); /** Represents a transaction input used to construct CTxIn in a confidential transaction. * * Examples: * ```ts * const { Scalar, ChildKey, OutPoint, Scalar, TokenId, CTxId, TxIn, CTX_ID_SIZE } = require('navio-blsct') * const { randomBytes } = require('crypto') * const cTxIdHex = randomBytes(CTX_ID_SIZE).toString('hex') * const cTxId = CTxId.deserialize(cTxIdHex) * const amount = 123 * const gamma = 100 * const s = Scalar.random() * const ck = new ChildKey(s) * const txk = ck.toTxKey() * const spendingKey = txk.toSpendingKey() * const tokenId = TokenId.default() * const outPoint = OutPoint.generate(cTxId, 0) * const txIn = TxIn.generate(amount, gamma, spendingKey, tokenId, outPoint) * txIn.getAmount() // 123 * txIn.getGamma() // 100 * txIn.getSpendingKey() * txIn.getTokenId() * txIn.getOutPoint() * txIn.getStakedCommitment() // false * txIn.getRbf() // false * const ser = txIn.serialize() * const deser = TxIn.deserialize(ser) * ser === deser.serialize() // true * ``` */ class TxIn extends managedObj_1.ManagedObj { constructor(obj) { super(obj); } /** Constructs a new `TxIn` instance. * @param amount - The amount of the input. * @param gamma - The gamma of the input. * @param spendingKey - The spending key associated with the input. * @param tokenId - The token ID associated with the input. * @param outPoint - The outpoint associated with the input. * @param isStakedCommitment - Indicates if the commitment is staked (default: false). * @param isRbf - Indicates if the transaction is replaceable by fee (default: false). * @returns A new `TxIn` instance. */ static generate(amount, gamma, spendingKey, tokenId, outPoint, isStakedCommitment = false, isRbf = false) { const rv = (0, blsct_1.buildTxIn)(amount, gamma, spendingKey.value(), tokenId.value(), outPoint.value(), isStakedCommitment, isRbf); if (rv.result !== 0) { const msg = `Failed to build TxIn. Error code = ${rv.result}`; (0, blsct_1.freeObj)(rv); throw new Error(msg); } const x = new TxIn(rv.value); x.objSize = rv.value_size; (0, blsct_1.freeObj)(rv); return x; } value() { return (0, blsct_1.castToTxIn)(this.obj); } /** Returns the amount of the transaction input. * @returns The amount of the transaction input. */ getAmount() { return (0, blsct_1.getTxInAmount)(this.value()); } /** Returns the gamma of the transaction input. * @returns The gamma of the transaction input. */ getGamma() { return (0, blsct_1.getTxInGamma)(this.value()); } /** Returns the spending key associated with the transaction input. * @returns The spending key associated with the transaction input. */ getSpendingKey() { const obj = (0, blsct_1.getTxInSpendingKey)(this.value()); return scalar_1.Scalar.fromObj(obj); } /** Returns the token ID associated with the transaction input. * @returns The token ID associated with the transaction input. */ getTokenId() { const obj = (0, blsct_1.getTxInTokenId)(this.value()); return tokenId_1.TokenId.fromObj(obj); } /** Returns the outpoint associated with the transaction input. * @returns The outpoint associated with the transaction input. */ getOutPoint() { const obj = (0, blsct_1.getTxInOutPoint)(this.value()); return outPoint_1.OutPoint.fromObj(obj); } /** Returns if the transaction input is a staked commitment. * @returns `true` if the transaction input is a staked commitment, otherwise `false`. */ getStakedCommitment() { return (0, blsct_1.getTxInStakedCommitment)(this.value()); } /** Returns if the transaction input is replaceable by fee (RBF). * @returns `true` if the transaction input is RBF, otherwise `false`. */ getRbf() { return (0, blsct_1.getTxInRbf)(this.value()); } /** Returns a deep copy of the instance. * @returns A new `TxIn` instance that is a deep copy of this instance. */ clone() { const ser = this.serialize(); return TxIn.deserialize(ser); } serialize() { const buf = (0, blsct_1.castToUint8_tPtr)(this.value()); return (0, blsct_1.toHex)(buf, this.size()); } /** Deserializes a hexadecimal string into a `TxIn` instance. * @param hex - The hexadecimal string to deserialize. * @returns A new `TxIn` instance. */ static deserialize(hex) { if (hex.length % 2 !== 0) { hex = `0${hex}`; } const obj = (0, blsct_1.hexToMallocedBuf)(hex); const x = new TxIn(obj); x.objSize = hex.length / 2; return x; } } exports.TxIn = TxIn;