Validate TRON address using solidity ecrecover

孤街醉人 提交于 2020-06-17 11:55:09

问题


I'm trying to validate a signed message using a smart contract running on the TRON network. I've tried a few different methods but all failed:

Based on this article I've deployed the following smart contract:

contract Verifier {
    function recoverAddr(bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) returns (address) {
        return ecrecover(msgHash, v, r, s);
    }

    function isSigned(address _addr, bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) returns (bool) {
        return ecrecover(msgHash, v, r, s) == _addr;
    }
}

And the following client (js) code:

let contract = await tronWeb.contract().at(contract_address);
let msg = tronWeb.sha3('This is my message');
let signature = await tronWeb.trx.sign(msg);

var r = signature.substr(0, 66);
var s = "0x" + signature.substr(66, 64);
var v = "0x" + signature.substr(signature.length - 2);

let hexAddress = await contract.recoverAddr(msg, v, r, s).call();

But got back a wrong address.

Based on this article I've deployed the following smart contract:

contract Verifier {


  //return the address according to the signature and and the data
  function validate(bytes32 hash, bytes signature)
  public
  pure
  returns (address){
      bytes memory signedString = signature;
      bytes32  r = convertToBytes32(slice(signedString, 0, 32));
      bytes32  s = convertToBytes32(slice(signedString, 32, 32));
      byte  v1 = slice(signedString, 64, 1)[0];
      uint8 v = uint8(v1) + 27;
      return ecrecover(hash, uint8(r), s, bytes32(v));
  }

  //slice function
  function slice(bytes memory data, uint start, uint len)
  private
  pure
  returns (bytes){
      bytes memory b = new bytes(len);
      for(uint i = 0; i < len; i++){
          b[i] = data[i + start];
      }
      return b;
  }
  //convert bytes to bytes32
  function convertToBytes32(bytes memory source)
  private
  pure
  returns (bytes32 result) {
      assembly {
          result := mload(add(source, 32))
      }
  }
}

And the following client (js) code:

let contract = await tronWeb.contract().at(contract_address);
let msg = tronWeb.sha3('Hello World');
let signature = await tronWeb.trx.sign(msg);
let hexAddress = await contract.validate(msg, signature).call();

Still got back a wrong address.

I managed to sign and recover an address using pure js code based on this:

const ethers = tronWeb.utils.ethersUtils;
let signingKey = new ethers.SigningKey(tronWeb.defaultPrivateKey);

let message = "This is some message";
let messageBytes = ethers.toUtf8Bytes(message);
let messageDigest = ethers.keccak256(messageBytes);

let signature = signingKey.signDigest(messageDigest);
let recovered = ethers.recoverAddress(messageDigest, signature);
console.log("Recovered address: " + recovered);

How can I sign a message using a js code and recover using a smart contract? What am I missing?


回答1:


I got it.

The smart contract code:

contract Verifier {
    function recoverAddr(bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) returns (address) {
        return ecrecover(msgHash, v, r, s);
    }

    function isSigned(address _addr, bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) returns (bool) {
        return ecrecover(msgHash, v, r, s) == _addr;
    }
}

The client code:

const ethers = tronWeb.utils.ethersUtils;
let contract = await tronWeb.contract().at(contract_address);
let signingKey = new ethers.SigningKey(tronWeb.defaultPrivateKey);

let message = "This is some message";
let messageBytes = ethers.toUtf8Bytes(message);
let messageDigest = ethers.keccak256(messageBytes);

let signature = signingKey.signDigest(messageDigest);
let hexAddress = await contract.recoverAddr(messageDigest, signature.v, signature.r, signature.s).call();


来源:https://stackoverflow.com/questions/62247259/validate-tron-address-using-solidity-ecrecover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!