code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_list_permissions.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package api
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/libs/jsonrpc"
    23  	"code.vegaprotocol.io/vega/wallet/wallet"
    24  
    25  	"github.com/mitchellh/mapstructure"
    26  )
    27  
    28  type AdminListPermissionsParams struct {
    29  	Wallet string `json:"wallet"`
    30  }
    31  
    32  type AdminListPermissionsResult struct {
    33  	Permissions map[string]wallet.PermissionsSummary `json:"permissions"`
    34  }
    35  
    36  type AdminListPermissions struct {
    37  	walletStore WalletStore
    38  }
    39  
    40  // Handle returns the permissions summary for all set hostnames.
    41  func (h *AdminListPermissions) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    42  	params, err := validateListPermissionsParams(rawParams)
    43  	if err != nil {
    44  		return nil, InvalidParams(err)
    45  	}
    46  
    47  	if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil {
    48  		return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err))
    49  	} else if !exist {
    50  		return nil, InvalidParams(ErrWalletDoesNotExist)
    51  	}
    52  
    53  	alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, params.Wallet)
    54  	if err != nil {
    55  		return nil, InternalError(fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err))
    56  	}
    57  	if !alreadyUnlocked {
    58  		return nil, RequestNotPermittedError(ErrWalletIsLocked)
    59  	}
    60  
    61  	w, err := h.walletStore.GetWallet(ctx, params.Wallet)
    62  	if err != nil {
    63  		return nil, InternalError(fmt.Errorf("could not retrieve the wallet: %w", err))
    64  	}
    65  
    66  	permissions := map[string]wallet.PermissionsSummary{}
    67  	for _, hostname := range w.PermittedHostnames() {
    68  		permissions[hostname] = w.Permissions(hostname).Summary()
    69  	}
    70  
    71  	return AdminListPermissionsResult{
    72  		Permissions: permissions,
    73  	}, nil
    74  }
    75  
    76  func validateListPermissionsParams(rawParams jsonrpc.Params) (AdminListPermissionsParams, error) {
    77  	if rawParams == nil {
    78  		return AdminListPermissionsParams{}, ErrParamsRequired
    79  	}
    80  
    81  	params := AdminListPermissionsParams{}
    82  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    83  		return AdminListPermissionsParams{}, ErrParamsDoNotMatch
    84  	}
    85  
    86  	if params.Wallet == "" {
    87  		return AdminListPermissionsParams{}, ErrWalletIsRequired
    88  	}
    89  
    90  	return params, nil
    91  }
    92  
    93  func NewAdminListPermissions(
    94  	walletStore WalletStore,
    95  ) *AdminListPermissions {
    96  	return &AdminListPermissions{
    97  		walletStore: walletStore,
    98  	}
    99  }