Skip to content

Instantly share code, notes, and snippets.

@thaarok
Created September 3, 2024 18:10
Show Gist options
  • Save thaarok/97c729d2a0797291d26ec300ac8f5703 to your computer and use it in GitHub Desktop.
Save thaarok/97c729d2a0797291d26ec300ac8f5703 to your computer and use it in GitHub Desktop.
Ethereum pubkey to address

Example data: (Opera fakenet)

Pubkey: 0xc0048d505c351f4837cec72bce6f4254f5e4bc3f2c9a4816841db64319eee8b714ef9173fbf66d039b782624713791840846b2788d4b65a425adeba85a4b57efe0cd

Converts to address: 0x239fA7623354eC26520dE878B52f13Fe84b06971

Solidity:

    function pubkeyToAddress(bytes calldata pubkey) external pure returns(address) {
        require(pubkey.length == 66 && pubkey[0] == 0xc0, "malformed pubkey");
        return address(uint160(uint256(keccak256(pubkey[2:]))));
    }

Golang:

	pubkey := crypto.FromECDSAPub(&key.PublicKey) // 65 bytes
	fmt.Printf("pubkey: %x\n", pubkey, len(pubkey))
	addr := crypto.PubkeyToAddress(key.PublicKey)
	fmt.Printf("addr: %v\n", addr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment