Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang authored and mmsqe committed May 3, 2024
1 parent 96a5eef commit 3aa9d02
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
27 changes: 15 additions & 12 deletions x/e2ee/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@ func NewKeeper(storeKey storetypes.StoreKey, addressCodec address.Codec) Keeper
}
}

func (k Keeper) registerEncryptionKey(
ctx context.Context,
address string,
key []byte,
) error {
bz, err := k.addressCodec.StringToBytes(address)
if err != nil {
return err

Check warning on line 38 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L35-L38

Added lines #L35 - L38 were not covered by tests
}
sdk.UnwrapSDKContext(ctx).KVStore(k.storeKey).Set(types.KeyPrefix(bz), key)
return nil

Check warning on line 41 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L40-L41

Added lines #L40 - L41 were not covered by tests
}

func (k Keeper) RegisterEncryptionKey(
ctx context.Context,
req *types.MsgRegisterEncryptionKey,
) (*types.MsgRegisterEncryptionKeyResponse, error) {
bz, err := k.addressCodec.StringToBytes(req.Address)
if err != nil {
if err := k.registerEncryptionKey(ctx, req.Address, []byte(req.Key)); err != nil {

Check warning on line 48 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L48

Added line #L48 was not covered by tests
return nil, err
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.KVStore(k.storeKey).Set(types.KeyPrefix(bz), []byte(req.Key))
return &types.MsgRegisterEncryptionKeyResponse{}, nil
}

Expand All @@ -46,14 +56,7 @@ func (k Keeper) InitGenesis(
state *types.GenesisState,
) error {
for _, key := range state.Keys {
m := types.MsgRegisterEncryptionKey{
Address: key.Address,
Key: key.Key,
}
if err := m.ValidateBasic(); err != nil {
return err
}
if _, err := k.RegisterEncryptionKey(ctx, &m); err != nil {
if err := k.registerEncryptionKey(ctx, key.Address, []byte(key.Key)); err != nil {

Check warning on line 59 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L59

Added line #L59 was not covered by tests
return err
}
}
Expand Down
7 changes: 1 addition & 6 deletions x/e2ee/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -39,8 +37,5 @@ func (e EncryptionKeyEntry) Validate() error {
if _, err := sdk.AccAddressFromBech32(e.Address); err != nil {
return err
}
if len(e.Key) == 0 {
return errors.New("key can't be nil")
}
return nil
return ValidateRecipientKey(e.Key)
}
17 changes: 6 additions & 11 deletions x/e2ee/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ import (
)

func (m *MsgRegisterEncryptionKey) ValidateBasic() error {
if m.Address == "" {
return fmt.Errorf("address cannot be empty")
}
if len(m.Key) == 0 {
return fmt.Errorf("key cannot be empty")
}

if _, err := age.ParseX25519Recipient(m.Key); err != nil {
return err
}
// validate bech32 format of Address
if _, err := sdk.AccAddressFromBech32(m.Address); err != nil {
return fmt.Errorf("invalid address: %s", err)
}
return nil
return ValidateRecipientKey(m.Key)

Check warning on line 15 in x/e2ee/types/msg.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/types/msg.go#L15

Added line #L15 was not covered by tests
}

func ValidateRecipientKey(key string) error {
_, err := age.ParseX25519Recipient(key)
return err
}

0 comments on commit 3aa9d02

Please sign in to comment.