UNPKG

cosmic-lib

Version:

A JavaScript implementation of the CosmicLink protocol for Stellar

150 lines (119 loc) 4.31 kB
"use strict"; /** * Contains the methods to encode values formatted in `transaction descriptor` * format into URI format. * * @private * @exports encode */ var encode = exports; var decode = require("./decode"); var specs = require("./specs"); encode.query = function (conf, tdesc) { if (conf.errors) return undefined; var command; if (!tdesc.operations.length || tdesc.operations.length > 1 || tdesc.operations[0].source) { command = "transaction"; } else { command = tdesc.operations[0].type; } var query = "?type=" + command; specs.transactionOptionalFields.forEach(function (field) { if (tdesc[field] !== undefined) query += encode.field(conf, field, tdesc[field]); }); tdesc.operations.forEach(function (odesc) { if (command === "transaction") query += "&operation=" + odesc.type; for (var field in odesc) { if (field === "type") continue; // Syntactic sugar for offer deletion. if (odesc.type === "manageOffer" && odesc.amount == "0" && odesc.offerId) { if (field === "price" && odesc.price === "1") continue; if (field === "buying" && odesc.buying.code === "XLM" && odesc.buying.issuer === specs.neutralAccountId) { continue; } } query += encode.field(conf, field, odesc[field]); } }); return query; }; /******************************************************************************/ /** * Encode `value` into cosmic link query format and return * `&${field}=${encodedValue}` * * @param {string} field * @param {*} value `value` should use cosmic link JSON format * @return {string} */ encode.field = function (conf, field, value) { var type = specs.fieldType[field]; if (!type) throw new Error("Invalid field: ".concat(field)); var encodedValue = encode.type(conf, type, value); if (encodedValue === "" && field !== "homeDomain") return "";else return "&".concat(field, "=").concat(encodedValue); }; /** * Encode `value` into cosmic link query format according to `type`. * * @param {string} field * @param {*} value `value` should use cosmic link JSON format * @return {string} */ encode.type = function (conf, type, value) { if (value === undefined) return ""; var encodedValue = process[type] ? process[type](conf, value) : value; if (encodedValue === undefined) return "";else return encodedValue; }; /******************************************************************************/ var process = {}; process.asset = function (conf, asset) { if (asset.issuer) return encodeURIComponent(asset.code) + ":" + encodeURIComponent(asset.issuer); }; process.assetsArray = function (conf, assetsArray) { return assetsArray.map(function (asset) { return encode.asset(conf, asset); }).toString(); }; process["boolean"] = function (conf, _boolean) { if (_boolean === false) return "false"; }; process.buffer = function (conf, buffer) { if (buffer.type === "text") { // Guard against prefix mis-interpretation. var decoded = decode.buffer(conf, buffer.value); if (decoded.type === "text") return encodeURIComponent(buffer.value); } else if (buffer) { return buffer.type + ":" + encodeURIComponent(buffer.value); } }; process.date = function (conf, date) { return date.replace(/Z$/, ""); }; process.memo = function (conf, memo) { if (memo.type === "text") { // Guard against prefix mis-interpretation. var decoded = decode.memo(conf, memo.value); if (decoded.type === "text") return encodeURIComponent(memo.value); } return memo.type + ":" + encodeURIComponent(memo.value); }; process.price = function (conf, price) { if (typeof price === "string") return price;else return price.n + ":" + price.d; }; process.signer = function (conf, signer) { return signer.weight + ":" + signer.type + ":" + signer.value; }; process.string = encode.buffer; process.url = function (conf, url) { if (url.substr(0, 8) === "https://") url = url.substr(8); return encodeURIComponent(url); }; /******************************************************************************/ /** * Provide dummy aliases for every other type for convenience & backward * compatibility. */ specs.types.forEach(function (type) { exports[type] = function (conf, value) { return encode.type(conf, type, value); }; });