2021-12-19 21:39:53 +00:00
|
|
|
package processor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2022-02-09 11:03:36 +00:00
|
|
|
"github.com/trustwallet/assets-go-libs/file"
|
2021-12-20 15:03:08 +00:00
|
|
|
"github.com/trustwallet/assets-go-libs/image"
|
|
|
|
"github.com/trustwallet/assets-go-libs/path"
|
|
|
|
"github.com/trustwallet/assets-go-libs/validation"
|
|
|
|
"github.com/trustwallet/assets-go-libs/validation/info"
|
2021-12-19 21:39:53 +00:00
|
|
|
"github.com/trustwallet/go-primitives/address"
|
|
|
|
"github.com/trustwallet/go-primitives/coin"
|
|
|
|
"github.com/trustwallet/go-primitives/types"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Service) FixJSON(f *file.AssetFile) error {
|
2022-02-09 11:03:36 +00:00
|
|
|
return file.FormatJSONFile(f.Path())
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) FixETHAddressChecksum(f *file.AssetFile) error {
|
|
|
|
if !coin.IsEVM(f.Chain().ID) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
assetDir := filepath.Base(f.Path())
|
|
|
|
|
|
|
|
err := validation.ValidateETHForkAddress(f.Chain(), assetDir)
|
|
|
|
if err != nil {
|
|
|
|
checksum, e := address.EIP55Checksum(assetDir)
|
|
|
|
if e != nil {
|
|
|
|
return fmt.Errorf("failed to get checksum: %s", e)
|
|
|
|
}
|
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
newName := path.GetAssetPath(f.Chain().Handle, checksum)
|
2021-12-19 21:39:53 +00:00
|
|
|
|
|
|
|
if e = os.Rename(f.Path(), newName); e != nil {
|
|
|
|
return fmt.Errorf("failed to rename dir: %s", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.fileService.UpdateFile(f, checksum)
|
|
|
|
|
|
|
|
log.WithField("from", assetDir).
|
|
|
|
WithField("to", checksum).
|
|
|
|
Debug("Renamed asset")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) FixLogo(f *file.AssetFile) error {
|
2021-12-20 15:03:08 +00:00
|
|
|
width, height, err := image.GetPNGImageDimensions(f.Path())
|
2021-12-19 21:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var isLogoTooLarge bool
|
|
|
|
if width > validation.MaxW || height > validation.MaxH {
|
|
|
|
isLogoTooLarge = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if isLogoTooLarge {
|
|
|
|
log.WithField("path", f.Path()).Debug("Fixing too large image")
|
|
|
|
|
|
|
|
targetW, targetH := calculateTargetDimension(width, height)
|
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
err = image.ResizePNG(f.Path(), targetW, targetH)
|
2021-12-19 21:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validation.ValidateLogoFileSize(f.Path())
|
2022-08-07 04:20:55 +00:00
|
|
|
if err != nil { //nolint:staticcheck
|
2021-12-19 21:39:53 +00:00
|
|
|
// TODO: Compress images.
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func calculateTargetDimension(width, height int) (targetW, targetH int) {
|
|
|
|
widthFloat := float32(width)
|
|
|
|
heightFloat := float32(height)
|
|
|
|
|
|
|
|
maxEdge := widthFloat
|
|
|
|
if heightFloat > widthFloat {
|
|
|
|
maxEdge = heightFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
ratio := validation.MaxW / maxEdge
|
|
|
|
|
|
|
|
targetW = int(widthFloat * ratio)
|
|
|
|
targetH = int(heightFloat * ratio)
|
|
|
|
|
|
|
|
return targetW, targetH
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) FixChainInfoJSON(f *file.AssetFile) error {
|
2022-01-31 14:11:27 +00:00
|
|
|
var chainInfo info.CoinModel
|
2021-12-19 21:39:53 +00:00
|
|
|
|
2022-02-09 11:03:36 +00:00
|
|
|
err := file.ReadJSONFile(f.Path(), &chainInfo)
|
2021-12-19 21:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedType := string(types.Coin)
|
|
|
|
if chainInfo.Type == nil || *chainInfo.Type != expectedType {
|
|
|
|
chainInfo.Type = &expectedType
|
|
|
|
|
2022-06-14 12:57:34 +00:00
|
|
|
data, err := file.PrepareJSONData(&chainInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.CreateJSONFile(f.Path(), data)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:30:49 +00:00
|
|
|
func (s *Service) FixAssetInfo(f *file.AssetFile) error {
|
2022-01-31 14:11:27 +00:00
|
|
|
var assetInfo info.AssetModel
|
2021-12-19 21:39:53 +00:00
|
|
|
|
2022-02-09 11:03:36 +00:00
|
|
|
err := file.ReadJSONFile(f.Path(), &assetInfo)
|
2021-12-19 21:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var isModified bool
|
|
|
|
|
|
|
|
// Fix asset type.
|
|
|
|
var assetType string
|
|
|
|
if assetInfo.Type != nil {
|
|
|
|
assetType = *assetInfo.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to skip error check to fix asset type if it's incorrect or empty.
|
|
|
|
chain, _ := types.GetChainFromAssetType(assetType)
|
|
|
|
|
2022-01-09 20:30:49 +00:00
|
|
|
expectedTokenType, ok := types.GetTokenType(f.Chain().ID, f.Asset())
|
2021-12-19 21:39:53 +00:00
|
|
|
if !ok {
|
|
|
|
expectedTokenType = strings.ToUpper(assetType)
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:30:49 +00:00
|
|
|
if chain.ID != f.Chain().ID || !strings.EqualFold(assetType, expectedTokenType) {
|
2021-12-19 21:39:53 +00:00
|
|
|
assetInfo.Type = &expectedTokenType
|
|
|
|
isModified = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix asset id.
|
2022-01-09 20:30:49 +00:00
|
|
|
assetID := f.Asset()
|
2021-12-19 21:39:53 +00:00
|
|
|
if assetInfo.ID == nil || *assetInfo.ID != assetID {
|
|
|
|
assetInfo.ID = &assetID
|
|
|
|
isModified = true
|
|
|
|
}
|
|
|
|
|
2022-01-20 13:14:34 +00:00
|
|
|
expectedExplorerURL, err := coin.GetCoinExploreURL(f.Chain(), f.Asset(), assetType)
|
2021-12-19 21:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix asset explorer url.
|
|
|
|
if assetInfo.Explorer == nil || !strings.EqualFold(expectedExplorerURL, *assetInfo.Explorer) {
|
|
|
|
assetInfo.Explorer = &expectedExplorerURL
|
|
|
|
isModified = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if isModified {
|
2022-06-14 12:57:34 +00:00
|
|
|
data, err := file.PrepareJSONData(&assetInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.CreateJSONFile(f.Path(), data)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|