Created
January 17, 2021 09:15
-
-
Save Pongch/278432b892dc3b7b32a7ea04d4b0938b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package checksum | |
import ( | |
"encoding/hex" | |
"golang.org/x/crypto/sha3" | |
"strconv" | |
"strings" | |
) | |
func checksum(address string) string { | |
address = strings.ToLower(address) | |
address = strings.Replace(address, "0x", "", 1) | |
sha := sha3.NewLegacyKeccak256() | |
sha.Write([]byte(address)) | |
hash := sha.Sum(nil) | |
hashstr := hex.EncodeToString(hash) | |
result := []string{"0x"} | |
for i, v := range address { | |
res, _ := strconv.ParseInt(string(hashstr[i]), 16, 64) | |
if res > 7 { | |
result = append(result, strings.ToUpper(string(v))) | |
continue | |
} | |
result = append(result, string(v)) | |
} | |
return strings.Join(result, "") | |
} | |
func isChecksum(address string) bool { | |
return checksum(address) == address | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment