Skip to content
On this page

Pack and unpack โ€‹

Pack and Unpack are used for serializing/deserializing pieces of data. It heavily relies on sp.TBytes type.

  • Packing Converts a given michelson value into sp.TBytes;

  • Unpacking Does the inverse of pack, by deserializing sp.TBytes back to the michelson value.

It is useful when building generic smart contracts with entrypoints expecting sp.TBytes to then deserialize it to an internal structure. Also necessary when verifying signed contents.

Packing data โ€‹

sp.pack(<data>)

Serialize a piece of data <data> to its optimized binary representation of type sp.TBytes.

python
someRecord = sp.record(x = 1)
bytes = sp.pack(someRecord)

Return an object of type sp.TBytes.

Michelson PACK

Unpacking Data โ€‹

sp.unpack(<bytes>,ย t=None)

Parse the serialized data from its optimized binary representation of type sp.TBytes into its original form.

There is an optional argument t to specify the type of its deserialized form.

python
someRecord = sp.unpack(someRecord, sp.TNat)

Return an object of type sp.TOption(sp.TNat)

Michelson UNPACK