Skip to content

Instantly share code, notes, and snippets.

@protortyp
Last active October 25, 2023 18:33

Revisions

  1. protortyp revised this gist Jan 12, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion getPubKey.js
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,8 @@ getPubKey = async () => {
    value: tx.value,
    nonce: tx.nonce,
    data: tx.data,
    chainId: tx.chainId
    chainId: tx.chainId,
    to: tx.to
    }
    const rsTx = await ethers.utils.resolveProperties(txData)
    const raw = ethers.utils.serializeTransaction(rsTx) // returns RLP encoded tx
  2. protortyp revised this gist Jan 12, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion getPubKey.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,6 @@ getPubKey = async () => {
    const expandedSig = {
    r: tx.r,
    s: tx.s,
    recoveryParam: 0,
    v: tx.v
    }
    const signature = ethers.utils.joinSignature(expandedSig)
  3. protortyp created this gist Jan 11, 2020.
    41 changes: 41 additions & 0 deletions getPubKey.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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,
    recoveryParam: 0,
    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
    }
    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()