1

there are two functions on solidity

function encodeString(string memory self)
        internal
        pure
        returns (bytes memory)
    {
        return encodeBytes(bytes(self));
    }

/**
     * @dev RLP encodes a byte string.
     * @param self The byte string to encode.
     * @return The RLP encoded string in bytes.
     */
    function encodeBytes(bytes memory self)
        internal
        pure
        returns (bytes memory)
    {
        bytes memory encoded;
        if (self.length == 1 && uint8(self[0]) <= 128) {
            encoded = self;
        } else {
            encoded = concat(encodeLength(self.length, 128), self);
        }
        return encoded;
    }
/**
     * @dev Encode the first byte, followed by the `len` in binary form if `length` is more than 55.
     * @param len The length of the string or the payload.
     * @param offset 128 if item is string, 192 if item is list.
     * @return RLP encoded bytes.
     */
    function encodeLength(uint256 len, uint256 offset)
        private
        pure
        returns (bytes memory)
    {
        bytes memory encoded;
        if (len < 56) {
            encoded = new bytes(1);
            encoded[0] = bytes32(len + offset)[31];
        } else {
            uint256 lenLen;
            uint256 i = 1;
            while (len / i != 0) {
                lenLen++;
                i *= 256;
            }

            encoded = new bytes(lenLen + 1);
            encoded[0] = bytes32(lenLen + offset + 55)[31];
            for (i = 1; i <= lenLen; i++) {
                encoded[i] = bytes32((len / (256**(lenLen - i))) % 256)[31];
            }
        }
        return encoded;
    }


It is being called this way _rlpBytes = abi.encodePacked(_rlpBytes, _link.encodeString());

Is this already implemented in smartpy ?

1 Answer 1

0

Yes, it is implemented in SmartPy. (I shared a few examples in previous threads)

Your solidity example:

pragma solidity >=0.4.0 < 0.6.0;
pragma experimental ABIEncoderV2;

import "./RLPEncode.sol";

contract Sample {
    using RLPEncode for string;

    function encode(bytes memory _rlpBytes, string memory _link) public pure returns (bytes memory) {
        return abi.encodePacked(_rlpBytes, _link.encodeString());
    }
}

SmartPy code that produces the same encoding:

import smartpy as sp

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

class Sample(sp.Contract):

    @sp.onchain_view()
    def encode(self, arg):
        encode_string = sp.build_lambda(Utils.RLP.Encoder.encode_string)

        sp.result(arg.rlpBytes + encode_string(arg.link))

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

    rlpBytes = sp.bytes("0x012345")
    result = sc.compute(c.encode(sp.record(rlpBytes = rlpBytes, link = "TEST")))
    sc.show(result)
    sc.verify(result == sp.bytes("0x0123458454455354"))

In both examples, if we use rlpBytes = 0x012345 and link = "TEST", we get 0x0123458454455354.

4
  • so I have a question , i tried in solidity on the code i posted to encode the string "helloworld" and another string "hello" and the result is "0x8dcccb8a68656c6c6f776f726c64" and "0x88c7c68568656c6c6f" respectively. But in case of smartpy the prefix is 0x12345, can I do something to get the prefix of rlp bytes same as that of solidity ?
    – Kin
    Commented May 14, 2023 at 23:23
  • You do not show how _rlpBytes is being computed initially, in the example above I used 0x012345 just as an example. Commented May 15, 2023 at 5:13
  • it is just initialized, bytes memory _rlpBytes; . where can I communicate with you directly, are you available on telegram or slack ?
    – Kin
    Commented May 15, 2023 at 5:30
  • hey can you look at this question too, tezos.stackexchange.com/questions/6070/…
    – Kin
    Commented May 18, 2023 at 9: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.