-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recover public key from contract deployment transaction #700
Comments
I just noticed that |
That looks right to me, but I may be missing something. Can you provide an example transaction on Etherscan (Ropsten is fine) and I can double check where the mis-agreement is happening. :) |
Cool, thanks for your help! Here's the transaction on ropsten: 0x35097c5644dfa2e7f1231212cf26e5040bcdb799ccfd3f7c57bb88569d749ab9. I did a signing test with this address using
|
Ah, so I think I've found the problem. In v4, the transaction returned doesn't have If you change your I'll make this change and push it out once the CI tests are complete too. You also helped me find a bug in the automatic v calculation in v5, so I'll be fixing that soon too. :) |
This should be fixed in Thanks! :) |
I tried |
Ah, I see the issue. So, if you get rid of the I'm not sure why it isn't detecting you are setting the value incorrectly. If the recoveryParam and the v disagree I would have thought it throws an error (in v5 it will), but it seems it takes the |
Perfect! Thanks for helping out :) |
Sorry I didn’t think of that sooner, but glad it worked. :) |
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: async function deriveEthereumPublicKeyFromEthereumAddress(ethereumAddress: string): Promise<string> {
console.log('ethereumAddress', ethereumAddress);
const etherscanProvider = new ethers.providers.EtherscanProvider('ropsten');
const transactionResponses: ReadonlyArray<ethers.providers.TransactionResponse> = await etherscanProvider.getHistory(ethereumAddress);
const aSignedTransaction: Readonly<ethers.providers.TransactionResponse> | undefined = transactionResponses.find((transactionResponse: Readonly<ethers.providers.TransactionResponse>) => {
return transactionResponse.from === ethereumAddress;
});
if (aSignedTransaction === undefined) {
throw new Error(`The Ethereum address has no signed transactions`);
}
const provider = ethers.getDefaultProvider('ropsten');
const transactionResponse = await provider.getTransaction(aSignedTransaction.hash);
console.log('transactionResponse', transactionResponse);
const signature: string = ethers.utils.joinSignature({
r: transactionResponse.r,
s: transactionResponse.s,
v: transactionResponse.v
});
console.log('signature', signature);
const txData = {
gasPrice: transactionResponse.gasPrice,
gasLimit: transactionResponse.gasLimit,
value: transactionResponse.value,
nonce: transactionResponse.nonce,
data: transactionResponse.data,
chainId: transactionResponse.chainId
};
const transaction = await ethers.utils.resolveProperties(txData);
const rawTransaction = ethers.utils.serializeTransaction(transaction);
const hashedTransaction = ethers.utils.keccak256(rawTransaction);
const hashedTransactionBytes = ethers.utils.arrayify(hashedTransaction);
console.log('hashedTransactionBytes', hashedTransactionBytes);
const publicKey: string = ethers.utils.recoverPublicKey(hashedTransactionBytes, signature)
console.log('publicKey', publicKey);
const originalAddress: string = ethers.utils.recoverAddress(hashedTransactionBytes, signature);
console.log('originalAddress', originalAddress);
return publicKey;
} It doesn't come out correctly, and I'm not sure why |
So it looks like I'm missing the I think of got it now, so thanks! |
I'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:
Note that
msgHash
is the signing hash.Unfortunately both the
recoveredPubKey
and therecoveredAddress
are false.The text was updated successfully, but these errors were encountered: