How to get sender’s Ethereum address and public key from signed transaction
ethers.js version of the article below:
In this article, I’d like to explain how to get Ethereum address and public key of sender (who signs to transaction) from signed transaction.
Example of signed transaction
code:signedTx
0xf86b808504a817c800825208942890228d4478e2c3b0ebf5a38479e3396c1d6074872386f26fc100008029a0520e5053c1b573d747f823a0b23d52e5a619298f46cd781d677d0e5e78fbc750a075be461137c2c2a5594beff76ecb11a215384c574a7e5b620dba5cc63b0a0f13
Retrieve sender’s Ethereum address and public key
code:js
(async () => {
const ethers = require('ethers')
const CHIAN_ID = 3 // Ropsten
const SIGNED_TX = '0xf86b808504a817c800825208942890228d4478e2c3b0ebf5a38479e3396c1d6074872386f26fc100008029a0520e5053c1b573d747f823a0b23d52e5a619298f46cd781d677d0e5e78fbc750a075be461137c2c2a5594beff76ecb11a215384c574a7e5b620dba5cc63b0a0f13'
const tx = ethers.utils.RLP.decode(ethers.utils.arrayify(SIGNED_TX))
const v = ethers.BigNumber.from(tx6).toNumber() const r = ethers.utils.hexZeroPad(tx7, 32) const s = ethers.utils.hexZeroPad(tx8, 32) const rawTx = tx.slice(0, 6)
// EIP-155
if (CHIAN_ID !== 0) {
rawTx.push(ethers.utils.hexlify(CHIAN_ID))
rawTx.push('0x')
rawTx.push('0x')
}
const digest = ethers.utils.keccak256(ethers.utils.RLP.encode(rawTx))
const signature = ethers.utils.joinSignature({v, r, s});
const publicKey = ethers.utils.recoverPublicKey(digest, signature)
console.log('publicKey:', publicKey)
const address = ethers.utils.recoverAddress(digest, signature)
console.log('address:', address)
})()
References
Support
If you find this article is helpful, it would be greatly appreciated if you could tip Ether to the address below. Thank you!
code:address
0x0089d53F703f7E0843953D48133f74cE247184c2