/
ethereum.go
111 lines (94 loc) · 3.4 KB
/
ethereum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package types
import (
"bytes"
"errors"
"fmt"
"regexp"
"strings"
"github.com/ethereum/go-ethereum/common"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// GravityDenomPrefix indicates the prefix for all assests minted by this module
const GravityDenomPrefix = "eth"
// GravityDenomSeparator is the separator for gravity denoms
const GravityDenomSeparator = ""
// ETHContractAddressLen is the length of contract address strings
const ETHContractAddressLen = 42
// GravityDenomLen is the length of the denoms generated by the gravity module
const GravityDenomLen = len(GravityDenomPrefix) + len(GravityDenomSeparator) + ETHContractAddressLen
// EthAddrLessThan migrates the Ethereum address less than function
func EthAddrLessThan(e, o string) bool {
return bytes.Compare([]byte(e)[:], []byte(o)[:]) == -1
}
// ValidateEthAddressAndValidateChecksum validates the ethereum address strings
func ValidateEthAddressAndValidateChecksum(a string) error {
if err := ValidateEthAddress(a); err != nil {
return err
}
// add ethereum address checksum check 2021-09-02.
if !common.IsHexAddress(a) {
return fmt.Errorf("invalid address: %s", a)
}
expectAddress := common.HexToAddress(a).Hex()
if expectAddress != a {
return fmt.Errorf("invalid address got:%s, expected:%s", a, expectAddress)
}
return nil
}
// ValidateEthAddress validates the ethereum address strings
func ValidateEthAddress(a string) error {
if a == "" {
return fmt.Errorf("empty")
}
if !regexp.MustCompile("^0x[0-9a-fA-F]{40}$").MatchString(a) {
return fmt.Errorf("address(%s) doesn't pass regex", a)
}
if len(a) != ETHContractAddressLen {
return fmt.Errorf("address(%s) of the wrong length exp(%d) actual(%d)", a, len(a), ETHContractAddressLen)
}
return nil
}
/////////////////////////
// ERC20Token //
/////////////////////////
// NewERC20Token returns a new instance of an ERC20
func NewERC20Token(amount uint64, contract string) *ERC20Token {
return &ERC20Token{Amount: sdk.NewIntFromUint64(amount), Contract: contract}
}
func NewSDKIntERC20Token(amount sdk.Int, contract string) *ERC20Token {
return &ERC20Token{Amount: amount, Contract: contract}
}
// GravityCoin returns the gravity representation of the ERC20
func (e *ERC20Token) GravityCoin() sdk.Coin {
return sdk.NewCoin(GravityDenom(e.Contract), e.Amount)
}
func GravityDenom(tokenContract string) string {
return fmt.Sprintf("%s%s%s", GravityDenomPrefix, GravityDenomSeparator, tokenContract)
}
// ValidateBasic permforms stateless validation
func (e *ERC20Token) ValidateBasic() error {
if err := ValidateEthAddressAndValidateChecksum(e.Contract); err != nil {
return sdkerrors.Wrap(err, "ethereum address")
}
if !e.Amount.IsPositive() {
return errors.New("invalid amount")
}
return nil
}
func GravityDenomToERC20(denom string) (string, error) {
fullPrefix := GravityDenomPrefix + GravityDenomSeparator
if !strings.HasPrefix(denom, fullPrefix) {
return "", fmt.Errorf("denom prefix(%s) not equal to expected(%s)", denom, fullPrefix)
}
contract := strings.TrimPrefix(denom, fullPrefix)
err := ValidateEthAddressAndValidateChecksum(contract)
switch {
case err != nil:
return "", fmt.Errorf("error(%s) validating ethereum contract address", err)
case len(denom) != GravityDenomLen:
return "", fmt.Errorf("len(denom)(%d) not equal to GravityDenomLen(%d)", len(denom), GravityDenomLen)
default:
return contract, nil
}
}