Skip to content

Instantly share code, notes, and snippets.

@Pongch
Created January 17, 2021 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pongch/278432b892dc3b7b32a7ea04d4b0938b to your computer and use it in GitHub Desktop.
Save Pongch/278432b892dc3b7b32a7ea04d4b0938b to your computer and use it in GitHub Desktop.
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