UNPKG

caip-hedera

Version:
437 lines (419 loc) 12.4 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = global || self, factory(global.caipHedera = {})); })(this, (function (exports) { function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } var CAIP2$1 = { name: "chainId", regex: "[-:a-zA-Z0-9]{5,41}", parameters: { delimiter: ":", values: { 0: { name: "namespace", regex: "[-a-z0-9]{3,8}" }, 1: { name: "reference", regex: "[-a-zA-Z0-9]{1,32}" } } } }; var CAIP10$1 = { name: "accountId", regex: "[-:a-zA-Z0-9]{7,106}", parameters: { delimiter: ":", values: { 0: { name: "namespace", regex: "[-a-z0-9]{3,8}" }, 1: { name: "reference", regex: "[-a-zA-Z0-9]{1,32}" }, 2: { name: "address", regex: "[a-zA-Z0-9]{1,64}" } } } }; var AssetName$1$1 = { name: "assetName", regex: "[-:a-zA-Z0-9]{5,73}", parameters: { delimiter: ":", values: { 0: { name: "namespace", regex: "[-a-z0-9]{3,8}" }, 1: { name: "reference", regex: "[-a-zA-Z0-9]{1,64}" } } } }; var CAIP19AssetType = { name: "assetType", regex: "[-:a-zA-Z0-9]{11,115}", parameters: { delimiter: "/", values: { 0: CAIP2$1, 1: AssetName$1$1 } } }; var CAIP19AssetId = { name: "assetId", regex: "[-:a-zA-Z0-9]{13,148}", parameters: { delimiter: "/", values: { 0: CAIP2$1, 1: AssetName$1$1, 2: { name: "tokenId", regex: "[-a-zA-Z0-9]{1,32}" } } } }; var CAIP = { "2": CAIP2$1, "10": CAIP10$1, "19": { assetName: AssetName$1$1, assetType: CAIP19AssetType, assetId: CAIP19AssetId } }; function splitParams$1(id, spec) { return id.split(spec.parameters.delimiter); } function getParams$1(id, spec) { var arr = splitParams$1(id, spec); var params = {}; arr.forEach(function (value, index) { params[spec.parameters.values[index].name] = value; }); return params; } function joinParams$1(params, spec) { return Object.values(spec.parameters.values).map(function (parameter) { var param = params[parameter.name]; return typeof param === "string" ? param : joinParams$1(param, parameter); }).join(spec.parameters.delimiter); } function isValidId$1(id, spec) { if (!new RegExp(spec.regex).test(id)) return false; var params = splitParams$1(id, spec); if (params.length !== Object.keys(spec.parameters.values).length) return false; var matches = params.map(function (param, index) { return new RegExp(spec.parameters.values[index].regex).test(param); }).filter(function (x) { return !!x; }); if (matches.length !== params.length) return false; return true; } var ChainId = /*#__PURE__*/function () { function ChainId(params) { if (typeof params === "string") { params = ChainId.parse(params); } this.namespace = params.namespace; this.reference = params.reference; } ChainId.parse = function parse(id) { if (!isValidId$1(id, this.spec)) { throw new Error("Invalid " + this.spec.name + " provided: " + id); } return new ChainId(getParams$1(id, this.spec)).toJSON(); }; ChainId.format = function format(params) { return joinParams$1(params, this.spec); }; var _proto = ChainId.prototype; _proto.toString = function toString() { return ChainId.format(this.toJSON()); }; _proto.toJSON = function toJSON() { return { namespace: this.namespace, reference: this.reference }; }; return ChainId; }(); ChainId.spec = CAIP["2"]; function splitParams(id, spec) { return id.split(spec.parameters.delimiter); } function getParams(id, spec) { var arr = splitParams(id, spec); var params = {}; arr.forEach(function (value, index) { params[spec.parameters.values[index].name] = value; }); return params; } function joinParams(params, spec) { return Object.values(spec.parameters.values).map(function (parameter) { var param = params[parameter.name]; return typeof param === "string" ? param : joinParams(param, parameter); }).join(spec.parameters.delimiter); } function isValidId(id, spec) { if (!new RegExp(spec.regex).test(id)) return false; var params = splitParams(id, spec); if (params.length !== Object.keys(spec.parameters.values).length) return false; var matches = params.map(function (param, index) { return new RegExp(spec.parameters.values[index].regex).test(param); }).filter(function (x) { return !!x; }); if (matches.length !== params.length) return false; return true; } var CAIP2 = { name: "chainId", regex: "hedera:[-a-zA-Z0-9]{5,32}", parameters: { delimiter: ":", values: { 0: { name: "namespace", regex: "hedera" }, 1: { name: "reference", regex: "[-a-zA-Z0-9]{5,32}" } } } }; var CAIP10 = { name: "accountId", regex: "hedera:[-a-zA-Z0-9]{5,32}:[0-9]{1,19}.[0-9]{1,19}.[0-9]{1,19}(-[a-z]{5}){0,1}", parameters: { delimiter: ":", values: { 0: { name: "namespace", regex: "hedera" }, 1: { name: "reference", regex: "[-a-zA-Z0-9]{5,32}" }, 2: { name: "address", regex: "[0-9]{1,19}.[0-9]{1,19}.[0-9]{1,19}(-[a-z]{5}){0,1}" } } } }; var AssetName$1 = { name: "assetName", regex: "[-a-zA-Z]{3,5}:[0-9]{1,19}.[0-9]{1,19}.[0-9]{1,19}(-[a-z]{5}){0,1}", parameters: { delimiter: ":", values: { 0: { name: "namespace", regex: "[-a-zA-Z]{3,5}" }, 1: { name: "reference", regex: "[0-9]{1,19}.[0-9]{1,19}.[0-9]{1,19}(-[a-z]{5}){0,1}" } } } }; var CAIP19_AssetType = { name: "assetType", regex: "hedera:[-a-zA-Z0-9]{5,32}/[-a-zA-Z]{3,5}:[0-9]{1,19}.[0-9]{1,19}.[0-9]{1,19}(-[a-z]{5}){0,1}", parameters: { delimiter: "/", values: { 0: CAIP2, 1: AssetName$1 } } }; var CAIP19_AssetId = { name: "assetId", regex: "hedera:[-a-zA-Z0-9]{5,32}/[-a-zA-Z]{3,5}:[0-9]{1,19}.[0-9]{1,19}.[0-9]{1,19}(-[a-z]{5}){0,1}/[0-9]{1,19}", parameters: { delimiter: "/", values: { 0: CAIP2, 1: AssetName$1, 2: { name: "tokenId", regex: "[0-9]{1,19}" } } } }; var Specs = [CAIP2, CAIP10, AssetName$1, CAIP19_AssetType, CAIP19_AssetId]; var AssetName = /*#__PURE__*/function () { AssetName.parse = function parse(id) { if (!isValidId(id, this.spec)) { throw new Error("Invalid " + this.spec.name + " provided: " + id); } return new AssetName(getParams(id, this.spec)).toJSON(); }; AssetName.format = function format(params) { return joinParams(params, this.spec); }; function AssetName(params) { this.namespace = void 0; this.reference = void 0; if (typeof params === "string") { params = AssetName.parse(params); } this.namespace = params.namespace; this.reference = params.reference; } var _proto = AssetName.prototype; _proto.toString = function toString() { return AssetName.format(this.toJSON()); }; _proto.toJSON = function toJSON() { return { namespace: this.namespace, reference: this.reference }; }; return AssetName; }(); AssetName.spec = Specs[2]; var Hedera = /*#__PURE__*/function () { Hedera.parse = function parse(id) { var params = { namespace: null, reference: null, chainId: null, address: null, assetName: null, tokenId: null }; if (new RegExp(Specs[4].regex).test(id) === true) { if (!isValidId(id, Specs[4])) { throw new Error("Invalid " + Specs[4].name + " provided " + id); } params = getParams(id, Specs[4]); } else if (new RegExp(Specs[3].regex).test(id) === true) { if (!isValidId(id, Specs[3])) { throw new Error("Invalid " + Specs[3].name + " provided " + id); } params = getParams(id, Specs[3]); } else if (new RegExp(Specs[1].regex).test(id) === true) { if (!isValidId(id, Specs[1])) { throw new Error("Invalid " + Specs[1].name + " provided " + id); } var _utils$getParams = getParams(id, Specs[1]), namespace = _utils$getParams.namespace, reference = _utils$getParams.reference, address = _utils$getParams.address; params = { chainId: { namespace: namespace, reference: reference }, address: address }; } else if (new RegExp(Specs[0].regex).test(id) === true) { if (!isValidId(id, Specs[0])) { throw new Error("Invalid " + Specs[0].name + " provided " + id); } params = getParams(id, Specs[0]); } return new Hedera(params).toJSON(); }; Hedera.format = function format(params) { if (params.hasOwnProperty("tokenId")) { return joinParams(params, Specs[4]); } else if (params.hasOwnProperty("assetName")) { return joinParams(params, Specs[3]); } else if (params.hasOwnProperty("address")) { var pars = params; var chain = new ChainId(params.chainId); var splitParams = _extends({}, chain.toJSON(), { address: pars.address }); return joinParams(splitParams, Specs[1]); } else if (params.hasOwnProperty("namespace")) { return joinParams(params, Specs[0]); } return ""; }; function Hedera(params) { this.namespace = void 0; this.reference = void 0; this.chainId = void 0; this.address = void 0; this.assetName = void 0; this.tokenId = void 0; if (typeof params === "string") { params = Hedera.parse(params); } this.namespace = params.namespace != null ? params.namespace : null; this.reference = params.reference != null ? params.reference : null; this.chainId = params.chainId != null ? new ChainId(params.chainId) : null; this.address = params.address != null ? params.address : null; this.assetName = params.assetName != null ? new AssetName(params.assetName) : null; this.tokenId = params.tokenId != null ? params.tokenId : null; } var _proto = Hedera.prototype; _proto.toString = function toString() { return Hedera.format(this.toJSON()); }; _proto.toJSON = function toJSON() { var json = {}; if (this.namespace !== null && this.namespace !== undefined) { json.namespace = this.namespace; } if (this.reference !== null && this.reference !== undefined) { json.reference = this.reference; } if (this.chainId !== null && this.chainId !== undefined) { json.chainId = this.chainId.toJSON(); } if (this.address !== null && this.address !== undefined) { json.address = this.address; } if (this.assetName !== null && this.assetName !== undefined) { json.assetName = this.assetName.toJSON(); } if (this.tokenId !== null && this.tokenId !== undefined) { json.tokenId = this.tokenId; } return json; }; return Hedera; }(); exports.Hedera = Hedera; })); //# sourceMappingURL=index.umd.js.map