Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(go/client): implement user tx confirmation #125

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions go/node/client/v1beta2/tx.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package v1beta2

import (
"bufio"
"context"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
"time"
"unsafe"

"github.com/boz/go-lifecycle"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/edwingeng/deque/v2"
"github.com/gogo/protobuf/proto"
"github.com/spf13/pflag"
Expand All @@ -33,6 +37,7 @@ var (
ErrAdjustGas = errors.New("tx client: couldn't adjust gas")
ErrSimulateOffline = errors.New("tx client: cannot simulate tx in offline mode")
ErrBroadcastOffline = errors.New("tx client: cannot broadcast tx in offline mode")
ErrTxCanceledByUser = errors.New("tx client: transaction declined by user input")
)

const (
Expand Down Expand Up @@ -360,6 +365,14 @@ func (c *serialBroadcaster) generateTxs(txf tx.Factory, msgs ...sdk.Msg) ([]byte
return data, nil
}

func DefaultTxConfirm(txn string) (bool, error) {
_, _ = fmt.Printf("%s\n\n", txn)

buf := bufio.NewReader(os.Stdin)

return input.GetConfirmation("confirm transaction before signing and broadcasting", buf, os.Stdin)
}

func (c *serialBroadcaster) broadcastTxs(txf tx.Factory, msgs ...sdk.Msg) (interface{}, uint64, error) {
var err error
var resp proto.Message
Expand Down Expand Up @@ -387,6 +400,22 @@ func (c *serialBroadcaster) broadcastTxs(txf tx.Factory, msgs ...sdk.Msg) (inter
return nil, txf.Sequence(), ErrBroadcastOffline
}

if !c.cctx.SkipConfirm {
out, err := c.cctx.TxConfig.TxJSONEncoder()(txn.GetTx())
if err != nil {
return nil, txf.Sequence(), err
}

isYes, err := DefaultTxConfirm(string(out))
if err != nil {
return nil, txf.Sequence(), err
}

if !isYes {
return nil, txf.Sequence(), ErrTxCanceledByUser
}
}

txn.SetFeeGranter(c.cctx.GetFeeGranterAddress())

err = tx.Sign(txf, c.info.GetName(), txn, true)
Expand Down Expand Up @@ -501,18 +530,3 @@ func PrepareFactory(cctx sdkclient.Context, txf tx.Factory) (tx.Factory, error)

return txf, nil
}

func AdjustGas(ctx sdkclient.Context, txf tx.Factory, msgs ...sdk.Msg) (proto.Message, tx.Factory, error) {
if ctx.Offline || (!ctx.Simulate && !txf.SimulateAndExecute()) {
return nil, txf, nil
}

resp, adjusted, err := tx.CalculateGas(ctx, txf, msgs...)
if err != nil {
return resp, txf, err
}

txf = txf.WithGas(adjusted)

return resp, txf, nil
}
Loading