/ EIPS / eip-1191.md
eip-1191.md
  1  ---
  2  eip: 1191 
  3  title: Add chain id to mixed-case checksum address encoding 
  4  author: Juliano Rizzo (@juli)
  5  status: Last Call
  6  review-period-end: 2019-11-18
  7  type: Standards Track
  8  category: ERC
  9  created: 2018-03-18
 10  requires: 55, 155
 11  discussions-to: https://github.com/ethereum/EIPs/issues/1121
 12  ---
 13  
 14  ## Simple Summary
 15  This EIP extends EIP-55 by optionally adding a chain id defined by EIP-155 to the checksum calculation.
 16  
 17  ## Abstract
 18  The EIP-55 was created to prevent users from losing funds by sending them to invalid addresses. This EIP extends EIP-55 to protect users from losing funds by sending them to addresses that are valid but that where obtained from a client of another network.For example, if this EIP is implemented, a wallet can alert the user that is trying to send funds to an Ethereum Testnet address from an Ethereum Mainnet wallet.  
 19  
 20  ## Motivation
 21  The motivation of this proposal is to provide a mechanism to allow software to distinguish addresses from different Ethereum based networks. This proposal is necessary because Ethereum addresses are hashes of public keys and do not include any metadata. By extending the EIP-55 checksum algorithm it is possible to achieve this objective.
 22  
 23  ## Specification
 24  Convert the address using the same algorithm defined by EIP-55 but if a registered chain id is provided, add it to the input of the hash function. If the chain id passed to the function belongs to a network that opted for using this checksum variant, prefix the address with the chain id and the `0x` separator before calculating the hash. Then convert the address to hexadecimal, but if the ith digit is a letter (ie. it's one of `abcdef`) print it in uppercase if the 4*ith bit of the calculated hash is 1 otherwise print it in lowercase.
 25  
 26  ## Rationale 
 27   Benefits:
 28   - By means of a minimal code change on existing libraries, users are protected from losing funds by mixing addresses of different Ethereum based networks.
 29  
 30  ## Backwards Compatibility
 31  This proposal is fully backward compatible. The checksum calculation is changed only for new networks that choose to adopt this EIP and add their chain numbers to the Adoption Table included in this document.
 32  
 33  ## Implementation
 34  ```python
 35  #!/usr/bin/python3
 36  from sha3 import keccak_256
 37  import random
 38  """
 39     addr (str): Hexadecimal address, 40 characters long with 2 characters prefix
 40     chainid (int): chain id from EIP-155 """
 41  def eth_checksum_encode(addr, chainid=1):
 42      adopted_eip1191 = [30, 31]
 43      hash_input = str(chainid) + addr.lower() if chainid in adopted_eip1191 else addr[2:].lower()
 44      hash_output = keccak_256(hash_input.encode('utf8')).hexdigest()
 45      aggregate = zip(addr[2:].lower(),hash_output)
 46      out = addr[:2] + ''.join([c.upper() if int(a,16) >= 8 else c for c,a in aggregate])
 47      return out
 48  ```
 49  
 50  ## Test Cases
 51  ```python
 52  eth_mainnet = [
 53  "0x27b1fdb04752bbc536007a920d24acb045561c26",
 54  "0x3599689E6292b81B2d85451025146515070129Bb",
 55  "0x42712D45473476b98452f434e72461577D686318",
 56  "0x52908400098527886E0F7030069857D2E4169EE7",
 57  "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
 58  "0x6549f4939460DE12611948b3f82b88C3C8975323",
 59  "0x66f9664f97F2b50F62D13eA064982f936dE76657",
 60  "0x8617E340B3D01FA5F11F306F4090FD50E238070D",
 61  "0x88021160C5C792225E4E5452585947470010289D",
 62  "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
 63  "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
 64  "0xde709f2102306220921060314715629080e2fb77",
 65  "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
 66  ]
 67  rsk_mainnet = [
 68  "0x27b1FdB04752BBc536007A920D24ACB045561c26",
 69  "0x3599689E6292B81B2D85451025146515070129Bb",
 70  "0x42712D45473476B98452f434E72461577d686318",
 71  "0x52908400098527886E0F7030069857D2E4169ee7",
 72  "0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD",
 73  "0x6549F4939460DE12611948B3F82B88C3C8975323",
 74  "0x66F9664f97f2B50F62d13EA064982F936de76657",
 75  "0x8617E340b3D01Fa5f11f306f4090fd50E238070D",
 76  "0x88021160c5C792225E4E5452585947470010289d",
 77  "0xD1220A0Cf47c7B9BE7a2e6ba89F429762E7B9adB",
 78  "0xDBF03B407c01E7CD3cBea99509D93F8Dddc8C6FB",
 79  "0xDe709F2102306220921060314715629080e2FB77",
 80  "0xFb6916095cA1Df60bb79ce92cE3EA74c37c5d359",
 81  ]
 82  rsk_testnet = [
 83  "0x27B1FdB04752BbC536007a920D24acB045561C26",
 84  "0x3599689e6292b81b2D85451025146515070129Bb",
 85  "0x42712D45473476B98452F434E72461577D686318",
 86  "0x52908400098527886E0F7030069857D2e4169EE7",
 87  "0x5aAeb6053F3e94c9b9A09F33669435E7EF1BEaEd",
 88  "0x6549f4939460dE12611948b3f82b88C3c8975323",
 89  "0x66f9664F97F2b50f62d13eA064982F936DE76657",
 90  "0x8617e340b3D01fa5F11f306F4090Fd50e238070d",
 91  "0x88021160c5C792225E4E5452585947470010289d",
 92  "0xd1220a0CF47c7B9Be7A2E6Ba89f429762E7b9adB",
 93  "0xdbF03B407C01E7cd3cbEa99509D93f8dDDc8C6fB",
 94  "0xDE709F2102306220921060314715629080e2Fb77",
 95  "0xFb6916095CA1dF60bb79CE92ce3Ea74C37c5D359",
 96  ]
 97  test_cases = {30 : rsk_mainnet, 31 : rsk_testnet, 1 : eth_mainnet}
 98  
 99  for chainid, cases in test_cases.items():
100      for addr in cases:
101          assert ( addr == eth_checksum_encode(addr,chainid) )
102  ```
103  ## Adoption
104  
105  ### Adoption  Table
106  
107  | Network      | Chain id | Supports this EIP |
108  |-|-|-|
109  | RSK Mainnet  | 30       | Yes               |
110  | RSK Testnet  | 31       | Yes               |
111  
112  ### Implementation Table
113  
114  | Project         | Adopted this EIP | Implementation |
115  |-|-|-|
116  | MyCrypto       | Yes              | [JavaScript](https://github.com/MyCryptoHQ/MyCrypto/blob/develop/common/utils/formatters.ts#L126) |
117  | MyEtherWallet  | Yes              | [JavaScript](https://github.com/MyEtherWallet/MyEtherWallet/blob/73c4a24f8f67c655749ac990c5b62efd92a2b11a/src/helpers/addressUtils.js#L22) |
118  | Ledger         | Yes              | [C](https://github.com/LedgerHQ/ledger-app-eth/blob/master/src_common/ethUtils.c#L203) |
119  | Trezor         | Yes              | [Python](https://github.com/trezor/trezor-core/blob/270bf732121d004a4cd1ab129adaccf7346ff1db/src/apps/ethereum/get_address.py#L32) and [C](https://github.com/trezor/trezor-crypto/blob/4153e662b60a0d83c1be15150f18483a37e9092c/address.c#L62) |
120  | Web3.js           | Yes              | [JavaScript](https://github.com/ethereum/web3.js/blob/aaf26c8806bc9fb60cf6dcb6658104963c6c7fc7/packages/web3-utils/src/Utils.js#L140) |
121  | EthereumJS-util   | Yes              | [JavaScript](https://github.com/ethereumjs/ethereumjs-util/pull/204/commits/cdf0b3c996b05ac5b1f758f17ea9f9ed1847c1eb) |
122  | ENS address-encoder | Yes | [TypeScript](https://github.com/ensdomains/address-encoder/commit/5bf53b13fa014646ea28c9e5f937361dc9b40590) |
123  
124  ## Copyright
125  
126  Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).
127