diff --git a/packages/mesh-core/src/utils/serializer.ts b/packages/mesh-core/src/utils/serializer.ts index 4e8d9d188..18f4ca1b9 100644 --- a/packages/mesh-core/src/utils/serializer.ts +++ b/packages/mesh-core/src/utils/serializer.ts @@ -1,6 +1,11 @@ +import JSONBig from "json-bigint"; + import { + BuilderData, + Data, DeserializedAddress, NativeScript, + PlutusDataType, PlutusScript, PubKeyAddress, ScriptAddress, @@ -100,3 +105,24 @@ export const serializeRewardAddress = ( ? core.scriptHashToRewardAddress(hash, networkId) : core.keyHashToRewardAddress(hash, networkId); }; + +/** + * Serialize the data from Mesh or JSON format into CBOR hex + * @param data The data in Mesh or JSON format + * @param type The data type + * @returns The CBOR hex string + */ +export const serializeData = ( + rawData: BuilderData["content"], + type: Omit, +): string => { + if (type === "Mesh") { + return core.toPlutusData(rawData as Data).to_hex(); + } + + let data = rawData; + if (typeof rawData === "object") { + data = JSONBig.stringify(data); + } + return core.csl.PlutusData.from_json(data as string, 1).to_hex(); +};