Skip to content

Instantly share code, notes, and snippets.

@protortyp
Last active October 25, 2023 18:33
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save protortyp/2b29809b8f7281b8f10bbe041c1b5e00 to your computer and use it in GitHub Desktop.
Save protortyp/2b29809b8f7281b8f10bbe041c1b5e00 to your computer and use it in GitHub Desktop.
Attempting to get the public key
const ethers = require("ethers")
const pk =
"0x0471c746523d16e93d4738f882d9b0beebf66c68caa0f895db15686b57b878cfc7b3e09813ba94f1bbfaa91a06566d3d18bbf69d10bcc947325bbcd6fea97ed692"
const ad = "0xcD3edF915387E2555A829567cE0dBbC919834B82"
getPubKey = async () => {
const infuraProvider = new ethers.providers.JsonRpcProvider(
"https://ropsten.infura.io/v3/<projectID>"
)
const tx = await infuraProvider.getTransaction(
"0x07035a057bdddc9c0e1c07c32b341f6082f9d6be2dc39452753587c2e69fbf96"
)
const expandedSig = {
r: tx.r,
s: tx.s,
v: tx.v
}
const signature = ethers.utils.joinSignature(expandedSig)
const txData = {
gasPrice: tx.gasPrice,
gasLimit: tx.gasLimit,
value: tx.value,
nonce: tx.nonce,
data: tx.data,
chainId: tx.chainId,
to: tx.to
}
const rsTx = await ethers.utils.resolveProperties(txData)
const raw = ethers.utils.serializeTransaction(rsTx) // returns RLP encoded tx
const msgHash = ethers.utils.keccak256(raw) // as specified by ECDSA
const msgBytes = ethers.utils.arrayify(msgHash) // create binary hash
const recoveredPubKey = ethers.utils.recoverPublicKey(msgBytes, signature)
const recoveredAddress = ethers.utils.recoverAddress(msgBytes, signature)
console.log(recoveredAddress)
console.log(recoveredPubKey)
console.log("Correct public key:", recoveredPubKey === pk)
console.log("Correct address:", recoveredAddress === ad)
}
getPubKey()
@KLM-GIT
Copy link

KLM-GIT commented Jul 28, 2021

Thanks very much for the useful code - thanks for sharing!
Just to note, there is no tx.chainId or tx.data, and also ethers excepts on gasPrice and value unless converted to BigNumber.

Pull the chainId from the provider, and use tx.input for data.

txData object should be:

const txData = {
        nonce: tx.nonce,
        gasPrice: ethers.BigNumber.from(tx.gasPrice),
        gasLimit: tx.gas,
        to: tx.to,
        value: ethers.BigNumber.from(tx.value),
        data: tx.input,
        chainId: chainId // passed in - pull from network object, etc.
    }

@pokrovskyy
Copy link

pokrovskyy commented Jan 27, 2022

Great piece, however today it lacks support for EIP-1559 transactions. I amended the code a bit to account for that (replace const txData = .... with this block):

...
    let txData;
    switch (tx.type) {
        case 0:
            txData = {
                gasPrice: tx.gasPrice,
                gasLimit: tx.gasLimit,
                value: tx.value,
                nonce: tx.nonce,
                data: tx.data,
                chainId: tx.chainId,
                to: tx.to
            };
            break;
        case 2:
            txData = {
                gasLimit: tx.gasLimit,
                value: tx.value,
                nonce: tx.nonce,
                data: tx.data,
                chainId: tx.chainId,
                to: tx.to,
                type: 2,
                maxFeePerGas: tx.maxFeePerGas,
                maxPriorityFeePerGas: tx.maxPriorityFeePerGas
            }
            break;
        default:
            throw "Unsupported tx type";
    }
...

@protortyp
Copy link
Author

Thanks @pokrovskyy! This was on my todo list for a while now 👍

@Azleal
Copy link

Azleal commented Aug 17, 2022

Great piece, however today it lacks support for EIP-1559 transactions. I amended the code a bit to account for that (replace const txData = .... with this block):

...
    let txData;
    switch (tx.type) {
        case 0:
            txData = {
                gasPrice: tx.gasPrice,
                gasLimit: tx.gasLimit,
                value: tx.value,
                nonce: tx.nonce,
                data: tx.data,
                chainId: tx.chainId,
                to: tx.to
            };
            break;
        case 2:
            txData = {
                gasLimit: tx.gasLimit,
                value: tx.value,
                nonce: tx.nonce,
                data: tx.data,
                chainId: tx.chainId,
                to: tx.to,
                type: 2,
                maxFeePerGas: tx.maxFeePerGas,
                maxPriorityFeePerGas: tx.maxPriorityFeePerGas
            }
            break;
        default:
            throw "Unsupported tx type";
    }
...

nice job👍🏻 @pokrovskyy @chrsengel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment