Last active
July 19, 2021 13:36
-
-
Save fbslo/ce74fc8545e624b856052f17d3d99cd2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PRIVATE_KEY = "" | |
const TronWeb = require('tronweb') | |
const tronWeb = new TronWeb({ | |
fullHost: 'https://api.trongrid.io', | |
privateKey: PRIVATE_KEY | |
}) | |
const ethers = tronWeb.utils.ethersUtils; | |
var hexStr2byteArray = function(str) { | |
var bytestrs = []; | |
for(var i=0;i<str.length;i+=2) { | |
bytestrs.push(str.substr(i,2)); | |
} | |
return eval("[0x" + bytestrs.join(",0x") + "]"); | |
} | |
//message: hash | |
async function start(message, signature){ | |
message = message.replace(/^0x/, ''); | |
let messageBytes = [ | |
...ethers.toUtf8Bytes(`\x19TRON Signed Message:\n32`), | |
...hexStr2byteArray(message) | |
]; | |
let messageDigest = ethers.keccak256(messageBytes); | |
let r = signature.slice(0, 66); | |
let s = '0x' + signature.slice(66, 130); | |
let v = '0x' + signature.slice(130, 132); | |
//contract used to recover address using `ecrecover` | |
let contract = await tronWeb.contract().at('TBSrTu8zPdH5FnKm57ijpXkZN3KQ4LEyLa'); | |
let hexAddress = await contract.recoverAddr(messageDigest, v, r, s).call(); | |
console.log(`Hex address: ${hexAddress}`) | |
console.log(`Address: ${tronWeb.address.fromHex(hexAddress)}`) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment