code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/service_endpoints.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 cmd
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  	"os"
    22  	"text/template"
    23  
    24  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli"
    25  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    26  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer"
    27  	"code.vegaprotocol.io/vega/paths"
    28  	svcStoreV1 "code.vegaprotocol.io/vega/wallet/service/store/v1"
    29  
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  const startupT = ` # Authentication
    34   - login:                   POST   {{.WalletServiceLocalAddress}}/api/v1/auth/token
    35   - logout:                  DELETE {{.WalletServiceLocalAddress}}/api/v1/auth/token
    36  
    37   # Network management
    38   - network:                 GET    {{.WalletServiceLocalAddress}}/api/v1/network
    39   - network chainid:         GET    {{.WalletServiceLocalAddress}}/api/v1/network/chainid
    40  
    41   # Wallet management
    42   - create a wallet:         POST   {{.WalletServiceLocalAddress}}/api/v1/wallets
    43   - import a wallet:         POST   {{.WalletServiceLocalAddress}}/api/v1/wallets/import
    44  
    45   # Key pair management
    46   - generate a key pair:     POST   {{.WalletServiceLocalAddress}}/api/v1/keys
    47   - list keys:               GET    {{.WalletServiceLocalAddress}}/api/v1/keys
    48   - describe a key pair:     GET    {{.WalletServiceLocalAddress}}/api/v1/keys/:keyid
    49   - taint a key pair:        PUT    {{.WalletServiceLocalAddress}}/api/v1/keys/:keyid/taint
    50   - annotate a key pair:     PUT    {{.WalletServiceLocalAddress}}/api/v1/keys/:keyid/metadata
    51  
    52   # Commands
    53   - sign a command:          POST   {{.WalletServiceLocalAddress}}/api/v1/command
    54   - sign a command (sync):   POST   {{.WalletServiceLocalAddress}}/api/v1/command/sync
    55   - sign a command (commit): POST   {{.WalletServiceLocalAddress}}/api/v1/command/commit
    56   - sign data:               POST   {{.WalletServiceLocalAddress}}/api/v1/sign
    57   - verify data:             POST   {{.WalletServiceLocalAddress}}/api/v1/verify
    58  
    59   # Information
    60   - get service status:      GET    {{.WalletServiceLocalAddress}}/api/v1/status
    61   - get the version:         GET    {{.WalletServiceLocalAddress}}/api/v1/version
    62  `
    63  
    64  var (
    65  	listEndpointsLong = cli.LongDesc(`
    66  		List the Vega wallet service HTTP endpoints
    67  	`)
    68  
    69  	listEndpointsExample = cli.Examples(`
    70  		# List service endpoints
    71  		{{.Software}} endpoints --network NETWORK
    72  	`)
    73  )
    74  
    75  type ListEndpointsHandler func(io.Writer, *RootFlags, *ListEndpointsFlags) error
    76  
    77  func NewCmdListEndpoints(w io.Writer, rf *RootFlags) *cobra.Command {
    78  	return BuildCmdListEndpoints(w, ListEndpoints, rf)
    79  }
    80  
    81  func BuildCmdListEndpoints(w io.Writer, handler ListEndpointsHandler, rf *RootFlags) *cobra.Command {
    82  	f := &ListEndpointsFlags{}
    83  
    84  	cmd := &cobra.Command{
    85  		Use:     "endpoints",
    86  		Short:   "List endpoints",
    87  		Long:    listEndpointsLong,
    88  		Example: listEndpointsExample,
    89  		RunE: func(_ *cobra.Command, _ []string) error {
    90  			if err := f.Validate(); err != nil {
    91  				return err
    92  			}
    93  
    94  			if err := handler(w, rf, f); err != nil {
    95  				return err
    96  			}
    97  
    98  			return nil
    99  		},
   100  	}
   101  
   102  	cmd.Flags().StringVarP(&f.Network,
   103  		"network", "n",
   104  		"",
   105  		"Network configuration to use",
   106  	)
   107  
   108  	return cmd
   109  }
   110  
   111  type ListEndpointsFlags struct {
   112  	Network string
   113  }
   114  
   115  func (f *ListEndpointsFlags) Validate() error {
   116  	if len(f.Network) == 0 {
   117  		return flags.MustBeSpecifiedError("network")
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  func ListEndpoints(w io.Writer, rf *RootFlags, f *ListEndpointsFlags) error {
   124  	p := printer.NewInteractivePrinter(w)
   125  
   126  	vegaPaths := paths.New(rf.Home)
   127  	svcStore, err := svcStoreV1.InitialiseStore(vegaPaths)
   128  	if err != nil {
   129  		return fmt.Errorf("couldn't initialise the service store: %w", err)
   130  	}
   131  
   132  	cfg, err := svcStore.GetConfig()
   133  	if err != nil {
   134  		return fmt.Errorf("couldn't retrieve the service configuration: %w", err)
   135  	}
   136  
   137  	str := p.String()
   138  	defer p.Print(str)
   139  
   140  	str.BlueArrow().InfoText("Available endpoints").NextLine()
   141  	printServiceEndpoints(cfg.Server.String())
   142  	str.NextLine()
   143  
   144  	return nil
   145  }
   146  
   147  func printServiceEndpoints(serviceHost string) {
   148  	params := struct {
   149  		WalletServiceLocalAddress string
   150  	}{
   151  		WalletServiceLocalAddress: serviceHost,
   152  	}
   153  
   154  	tmpl, err := template.New("wallet-cmdline").Parse(startupT)
   155  	if err != nil {
   156  		panic(err)
   157  	}
   158  	err = tmpl.Execute(os.Stdout, params)
   159  	if err != nil {
   160  		panic(err)
   161  	}
   162  }