Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic array's encode/decode issue #122

Open
Crypto-Doby opened this issue Jun 22, 2022 · 3 comments
Open

Dynamic array's encode/decode issue #122

Crypto-Doby opened this issue Jun 22, 2022 · 3 comments

Comments

@Crypto-Doby
Copy link

Crypto-Doby commented Jun 22, 2022

now the dynamic array's encoding logic is to encode every item, then concat the hex string follow the length encoded hex.:

public func abiEncodeDynamic() -> String? {
    // get values
    let values = self.compactMap { value -> String? in
        return value.abiEncode(dynamic: true)
    }
    // number of elements in the array, padded left
    let length = String(values.count, radix: 16).paddingLeft(toLength: 64, withPad: "0")
    // values, joined with no separator
    return length + values.joined()
}

but follw the document: https://docs.soliditylang.org/en/v0.8.13/abi-spec.html#formal-specification-of-the-encoding
it seems that the dynamic array's encoded data should have extra bytes between length hex and item's hex, to represent the dynamic item's data location.

for example:
I want to encode a dynamic array of 4 datas:

let testArr = [
    Data.init(hex: "0x01"),
    Data.init(hex: "0x02"),
    Data.init(hex: "0x03"),
    Data.init(hex: "0x04")
]

let testData = testArr.abiEncode(dynamic: true)

the testData now is this:

0000000000000000000000000000000000000000000000000000000000000004

0000000000000000000000000000000000000000000000000000000000000001
0100000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000001
0200000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000001
0300000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000001
0400000000000000000000000000000000000000000000000000000000000000

it seems should add the extra bytes like this:

0000000000000000000000000000000000000000000000000000000000000004

0000000000000000000000000000000000000000000000000000000000000080
00000000000000000000000000000000000000000000000000000000000000c0
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000140

0000000000000000000000000000000000000000000000000000000000000001
0100000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000001
0200000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000001
0300000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000001
0400000000000000000000000000000000000000000000000000000000000000

I was trying to send a eth_call with dynamic array's encoded data but got error. after add the extra bytes. the call is success.

is there any wrong usage of me or any missunderstanding?

@koraykoska
Copy link
Member

@Crypto-Doby Can you fix this method and make a PR? Please also include the official test cases from Solidity's docs.

@koraykoska
Copy link
Member

@Crypto-Doby Please check #135

That's just decoding for now. But after making a few tests I will recreate the encoding API as well.

@rkingxbank
Copy link

This issue has not been fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants