Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
84 lines (65 sloc) 3.13 KB

Checksum Address Encoding

RSKIP 60
Title Checksum Address Encoding
Created 25-JUN-2016
Author JL,IO
Purpose ST
Layer Net
Complexity 1
Status Draft*

Motivation

  • Avoid typing confusion in adresses.
  • Differentiate addresses of different networks.

Abstract

Addresses can be validated using an injective function that makes capital letters redundant. RSKIP-0060 describes an address checksum mechanism that can be implemented in any network based on EIP-55.

Specification

In Javascript:

function toChecksumAddress(address, chainId = null) {
    const strip_address = stripHexPrefix(address).toLowerCase()
    const prefix = chainId != null ? (chainId.toString() + '0x') : ''
    const keccak_hash = keccak(prefix + strip_address).toString('hex')
    let output = '0x'

    for (let i = 0; i < strip_address.length; i++)
        output += parseInt(keccak_hash[i], 16) >= 8 ?
            strip_address[i].toUpperCase() :
            strip_address[i]

    return output
}

Adds the chain id as a prefix. Converts the address to hexadecimal. Calculates keccak with the prefixed address. Prints i digit if it's a number, otherwise checks if i byte of the hash of the keccak. If it's grater than 8 prints uppercase, otherwise lowercase.

Implementation

chainId unique values defined in EIP-155.

This algorithm is compatible with EIP-55. This can be achieved using chainId = null.

Rationale

Benefit:

  • Allows implementation in any network. Can distinguish between testnets and mainnets.
  • Backwards compatiblity with many hex parsers that accept mixed case, allowing it to be easily introduced over time.
  • Compatibility with EIP-55 checksum implementation.

Test cases

Valid with network id: 30
    0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD
    0xFb6916095cA1Df60bb79ce92cE3EA74c37c5d359
    0xDBF03B407c01E7CD3cBea99509D93F8Dddc8C6FB
    0xD1220A0Cf47c7B9BE7a2e6ba89F429762E7B9adB

Valid with network id: 31
    0x5aAeb6053F3e94c9b9A09F33669435E7EF1BEaEd
    0xFb6916095CA1dF60bb79CE92ce3Ea74C37c5D359
    0xdbF03B407C01E7cd3cbEa99509D93f8dDDc8C6fB
    0xd1220a0CF47c7B9Be7A2E6Ba89f429762E7b9adB

Valid with network id: None
    0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed
    0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359
    0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB
    0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb

Invalid with network id: 1234
    0x5aaeb6053F3E94C9B9A09f33669435E7Ef1BeAed
    0xfb6916095ca1df60bb79Ce92ce3ea74c37c5d359
    0xDBF03B407C01E7CD3CBEA99509D93f8DDDC8C6FB
    0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb

Copyright

Copyright and related rights waived via CC0.