-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
357 lines (305 loc) · 9.27 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package types
import (
"bytes"
"encoding/json"
"fmt"
"github.com/tendermint/go-amino"
"gopkg.in/yaml.v2"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
sdkerrors "github.com/evoblockchain/evochain/libs/cosmos-sdk/types/errors"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/auth"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/auth/exported"
authtypes "github.com/evoblockchain/evochain/libs/cosmos-sdk/x/auth/types"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
)
var _ exported.Account = (*EthAccount)(nil)
var _ exported.GenesisAccount = (*EthAccount)(nil)
var emptyCodeHash = crypto.Keccak256(nil)
func init() {
authtypes.RegisterAccountTypeCodec(&EthAccount{}, EthAccountName)
}
// ----------------------------------------------------------------------------
// Main EVOChain account
// ----------------------------------------------------------------------------
// EthAccount implements the auth.Account interface and embeds an
// auth.BaseAccount type. It is compatible with the auth.AccountKeeper.
type EthAccount struct {
*authtypes.BaseAccount `json:"base_account" yaml:"base_account"`
CodeHash []byte `json:"code_hash" yaml:"code_hash"`
}
func (acc *EthAccount) UnmarshalFromAmino(cdc *amino.Codec, data []byte) error {
var dataLen uint64 = 0
var baseAccountFlag bool
for {
data = data[dataLen:]
if len(data) <= 0 {
break
}
pos, pbType, err := amino.ParseProtoPosAndTypeMustOneByte(data[0])
if err != nil {
return err
}
// all EthAccount fields are (2)
if pbType != amino.Typ3_ByteLength {
return fmt.Errorf("invalid pbType: %v", pbType)
}
data = data[1:]
var n int
dataLen, n, err = amino.DecodeUvarint(data)
if err != nil {
return err
}
data = data[n:]
if len(data) < int(dataLen) {
return fmt.Errorf("not enough data for field %d", pos)
}
subData := data[:dataLen]
switch pos {
case 1:
baseAccountFlag = true
if acc.BaseAccount == nil {
acc.BaseAccount = &auth.BaseAccount{}
} else {
*acc.BaseAccount = auth.BaseAccount{}
}
err = acc.BaseAccount.UnmarshalFromAmino(cdc, subData)
if err != nil {
return err
}
case 2:
acc.CodeHash = make([]byte, len(subData))
copy(acc.CodeHash, subData)
default:
return fmt.Errorf("unexpect feild num %d", pos)
}
}
if !baseAccountFlag {
acc.BaseAccount = nil
}
return nil
}
type componentAccount struct {
ethAccount EthAccount
baseAccount authtypes.BaseAccount
}
func (acc EthAccount) Copy() sdk.Account {
// we need only allocate one object on the heap with componentAccount
var cacc componentAccount
cacc.baseAccount.Address = acc.Address
cacc.baseAccount.Coins = acc.Coins
cacc.baseAccount.PubKey = acc.PubKey
cacc.baseAccount.AccountNumber = acc.AccountNumber
cacc.baseAccount.Sequence = acc.Sequence
cacc.ethAccount.BaseAccount = &cacc.baseAccount
cacc.ethAccount.CodeHash = acc.CodeHash
return &cacc.ethAccount
}
func (acc EthAccount) AminoSize(cdc *amino.Codec) int {
size := 0
if acc.BaseAccount != nil {
baccSize := acc.BaseAccount.AminoSize(cdc)
size += 1 + amino.UvarintSize(uint64(baccSize)) + baccSize
}
if len(acc.CodeHash) != 0 {
size += 1 + amino.ByteSliceSize(acc.CodeHash)
}
return size
}
func (acc EthAccount) MarshalToAmino(cdc *amino.Codec) ([]byte, error) {
var buf bytes.Buffer
buf.Grow(acc.AminoSize(cdc))
err := acc.MarshalAminoTo(cdc, &buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (acc EthAccount) MarshalAminoTo(cdc *amino.Codec, buf *bytes.Buffer) error {
// field 1
if acc.BaseAccount != nil {
const pbKey = 1<<3 | 2
buf.WriteByte(pbKey)
baccSize := acc.BaseAccount.AminoSize(cdc)
err := amino.EncodeUvarintToBuffer(buf, uint64(baccSize))
if err != nil {
return err
}
lenBeforeData := buf.Len()
err = acc.BaseAccount.MarshalAminoTo(cdc, buf)
if err != nil {
return err
}
if buf.Len()-lenBeforeData != baccSize {
return amino.NewSizerError(acc.BaseAccount, baccSize, buf.Len()-lenBeforeData)
}
}
// field 2
if len(acc.CodeHash) != 0 {
const pbKey = 2<<3 | 2
err := amino.EncodeByteSliceWithKeyToBuffer(buf, acc.CodeHash, pbKey)
if err != nil {
return err
}
}
return nil
}
// ProtoAccount defines the prototype function for BaseAccount used for an
// AccountKeeper.
func ProtoAccount() exported.Account {
return &EthAccount{
BaseAccount: &auth.BaseAccount{},
CodeHash: ethcrypto.Keccak256(nil),
}
}
// EthAddress returns the account address ethereum format.
func (acc EthAccount) EthAddress() ethcmn.Address {
return ethcmn.BytesToAddress(acc.Address.Bytes())
}
// TODO: remove on SDK v0.40
// Balance returns the balance of an account.
func (acc EthAccount) Balance(denom string) sdk.Dec {
return acc.GetCoins().AmountOf(denom)
}
// SetBalance sets an account's balance of the given coin denomination.
//
// CONTRACT: assumes the denomination is valid.
func (acc *EthAccount) SetBalance(denom string, amt sdk.Dec) {
coins := acc.GetCoins()
diff := amt.Sub(coins.AmountOf(denom))
switch {
case diff.IsPositive():
// Increase coins to amount
coins = coins.Add(sdk.NewCoin(denom, diff))
case diff.IsNegative():
// Decrease coins to amount
coins = coins.Sub(sdk.NewCoins(sdk.NewCoin(denom, diff.Neg())))
default:
return
}
if err := acc.SetCoins(coins); err != nil {
panic(fmt.Errorf("could not set %s coins for address %s: %w", denom, acc.EthAddress().String(), err))
}
}
type ethermintAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
EthAddress string `json:"eth_address" yaml:"eth_address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
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) {
ethAddress := ""
if !sdk.IsWasmAddress(acc.Address) {
ethAddress = acc.EthAddress().String()
}
alias := ethermintAccountPretty{
Address: acc.Address,
EthAddress: ethAddress,
Coins: acc.Coins,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
}
var err error
if acc.PubKey != nil {
alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PubKey)
if err != nil {
return nil, err
}
}
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 != nil {
if !sdk.IsWasmAddress(acc.Address) {
ethAddress = acc.EthAddress().String()
}
}
alias := ethermintAccountPretty{
Address: acc.Address,
EthAddress: ethAddress,
Coins: acc.Coins,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
}
var err error
if acc.PubKey != nil {
alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PubKey)
if err != nil {
return nil, err
}
}
return json.Marshal(alias)
}
// UnmarshalJSON unmarshals raw JSON bytes into an EthAccount.
func (acc *EthAccount) UnmarshalJSON(bz []byte) error {
var (
alias ethermintAccountPretty
err error
)
if err := json.Unmarshal(bz, &alias); err != nil {
return err
}
switch {
case !alias.Address.Empty() && alias.EthAddress != "":
// Both addresses provided. Verify correctness
ethAddress := ethcmn.HexToAddress(alias.EthAddress)
ethAddressFromAccAddress := ethcmn.BytesToAddress(alias.Address.Bytes())
if !bytes.Equal(ethAddress.Bytes(), alias.Address.Bytes()) {
err = sdkerrors.Wrapf(
sdkerrors.ErrInvalidAddress,
"expected %s, got %s",
ethAddressFromAccAddress.String(), ethAddress.String(),
)
}
case !alias.Address.Empty() && alias.EthAddress == "":
// unmarshal sdk.AccAddress only. Do nothing here
case alias.Address.Empty() && alias.EthAddress != "":
// retrieve sdk.AccAddress from ethereum address
ethAddress := ethcmn.HexToAddress(alias.EthAddress)
alias.Address = sdk.AccAddress(ethAddress.Bytes())
case alias.Address.Empty() && 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{
Coins: alias.Coins,
Address: alias.Address,
AccountNumber: alias.AccountNumber,
Sequence: alias.Sequence,
}
acc.CodeHash = ethcmn.Hex2Bytes(alias.CodeHash)
if alias.PubKey != "" {
acc.BaseAccount.PubKey, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey)
if err != nil {
return err
}
}
return nil
}
// String implements the fmt.Stringer interface
func (acc EthAccount) String() string {
out, _ := yaml.Marshal(acc)
return string(out)
}
// IsContract returns if the account contains contract code.
func (acc EthAccount) IsContract() bool {
return !bytes.Equal(acc.CodeHash, emptyCodeHash)
}