Mesh LogoMesh

Serializers

Encode Cardano scripts and data into CBOR or bech32 format

Use serializer functions to encode Cardano data structures into formats suitable for on-chain storage or transmission. These utilities convert scripts into addresses and data objects into their serialized representations.

Overview

Serialization is essential for smart contract development. It encodes data structures into formats that can be stored on-chain or transmitted across the network. MeshJS provides serializers for:

  • Native scripts (multisig, time-locked)
  • Plutus scripts (smart contracts)
  • Address objects (payment and stake credentials)

Quick Start

import { serializePlutusScript, PlutusScript } from "@meshsdk/core";

const script: PlutusScript = {
  code: "5906f4010100...",
  version: "V2",
};

const address = serializePlutusScript(script);

serializeNativeScript

Convert a native script into a bech32 address and script CBOR.

Function Signature

serializeNativeScript(
  nativeScript: NativeScript,
  networkId?: number,
  stakeCredentialHash?: string
): { address: string; scriptCbor: string }

Parameters

ParameterTypeRequiredDescription
nativeScriptNativeScriptYesThe native script object to serialize
networkIdnumberNoNetwork ID (0 for testnet, 1 for mainnet). Defaults to 0
stakeCredentialHashstringNoOptional stake credential hash for the address

Returns

PropertyTypeDescription
addressstringThe bech32 script address
scriptCborstringThe script in CBOR format

Example

import {
  serializeNativeScript,
  deserializeAddress,
  NativeScript,
} from "@meshsdk/core";

// Get the public key hash from an address
const { pubKeyHash: keyHash } = deserializeAddress(
  "addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9"
);

// Create a time-locked multisig script
const nativeScript: NativeScript = {
  type: "all",
  scripts: [
    {
      type: "before",
      slot: "99999999",
    },
    {
      type: "sig",
      keyHash: keyHash,
    },
  ],
};

// Serialize to get address and CBOR
const { address, scriptCbor } = serializeNativeScript(nativeScript);

console.log("Script Address:", address);
console.log("Script CBOR:", scriptCbor);

Native Script Types

The NativeScript type supports these script types:

TypeDescriptionRequired Fields
sigRequires a signature from a keykeyHash
allRequires all scripts to be satisfiedscripts
anyRequires any one script to be satisfiedscripts
atLeastRequires at least N scripts to be satisfiedrequired, scripts
beforeValid before a slotslot
afterValid after a slotslot

serializePlutusScript

Convert a Plutus script into a bech32 address.

Function Signature

serializePlutusScript(
  plutusScript: PlutusScript,
  networkId?: number,
  stakeCredentialHash?: string
): string

Parameters

ParameterTypeRequiredDescription
plutusScriptPlutusScriptYesThe Plutus script object to serialize
networkIdnumberNoNetwork ID (0 for testnet, 1 for mainnet). Defaults to 0
stakeCredentialHashstringNoOptional stake credential hash for the address

Returns

TypeDescription
stringThe bech32 script address

Example

import { serializePlutusScript, PlutusScript } from "@meshsdk/core";

const plutusScript: PlutusScript = {
  code: "5906f4010100323232323232...", // Your compiled Plutus script
  version: "V2",
};

// Get the script address on testnet
const address = serializePlutusScript(plutusScript, 0);
console.log("Script Address:", address);

// Get the script address on mainnet with stake credential
const mainnetAddress = serializePlutusScript(
  plutusScript,
  1,
  "9e8a6e5fcbbb5b84deefc71d7cb6319a3da9cc3d19765efb303647ef"
);
console.log("Mainnet Address:", mainnetAddress);

PlutusScript Type

interface PlutusScript {
  code: string;    // The compiled script CBOR
  version: string; // "V1", "V2", or "V3"
}

serializeAddressObj

Convert a Cardano data address object into a bech32 address string.

Function Signature

serializeAddressObj(
  address: PubKeyAddress | ScriptAddress,
  networkId?: number
): string

Parameters

ParameterTypeRequiredDescription
addressPubKeyAddress | ScriptAddressYesThe address object in Cardano data format
networkIdnumberNoNetwork ID (0 for testnet, 1 for mainnet). Defaults to 0

Returns

TypeDescription
stringThe bech32 address

Example

import { pubKeyAddress, serializeAddressObj } from "@meshsdk/core";

// Create a public key address object
const address = pubKeyAddress(
  "aa048e4cc8a1e67e1d97ffbd4be614388014cbc2b2451527202943b6",
  "9d4dcd7e454d2434164f4efb8edeb358d86a1dad9ec6224cfcbce3e6"
);

