github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/sdk/auth/handler.go (about)

     1  package auth
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/gnolang/gno/tm2/pkg/amino"
     8  	abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
     9  	"github.com/gnolang/gno/tm2/pkg/crypto"
    10  	"github.com/gnolang/gno/tm2/pkg/sdk"
    11  	"github.com/gnolang/gno/tm2/pkg/std"
    12  )
    13  
    14  type authHandler struct {
    15  	acck AccountKeeper
    16  }
    17  
    18  // NewHandler returns a handler for "auth" type messages.
    19  func NewHandler(acck AccountKeeper) authHandler {
    20  	return authHandler{
    21  		acck: acck,
    22  	}
    23  }
    24  
    25  func (ah authHandler) Process(ctx sdk.Context, msg std.Msg) sdk.Result {
    26  	// no messages supported yet.
    27  	errMsg := fmt.Sprintf("unrecognized auth message type: %T", msg)
    28  	return abciResult(std.ErrUnknownRequest(errMsg))
    29  }
    30  
    31  //----------------------------------------
    32  // Query
    33  
    34  // query account path
    35  const QueryAccount = "accounts"
    36  
    37  func (ah authHandler) Query(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
    38  	switch secondPart(req.Path) {
    39  	case QueryAccount:
    40  		return ah.queryAccount(ctx, req)
    41  	default:
    42  		res = sdk.ABCIResponseQueryFromError(
    43  			std.ErrUnknownRequest("unknown auth query endpoint"))
    44  		return
    45  	}
    46  }
    47  
    48  // queryAccount fetch an account for the supplied height.
    49  // Account address are passed as path component.
    50  func (ah authHandler) queryAccount(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
    51  	// parse addr from path.
    52  	b32addr := thirdPart(req.Path)
    53  	addr, err := crypto.AddressFromBech32(b32addr)
    54  	if err != nil {
    55  		res = sdk.ABCIResponseQueryFromError(
    56  			std.ErrInvalidAddress(
    57  				"invalid query address " + b32addr))
    58  		return
    59  	}
    60  
    61  	// get account from addr.
    62  	bz, err := amino.MarshalJSONIndent(
    63  		ah.acck.GetAccount(ctx, addr),
    64  		"", "  ")
    65  	if err != nil {
    66  		res = sdk.ABCIResponseQueryFromError(
    67  			std.ErrInternal(fmt.Sprintf("could not marshal result to JSON: %s", err.Error())))
    68  		return
    69  	}
    70  
    71  	res.Data = bz
    72  	return
    73  }
    74  
    75  //----------------------------------------
    76  // misc
    77  
    78  // returns the second component of a path.
    79  func secondPart(path string) string {
    80  	parts := strings.Split(path, "/")
    81  	if len(parts) < 2 {
    82  		return ""
    83  	} else {
    84  		return parts[1]
    85  	}
    86  }
    87  
    88  // returns the third component of a path.
    89  func thirdPart(path string) string {
    90  	parts := strings.Split(path, "/")
    91  	if len(parts) < 3 {
    92  		return ""
    93  	} else {
    94  		return parts[2]
    95  	}
    96  }