Handle reservation for sub authority

This commit is contained in:
Prathamesh Musale 2024-02-22 16:49:57 +05:30
parent 0ac8398979
commit 332fa8da37

View File

@ -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 {