Recover public key from contract deployment transaction
See original GitHub issueI’m trying to get the public key of a user during the contract deployment but I’m not sure if it’s possible with ethers.js. I’d like to solve it using only this package as I don’t want to bloat my react builds with web3.js
. I’m using ethers 4.0.42.
I took a deep dive into your source code, ECDSA and looked at a related issue and came up with the following code.
I think my biggest issue is to get the signing hash. So here’s my attempt:
// before: contract = await factory.deploy(...);
// get the signing hash
const deployTx = contract.deployTransaction;
const txData = {
gasPrice: deployTx.gasPrice,
gasLimit: deployTx.gasLimit,
value: deployTx.value,
nonce: deployTx.nonce,
data: deployTx.data,
chainId: deployTx.chainId
};
const tx = await ethers.utils.resolveProperties(txData);
const rawTx = ethers.utils.serializeTransaction(tx); // returns RLP encoded tx
const msgHash = ethers.utils.keccak256(rawTx); // as specified by ECDSA
Note that msgHash
is the signing hash.
// get flat signature for recovery
const expandedSig = {
r: deployTx.r,
s: deployTx.s,
recoveryParam: 0, // not really sure what this does
v: deployTx.v // i think this value is only needed because of EIP155 (CHAIN_ID * 2 + 35 or 36)
};
const signature = ethers.utils.joinSignature(expandedSig);
const msgBytes = ethers.utils.arrayify(msgHash); // create binary hash
const recoveredPubKey = ethers.utils.recoverPublicKey(
msgBytes,
signature
);
const recoveredAddress = ethers.utils.recoverAddress(msgBytes, signature);
Unfortunately both the recoveredPubKey
and the recoveredAddress
are false.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
ethers.js Recover public key from contract deployment via v,r,s ...
I wrote a gist that correctly recovers the public key given the transaction. In short: const tx = await provider.getTransaction(.
Read more >How to extract public key for a signed transaction in Solidity ...
I want an user to: sign a message in a different blockchain (non-EVM) and retrieve the public key from a Solidity smart contract...
Read more >The Lost Key Recovery System has been proposed ... - Medium
The Lost Key Recovery System smart contract has been proposed for deployment to the EOS Mainnet. This is the final step of the...
Read more >Accounts in Nethereum
If using the TransactionManager, deploying a contract or using a contract function, the transaction will either be signed offline using the private key...
Read more >Chapter 6: Transactions · GitBook
You can send to an address that has no corresponding private key or contract, thereby "burning" the ether, rendering it forever unspendable. Validation...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m having a hard time getting the public key from a transaction response on ropsten. Would anyone mind giving me an example of how to do this with a simple transaction response, just a normal transaction from an external account?
Here’s the function from my application:
It doesn’t come out correctly, and I’m not sure why
Perfect! Thanks for helping out 😃