From 4eff57189daabfcec3d2e0d25de1528de5aa5596 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 30 Sep 2019 17:28:10 +0200 Subject: [PATCH] Fix ETH address API - some tokens are missing #271 --- bchain/coins/eth/erc20.go | 14 ++++++++++++++ bchain/coins/eth/erc20_test.go | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/bchain/coins/eth/erc20.go b/bchain/coins/eth/erc20.go index 135859e7..d001722d 100644 --- a/bchain/coins/eth/erc20.go +++ b/bchain/coins/eth/erc20.go @@ -2,11 +2,13 @@ package eth import ( "blockbook/bchain" + "bytes" "context" "encoding/hex" "math/big" "strings" "sync" + "unicode/utf8" ethcommon "github.com/ethereum/go-ethereum/common" "github.com/golang/glog" @@ -150,6 +152,18 @@ func parseErc20StringProperty(contractDesc bchain.AddressDescriptor, data string } } } + } else if len(data) == 64 { + // allow string properties as 32 bytes of UTF-8 data + b, err := hex.DecodeString(data) + if err == nil { + i := bytes.Index(b, []byte{0}) + if i > 0 { + b = b[:i] + } + if utf8.Valid(b) { + return string(b) + } + } } if glog.V(1) { glog.Warning("Cannot parse '", data, "' for contract ", contractDesc) diff --git a/bchain/coins/eth/erc20_test.go b/bchain/coins/eth/erc20_test.go index 324ec3c3..0a12738b 100644 --- a/bchain/coins/eth/erc20_test.go +++ b/bchain/coins/eth/erc20_test.go @@ -128,6 +128,16 @@ func TestErc20_parseErc20StringProperty(t *testing.T) { args: "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000022426974436c617665202d20436f6e73756d657220416374697669747920546f6b656e00000000000000", want: "BitClave - Consumer Activity Token", }, + { + name: "short", + args: "0x44616920537461626c65636f696e2076312e3000000000000000000000000000", + want: "Dai Stablecoin v1.0", + }, + { + name: "short2", + args: "0x44616920537461626c65636f696e2076312e3020444444444444444444444444", + want: "Dai Stablecoin v1.0 DDDDDDDDDDDD", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {