Within my smartpy contract, I want to convert from one type into another using sp.pack() and sp.unpack.

Below is an example of the method I am trying to use, converting a string to bytes.

        myString = sp.local("myString", "dummyString")
        packedString = sp.pack(myString.value)
        myBytes = sp.unpack(packedString, t = sp.TBytes)

The problem is the type of the output I get is


when what I wanted was just


What else should I do to make sure the type isn't sp.TOption?

1 Answer 1


sp.unpack gives you an option (because Michelson gives an option when you call UNPACK) that you need to open.

You can do it like this

        myBytes = sp.unpack(packedString, t = sp.TBytes).open_some()

if you're sure that packedString is indeed a packed sp.TBytes.

If you're not 100% sure, you can add an optional error message to open_some().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.