-
Notifications
You must be signed in to change notification settings - Fork 41
/
account.go
184 lines (150 loc) · 4.89 KB
/
account.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package types
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"strings"
"gopkg.in/yaml.v2"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
ethcmn "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
)
var (
_ authtypes.AccountI = (*EthAccount)(nil)
_ authtypes.GenesisAccount = (*EthAccount)(nil)
_ codectypes.UnpackInterfacesMessage = (*EthAccount)(nil)
)
// ----------------------------------------------------------------------------
// Main Ethermint account
// ----------------------------------------------------------------------------
// ProtoAccount defines the prototype function for BaseAccount used for an
// AccountKeeper.
func ProtoAccount() authtypes.AccountI {
return &EthAccount{
BaseAccount: &authtypes.BaseAccount{},
CodeHash: ethcrypto.Keccak256(nil),
}
}
// EthAddress returns the account address ethereum format.
func (acc EthAccount) EthAddress() ethcmn.Address {
return ethcmn.BytesToAddress(acc.GetAddress().Bytes())
}
type ethAccountPretty struct {
Address string `json:"address" yaml:"address"`
EthAddress string `json:"eth_address" yaml:"eth_address"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
CodeHash string `json:"code_hash" yaml:"code_hash"`
}
// MarshalYAML returns the YAML representation of an account.
func (acc EthAccount) MarshalYAML() (interface{}, error) {
alias := ethAccountPretty{
Address: acc.Address,
EthAddress: acc.EthAddress().String(),
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
PubKey: "",
}
var err error
bz, err := yaml.Marshal(alias)
if err != nil {
return nil, err
}
return string(bz), err
}
// MarshalJSON returns the JSON representation of an EthAccount.
func (acc EthAccount) MarshalJSON() ([]byte, error) {
var ethAddress = ""
if acc.BaseAccount != nil && acc.Address == "" {
ethAddress = acc.EthAddress().String()
}
alias := ethAccountPretty{
Address: acc.Address,
EthAddress: ethAddress,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
PubKey: "",
}
return json.Marshal(alias)
}
// UnmarshalJSON unmarshals raw JSON bytes into an EthAccount.
func (acc *EthAccount) UnmarshalJSON(bz []byte) error {
var (
alias ethAccountPretty
err error
)
if err := json.Unmarshal(bz, &alias); err != nil {
return err
}
switch {
case alias.Address != "" && alias.EthAddress != "":
// Both addresses provided. Verify correctness
ethAddress := ethcmn.HexToAddress(alias.EthAddress)
var address sdk.AccAddress
address, err = sdk.AccAddressFromBech32(alias.Address)
if err != nil {
return err
}
ethAddressFromAccAddress := ethcmn.BytesToAddress(address.Bytes())
if !bytes.Equal(ethAddress.Bytes(), address.Bytes()) {
err = sdkerrors.Wrapf(
sdkerrors.ErrInvalidAddress,
"expected %s, got %s",
ethAddressFromAccAddress.String(), ethAddress.String(),
)
}
case alias.Address != "" && alias.EthAddress == "":
// unmarshal sdk.AccAddress only. Do nothing here
case alias.Address == "" && alias.EthAddress != "":
// retrieve sdk.AccAddress from ethereum address
ethAddress := ethcmn.HexToAddress(alias.EthAddress)
alias.Address = sdk.AccAddress(ethAddress.Bytes()).String()
case alias.Address == "" && alias.EthAddress == "":
err = sdkerrors.Wrapf(
sdkerrors.ErrInvalidAddress,
"account must contain address in Ethereum Hex or Cosmos Bech32 format",
)
}
if err != nil {
return err
}
acc.BaseAccount = &authtypes.BaseAccount{
Address: alias.Address,
AccountNumber: alias.AccountNumber,
Sequence: alias.Sequence,
}
acc.CodeHash = ethcmn.HexToHash(alias.CodeHash).Bytes()
return nil
}
// String implements the fmt.Stringer interface
func (acc EthAccount) String() string {
out, _ := yaml.Marshal(acc)
return string(out)
}
func CosmosAddressToEthAddress(addr string) (ethcmn.Address, error) {
if strings.HasPrefix(addr, sdk.GetConfig().GetBech32AccountAddrPrefix()) {
// Check to see if address is Cosmos bech32 formatted
toAddr, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return ethcmn.Address{}, errors.Wrap(err, "must provide a valid Bech32 address")
}
ethAddr := ethcmn.BytesToAddress(toAddr.Bytes())
return ethAddr, nil
}
if !strings.HasPrefix(addr, "0x") {
addr = "0x" + addr
}
valid := ethcmn.IsHexAddress(addr)
if !valid {
return ethcmn.Address{}, fmt.Errorf("%s is not a valid Ethereum or Cosmos address", addr)
}
ethAddr := ethcmn.HexToAddress(addr)
return ethAddr, nil
}