From 332fa8da37c6d4cb12cc170f452ae21d28197de1 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 22 Feb 2024 16:49:57 +0530 Subject: [PATCH] Handle reservation for sub authority --- x/registry/keeper/naming_keeper.go | 47 +++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/x/registry/keeper/naming_keeper.go b/x/registry/keeper/naming_keeper.go index fe829a80..a0433e5f 100644 --- a/x/registry/keeper/naming_keeper.go +++ b/x/registry/keeper/naming_keeper.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/url" + "strings" "cosmossdk.io/collections" errorsmod "cosmossdk.io/errors" @@ -185,10 +186,9 @@ func (k Keeper) ReserveAuthority(ctx sdk.Context, msg registrytypes.MsgReserveAu return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid name") } - // TODO - // if strings.Contains(name, ".") { - // return k.ProcessReserveSubAuthority(ctx, name, msg) - // } + if strings.Contains(name, ".") { + return k.ReserveSubAuthority(ctx, name, msg) + } err = k.createAuthority(ctx, name, msg.GetSigner(), true) if err != nil { @@ -198,6 +198,45 @@ func (k Keeper) ReserveAuthority(ctx sdk.Context, msg registrytypes.MsgReserveAu return nil } +// ReserveSubAuthority reserves a sub-authority. +func (k Keeper) ReserveSubAuthority(ctx sdk.Context, name string, msg registrytypes.MsgReserveAuthority) error { + // Get parent authority name. + names := strings.Split(name, ".") + parent := strings.Join(names[1:], ".") + + // Check if parent authority exists. + if has, err := k.HasNameAuthority(ctx, parent); !has { + if err != nil { + return err + } + + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Parent authority not found.") + } + parentAuthority, err := k.GetNameAuthority(ctx, parent) + if err != nil { + return err + } + + // Sub-authority creator needs to be the owner of the parent authority. + if parentAuthority.OwnerAddress != msg.Signer { + return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Access denied.") + } + + // Sub-authority owner defaults to parent authority owner. + subAuthorityOwner := msg.Signer + if len(msg.Owner) != 0 { + // Override sub-authority owner if provided in message. + subAuthorityOwner = msg.Owner + } + + sdkErr := k.createAuthority(ctx, name, subAuthorityOwner, false) + if sdkErr != nil { + return sdkErr + } + + return nil +} + func (k Keeper) createAuthority(ctx sdk.Context, name string, owner string, isRoot bool) error { moduleParams, err := k.GetParams(ctx) if err != nil {