cli: add --out flag to the wallet transfer command

When transferring assets from multisig accounts,
it is useful to export tx into a file, so that
other participants can sign it too.
This commit is contained in:
Evgenii Stratonikov 2020-03-03 10:35:21 +03:00
parent cd487e3ad4
commit 0d4ad9f76c

View file

@ -3,8 +3,10 @@ package wallet
import ( import (
"bufio" "bufio"
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strings" "strings"
"syscall" "syscall"
@ -15,6 +17,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/rpc/request" "github.com/nspcc-dev/neo-go/pkg/rpc/request"
context2 "github.com/nspcc-dev/neo-go/pkg/smartcontract/context"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -47,6 +50,10 @@ var (
Name: "timeout, t", Name: "timeout, t",
Usage: "Timeout for the operation", Usage: "Timeout for the operation",
} }
outFlag = cli.StringFlag{
Name: "out",
Usage: "file to put JSON transaction to",
}
) )
// NewCommands returns 'wallet' command. // NewCommands returns 'wallet' command.
@ -144,12 +151,13 @@ func NewCommands() []cli.Command {
Name: "transfer", Name: "transfer",
Usage: "transfer NEO/GAS", Usage: "transfer NEO/GAS",
UsageText: "transfer --path <path> --from <addr> --to <addr>" + UsageText: "transfer --path <path> --from <addr> --to <addr>" +
" --amount <amount> --asset [NEO|GAS|<hex-id>]", " --amount <amount> --asset [NEO|GAS|<hex-id>] [--out <path>]",
Action: transferAsset, Action: transferAsset,
Flags: []cli.Flag{ Flags: []cli.Flag{
walletPathFlag, walletPathFlag,
rpcFlag, rpcFlag,
timeoutFlag, timeoutFlag,
outFlag,
cli.StringFlag{ cli.StringFlag{
Name: "from", Name: "from",
Usage: "Address to send an asset from", Usage: "Address to send an asset from",
@ -431,9 +439,23 @@ func transferAsset(ctx *cli.Context) error {
Position: 1, Position: 1,
}) })
_ = acc.SignTx(tx) if outFile := ctx.String("out"); outFile != "" {
if err := c.SendRawTransaction(tx); err != nil { priv := acc.PrivateKey()
return cli.NewExitError(err, 1) pub := priv.PublicKey()
sign := priv.Sign(tx.GetSignedPart())
c := context2.NewParameterContext("Neo.Core.ContractTransaction", tx)
if err := c.AddSignature(acc.Contract, pub, sign); err != nil {
return cli.NewExitError(fmt.Errorf("can't add signature: %v", err), 1)
} else if data, err := json.Marshal(c); err != nil {
return cli.NewExitError(fmt.Errorf("can't marshal tx to JSON: %v", err), 1)
} else if err := ioutil.WriteFile(outFile, data, 0644); err != nil {
return cli.NewExitError(fmt.Errorf("can't write tx to file: %v", err), 1)
}
} else {
_ = acc.SignTx(tx)
if err := c.SendRawTransaction(tx); err != nil {
return cli.NewExitError(err, 1)
}
} }
fmt.Println(tx.Hash().StringLE()) fmt.Println(tx.Hash().StringLE())