trustwallet / wallet-connect-swift

WalletConnect Swift client SDK
MIT License
116 stars 131 forks source link

Not able to successfully sign the data for eth_sign and eth_send_transaction #40

Closed AyushBherwani1998 closed 3 years ago

AyushBherwani1998 commented 3 years ago

Hey, I was trying to integrate the pod. I have followed the same steps taking a look at the sample. The PrivateKey.sign seems to generate invalid signature every time. I tried for eth_personal_sign and also for eth_send_transaction.

For eth_personal_sign, it says valid = false after approving the request, and for eth_send_transaction, it generates a invalid transaction hash. For eth_send_transaction, I did verify the values for transaction, and they were correct, still it was generating an invalid hash.

Snippet for eth_personal_sign

let data: Data = {
            switch payload {
            case .sign(let data, _):
               return data
            case .personalSign(let data, _):
                let prefix = "\u{19}Ethereum Signed Message:\n\(data)".data(using: .utf8)!
               return prefix + data
            case .signTypeData(_, let data, _):
               return data
            }
        }()

var result = self.privateKey.sign(digest: Hash.keccak256(data: data), curve: .secp256k1)!
result[64] += 27

self.interactor!.approveRequest(id: id, result: "0x" + result.hexString).done{}.cauterize()

At first I thought there might be some issue with private key, but I tried to get the wallet address, and it was accurate. Thanks for help :)

AyushBherwani1998 commented 3 years ago

Update, changing prefix to "\u{19}Ethereum Signed Message:\n\(data.count)".data(using: .utf8)! works perfectly.

AyushBherwani1998 commented 3 years ago

For eth_send_transaction, I'm following this steps

let data =  try! JSONSerialization.data(withJSONObject: result, options: JSONSerialization.WritingOptions.prettyPrinted)

var signedData = self.privateKey.sign(digest: Hash.keccak256(data: data), curve: .secp256k1)!
signedData[64] += 27
interactor.approveRequest(id: id, result: "0x" + signedData.hexString).done{}.cauterize()

where result is WCEthereumTransaction

hewigovens commented 3 years ago

Can you create a pull request for the data.count typo? eth_sendTransaction requires wallet to send and return the transaction hash

AyushBherwani1998 commented 3 years ago

Thanks for the help :)

I'll send a PR

AyushBherwani1998 commented 3 years ago

@hewigovens can you help me with .signTypeData. I see the trust core had the EIP721TypedData but it's not longer supported. I checked wallet core but was not able to find there. Can you point me to the right direction? Thanks

AyushBherwani1998 commented 3 years ago

Got it, EthereumAbiValueEncoder.