@pokt-network/pocket-js
Version:
Pocket-js core package with the main functionalities to interact with the Pocket Network.
90 lines • 3.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transaction = void 0;
var hex_1 = require("../../utils/hex");
var tx_proof_1 = require("./tx-proof");
var response_deliver_tx_1 = require("./response-deliver-tx");
var stdtx_1 = require("./stdtx");
/**
*
*
* @class Transaction
*/
var Transaction = /** @class */ (function () {
/**
* Transaction.
* @constructor
* @param {string} hash - Transaction hash.
* @param {BigInt} height - Session Block Height.
* @param {BigInt} index - Transaction index in the block.
* @param {ResponseDeliverTx} txResult - Transaction result object.
* @param {string} tx - Transaction hex.
* @param {TxProof} proof - Transaction Proof.
* @param {StdTx} stdTx - Standard transaction object.
*/
function Transaction(hash, height, index, txResult, tx, proof, stdTx) {
this.hash = hash;
this.height = height;
this.index = index;
this.tx = tx;
this.proof = proof;
this.txResult = txResult;
this.stdTx = stdTx;
if (!this.isValid()) {
throw new TypeError("Invalid Transaction properties.");
}
}
/**
*
* Creates a Transaction object using a JSON string
* @param {string} json - JSON string.
* @returns {Transaction} - Transaction object.
* @memberof Transaction
*/
Transaction.fromJSON = function (json) {
try {
var jsonObject = JSON.parse(json);
var txResult = response_deliver_tx_1.ResponseDeliverTx.fromJSON(JSON.stringify(jsonObject.tx_result));
return new Transaction(jsonObject.hash, BigInt(jsonObject.height), BigInt(jsonObject.index), txResult, jsonObject.tx, tx_proof_1.TxProof.fromJSON(JSON.stringify(jsonObject.proof)), stdtx_1.StdTxModel.fromJSON(JSON.stringify(jsonObject.stdTx)));
}
catch (error) {
throw error;
}
};
/**
*
* Creates a JSON object with the Transaction properties
* @returns {JSON} - JSON Object.
* @memberof Transaction
*/
Transaction.prototype.toJSON = function () {
return {
hash: this.hash,
height: Number(this.height.toString()),
index: Number(this.index.toString()),
proof: this.proof.toJSON(),
tx: this.tx,
tx_result: this.txResult.toJSON(),
stdTx: this.stdTx.toJSON()
};
};
/**
*
* Check if the Transaction object is valid
* @returns {boolean} - True or false.
* @memberof Transaction
*/
Transaction.prototype.isValid = function () {
var validHash = true;
if (this.hash) {
validHash = hex_1.Hex.isHex(this.hash);
}
return validHash &&
Number(this.height.toString()) >= 0 &&
Number(this.index.toString()) >= 0 &&
this.txResult.isValid();
};
return Transaction;
}());
exports.Transaction = Transaction;
//# sourceMappingURL=transaction.js.map