UNPKG

@roochnetwork/rooch-sdk

Version:
155 lines (154 loc) 5.4 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var serializer_exports = {}; __export(serializer_exports, { Serializer: () => Serializer }); module.exports = __toCommonJS(serializer_exports); var import_bcs = require("@mysten/bcs"); var import_utils = require("../utils/index.js"); var import_address = require("../address/index.js"); const VECTOR_REGEX = /^vector<(.+)>$/; const STRUCT_REGEX = /^([^:]+)::([^:]+)::([^<]+)(<(.+)>)?/; class Serializer { static structTagToObjectID(input) { return `0x${(0, import_utils.toHEX)((0, import_utils.sha3_256)(typeof input === "string" ? input : Serializer.structTagToCanonicalString(input)))}`; } //TODO: rooch Address bug static accountNamedObjectID(address2, structTag) { let addressBytes; if (typeof address2 === "string") { const finalAddress = (0, import_utils.isHex)(address2) ? (0, import_address.normalizeRoochAddress)(address2) : address2; addressBytes = new import_address.RoochAddress(finalAddress).toBytes(); } else if ((0, import_utils.isBytes)(address2)) { addressBytes = address2; } else { addressBytes = address2.toBytes(); } const tagBytes = (0, import_utils.stringToBytes)( "utf8", typeof structTag === "string" ? structTag : Serializer.structTagToCanonicalString(structTag) ); return `0x${(0, import_utils.toHEX)((0, import_utils.sha3_256)((0, import_utils.concatBytes)(addressBytes, tagBytes)))}`; } static structTagToCanonicalString(input) { let result = `${(0, import_address.canonicalRoochAddress)(input.address)}::${input.module}::${input.name}`; if (input.typeParams && input.typeParams.length > 0) { const typeParams = input.typeParams.map(Serializer.typeTagToString).join(","); result += `<${typeParams}>`; } return result; } static typeTagToString(input) { if (typeof input === "string") { return input; } if ("Vector" in input) { return `vector<${Serializer.typeTagToString(input.Vector)}>`; } if ("Struct" in input) { return Serializer.structTagToCanonicalString(input.Struct); } throw new Error("Invalid TypeTag"); } static typeTagParseFromStr(str, normalizeAddress = false) { if (str === "address") { return { address: null }; } else if (str === "bool") { return { bool: null }; } else if (str === "u8") { return { u8: null }; } else if (str === "u16") { return { u16: null }; } else if (str === "u32") { return { u32: null }; } else if (str === "u64") { return { u64: null }; } else if (str === "u128") { return { u128: null }; } else if (str === "u256") { return { u256: null }; } else if (str === "signer") { return { signer: null }; } const vectorMatch = str.match(VECTOR_REGEX); if (vectorMatch) { return { vector: Serializer.typeTagParseFromStr(vectorMatch[1], normalizeAddress) }; } const structMatch = str.match(STRUCT_REGEX); if (structMatch) { const address2 = normalizeAddress ? (0, import_address.normalizeRoochAddress)(structMatch[1]) : structMatch[1]; return { struct: { address: address2, module: structMatch[2], name: structMatch[3], typeParams: structMatch[5] === void 0 ? [] : Serializer.parseStructTypeArgs(structMatch[5], normalizeAddress) } }; } throw new Error(`Encountered unexpected token when parsing type args for ${str}`); } static parseStructTypeArgs(str, normalizeAddress = false) { return (0, import_bcs.splitGenericParameters)(str).map( (tok) => Serializer.typeTagParseFromStr(tok, normalizeAddress) ); } static tagToString(tag) { if ("bool" in tag) { return "bool"; } if ("u8" in tag) { return "u8"; } if ("u16" in tag) { return "u16"; } if ("u32" in tag) { return "u32"; } if ("u64" in tag) { return "u64"; } if ("u128" in tag) { return "u128"; } if ("u256" in tag) { return "u256"; } if ("address" in tag) { return "address"; } if ("signer" in tag) { return "signer"; } if ("vector" in tag) { return `vector<${Serializer.tagToString(tag.vector)}>`; } if ("struct" in tag) { const struct = tag.struct; const typeParams = struct.typeParams.map(Serializer.tagToString).join(", "); return `${struct.address}::${struct.module}::${struct.name}${typeParams ? `<${typeParams}>` : ""}`; } throw new Error("Invalid TypeTag"); } } //# sourceMappingURL=serializer.js.map