// Serialize to bech32 on mainnet
const bech32Address = serializeAddressObj(address, 1);
console.log("Bech32 Address:", bech32Address);

pubKeyAddress

Create a public key address object for use with serializeAddressObj.

Function Signature

pubKeyAddress(
  bytes: string,
  stakeCredential?: string,
  isStakeScriptCredential?: boolean
): PubKeyAddress

Parameters

ParameterTypeRequiredDescription
bytesstringYesThe payment key hash (56 hex characters)
stakeCredentialstringNoThe stake credential hash
isStakeScriptCredentialbooleanNoWhether the stake credential is a script. Defaults to false

Returns

TypeDescription
PubKeyAddressThe address object in Cardano data format

Example

import { pubKeyAddress, serializeAddressObj } from "@meshsdk/core";

// Enterprise address (no stake key)
const enterpriseAddress = pubKeyAddress(
  "aa048e4cc8a1e67e1d97ffbd4be614388014cbc2b2451527202943b6"
);

// Base address (with stake key)
const baseAddress = pubKeyAddress(
  "aa048e4cc8a1e67e1d97ffbd4be614388014cbc2b2451527202943b6",
  "9d4dcd7e454d2434164f4efb8edeb358d86a1dad9ec6224cfcbce3e6"
);

console.log(serializeAddressObj(enterpriseAddress, 0));
console.log(serializeAddressObj(baseAddress, 0));

scriptAddress

Create a script address object for use with serializeAddressObj.

Function Signature

scriptAddress(
  bytes: string,
  stakeCredential?: string,
  isStakeScriptCredential?: boolean
): ScriptAddress

Parameters

ParameterTypeRequiredDescription
bytesstringYesThe script hash (56 hex characters)
stakeCredentialstringNoThe stake credential hash
isStakeScriptCredentialbooleanNoWhether the stake credential is a script. Defaults to false

Returns

TypeDescription
ScriptAddressThe address object in Cardano data format

Example

import { scriptAddress, serializeAddressObj } from "@meshsdk/core";

// Script address with stake credential
const address = scriptAddress(
  "5867c3b8e27840f556ac268b781578b14c5661fc63ee720dbeab663f",
  "9d4dcd7e454d2434164f4efb8edeb358d86a1dad9ec6224cfcbce3e6"
);

const bech32 = serializeAddressObj(address, 0);
console.log("Script Address:", bech32);

Complete Example

This example demonstrates a complete workflow for serializing different script types:

import {
  serializeNativeScript,
  serializePlutusScript,
  pubKeyAddress,
  scriptAddress,
  serializeAddressObj,
  deserializeAddress,
  NativeScript,
  PlutusScript,
} from "@meshsdk/core";

// 1. Serialize a Native Script
const { pubKeyHash } = deserializeAddress(
  "addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9"
);

const nativeScript: NativeScript = {
  type: "all",
  scripts: [
    { type: "sig", keyHash: pubKeyHash },
    { type: "after", slot: "1000000" },
  ],
};

const { address: nativeAddress, scriptCbor } = serializeNativeScript(nativeScript, 0);
console.log("Native Script Address:", nativeAddress);

// 2. Serialize a Plutus Script
const plutusScript: PlutusScript = {
  code: "5906f4010100...",
  version: "V2",
};

const plutusAddress = serializePlutusScript(plutusScript, 0);
console.log("Plutus Script Address:", plutusAddress);

// 3. Serialize Address Objects
const pkAddress = pubKeyAddress(
  "aa048e4cc8a1e67e1d97ffbd4be614388014cbc2b2451527202943b6",
  "9d4dcd7e454d2434164f4efb8edeb358d86a1dad9ec6224cfcbce3e6"
);
console.log("Public Key Address:", serializeAddressObj(pkAddress, 1));

const scAddress = scriptAddress(
  "5867c3b8e27840f556ac268b781578b14c5661fc63ee720dbeab663f"
);
console.log("Script Address:", serializeAddressObj(scAddress, 0));

Troubleshooting

Address returns wrong network prefix

Verify you are passing the correct networkId parameter. Use 0 for testnet (addr_test1...) and 1 for mainnet (addr1...).

Invalid script CBOR error

Ensure your Plutus script code is valid CBOR hex. The code should be the raw compiled output from your Plutus or Aiken compiler.

Native script serialization fails

Check that all required fields are present for each script type. For example, sig scripts require a keyHash, and atLeast scripts require both required and scripts fields.


On this page