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