github.com/keybase/client/go@v0.0.0-20240520164431-4f512a4c85a3/client/cmd_wallet_sign.go (about)

     1  // Copyright 2019 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  	"io"
     8  	"os"
     9  	"strings"
    10  
    11  	"github.com/keybase/cli"
    12  	"github.com/keybase/client/go/libcmdline"
    13  	"github.com/keybase/client/go/libkb"
    14  	"github.com/keybase/client/go/protocol/stellar1"
    15  	"golang.org/x/net/context"
    16  )
    17  
    18  type CmdWalletSign struct {
    19  	libkb.Contextified
    20  	XDR       string
    21  	AccountID stellar1.AccountID
    22  	Submit    bool
    23  }
    24  
    25  func newCmdWalletSign(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    26  	cmd := &CmdWalletSign{
    27  		Contextified: libkb.NewContextified(g),
    28  	}
    29  	flags := []cli.Flag{
    30  		cli.StringFlag{
    31  			Name:  "xdr",
    32  			Usage: "Base64-encoded XDR of transaction envelope. If not provided, will be read from stdin.",
    33  		},
    34  		cli.StringFlag{
    35  			Name:  "account",
    36  			Usage: "Account ID to sign with. Optional, if not provided, SourceAccount of the transaction will be used.",
    37  		},
    38  		cli.BoolFlag{
    39  			Name:  "submit",
    40  			Usage: "Submit signed transaction to the network.",
    41  		},
    42  	}
    43  	return cli.Command{
    44  		Name:  "sign",
    45  		Usage: "Sign a Stellar transaction created elsewhere",
    46  		Action: func(c *cli.Context) {
    47  			cl.ChooseCommand(cmd, "sign", c)
    48  		},
    49  		Flags: flags,
    50  	}
    51  }
    52  
    53  func (c *CmdWalletSign) ParseArgv(ctx *cli.Context) error {
    54  	c.XDR = ctx.String("xdr")
    55  	c.AccountID = stellar1.AccountID(ctx.String("account"))
    56  	c.Submit = ctx.Bool("submit")
    57  	return nil
    58  }
    59  
    60  func (c *CmdWalletSign) Run() (err error) {
    61  	defer transformStellarCLIError(&err)
    62  	cli, err := GetWalletClient(c.G())
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	if c.XDR == "" {
    68  		bytes, err := io.ReadAll(os.Stdin)
    69  		if err != nil {
    70  			return err
    71  		}
    72  		c.XDR = string(bytes)
    73  	}
    74  
    75  	c.XDR = strings.TrimSpace(c.XDR)
    76  
    77  	var maybeAccount *stellar1.AccountID
    78  	if !c.AccountID.IsNil() {
    79  		maybeAccount = &c.AccountID
    80  	}
    81  	arg := stellar1.SignTransactionXdrLocalArg{
    82  		EnvelopeXdr: c.XDR,
    83  		AccountID:   maybeAccount,
    84  		Submit:      c.Submit,
    85  	}
    86  	res, err := cli.SignTransactionXdrLocal(context.Background(), arg)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	ui := c.G().UI.GetDumbOutputUI()
    91  	_, _ = ui.PrintfStderr(ColorString(c.G(), "green", "Signing with account ID: %s\n", res.AccountID.String()))
    92  	_, _ = ui.Printf("%s\n", res.SingedTx)
    93  	if res.SubmitErr != nil {
    94  		_, _ = ui.PrintfStderr(ColorString(c.G(), "red", "Failed to submit the transaction: %s\n", *res.SubmitErr))
    95  	} else if res.SubmitTxID != nil {
    96  		_, _ = ui.PrintfStderr(ColorString(c.G(), "green", "Transaction submitted to the network. Transaction ID: %s\n", res.SubmitTxID.String()))
    97  	}
    98  	return nil
    99  }
   100  
   101  func (c *CmdWalletSign) GetUsage() libkb.Usage {
   102  	return libkb.Usage{
   103  		Config:    true,
   104  		API:       true,
   105  		KbKeyring: true,
   106  	}
   107  }