mirror of
https://github.com/GrassrootsEconomics/cic-dw.git
synced 2026-05-19 20:40:56 +02:00
add: (feat) balances query api
- add address utils - return uint64 for balances instead of float
This commit is contained in:
45
pkg/address/address.go
Normal file
45
pkg/address/address.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package address
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/sha3"
|
||||
)
|
||||
|
||||
func SarafuAddress(address string) (string, error) {
|
||||
l := len(address)
|
||||
|
||||
if l < 40 || l > 42 {
|
||||
return "", fmt.Errorf("%s is not a valid eth address", address)
|
||||
}
|
||||
|
||||
if len(address) == 42 {
|
||||
return strings.ToLower(address)[2:], nil
|
||||
}
|
||||
|
||||
return strings.ToLower(address), nil
|
||||
}
|
||||
|
||||
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, "")
|
||||
}
|
||||
Reference in New Issue
Block a user