1

I am trying to translate solidity code in smartpy .

bytes memory rlpBytes;

rlpBytes = abi.encodePacked(rlpBytes, _link.encodeString());

rlpBytes = abi.encodePacked( rlpBytes.length.addLength(false), rlpBytes );

here _link is a string data type.

I am using the below function of rlp for decoding https://github.com/airgap-it/ibcf/blob/291964a41ebecc25961435e106086854390662c5/contracts/tezos/libs/utils.py#L179

I am using the function in this way , rlp_bytes = sp.local("rlp_bytes", sp.build_lambda(rlp.Encoder.encode_string)(link))

But i got stuck on the second line rlpBytes = abi.encodePacked( rlpBytes.length.addLength(false), rlpBytes );

Can someone help me out here on this line ? I am having trouble translating this .

1 Answer 1

1

Does the encoding need to be the same in both Tezos and EVM contracts? Or can you use the Tezos encoding instead (with sp.pack and sp.unpack)?

If the encoding needs to be the same:

Solidity:

function encode(bytes memory rlpBytes, string memory link) external pure returns(bytes memory) {
  // rlpBytes = 0x80
  // link = "TEST"
  // result = 0x8054455354
  return abi.encodePacked(rlpBytes, link);
}

SmartPy

import smartpy as sp

Utils = sp.io.import_script_from_url("https://raw.githubusercontent.com/Acurast/acurast-hyperdrive/main/contracts/tezos/libs/utils.py")

@sp.add_test("Tests")
def test():
 sc = sp.test_scenario()

 rlpBytes = sp.bytes("0x80") # can be any bytes
 encode_string_packed = sp.build_lambda(Utils.Bytes.of_string)
 sc.show(rlpBytes + encode_string_packed("TEST")) # 0x8054455354

About rlpBytes = abi.encodePacked( rlpBytes.length.addLength(false), rlpBytes );, it should be the same as doing RLP.Encoder.with_length_prefix(rlpBytes). Need more context to give a precise answer.

Also, https://github.com/airgap-it/ibcf has been moved to https://github.com/Acurast/acurast-hyperdrive.

6
  • So I added , sc.show(Utils.RLP.Encoder.with_length_prefix(rlpBytes)) on the above code , to achieve this code rlpBytes = abi.encodePacked( rlpBytes.length.addLength(false), rlpBytes ); but it says error while compiling, Error: New command outside of contract
    – lee brune
    Apr 2 at 3:48
  • bytes memory _rlpBytes; <br/> _rlpBytes = abi.encodePacked(_rlpBytes, _link.encodeString()); _rlpBytes = abi.encodePacked( _rlpBytes.length.addLength(false),_rlpBytes); _rlpBytes = abi .encodePacked(_rlpBytes.length.addLength(false), _rlpBytes) .encodeBytes(); This is the part of a code from a function of solidity that I am trying to translate using smartpy.
    – lee brune
    Apr 2 at 3:52
  • You need to build the lambda first: with_length_prefix = sp.build_lambda(Utils.RLP.with_length_prefix) and then just use with_length_prefix(rlpBytes) Apr 2 at 4:20
  • 1
    can you help me out here too on decoding tezos.stackexchange.com/questions/5972/rlp-decoding-on-tezos
    – Kin
    Apr 20 at 5:29
  • Hi can you see this post once tezos.stackexchange.com/questions/6056/rlp-encoding-of-solidity
    – Kin
    May 12 at 8:54

Your Answer

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

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