OCamlPro/liquidity

Bytes.pack and Bytes.unpack behaviour is not documented.

Closed this issue · 1 comments

I'd like to reproduce the behaviour of Bytes.pack, but it seems not to be documented how the data is converted in bytes.

For example, using the liquidity editor, the main entry point of this contract returns "0x050707000107070080dac409010000000474657374", but how is this result generated?

[%%version 0.4]

type storage = bytes option

let%init init = (None: bytes option)
                
let%entry main _ _ = 
  (([]: operation list), Some(Bytes.pack (1, 10tz,"test")))

This is not documented because encodings used in serialization of data depend on the Tezos protocol and so they can vary. However, I've added a command to the liquidity command line tool to pack constants (this makes an RPC to a tezos node) :

> liquidity --tezos-node https://rpc.tezrpc.me --pack '(1, 10tz,"test")' 'int * tez * string'
0x050707000107070080dac409010000000474657374

This is the safest way to do it, however for some simple types the binary serialization can be easily inferred. For instance, for strings it is:

| 0x | 0501 | <size of the string on 4 bytes> | <ascii string in hexa> |

for "message" (of length 7), it is

| 0x | 0501 | 00000007 | 6d657373616765 |

or 0x0501000000076d657373616765.