75285

Validate TRON address using solidity ecrecover

<h3>Question</h3>

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?


<h3>Answer1:</h3>

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

Recommend

  • response time is higher, when I call procedure in oracle through simple Jdbc call compared to the lo
  • Retrieving raw private key from openssl_pkey_get_private()
  • CodeIgniter: set_value() or post() - what is faster and what is the best practice to store data into
  • How to run a .jar file, which contains OpenCV code and was created using ant, from command line?
  • Multi-users chat room data structure in Firebase Database
  • Getting this “org.xhtmlrenderer.render.BlockBox cannot be cast to org.xhtmlrenderer.newtable.TableBo
  • Change div id into javascript variable
  • Django Forms: Customizing html Output
  • How to set different fields on and off on basis of value of other field in FormFlow
  • PNG file is NOT keeping transparency?
  • WebSocket connection failure. Due to security constraints in your web browser
  • Matplotlib: subplot
  • How do I send Email with inline Attachments
  • How to implement 'if' in Gherkin
  • Lazy Evaluation - Space Leak
  • Using Fractions in Python
  • Website Scraping Using PHP
  • Appending spaces with str_pad
  • Parse fasta sequence to the dictionary
  • Static analysis of header inclusion in C++
  • When scaling and drawing an image to canvas in iOS Safari, width is correct but height is squished
  • Advertising Identifier for devices lower than iOS 6.0
  • how to transform dataframe that contains list in every row of each column
  • Bootstrap 3 Validation
  • How exactly do you use json_decode to pass a javascript array to php?
  • SELECT on JSONField with Django
  • Get spring boot pagination number starts from 1 instead of 0
  • Check 'Manager can update membership list' in AD
  • Bison does not appear to recognize C string literals appropriately
  • Authorize Attribute Authentication with Postman in Web Api
  • System.Drawing.DrawString() weird wrapping of long string
  • Swift manually rotate view controller
  • 'Edit' function for forum posts and such
  • openpyxl - adding new rows in excel file with merged cell existing
  • How to write seo friendly url's using htaccess?
  • Terminal run dalvikvm with am.jar
  • How to handle div that is created dynamically in a table
  • Make checkout phone field optional for specific countries in WooCommerce
  • ReferenceError: TextEncoder is not defined