Last active
August 10, 2022 17:37
-
-
Save bafu/4f266856a8c25adb07477d24aea6a2cc to your computer and use it in GitHub Desktop.
Convert compressed public key to Ethereum wallet address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ethers } = require("ethers"); | |
/* Compressed public key stored in 264 bits (66 hex digits) | |
* https://www.oreilly.com/library/view/mastering-bitcoin-2nd/9781491954379/ch04.html | |
*/ | |
const compressedPubkey = "0x03aced43f9dddc120291f5cdf73580fbb592b5b21054ce61eb73cbaf98efcbe82e"; | |
const pubkey = ethers.utils.computePublicKey(compressedPubkey); | |
const walletAddress1 = ethers.utils.computeAddress(pubkey); | |
const walletAddress2 = ethers.utils.computeAddress(compressedPubkey); | |
// 0x5f5AD77F4f924232a6E486216Ddefba8a732b96B | |
console.log(`compuressed public key: ${compressedPubkey} (${compressedPubkey.length - 2}, no 0x)`) | |
console.log(`public key: ${pubkey} (${pubkey.length - 2}, no 0x)`) | |
console.log(`wallet address: ${walletAddress1}, ${walletAddress2}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ecdsa | |
from sha3 import keccak_256 | |
def public_key_to_wallet_address(eth_public_key): | |
vk = ecdsa.keys.VerifyingKey.from_string(bytes.fromhex(eth_public_key), curve=ecdsa.curves.SECP256k1) | |
# strip the leading 04 | |
public_key_bytes = vk.to_string('uncompressed')[1:] | |
wallet_address = '0x' + keccak_256(public_key_bytes).digest()[-20:].hex() | |
print(f'public key: {public_key_bytes.hex()}, wallet address: {wallet_address}') | |
return wallet_address | |
if __name__ == '__main__': | |
pubkey = '03aced43f9dddc120291f5cdf73580fbb592b5b21054ce61eb73cbaf98efcbe82e' | |
# 0x5f5ad77f4f924232a6e486216ddefba8a732b96b | |
print(public_key_to_wallet_address(pubkey)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment