@pokt-network/pocket-js
Version:
Pocket-js core package with the main functionalities to interact with the Pocket Network.
84 lines • 2.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DispatchResponse = void 0;
var __1 = require("../");
var input_1 = require("../input");
/**
*
*
* @class DispatchResponse
*/
var DispatchResponse = /** @class */ (function () {
/**
* Dispatch Response.
* @constructor
* @param {BigInt} blockHeight - blockheight the dispatch response was received
* @param {SessionHeader} header -
* @param {string} key -
* @param {Node[]} nodes -
*/
function DispatchResponse(blockHeight, header, key, nodes) {
this.blockHeight = blockHeight;
this.header = header;
this.key = key;
this.nodes = nodes;
if (!this.isValid()) {
throw new TypeError("Invalid DispatchResponse properties.");
}
}
/**
*
* Creates a DispatchResponse object using a JSON string
* @param {String} json - JSON string.
* @returns {DispatchResponse} - DispatchResponse object.
* @memberof DispatchResponse
*/
DispatchResponse.fromJSON = function (json) {
try {
var jsonObject = JSON.parse(json);
// Initially load the session header timestamp with 0; it will be computed when the session is saved
jsonObject.session.header.session_timestamp = 0;
var sessionHeader = input_1.SessionHeader.fromJSON(JSON.stringify(jsonObject.session.header));
// Handle nodes
var nodes_1 = [];
jsonObject.session.nodes.forEach(function (nodeJSON) {
var node = __1.Node.fromJSON(JSON.stringify(nodeJSON));
nodes_1.push(node);
});
return new DispatchResponse(BigInt(jsonObject.block_height), sessionHeader, jsonObject.session.key, nodes_1);
}
catch (error) {
throw error;
}
};
/**
*
* Creates a JSON object with the DispatchResponse properties
* @returns {JSON} - JSON Object.
* @memberof DispatchResponse
*/
DispatchResponse.prototype.toJSON = function () {
return {
block_height: Number(this.blockHeight.toString()),
session: {
header: this.header.toJSON(),
key: this.key,
nodes: JSON.parse(JSON.stringify(this.nodes))
}
};
};
/**
*
* Check if the DispatchResponse object is valid
* @returns {boolean} - True or false.
* @memberof DispatchResponse
*/
DispatchResponse.prototype.isValid = function () {
return this.header.isValid()
&& this.key.length !== 0
&& Number(this.blockHeight.toString()) >= 0;
};
return DispatchResponse;
}());
exports.DispatchResponse = DispatchResponse;
//# sourceMappingURL=dispatch-response.js.map