swiftethereumcryptocurrencywallet-connect

How to implement eth_signTypedData in swift?


I am developing a crypto wallet, I have implemented personal_sign and eth_sign in swift, how to implement eth_signTypedData, the official walletConnect documentation seems to have no relevant information. Any idears? Thanks!


Solution

  • Very late answer, but I'll try to post some finding and code here, in the hopes of it helps someone looking for it in the future.

    First some preliminary information:

    You will need all the files from https://github.com/muratogat/wallet-connect-swift/tree/master/WalletConnect/Models/EIP-712 This is my forked version of WalletConnect (v1) , where I copied necessary files for EIP-712 support from https://github.com/trustwallet/trust-core/tree/833ec3f263bb9b257b6ec04359cb98d1bae7cf17/Sources/Ethereum/Solidity

    The important file here is EIP712TypedData, other files are models that it depends on. With some changes though:

    If you manage to get this working in your project and fix any other warnings / errors you may have encountered, you should be able to call :

    let decodedEIP712TypedData = new JSONDecoder().decode(EIP712TypedData.self, from: params[1].data(using: .utf8)!)
    

    Here params is the payload string array you get from the WalletConnect JSONRPCRequest, where the first string is the address and the second string is the structured data JSON object.

    Afterwards,

    decodedEIP712TypedData.signHash
    

    should give you the byte[] to be actually signed.

    Then, depending on your web3 swift library, you can actually have this byte[] signed and respond to the WC request. Note that you should not use personal_sign that adds the Ethereum Signed Message prefix. If your web3 swift library doesn't have convenience methods for directly signing data, you may keccak256 this data manually and use lower level SECP256K1.signForRecovery manually to sign it with your private key.

    Good luck.