This SIP extends SIP-55 by optionally adding a chain id defined by SIP-155 to the checksum calculation.
Abstract
The SIP-55 was created to prevent users from losing funds by sending them to invalid addresses. This SIP extends SIP-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 SIP is implemented, a wallet can alert the user that is trying to send funds to an Sila Testnet address from an Sila SilaMainnet wallet.
Motivation
The motivation of this proposal is to provide a mechanism to allow software to distinguish addresses from different Sila based networks. This proposal is necessary because Sila addresses are hashes of public keys and do not include any metadata. By extending the SIP-55 checksum algorithm it is possible to achieve this objective.
Specification
Convert the address using the same algorithm defined by SIP-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.
Rationale
Benefits:
By means of a minimal code change on existing libraries, users are protected from losing funds by mixing addresses of different Sila based networks.
Implementation
#!/usr/bin/python3
fromsha3importkeccak_256importrandom"""
addr (str): Hexadecimal address, 40 characters long with 2 characters prefix
chainid (int): chain id from SIP-155 """defsil_checksum_encode(addr,chainid=1):adopted_sip1191=[30,31]hash_input=str(chainid)+addr.lower()ifchainidinadopted_sip1191elseaddr[2:].lower()hash_output=keccak_256(hash_input.encode('utf8')).hexdigest()aggregate=zip(addr[2:].lower(),hash_output)out=addr[:2]+''.join([c.upper()ifint(a,16)>=8elsecforc,ainaggregate])returnout