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

(EIP712) - Encoding TypedData containing bytes #102

Closed
fdematos opened this issue Jul 20, 2020 · 2 comments
Closed

(EIP712) - Encoding TypedData containing bytes #102

fdematos opened this issue Jul 20, 2020 · 2 comments

Comments

@fdematos
Copy link

I want to encode the following typed message:

{
  "types": {
      "EIP712Domain": [
          {"name": "verifyingContract", "type": "address"},
          {"name": "chainId", "type": "uint256"},
      ],
      "TxMessage": [
          {"name": "signer", "type": "address"},
          {"name": "to", "type": "address"},
          {"name": "data", "type": "bytes"},
          {"name": "nonce", "type": "uint256"}
      ]
  },
  "primaryType": "TxMessage",
  "domain": {
      "chainId": 3,
      "verifyingContract": "0x9f733Fd052A5526cdc646E178c684B1Bf2313C57"
  },
  "message": {
      "signer": "0x2c68bfBc6F2274E7011Cd4AB8D5c0e69B2341309",
      "to": "0x68f3cEdf21B0f9ce31AAdC5ed110014Af5DA1828",
      "data": "0xa21f3c6a68656c6c6f000000000000000000000000000000000000000000000000000000776f726c64202100000000000000000000000000000000000000000000000000",
      "nonce": 0
    }
}

Calling :

let typedData = try! JSONDecoder().decode(TypedData.self, from: jsonString.data(using: .utf8)!)
try! typedData.signableHash().web3.hexString

Returns:

0x064198dd39fd7731b32f3ea8873c8811a58d0a161374a7755533f67c8fb3c7fa

Expected returns:

0x1f177092c4fbedf53f392389d4512f0a61babf07acc05303a4f1ef7e90b67d92

Fix proposal

I thinks the problem come from the method parseAtomicType on the file Utils/TypedData.swift.

You have line 110:

       case .DynamicString, .DynamicBytes:
            guard let value = data.stringValue?.web3.keccak256 else {
                throw ABIError.invalidValue
            }
            return try ABIEncoder.encode(value, forType: .FixedBytes(32))
        case .FixedAddress, .FixedBytes:

You are considering 0xa21f3c6a68656c6c6f000000000000000000000000000000000000000000000000000000776f726c64202100000000000000000000000000000000000000000000000000" as a String, not as a Hex encoded Data.

I think it should be:

case .DynamicBytes:
            guard let stringValue = data.stringValue, let value = Data(hex:stringValue)?.web3.keccak256 else {
                throw ABIError.invalidValue
            }
            return try ABIEncoder.encode(value, forType: .FixedBytes(32))
        case .DynamicString:
            guard let value = data.stringValue?.web3.keccak256 else {
                throw ABIError.invalidValue
            }
            return try ABIEncoder.encode(value, forType: .FixedBytes(32))

Using this code, I got the expected Hash.

I think you based your code from TrustCore iOS SDK because it has the same logic with the same issues.

Thanks

@DarthMike
Copy link
Member

Hello @fdematos, I'll take a look today. Thank you for reporting the issue.

@DarthMike
Copy link
Member

Issue is now fixed and published in 0.4.1

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

2 participants