def tb(l): return b''.join(map(lambda x: x.to_bytes(1, 'big'), l))

The above function takes a list of number and is being converted into bytes,

and the returned value is used this way,

base58.b58encode_check(tb([6, 161, 161]) + v).decode()

here v is also a byte data type.

How do we achieve this in smartpy ?

1 Answer 1


Look at string_of_nat in stringManipulation.py.

def string_of_nat(params):
    c   = sp.map({x : str(x) for x in range(0, 10)})
    x   = sp.local('x', params)
    res = sp.local('res', [])
    sp.if x.value == 0:
    sp.while 0 < x.value:
        res.value.push(c[x.value % 10])
        x.value //= 10
    return sp.concat(res.value)

Similarly you can compute a map like c in this example that associates every x to the encoded version. Then when you have this map, you can use it in your contract.

  1. Do this computation in Python (outside SmartPy).
  2. Create a map that associates every x to the encoding you just computed.
  3. Use this map.
  • I don't think I understood the answer, I need to achieve the above encoding in smartpy inside a contract.
    – Kin
    Commented Apr 14, 2023 at 3:00
  • I updated my answer, is it clear now?
    – Jordan_tez
    Commented Apr 14, 2023 at 8:08
  • so basically I think I understand what you are saying but I am having hard time converting a number to big endian bytes, x.to_bytes(1, 'big') the way it is done in python. I am trying to achieve this in smartpy
    – Kin
    Commented Apr 15, 2023 at 7:45

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.