-
Notifications
You must be signed in to change notification settings - Fork 1
/
ethereum.go
99 lines (78 loc) · 2.8 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
package types
import (
"bytes"
"encoding/hex"
"fmt"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gethcommon "github.com/ethereum/go-ethereum/common"
)
const (
// ETHContractAddressLen is the length of contract address strings
ETHContractAddressLen = 42
// ZeroAddress is an EthAddress containing the zero ethereum address
ZeroAddressString = "0x0000000000000000000000000000000000000000"
)
// Regular EthAddress
type EthAddress struct {
address gethcommon.Address
}
// Returns the contained address as a string
func (ea EthAddress) GetAddress() gethcommon.Address {
return ea.address
}
// Sets the contained address, performing validation before updating the value
func (ea *EthAddress) SetAddress(address string) error {
if err := ValidateEthAddress(address); err != nil {
return err
}
ea.address = gethcommon.HexToAddress(address)
return nil
}
func NewEthAddressFromBytes(address []byte) (*EthAddress, error) {
if err := ValidateEthAddress(hex.EncodeToString(address)); err != nil {
return nil, sdkerrors.Wrap(err, "invalid input address")
}
addr := EthAddress{gethcommon.BytesToAddress(address)}
return &addr, nil
}
// Creates a new EthAddress from a string, performing validation and returning any validation errors
func NewEthAddress(address string) (*EthAddress, error) {
if err := ValidateEthAddress(address); err != nil {
return nil, sdkerrors.Wrap(err, "invalid input address")
}
addr := EthAddress{gethcommon.HexToAddress(address)}
return &addr, nil
}
// Returns a new EthAddress with 0x0000000000000000000000000000000000000000 as the wrapped address
func ZeroAddress() EthAddress {
return EthAddress{gethcommon.HexToAddress(ZeroAddressString)}
}
// Validates the input string as an Ethereum Address
// Addresses must not be empty, have 42 character length, start with 0x and have 40 remaining characters in [0-9a-fA-F]
func ValidateEthAddress(address string) error {
if address == "" {
return fmt.Errorf("empty")
}
// An auditor recommended we should check the error of hex.DecodeString, given that geth's HexToAddress ignores it
if has0xPrefix(address) {
address = address[2:]
}
if _, err := hex.DecodeString(address); err != nil {
return fmt.Errorf("invalid hex with error: %s", err)
}
if !gethcommon.IsHexAddress(address) {
return fmt.Errorf("address(%s) doesn't pass format validation", address)
}
return nil
}
// Performs validation on the wrapped string
func (ea EthAddress) ValidateBasic() error {
return ValidateEthAddress(ea.address.Hex())
}
// EthAddrLessThan migrates the Ethereum address less than function
func EthAddrLessThan(e EthAddress, o EthAddress) bool {
return bytes.Compare([]byte(e.GetAddress().Hex()), []byte(o.GetAddress().Hex())) == -1
}
func has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}