/
module_account.go
76 lines (68 loc) · 3.03 KB
/
module_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
package utils
// Borrowed from Osmosis
// https://github.com/osmosis-labs/osmosis/blob/5297ea4b641c0c1090cb253a34a510dd0c85b407/osmoutils/module_account.go
// Allows the safe creation of module accounts in the event that an account has already been initialized at an address
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"
)
type AccountKeeper interface {
NewAccount(sdk.Context, authtypes.AccountI) authtypes.AccountI
GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI
SetAccount(ctx sdk.Context, acc authtypes.AccountI)
}
// 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.
func CanCreateModuleAccountAtAddr(ctx sdk.Context, ak 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{}): {},
}
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")
}
// CreateModuleAccount creates a module account at the provided address.
// It overrides an account if it exists at that address, with a non-zero sequence number & pubkey
// Contract: addr is derived from `address.Module(ModuleName, key)`
func CreateModuleAccount(ctx sdk.Context, ak AccountKeeper, addr sdk.AccAddress) error {
err := CanCreateModuleAccountAtAddr(ctx, ak, addr)
if err != nil {
return err
}
acc := ak.NewAccount(
ctx,
authtypes.NewModuleAccount(
authtypes.NewBaseAccountWithAddress(addr),
addr.String(),
),
)
ak.SetAccount(ctx, acc)
return nil
}