Bytes โ
The type used to represent sequences of bytes in SmartPy is sp.TBytes.
The corresponding type in Michelson is
See reference Strings and Bytes template.
Literals โ
sp.bytes(<byte_sequence_string>)
Create a literal of type sp.TBytes in hexadecimal notation.
Examples โ
sp.bytes("0x0dae11")
Operations โ
Concatenation โ
expr1 + expr2
Concatenate two sequences of sp.TBytes.
Example โ
result = sp.bytes("0x0dae11") + sp.bytes("0x0dae11")
sp.concat(<l>)
Concatenate a list l
of sp.TBytes.
Example โ
result = sp.concat([sp.bytes("0x0dae11"), sp.bytes("0x0dae11")])
Obtaining Size โ
sp.len(<expression>)
Return the length of <expression>
.
Example โ
size = sp.len(sp.bytes("0x0dae11"))
Slicing โ
sp.slice(<expression>,ย offset=<offset>,ย length=<length>)
Slice expression
of type sp.TBytes from offset
for length
characters.
It returns an expression of type sp.TOption(sp.TBytes).
Example โ
sp.slice(sp.bytes("0x0dae11"), 1, 2) # 0xae11
Other operations โ
Packing and Unpacking Data โ
sp.pack and sp.unpack are used for serializing/deserializing pieces of data. It heavily relies on sp.TBytes type.
Hashing Functions โ
sp.blake2b, sp.sha256, sp.sha512, sp.sha3, sp.keccak expect a single argument of type sp.TBytes representing the content to be hashed and return the result as sp.TBytes.
Hashing functions are documented here.
Utils โ
Conversion from string to bytes โ
sp.utils.bytes_of_string(<string>)
Encode a constant string to sp.TBytes.
WARNING
This expression is not evaluated at runtime because no such instruction exists in Michelson.
It is only evaluated during compilation.
Example โ
result = sp.utils.bytes_of_string("A String")