Open altafan opened 2 years ago
Depends: not all the time the casting is done with hardcoded values, but also with result of computation
Cashscript uses the bytes()
| bytesN()
primitive to convert the given arg to a byte array with either unspecified or fixed length.
On Ivy the bytes
primitive is already used (and we want to increase its powers - #27).
We could add new primitives byte() | bytes1()
, bytes4()
, bytes8()
, bytes16()
, bytes32()
and bytes64()
, all mapping to the usage of the new opcodes OP_SCRIPTNUMTOLE64
(with the help of OP_DROP
or OP_LEFT
to trim trailing bytes if needed) to cast numbers, bools, or expressions resulting in one of those types to little-endian byte arrays.
On the other side, I think it could be enough to have a int()
primitive to cast any byte array to a number by padding it with training zeros if needed. As stated in the reference doc, passing a byte array longer than 64 (8 bytes) must not be possible.
Taking inspiration by cashscript, it would be nice if Ionio supported type casting:
Note: In cashscript, the conversion bytes <> int is done with the native opcodes
OP_NUM2BIN
andOP_BIN2NUM
. In Ionio, we could useOP_SCRIPTNUMTOLE64
andOP_LE64TOSCRIPTNUM
in a similar way, but we can achieve the same goal in pure js and simply updating the top value of the stack. This way, we would prevent adding more opcodes to the contract.