Serializers
Encode objects into CBOR or bech32 format.
In smart contract manipulations, serialization is a crucial process that encode data structures or objects into a format that can be easily stored or transmitted and later reconstructed. Below are utilities to help serialize various Cardano smart contracts components.
Serialize Native Script
The function serializeNativeScript
allows you to provide the nativeScript
with an option of networkId
and stakeCredentialHash
, returns:
- Bech32 address
- Script Cbor
This example demonstrates how to derive the native script from the pubKeyHash
with the deserializeAddress
then serialize the native script to a bech32 address and script Cbor. To read more on deserializeAddress.
Serialize Native Script
Serialize Native script into bech32 address
import {
serializeNativeScript,
NativeScript,
deserializeAddress
} from "@meshsdk/core";
const { pubKeyHash: keyHash } = deserializeAddress(
'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9',
);
const nativeScript: NativeScript = {
type: "all",
scripts: [
{
type: "before",
slot: "99999999",
},
{
type: "sig",
keyHash: keyHash,
},
],
};
serializeNativeScript(nativeScript);
Serialize Plutus Script
The function serializePlutusScript
allows you to provide the plutusScript
with an option of networkId
and stakeCredentialHash
, returns:
- Bech32 address
This example demonstrates how to derive and serialize a plutus script into a bech32 address.
Serialize Plutus Script
Serialize Plutus script into bech32 address
import { PlutusScript, serializePlutusScript } from "@meshsdk/core";
const plutusScript: PlutusScript = {
code: demoPlutusAlwaysSucceedScript,
version: "V2",
};
serializePlutusScript(plutusScript);
Serialize Address Object
Serialize address in Cardano data JSON format into bech32 address with serializeAddressObj()
.
First you need to create an address object with pubKeyAddress()
or scriptAddress()
.
pubKeyAddress()
accepts the following parameters:
pubKeyAddress(
bytes: string,
stakeCredential?: string,
isStakeScriptCredential?: boolean
): PubKeyAddress
scriptAddress()
accepts the following parameters:
scriptAddress(
bytes: string,
stakeCredential?: string,
isStakeScriptCredential?: boolean
): ScriptAddress
serializeAddressObj()
accepts the following parameters:
serializeAddressObj(
address: PubKeyAddress | ScriptAddress,
networkId?: number
): string
Serialize Address Object
Serialize address in Cardano data JSON format into bech32 address
import { pubKeyAddress, serializeAddressObj } from "@meshsdk/core";
const address = pubKeyAddress(
'aa048e4cc8a1e67e1d97ffbd4be614388014cbc2b2451527202943b6',
'9d4dcd7e454d2434164f4efb8edeb358d86a1dad9ec6224cfcbce3e6'
);
serializeAddressObj(address, 1);