-
Notifications
You must be signed in to change notification settings - Fork 44
/
can_create_module_account_at_addr.go
55 lines (51 loc) · 2.41 KB
/
can_create_module_account_at_addr.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
package utils
import (
"errors"
"fmt"
"reflect"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/elys-network/elys/x/amm/types"
)
// CanCreateModuleAccountAtAddr tells us if we can safely make a module account at
// a given address. By collision resistance of the address (given API safe construction),
// the only way for an account to be already be at this address is if its claimed by the same
// pre-image from the correct module,
// or some SDK command breaks assumptions and creates an account at designated address.
// This function checks if there is an account at that address, and runs some safety checks
// to be extra-sure its not a user account (e.g. non-zero sequence, pubkey, of fore-seen account types).
// If there is no account, or if we believe its not a user-spendable account, we allow module account
// creation at the address.
// else, we do not.
//
// TODO: This is generally from an SDK design flaw
// code based off wasmd code: https://github.com/CosmWasm/wasmd/pull/996
// Its _mandatory_ that the caller do the API safe construction to generate a module account addr,
// namely, address.Module(ModuleName, {key})
func CanCreateModuleAccountAtAddr(ctx sdk.Context, ak types.AccountKeeper, addr sdk.AccAddress) error {
existingAcct := ak.GetAccount(ctx, addr)
if existingAcct == nil {
return nil
}
if existingAcct.GetSequence() != 0 || existingAcct.GetPubKey() != nil {
return fmt.Errorf("cannot create module account %s, "+
"due to an account at that address already existing & having sent txs", addr)
}
overrideAccountTypes := map[reflect.Type]struct{}{
reflect.TypeOf(&authtypes.BaseAccount{}): {},
reflect.TypeOf(&vestingtypes.DelayedVestingAccount{}): {},
reflect.TypeOf(&vestingtypes.ContinuousVestingAccount{}): {},
reflect.TypeOf(&vestingtypes.BaseVestingAccount{}): {},
reflect.TypeOf(&vestingtypes.PeriodicVestingAccount{}): {},
reflect.TypeOf(&vestingtypes.PermanentLockedAccount{}): {},
}
for extraAccountType := range ExtraAccountTypes {
overrideAccountTypes[extraAccountType] = struct{}{}
}
if _, clear := overrideAccountTypes[reflect.TypeOf(existingAcct)]; clear {
return nil
}
return errors.New("cannot create module account %s, " +
"due to an account at that address already existing & not being an overrideable type")
}