Skip to content

Commit

Permalink
feat: add serializer utils
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Dec 9, 2024
1 parent cbf160f commit 38c452d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/mesh-core/src/utils/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import JSONBig from "json-bigint";

import {
BuilderData,
Data,
DeserializedAddress,
NativeScript,
PlutusDataType,
PlutusScript,
PubKeyAddress,
ScriptAddress,
Expand Down Expand Up @@ -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<PlutusDataType, "CBOR">,
): 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();
};

0 comments on commit 38c452d

Please sign in to comment.