TruAddress

Title: TruAddress
Description: Library of helper functions surrounding the Solidity Address type
Author: Ian Bray, Tru Ltd
Solidity Version: 0.4.18
Relative Path: ./contracts/supporting/TruAddress.sol
License: Apache 2 License
Current Version: 0.1.9

1. Imports & Dependencies

The following imports and dependencies exist for the TruAddress Solidity Library:

Name Description
SafeMath Zeppelin Solidity Library to perform mathematics safely inside Solidity

2. Variables

There are no variables for the TruAddress Solidity Library.

3. Enums

There are no enums for the TruAddress Solidity Library.

4. Events

There are no events for the TruAddress Solidity Library.

5. Mappings

There are no mappings for the TruAddress Solidity Library.

6. Modifiers

There are no modifiers for the TruAddress Solidity Library.

7. Functions

The following functions exist for the TruAddress Solidity Library:

Name Description
isValid Function to validate a supplied ethereum address
toString Function to convert an Address to a String
addressLength Function to return the length of a given Address

isValid

Function Name: isValid
Description: Function to validate a supplied address is the correct length & format
Function Type: Pure
Function Visibility: Public
Function Modifiers: N/A
Return Type: Bool
Return Details: Returns true for valid input address; false for invalid input address

Code

The code for the isValid is as follows:

isValid Code
function isValid(address input) public pure returns (bool) {
    uint addrLength = addressLength(input);
    return ((addrLength == 20) && (input != address(0)));
}

The isValid function performs the following:

  • Retrieves the address length
  • returns a bool check that the address is both 20 characters long and not an empty address

Usage

The isValid function has the following usage syntax and arguments:

  Argument Type Details
1 input address Address to be validated
isValid Usage Example
isValid(0x123456789abcdefghijklmnopqrstuvwxyz98765);

toString

Function Name: toString
Description: Function to convert an address to a string
Function Type: Pure
Function Visibility: Internal
Function Modifiers: N/A
Return Type: String
Return Details: Returns the address in string format

Code

The code for the toString is as follows:

toString Code
function toString(address input) internal pure returns (string) {
    bytes memory byteArray = new bytes(20);
    for (uint i = 0; i < 20; i++) {
        byteArray[i] = byte(uint8(uint(input) / (2**(8*(19 - i)))));
    }
    return string(byteArray);
}

The toString function performs the following:

  • Creates a 20 byte array
  • iterates through the address and converts each byte
  • returns the byteArray as a string

Usage

The toString function has the following usage syntax and arguments:

  Argument Type Details
1 input address Address to be converted to a string
toString Usage Example
toString(0x123456789abcdefghijklmnopqrstuvwxyz98765);

addressLength

Function Name: addressLength
Description: Function to return the length of an address
Function Type: Pure
Function Visibility: Internal
Function Modifiers: N/A
Return Type: String
Return Details: Returns the length of the supplied address

Code

The code for the addressLength is as follows:

addressLength Code
 function addressLength(address input) internal pure returns (uint) {
     string memory addressStr = toString(input);
     return bytes(addressStr).length;
 }

The addressLength function performs the following:

  • Converts the supplied address to a string
  • returns the byte length of the string

Usage

The addressLength function has the following usage syntax and arguments:

  Argument Type Details
1 input address Address to calculate the length of
addressLength Usage Example
 addressLength(0x123456789abcdefghijklmnopqrstuvwxyz98765);