Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed May 3, 2024
1 parent 96a5eef commit 29beb9f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 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
6 changes: 2 additions & 4 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,8 @@ 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")
if err := ValidateRecipientKey(e.Key); err != nil {
return err

Check warning on line 41 in x/e2ee/types/keys.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/types/keys.go#L41

Added line #L41 was not covered by tests
}
return nil
}
18 changes: 8 additions & 10 deletions x/e2ee/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ 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)
}
if err := ValidateRecipientKey(m.Key); err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

x/e2ee/types/msg.go#L15-L16

Added lines #L15 - L16 were not covered by tests
}
return nil
}

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

0 comments on commit 29beb9f

Please sign in to comment.