trustwallet / trust-wallet-ios

:iphone: Trust - Ethereum Wallet and Web3 DApp Browser for iOS
https://trustwallet.com
GNU General Public License v3.0
1.52k stars 718 forks source link

personal_sign fails to sign 32 byte hex payload in iOS #966

Closed SilentCicero closed 5 years ago

SilentCicero commented 5 years ago

When using personal_sign for Ethereum in iOS, TrustWallet states a different 20 byte payload from the 32 byte payload requested to sign. The resulting signature thus does not reduce to the "x19..." + len + hash below.

Try to run the example code below to reproduce.

const hash = '0x87b2d4b93731813f851f44e4a8e98e8e6e3680cc98081ea0c5c4ba4f6948c5a6';

// metamask/trust/coinbase if you have it
if (typeof window.web3 !== "undefined") {

  // enable eth
  window.ethereum.enable();

  // get accounts
  web3.currentProvider.sendAsync({ method: "eth_accounts", params: [] }, (err, accountResult) => {
    if (err) return console.error(err);

    const signerAddress = accountResult.result[0];

    web3.currentProvider.sendAsync({
      method: "personal_sign",
      params: [signerAddress, hash],
    }, (err, signerResult) => {
      // if (err) return console.error(err);
      console.log(signerResult);
    });
  });

}
vikmeup commented 5 years ago

Will look into this

hewigovens commented 5 years ago

@SilentCicero what's this hash?

const hash = '0x87b2d4b93731813f851f44e4a8e98e8e6e3680cc98081ea0c5c4ba4f6948c5a6';

from the doc, it should be data before UTF-8 HEX decoded. Trust will do following: envelope ethereum signed message prefix -> keccak256 -> sign

hewigovens commented 5 years ago

please check if this comment helps: https://github.com/TrustWallet/trust-wallet-ios/issues/947#issuecomment-453736156

hewigovens commented 5 years ago

Just tried metamask and Trust iOS, it works for me, note the first parameter is message to sign, not address. If you want to sign string, please wrap it with `web3.toHex("")

web3.currentProvider.sendAsync({
  method: "personal_sign",
  params: ["0x87b2d4b93731813f851f44e4a8e98e8e6e3680cc98081ea0c5c4ba4f6948c5a6", web3.eth.accounts[0]],
}, function(err, signerResult) {
  if (err) return console.error(err);
  console.log(signerResult);
});