Skip to content

Commit

Permalink
moving stateless GenerateAddress func to types (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan authored Aug 27, 2021
1 parent 8acf6e6 commit 1bf7930
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
13 changes: 3 additions & 10 deletions modules/apps/27-interchain-accounts/keeper/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/tendermint/tendermint/crypto/tmhash"

"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
Expand Down Expand Up @@ -45,9 +44,9 @@ func (k Keeper) InitInterchainAccount(ctx sdk.Context, connectionID, counterpart

// Register interchain account if it has not already been created
func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, portId string) {
address := k.GenerateAddress(portId)
account := k.accountKeeper.GetAccount(ctx, address)
address := types.GenerateAddress(portId)

account := k.accountKeeper.GetAccount(ctx, address)
if account != nil {
// account already created, return no-op
return
Expand All @@ -57,11 +56,10 @@ func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, portId string) {
authtypes.NewBaseAccountWithAddress(address),
portId,
)

k.accountKeeper.NewAccount(ctx, interchainAccount)
k.accountKeeper.SetAccount(ctx, interchainAccount)
_ = k.SetInterchainAccountAddress(ctx, portId, interchainAccount.Address)

return
}

func (k Keeper) SetInterchainAccountAddress(ctx sdk.Context, portId string, address string) string {
Expand All @@ -82,11 +80,6 @@ func (k Keeper) GetInterchainAccountAddress(ctx sdk.Context, portId string) (str
return interchainAccountAddr, nil
}

// Determine account's address that will be created.
func (k Keeper) GenerateAddress(identifier string) []byte {
return tmhash.SumTruncated(append([]byte(identifier)))
}

func (k Keeper) GetInterchainAccount(ctx sdk.Context, addr sdk.AccAddress) (types.InterchainAccount, error) {
acc := k.accountKeeper.GetAccount(ctx, addr)
if acc == nil {
Expand Down
6 changes: 6 additions & 0 deletions modules/apps/27-interchain-accounts/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/tendermint/tendermint/crypto/tmhash"

connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types"
)
Expand All @@ -19,6 +20,11 @@ const (
ICAPrefix string = "ics-27"
)

// GenerateAddress returns a truncated SHA256 hash using the provided port string
func GenerateAddress(port string) []byte {
return tmhash.SumTruncated([]byte(port))
}

// GeneratePortID generates the portID for a specific owner
// on the controller chain in the format:
//
Expand Down

0 comments on commit 1bf7930

Please sign in to comment.