UNPKG

@pokt-network/pocket-js

Version:

Pocket-js core package with the main functionalities to interact with the Pocket Network.

159 lines 6.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RelayProof = void 0; var aat_js_1 = require("@pokt-network/aat-js"); var utils_1 = require("../../utils"); var js_sha3_1 = require("js-sha3"); var request_hash_1 = require("./input/request-hash"); /** * * * @class Proof */ var RelayProof = /** @class */ (function () { /** * Proof. * @constructor * @param {BigInt} entropy - Index entropy value. * @param {BigInt} sessionBlockHeight - Session Block Height. * @param {string} servicerPubKey - Service PublicKey. * @param {string} blockchain - Blockchain hash. * @param {PocketAAT} token - Application Authentication Token object. * @param {string} signature - Proof's signature. * @param {RequestHash} requestHash - RequestHash object. */ function RelayProof(entropy, sessionBlockHeight, servicerPubKey, blockchain, token, signature, requestHash) { this.entropy = entropy; this.sessionBlockHeight = sessionBlockHeight; this.servicerPubKey = servicerPubKey; this.blockchain = blockchain; this.token = token; this.signature = signature; this.requestHash = requestHash; if (!this.isValid()) { throw new TypeError("Invalid Proof properties."); } } /** * * Creates a Proof object using a JSON string * @param {string} json - JSON string. * @returns {RelayProof} - Proof object. * @memberof Proof */ RelayProof.fromJSON = function (json) { try { var jsonObject = JSON.parse(json); var pocketAAT = void 0; if (jsonObject.aat !== undefined) { pocketAAT = new aat_js_1.PocketAAT(jsonObject.aat.version, jsonObject.aat.client_pub_key, jsonObject.aat.app_pub_key, jsonObject.aat.signature); return new RelayProof(BigInt(jsonObject.entropy), BigInt(jsonObject.session_block_height), jsonObject.servicer_pub_key, jsonObject.blockchain, pocketAAT, jsonObject.signature, request_hash_1.RequestHash.fromJSON(jsonObject.request_hash)); } else { throw new Error("Failed to retrieve PocketAAT, property is undefined"); } } catch (error) { throw new Error("Failed to retrieve PocketAAT for Proof with error: " + error); } }; /** * * Creates a Proof object using a JSON string * @param {BigInt} entropy - Entropy big int value. * @param {BigInt} sessionBlockHeight - Session Block Height. * @param {string} servicerPubKey - Servicer Public Key. * @param {string} blockchain - Blockchain hash. * @param {PocketAAT} token - PocketAAT token. * @param {RequestHash} requestHash - RequestHash object. * @returns {Buffer} - Buffer. * @memberof RelayProof */ RelayProof.bytes = function (entropy, sessionBlockHeight, servicerPubKey, blockchain, token, requestHash) { var proofJSON = { entropy: Number(entropy.toString()), session_block_height: Number(sessionBlockHeight.toString()), servicer_pub_key: servicerPubKey, blockchain: blockchain, signature: "", token: RelayProof.hashAAT(token), request_hash: RelayProof.hashRequest(requestHash) }; var proofJSONStr = JSON.stringify(proofJSON); // Hash proofJSONStr var hash = js_sha3_1.sha3_256.create(); hash.update(proofJSONStr); return Buffer.from(hash.hex(), "hex"); }; /** * * Creates a hash using the PocketAAT object * @param {PocketAAT} aat - PocketAAT token. * @returns {string} - PocketAAT Hash string. * @memberof RelayProof */ RelayProof.hashAAT = function (aat) { var aatObj = { version: aat.version, app_pub_key: aat.applicationPublicKey, client_pub_key: aat.clientPublicKey, signature: "" }; // Generate sha3 hash of the aat payload object var hash = js_sha3_1.sha3_256.create(); hash.update(JSON.stringify(aatObj)); return hash.hex(); }; /** * * Creates a hash using the RequestHash object * @param {RequestHash} request - RequestHash object. * @returns {string} - RequestHash string. * @memberof RelayProof */ RelayProof.hashRequest = function (request) { var requestObjStr = JSON.stringify(request.toJSON()); var hash = js_sha3_1.sha3_256.create(); hash.update(requestObjStr); return hash.hex(); }; /** * * Creates a JSON object with the Proof properties * @returns {JSON} - JSON Object. * @memberof Proof */ RelayProof.prototype.toJSON = function () { return { entropy: Number(this.entropy.toString()), session_block_height: Number(this.sessionBlockHeight.toString()), servicer_pub_key: this.servicerPubKey, blockchain: this.blockchain, aat: { version: this.token.version, app_pub_key: this.token.applicationPublicKey, client_pub_key: this.token.clientPublicKey, signature: this.token.applicationSignature }, signature: this.signature, request_hash: RelayProof.hashRequest(this.requestHash) }; }; /** * * Check if the Proof object is valid * @returns {boolean} - True or false. * @memberof Proof */ RelayProof.prototype.isValid = function () { return this.requestHash.isValid() && this.blockchain.length !== 0 && Number(this.entropy.toString()) !== undefined && utils_1.Hex.isHex(this.servicerPubKey) && Number(this.sessionBlockHeight) > 0 && this.token.isValid(); }; return RelayProof; }()); exports.RelayProof = RelayProof; //# sourceMappingURL=relay-proof.js.map