How To Convert Bytes To Address In Solidity?

In Solidity, you may need to convert a byte array (usually 20 bytes long for Ethereum addresses) into an Ethereum address. Below is a step-by-step guide.

Step 1: Understand the Byte Array

Ethereum addresses are 20 bytes long. If you have a byte array (like bytes or bytes20), make sure it is exactly 20 bytes to create a valid address.

Step 2: Define the Function

You need a function that takes a byte array as input and converts it to an address.

Example Code

pragma solidity ^0.8.0;

contract AddressConverter {
    
    function bytesToAddress(bytes memory b) public pure returns (address) {
        require(b.length == 20, "Byte array must be 20 bytes long");
        address convertedAddress;

        assembly {
            convertedAddress := mload(add(b, 20))
        }

        return convertedAddress;
    }
}

Code Explanation

  1. Function Declaration:
    function bytesToAddress(bytes memory b) public pure returns (address)
    
    • bytes memory b: Takes a dynamic byte array as input.
    • public: The function can be called both internally and by other contracts.
    • pure: The function does not modify or read from the contract’s state.
    • returns (address): Specifies that the function will return an Ethereum address.
  2. Require Statement:
    require(b.length == 20, "Byte array must be 20 bytes long");
    
    • This checks if the provided byte array has a length of 20 bytes. If not, it throws an error with the message “Byte array must be 20 bytes long”.
  3. Assembly Code:
assembly {
    convertedAddress := mload(add(b, 20))
}
  • Uses inline assembly for efficient processing:
    • add(b, 20): Moves the pointer to the start of the data in the byte array (adding 20 accounts for the first 32 bytes that Solidity allocates for dynamic arrays).
    • mload: Loads the next 20 bytes starting from the adjusted pointer location into convertedAddress.
  1. Return Statement:
    return convertedAddress;
    
    • Returns the converted Ethereum address.

Step 3: Call the Function

To use the bytesToAddress function, you need to call it with a valid byte array.

Example Usage

contract Test {
    AddressConverter converter = new AddressConverter();

    function testConversion() public view returns (address) {
        bytes memory byteArray = hex"1234567890abcdef1234567890abcdef12345678";  // Example byte array
        return converter.bytesToAddress(byteArray);
    }
}

Explanation of Example Usage

  1. Contract Declaration:
    contract Test {
    
    • Creates a new contract to test the address conversion.
  2. Instantiate Converter:
    AddressConverter converter = new AddressConverter();
    
    • Creates an instance of the AddressConverter contract.
  3. Test Function:
function testConversion() public view returns (address)
  • Calls the bytesToAddress method with a sample byte array.
  1. Byte Array:
    bytes memory byteArray = hex"1234567890abcdef1234567890abcdef12345678";
    
    • This is a HEX representation of a 20-byte array (the first 12 bytes will be ignored since the address is only 20 bytes).
  2. Return Statement:
    return converter.bytesToAddress(byteArray);
    
    • Returns the converted address.

By following these steps, you can successfully convert a byte array to an address in Solidity.