github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/client/cmd_currency.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package client
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  
    10  	"github.com/keybase/cli"
    11  	"github.com/keybase/client/go/libcmdline"
    12  	"github.com/keybase/client/go/libkb"
    13  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    14  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    15  	"golang.org/x/net/context"
    16  )
    17  
    18  type CmdCurrencyAdd struct {
    19  	libkb.Contextified
    20  	address string
    21  	force   bool
    22  	wanted  string
    23  }
    24  
    25  func NewCmdCurrency(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    26  	return cli.Command{
    27  		Name:         "currency",
    28  		Usage:        "Manage cryptocurrency addresses",
    29  		ArgumentHelp: "[arguments...]",
    30  		Subcommands: []cli.Command{
    31  			NewCmdCurrencyAdd(cl, g),
    32  		},
    33  	}
    34  }
    35  
    36  func NewCmdBTC(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    37  	return cli.Command{
    38  		Name: "btc",
    39  		Action: func(c *cli.Context) {
    40  			cl.ChooseCommand(NewCmdBTCRunner(g), "btc", c)
    41  		},
    42  	}
    43  }
    44  
    45  type CmdBTC struct {
    46  	libkb.Contextified
    47  }
    48  
    49  func NewCmdBTCRunner(g *libkb.GlobalContext) *CmdBTC {
    50  	return &CmdBTC{
    51  		Contextified: libkb.NewContextified(g),
    52  	}
    53  }
    54  
    55  func (c *CmdBTC) Run() (err error) {
    56  	return errors.New("this command is deprecated; use `keybase currency add` instead")
    57  }
    58  
    59  func (c *CmdBTC) GetUsage() libkb.Usage {
    60  	return libkb.Usage{}
    61  }
    62  
    63  func (c *CmdBTC) ParseArgv(ctx *cli.Context) error {
    64  	return nil
    65  }
    66  
    67  func (c *CmdCurrencyAdd) ParseArgv(ctx *cli.Context) error {
    68  	if len(ctx.Args()) != 1 {
    69  		return fmt.Errorf("Must provide exactly one address.")
    70  	}
    71  	c.address = ctx.Args()[0]
    72  	c.force = ctx.Bool("force")
    73  	w := ctx.String("type")
    74  	if !(w == "bitcoin" || w == "zcash" || w == "") {
    75  		return fmt.Errorf("Bad address type; can only handle 'zcash' or 'bitcoin")
    76  	}
    77  	c.wanted = w
    78  	return nil
    79  }
    80  
    81  func (c *CmdCurrencyAdd) SetAddress(s string) {
    82  	c.address = s
    83  }
    84  
    85  func (c *CmdCurrencyAdd) Run() (err error) {
    86  	cli, err := GetCryptocurrencyClient(c.G())
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	protocols := []rpc.Protocol{
    92  		NewSecretUIProtocol(c.G()),
    93  	}
    94  	if err = RegisterProtocolsWithContext(protocols, c.G()); err != nil {
    95  		return err
    96  	}
    97  
    98  	res, err := cli.RegisterAddress(context.TODO(), keybase1.RegisterAddressArg{
    99  		Address:      c.address,
   100  		Force:        c.force,
   101  		WantedFamily: c.wanted,
   102  	})
   103  	if err != nil {
   104  		if _, ok := err.(libkb.ExistsError); ok {
   105  			err = fmt.Errorf("You already have a %s address; use --force to overwrite", err.Error())
   106  		}
   107  		return err
   108  	}
   109  	c.G().UI.GetTerminalUI().Printf("Added %s address %s\n", res.Family, c.address)
   110  	return nil
   111  }
   112  
   113  func NewCmdCurrencyAdd(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
   114  	return cli.Command{
   115  		Name:         "add",
   116  		Usage:        "Sign a cryptocurrency (bitcoin or zcash) address into your identity",
   117  		ArgumentHelp: "<address>",
   118  		Flags: []cli.Flag{
   119  			cli.BoolFlag{
   120  				Name:  "f, force",
   121  				Usage: "Overwrite an existing address.",
   122  			},
   123  			cli.StringFlag{
   124  				Name:  "t, type",
   125  				Usage: "assert a type of address ('bitcoin' or 'zcash')",
   126  			},
   127  		},
   128  		Action: func(c *cli.Context) {
   129  			cl.ChooseCommand(NewCmdCurrencyAddRunner(g), "add", c)
   130  		},
   131  	}
   132  }
   133  
   134  func NewCmdCurrencyAddRunner(g *libkb.GlobalContext) *CmdCurrencyAdd {
   135  	return &CmdCurrencyAdd{
   136  		Contextified: libkb.NewContextified(g),
   137  	}
   138  }
   139  
   140  func (c *CmdCurrencyAdd) GetUsage() libkb.Usage {
   141  	return libkb.Usage{
   142  		Config:     true,
   143  		GpgKeyring: true,
   144  		KbKeyring:  true,
   145  		API:        true,
   146  	}
   147  }