UNPKG

oip-js

Version:

The core module of OIP written in Javascript!

219 lines (202 loc) 7.08 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } module.exports = function () { function Multipart(inputString, txid) { _classCallCheck(this, Multipart); this.prefix = "oip-mp"; this.partNumber = 0; this.totalParts = 0; this.publisherAddress = undefined; this.firstPartTXID = ""; this.signature = ""; this.choppedStringData = ""; // This is used to track a reference if someone pulled a multipart from an endpoint this.txid = ""; if (txid) this.setTXID(txid); if (inputString) this.fromString(inputString); } _createClass(Multipart, [{ key: "setPrefix", value: function setPrefix(prefix) { this.prefix = prefix; } }, { key: "getPrefix", value: function getPrefix() { return this.prefix; } }, { key: "setPartNumber", value: function setPartNumber(partNumber) { this.partNumber = partNumber; } }, { key: "getPartNumber", value: function getPartNumber() { return this.partNumber; } }, { key: "setTotalParts", value: function setTotalParts(totalParts) { this.totalParts = totalParts; } }, { key: "getTotalParts", value: function getTotalParts() { return this.totalParts; } }, { key: "setPublisherAddress", value: function setPublisherAddress(publisherAddress) { this.publisherAddress = publisherAddress; } }, { key: "getPublisherAddress", value: function getPublisherAddress() { return this.publisherAddress; } }, { key: "setFirstPartTXID", value: function setFirstPartTXID(firstTXID) { this.firstPartTXID = firstTXID; } }, { key: "getFirstPartTXID", value: function getFirstPartTXID() { return this.firstPartTXID; } }, { key: "setSignature", value: function setSignature(signature) { this.signature = signature; } }, { key: "getSignature", value: function getSignature() { return this.signature; } }, { key: "getSignatureData", value: function getSignatureData() { return this.partNumber + "-" + this.totalParts + "-" + this.publisherAddress + "-" + this.firstPartTXID + "-" + this.choppedStringData; } }, { key: "validateSignature", value: function validateSignature() { return true; } }, { key: "setChoppedStringData", value: function setChoppedStringData(strData) { this.choppedStringData = strData; } }, { key: "getChoppedStringData", value: function getChoppedStringData() { return this.choppedStringData; } }, { key: "setTXID", value: function setTXID(id) { this.txid = id; } }, { key: "getTXID", value: function getTXID() { return this.txid; } }, { key: "addJSONIdentifier", value: function addJSONIdentifier() { if (this.getPartNumber() === 0) return "json:"; return ""; } }, { key: "isValid", value: function isValid() { if (this.getPrefix() !== "oip-mp") { return { success: false, message: "Invalid Multipart Prefix!" }; } if (this.getPartNumber() < 0) { return { success: false, message: "Part number must be positive!" }; } if (this.getPartNumber() > this.getTotalParts()) { return { success: false, message: "Part number too high for total parts!" }; } if (this.getTotalParts() < 1) { return { success: false, message: "Must have more than one part to be a MULTIPART message!" }; } if (this.getPublisherAddress() === "") { return { success: false, message: "Must have a Publisher Address!" }; } if (this.getFirstPartTXID() === "" && this.getPartNumber() !== 0) { console.log(this.getFirstPartTXID(), this.getPartNumber()); return { success: false, message: "Only the first part in a multipart message can have a blank first part TXID!" }; } if (!this.validateSignature()) { return { success: false, message: "Invalid Signature!" }; } return { success: true }; } }, { key: "toString", value: function toString() { return this.getPrefix() + "(" + this.getPartNumber() + "," + this.getTotalParts() + "," + this.getPublisherAddress() + "," + this.getFirstPartTXID() + "," + this.getSignature() + "):" + this.addJSONIdentifier() + this.getChoppedStringData(); } }, { key: "fromString", value: function fromString(multipartString) { if (!multipartString || typeof multipartString !== "string") return false; // Split the input string into an array of all the characters var characters = multipartString.split(''); // A string to hold the currently being built string var builtString = ""; // Information about what split points we have hit in the loop below var prefixSet = false; var parenValuesComplete = 0; var totalParenValues = 4; var closeSet = false; // Now we go through the array to find all the split points and break the string up. for (var i = 0; i < characters.length; i++) { // The first split point is an open paren, we will pull the prefix from the first built part if (characters[i] === "(" && !prefixSet) { this.setPrefix(builtString); builtString = ""; prefixSet = true; } else if (characters[i] === "," && prefixSet && parenValuesComplete < totalParenValues) { switch (parenValuesComplete) { case 0: this.setPartNumber(parseInt(builtString)); case 1: this.setTotalParts(parseInt(builtString)); case 2: this.setPublisherAddress(builtString); case 3: this.setFirstPartTXID(builtString); } builtString = ""; parenValuesComplete++; } else if (characters[i] === ")" && prefixSet && parenValuesComplete === totalParenValues && !closeSet) { this.setSignature(builtString); builtString = ""; closeSet = true; if (characters.length >= i + 1 && characters[i + 1] === ":") { // Check if we are prefixed with "json:", if so, skip ahead :) if (characters.length >= i + 2 && characters[i + 2] === "j" && characters.length >= i + 3 && characters[i + 3] === "s" && characters.length >= i + 4 && characters[i + 4] === "o" && characters.length >= i + 5 && characters[i + 5] === "n" && characters.length >= i + 6 && characters[i + 6] === ":") { i += 6; } else { i++; } } } else { // If we are not the first split point, then add our character to the build string builtString += characters[i]; } } // Set the final built string to the appended string data this.setChoppedStringData(builtString); } }]); return Multipart; }();