@pokt-network/pocket-js
Version:
Pocket-js core package with the main functionalities to interact with the Pocket Network.
104 lines • 3.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Node = void 0;
var hex_1 = require("../../utils/hex");
var staking_status_1 = require("./staking-status");
/**
*
*
* @class Node
*/
var Node = /** @class */ (function () {
/**
* Creates a Node.
* @constructor
* @param {string} address - Node address hex.
* @param {string} publicKey - Node public key hex.
* @param {string} outputAddress - Output address hex.
* @param {boolean} jailed - True or false if the validator been jailed.
* @param {StakingStatus} status - Validator staking status
* @param {BigInt} stakedTokens - How many tokens are staked
* @param {URL} serviceURL - Service node URL
* @param {string[]} chains - A list of blockchain hash
* @param {string} unstakingCompletionTimestamp - If unstaking, the minimum time for the validator to complete unstaking
*/
function Node(address, publicKey, outputAddress, jailed, status, stakedTokens, serviceURL, chains, unstakingCompletionTimestamp) {
if (chains === void 0) { chains = []; }
if (unstakingCompletionTimestamp === void 0) { unstakingCompletionTimestamp = ""; }
this.alreadyInConsensus = false;
this.address = address;
this.publicKey = publicKey;
this.outputAddress = outputAddress;
this.jailed = jailed;
this.status = status;
this.stakedTokens = stakedTokens;
this.serviceURL = new URL(serviceURL);
this.chains = chains;
this.unstakingCompletionTimestamp = unstakingCompletionTimestamp;
if (!this.isValid()) {
throw new TypeError("Invalid Node properties.");
}
}
Node.fromJSON = function (json) {
try {
var rawObjValue = JSON.parse(json);
var status_1 = staking_status_1.StakingStatus.getStatus(rawObjValue.status);
return new Node(rawObjValue.address, rawObjValue.public_key, rawObjValue.output_address, rawObjValue.jailed, status_1, BigInt(rawObjValue.tokens), rawObjValue.service_url, rawObjValue.chains, rawObjValue.unstaking_time);
}
catch (error) {
throw error;
}
};
/**
*
* Creates a JSON object with the Node properties
* @returns {JSON} - JSON Object.
* @memberof Node
*/
Node.prototype.toJSON = function () {
return {
address: this.address,
chains: this.chains,
public_key: this.publicKey,
jailed: this.jailed,
service_url: this.serviceURL,
status: this.status.toString(),
tokens: Number(this.stakedTokens.toString()),
unstaking_time: this.unstakingCompletionTimestamp
};
};
/**
*
* Verify if all properties are valid
* @returns {boolean} - True or false.
* @memberof Node
*/
Node.prototype.isValid = function () {
var validAddress = true;
var validPubKey = true;
var validOutputAddress = true;
var validServiceUrl = true;
if (this.address) {
validAddress = hex_1.Hex.isHex(this.address);
}
if (this.publicKey) {
validPubKey = hex_1.Hex.isHex(this.publicKey);
}
if (this.outputAddress) {
validOutputAddress = hex_1.Hex.isHex(this.outputAddress);
}
if (this.serviceURL && this.serviceURL.pathname) {
validServiceUrl = this.serviceURL.pathname.length !== 0;
}
return validAddress &&
validPubKey &&
validOutputAddress &&
this.jailed !== undefined &&
validServiceUrl &&
this.status !== undefined &&
Number(this.stakedTokens.toString()) >= 0;
};
return Node;
}());
exports.Node = Node;
//# sourceMappingURL=node.js.map