Skip to content

Instantly share code, notes, and snippets.

@swkim109
Last active December 15, 2023 21:41
Show Gist options
  • Save swkim109/2b9c215a96c3fa8c87859e35ee8a5a84 to your computer and use it in GitHub Desktop.
Save swkim109/2b9c215a96c3fa8c87859e35ee8a5a84 to your computer and use it in GitHub Desktop.
Public Key
/**
* "@ethereumjs/tx": "^5.1.0",
* "@ethereumjs/util": "^9.0.1",
*/
const { hexToBytes, bytesToHex } = require('@ethereumjs/util');
const { ecrecover } = require('@ethereumjs/util');
const { TransactionFactory } = require('@ethereumjs/tx');
// https://etherscan.io/tx/0x5ad6831e50960ffda005fc59c1970ce8fafaa3cff8b9d6695cd1f65cb3c2acf9
const txRaw = hexToBytes("0x02f87401822fc584b2eee28085064e6c8e9d82520894aea82a310c6dc710f438f1d9ad43df673dae408d872d2601128c800080c080a0ade1fb5fea776840fa1d35f28f89b047083e0f63ae214866bbb691ed4ddcc01ea04c59d6ed0122bf62384f069ad225bbe1072bc324936108eecceffb268c260050");
//const msgHash = hexToBytes("0xab05e7d121bf2bb6a1eea90ee057047c24b2682fba6127f3d2cf029898af1965");
const msgHash = TransactionFactory.fromSerializedData(txRaw).getHashedMessageToSign();
// from eth_getTransactionByHash
const v = BigInt(37); // 0 + (1 * 2 + 35)
const r = hexToBytes("0xade1fb5fea776840fa1d35f28f89b047083e0f63ae214866bbb691ed4ddcc01e");
const s = hexToBytes("0x4c59d6ed0122bf62384f069ad225bbe1072bc324936108eecceffb268c260050");
const publicKey = ecrecover(msgHash,v,r,s, BigInt(1));
console.log(`Public Key=${bytesToHex(publicKey)}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment