使用 solidity ecrecover 验证 TRON 地址

Validate TRON address using solidity ecrecover

我正在尝试在 TRON 网络上使用智能合约 运行 验证已签名的消息。 我尝试了几种不同的方法,但都失败了:

基于this article我部署了以下智能合约:

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;
    }
}

以及以下客户端 (js) 代码:

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();

但是找回了错误的地址。

基于this article我部署了以下智能合约:

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))
      }
  }
}

以及以下客户端 (js) 代码:

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();

还是找回了错误的地址。

我成功地使用基于 this:

的纯 js 代码签名并恢复了一个地址
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);

如何使用js代码签署消息并使用智能合约恢复?我错过了什么?

我知道了。

智能合约代码:

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;
    }
}

客户代码:

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();