ethereum/EIPs-55 Mixed-case checksum address encoding

eip title author type category status created

55

Mixed-case checksum address encoding

Vitalik Buterin

Standards Track

ERC

Final

2016-01-14

Specification(python)

from ethereum import utils

def checksum_encode(addr): # Takes a 20-byte binary address as input
    o = ‘‘
    v = utils.big_endian_to_int(utils.sha3(addr.hex()))
    for i, c in enumerate(addr.hex()):
        if c in ‘0123456789‘://就是如果address在i位置上的值是数字的话,就不做任何改变
            o += c
        else: //但是如果是字符的话,就要另进行判断,(2**(255 - 4*i))这个的二进制的结果就是从后向前数的255 - 4*i个位置上的值为1,即是为了实现从前往后数为4*i位置的数字
            o += c.upper() if (v & (2**(255 - 4*i))) else c.lower()//255的原因是hash的v有32bytes,即32*8=256,从0开始                                                                   //所以意思是如果小写十六进制地址的散列v的第4*i位也是1,则以大写形式打印,否则以小写形式打印。
    return ‘0x‘+o

def test(addrstr):
    assert(addrstr == checksum_encode(bytes.fromhex(addrstr[2:])))

test(‘0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed‘)
test(‘0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359‘)
test(‘0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB‘)
test(‘0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb‘)

注意:v = utils.big_endian_to_int(utils.sha3(addr.hex()))

所谓的大端模式(Big-endian),是指数据的高字节,保存在内存的低地址中,而数据的低字节,保存在内存的高地址中,这样的存储模式有点儿类似于把数据当作字符串顺序处理:地址由小向大增加,而数据从高位往低位放;比如1234,如果是数字的话应该从低位开始读4321(即小端模式);但是这里为大端模式,所以读法就是1234。这是为了顺序处理addr的hash值

In English, convert the address to hex, 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 hash of the lowercase hexadecimal address is 1 otherwise print it in lowercase.

Rationale

Benefits:

  • Backwards compatible with many hex parsers that accept mixed case, allowing it to be easily introduced over time
  • Keeps the length at 40 characters。 address长度为40个字符
  • On average there will be 15 check bits per address, and the net probability that a randomly generated address if mistyped will accidentally pass a check is 0.0247%. This is a ~50x improvement over ICAP, but not as good as a 4-byte check code.  上面bit的转换效率没有字节的转化效率快,所以下面实现的是半字节(16进制一字符4bits)形式的转换方法:

例子:

const createKeccakHash = require(‘keccak‘);

function toChecksumAddress (address) {
  address = address.toLowerCase().replace(‘0x‘, ‘‘);
  console.log(address);
  var hash = createKeccakHash(‘keccak256‘).update(address).digest(‘hex‘);//update就是输入要加密的值,digest就是将加密好的hash值以16进制的形式输出
  console.log(hash);//5cfac663f45837b409c4d3dc1cef5f4759734f4989dd53a31b1265734c0b28f4,64个
  var ret = ‘0x‘;

  for (var i = 0; i < address.length; i++) {//所以只用得到hash的前40个字符
    if (parseInt(hash[i], 16) >= 8) {//即将16进制的hash[i]转化成10进制的数值后与8进行比较,如果在i位置的hash的值大于或等于8,相应位置的address的值就换成大写,否则就还是小写
      ret += address[i].toUpperCase();//其实就是根据得到的hash值来相应将address转换成大小写皆有的形式
      console.log(‘if‘);
      console.log(ret);
    } else {
      ret += address[i];
      console.log(‘else‘);
      console.log(ret);
    }
  }

  return ret;
}

var addr = ‘0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359‘;//去掉0x是40个
console.log(toChecksumAddress(addr));

这里的例子address是16进制的,当然,如果the hex address encoded as ASCII,那么就写成:

var hash = createKeccakHash(‘keccak256‘).update(Buffer.from(address.toLowerCase(), ‘ascii‘)).digest()

Adoption

Wallet displays checksummed addresses rejects invalid mixed-case rejects too short rejects too long
Etherwall 2.0.1 Yes Yes Yes Yes
Jaxx 1.2.17 No Yes Yes Yes
MetaMask 3.7.8 Yes Yes Yes Yes
Mist 0.8.10 Yes Yes Yes Yes
MyEtherWallet v3.9.4 Yes Yes Yes Yes
Parity 1.6.6-beta (UI) Yes Yes Yes Yes

Exchange support for mixed-case address checksums, as of 2017-05-27:

Exchange displays checksummed deposit addresses rejects invalid mixed-case rejects too short rejects too long
Bitfinex No Yes Yes Yes
Coinbase Yes No Yes Yes
GDAX Yes Yes Yes Yes
Kraken No No Yes Yes
Poloniex No No Yes Yes
Shapeshift No No Yes Yes

References

  1. EIP 55 issue and discussion https://github.com/ethereum/eips/issues/55
  2. Python implementation in ethereum-utils
  3. Ethereumjs-util implementation https://github.com/ethereumjs/ethereumjs-util/blob/75f529458bc7dc84f85fd0446d0fac92d991c262/index.js#L452-L466
/**
 * Returns a checksummed address
 * @param {String} address
 * @return {String}
 */
exports.toChecksumAddress = function (address) {
  address = exports.stripHexPrefix(address).toLowerCase()
  var hash = exports.sha3(address).toString(‘hex‘)
  var ret = ‘0x‘

  for (var i = 0; i < address.length; i++) {
    if (parseInt(hash[i], 16) >= 8) {
      ret += address[i].toUpperCase()
    } else {
      ret += address[i]
    }
  }

  return ret
}

  4.Swift implementation in EthereumKit

原文地址:https://www.cnblogs.com/wanghui-garcia/p/9709389.html

时间: 2024-08-02 09:38:01

ethereum/EIPs-55 Mixed-case checksum address encoding的相关文章

【Ethereum】以太坊ERC20 Token标准完整说明

什么是ERC20 token 市面上出现了大量的用ETH做的代币,他们都遵守REC20协议,那么我们需要知道什么是REC20协议. 概述 token代表数字资产,具有价值,但是并不是都符合特定的规范. 基于ERC20的货币更容易互换,并且能够在Dapps上相同的工作. 新的标准可以让token更兼容,允许其他功能,包括投票标记化.操作更像一个投票操作 Token的持有人可以完全控制资产,遵守ERC20的token可以跟踪任何人在任何时间拥有多少token.基于eth合约的子货币,所以容易实施.只

ethereum/EIPs-725

https://github.com/ethereum/EIPs/blob/master/EIPS/eip-725.md eip title author discussions-to status type category created 725 Proxy Identity Fabian Vogelsteller (@frozeman) https://github.com/ethereum/EIPs/issues/725 Draft Standards Track ERC 2017-10

逆向知识第九讲,switch case语句在汇编中表达的方式

一丶Switch Case语句在汇编中的第一种表达方式 (引导性跳转表) 第一种表达方式生成条件: case 个数偏少,那么汇编中将会生成引导性的跳转表,会做出 if else的情况(类似,但还是能分辨出来的) 1.高级代码: #include "stdafx.h" int main(int argc, char* argv[]) { switch(argc) { case 0: printf("case 0\n"); break; case 1: printf(&

smtp

Network Working Group J. Klensin, EditorRequest for Comments: 2821 AT&T LaboratoriesObsoletes: 821, 974, 1869 April 2001Updates: 1123Category: Standards Track Simple Mail Transfer Protocol Status of this Memo This document specifies an Internet stand

Mule ESB 自带例子hello初体验

1 配置的流的效果图 2 应用配置文件hello.xml内容 1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://w

dnsmasq多物理网口dhcp配置

家里有一台pc安装了openwrt做成软路由,升级主板后.不知道什么原因openwrt无法运行,只好自己配置了. OS使用的是centos7.3mini版,DHCP使用的是dnsmasq2.77(最新版) 硬件:intel j1900主板 ,Intel e1000 pcie四口网卡 由于7.3自带的2.66在配置多网口dhcp时无法分配ip,不知道是什么问题造成的.2.77版本就没有这个问题. dnsmasq系统启动文件: [Unit] Description=DNS caching serve

Google C++ 代码规范

Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard Forward Declarations Inline Functions Names and Order of Includes Scoping Namespaces Unnamed Namespaces and Static Variables Nonmember, Static Member, and

pdi vcard-2.1

vCard The Electronic Business Card Version 2.1 A versit Consortium Specification September 18, 1996 Copyrights © 1996, International Business Machines Corp., Lucent Technologies, Inc., and Siemens. All rights reserved. Permission is granted to copy a

Google C++ Style Guide----英文版

转载请注明出处<http://blog.csdn.net/qianqin_2014/article/details/51354326> Background C++ is the main development language used by many of Google's open-source projects. As every C++ programmer knows, the language has many powerful features, but this power