@pokt-network/pocket-js
Version:
Pocket-js core package with the main functionalities to interact with the Pocket Network.
118 lines • 4.76 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MsgProtoNodeStakeTx = void 0;
var tx_signer_1 = require("./../proto/generated/tx-signer");
var any_1 = require("../proto/generated/google/protobuf/any");
var tx_msg_1 = require("./tx-msg");
var utils_1 = require("./../../../utils");
/**
* Model representing a MsgNodeStake to stake as an Node in the Pocket Network
*/
var MsgProtoNodeStakeTx = /** @class */ (function (_super) {
__extends(MsgProtoNodeStakeTx, _super);
/**
* @param {Buffer} pubKey - Public key
* @param {Buffer} outputAddress - Output address when unstaking
* @param {string[]} chains - String array containing a list of blockchain hashes
* @param {string} amount - Amount to be sent, has to be a valid number and cannot be lesser than 0
* @param {URL} serviceURL - Service node URL, needs to be https://
*/
function MsgProtoNodeStakeTx(pubKey, outputAddress, chains, amount, serviceURL) {
var _this = _super.call(this) || this;
_this.KEY = "/x.nodes.MsgProtoStake8";
_this.AMINO_KEY = "pos/8.0MsgStake";
_this.DEFAULT_PORT = "443";
_this.DEFAULT_PROTOCOL = "https:";
_this.pubKey = pubKey;
_this.chains = chains;
_this.amount = amount;
_this.serviceURL = serviceURL;
_this.outputAddress = outputAddress;
if (!!!_this.serviceURL.port) {
_this.serviceURL.port = "443";
}
var outputErrorOrUndefined = utils_1.validateAddressHex(_this.outputAddress.toString("hex"));
if (utils_1.typeGuard(outputErrorOrUndefined, Error)) {
throw outputErrorOrUndefined;
}
var amountNumber = Number(_this.amount) || -1;
if (isNaN(amountNumber)) {
throw new Error("Amount is not a valid number");
}
else if (amountNumber < 0) {
throw new Error("Amount < 0");
}
else if (_this.chains.length === 0) {
throw new Error("Chains is empty");
}
else if (!utils_1.validatePublicKey(_this.pubKey)) {
throw new Error("Invalid public key");
}
else if (!utils_1.validateServiceURL(_this.serviceURL)) {
throw new Error("Invalid Service URL");
}
return _this;
}
/**
* Returns the parsed serviceURL
* @returns {string} - Parsed serviceURL
* @memberof MsgNodeStake
*/
MsgProtoNodeStakeTx.prototype.getParsedServiceURL = function () {
return (this.serviceURL.protocol ? this.serviceURL.protocol : this.DEFAULT_PROTOCOL) + "//" + this.serviceURL.hostname + ":" + (this.serviceURL.port ? this.serviceURL.port : this.DEFAULT_PORT);
};
/**
* Converts an Msg Object to StdSignDoc
* @returns {object} - Msg type key value.
* @memberof MsgNodeStake
*/
MsgProtoNodeStakeTx.prototype.toStdSignDocMsgObj = function () {
return {
type: this.AMINO_KEY,
value: {
chains: this.chains,
output_address: this.outputAddress.toString("hex"),
public_key: {
type: "crypto/ed25519_public_key",
value: this.pubKey.toString("hex")
},
service_url: this.getParsedServiceURL(),
value: this.amount,
}
};
};
/**
* Converts an Msg Object to StdSignDoc
* @returns {any} - Msg type key value.
* @memberof MsgNodeStake
*/
MsgProtoNodeStakeTx.prototype.toStdTxMsgObj = function () {
var data = {
Publickey: this.pubKey,
Chains: this.chains,
value: this.amount,
ServiceUrl: this.getParsedServiceURL(),
OutAddress: this.outputAddress
};
return any_1.Any.fromJSON({
"typeUrl": this.KEY,
"value": Buffer.from(tx_signer_1.MsgProtoNodeStake8.encode(data).finish()).toString("base64")
});
};
return MsgProtoNodeStakeTx;
}(tx_msg_1.TxMsg));
exports.MsgProtoNodeStakeTx = MsgProtoNodeStakeTx;
//# sourceMappingURL=msg-proto-node-stake.js.map