Skip to main content

How to Handle Checksum Addresses

tokenviewAbout 1 min

How to Handle Checksum Addresses

A checksum address is a case-sensitive representation of an address that is used to help prevent errors when manually entering or copying addresses. Ethereumopen in new window addresses are typically represented as hexadecimal strings, when these strings are manually entered or copied, mistakes such as typos or incorrect capitalization can occur, potentially leading to lost or misdirected funds.

To address this issue, Ethereum introduced checksum addressesopen in new window, which utilize a combination of uppercase and lowercase letters together. For example, the checksum address corresponding to "0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5" would be "0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5". The presence of uppercase letters in the address indicates that it is a checksum address.

How to compute the checksum of an address?

  1. Convert the original Ethereum address into bytes.
  2. Do keccak256 for the hex of bytes.
  3. Iterate the character of the keccak256 result, if a character is a number (0-9), then leave the corresponding character as lowercase, if a character in the hash is a letter (a-f), then check the hex digit of the character index in keccak256 result, if the hex digit is greater than 7, then the corresponding character should be uppercase, otherwise, the character should be left as lowercase.
  4. The final result is the checksummed version of the address.
import eth_utils

def checksum_encode(addr): # Takes an address as input
    addr_bytes = eth_utils.to_bytes(hexstr=addr)
    hex_addr = addr_bytes.hex()
    checksummed_buffer = ""
    # Treat the hex address as ascii/utf-8 for keccak256 hashing
    hashed_address = eth_utils.keccak(text=hex_addr).hex()
    # Iterate over each character in the hex address
    for nibble_index, character in enumerate(hex_addr):
        if character in "0123456789":
            # We can't upper-case the decimal digits
            checksummed_buffer += character
        elif character in "abcdef":
            # Check if the corresponding hex digit (nibble) in the hash is 8 or higher
            hashed_address_nibble = int(hashed_address[nibble_index], 16)
            if hashed_address_nibble > 7:
                checksummed_buffer += character.upper()
            else:
                checksummed_buffer += character
    return "0x" + checksummed_buffer

def getCheckSum(addr_str):
    checksum_encoded = checksum_encode(addr_str)
    print(checksum_encoded)

getCheckSum("0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5")

Checksum addresses is helping the validation when entering or copying Ethereum addresses, helping to reduce the risk of errors and ensuring that transactions are sent to the correct recipient. They are commonly used in wallets, exchanges, and other Ethereum-related applications to improve the accuracy and reliability of address handling.

About Tokenview Blockchain APIs & Data Service Platform:

Tokenview blockchain APIs & data service platform contained 120+ blockchains has powerful endpoints that simplify complex blockchain data into single API calls. Code for all supported blockchains using unified API calls. From here to easily get transaction, address, gas, contract, token, NFT, logs and any information from chain. And Yes you can also send tx to chain. One-click configuration with APIKey helps developers to Use, to Create, to Build.
Last update: