diff options
author | Jim McDonald <Jim@mcdee.net> | 2017-07-16 20:26:16 +0800 |
---|---|---|
committer | Jim McDonald <Jim@mcdee.net> | 2017-07-16 20:26:16 +0800 |
commit | 9e80d9bee1363520f4868bd63e42dea79ada24e5 (patch) | |
tree | 7f277538eb8ae88dde262ec11e6912d919d42d0d /common | |
parent | 0ff35e170d1b913082313089d13e3e6d5490839b (diff) | |
download | go-tangerine-9e80d9bee1363520f4868bd63e42dea79ada24e5.tar go-tangerine-9e80d9bee1363520f4868bd63e42dea79ada24e5.tar.gz go-tangerine-9e80d9bee1363520f4868bd63e42dea79ada24e5.tar.bz2 go-tangerine-9e80d9bee1363520f4868bd63e42dea79ada24e5.tar.lz go-tangerine-9e80d9bee1363520f4868bd63e42dea79ada24e5.tar.xz go-tangerine-9e80d9bee1363520f4868bd63e42dea79ada24e5.tar.zst go-tangerine-9e80d9bee1363520f4868bd63e42dea79ada24e5.zip |
common: Address.Hex() outputs EIP55-compliant string
Diffstat (limited to 'common')
-rw-r--r-- | common/types.go | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/common/types.go b/common/types.go index 803726634..eaf8352fb 100644 --- a/common/types.go +++ b/common/types.go @@ -24,6 +24,7 @@ import ( "reflect" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto/sha3" ) const ( @@ -163,7 +164,28 @@ func (a Address) Str() string { return string(a[:]) } func (a Address) Bytes() []byte { return a[:] } func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) } func (a Address) Hash() Hash { return BytesToHash(a[:]) } -func (a Address) Hex() string { return hexutil.Encode(a[:]) } + +// Hex returns an EIP55-compliant hex string representation of the address. +func (a Address) Hex() string { + unchecksummed := hex.EncodeToString(a[:]) + sha := sha3.NewKeccak256() + sha.Write([]byte(unchecksummed)) + hash := sha.Sum(nil) + + result := []byte(unchecksummed) + for i := 0; i < len(result); i++ { + hashByte := hash[i/2] + if i%2 == 0 { + hashByte = hashByte >> 4 + } else { + hashByte &= 0xf + } + if result[i] > '9' && hashByte > 7 { + result[i] -= 32 + } + } + return "0x" + string(result) +} // String implements the stringer interface and is used also by the logger. func (a Address) String() string { |