2020-08-31 09:22:09 +00:00
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
2021-09-15 09:40:30 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-31 09:22:09 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
|
2021-09-15 09:40:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-10-14 08:40:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2021-02-10 08:53:01 +00:00
|
|
|
"golang.org/x/term"
|
2020-08-31 09:22:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Terminal is a terminal used for input. If `nil`, stdin is used.
|
2021-02-10 08:53:01 +00:00
|
|
|
var Terminal *term.Terminal
|
|
|
|
|
|
|
|
// ReadWriter combiner reader and writer.
|
|
|
|
type ReadWriter struct {
|
|
|
|
io.Reader
|
|
|
|
io.Writer
|
|
|
|
}
|
2020-08-31 09:22:09 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ReadLine reads a line from the input without trailing '\n'.
|
2021-02-10 08:53:01 +00:00
|
|
|
func ReadLine(prompt string) (string, error) {
|
|
|
|
trm := Terminal
|
|
|
|
if trm == nil {
|
2021-11-01 08:15:35 +00:00
|
|
|
s, err := term.MakeRaw(int(syscall.Stdin))
|
2020-08-31 09:22:09 +00:00
|
|
|
if err != nil {
|
2021-11-03 16:35:07 +00:00
|
|
|
return "", err
|
2020-08-31 09:22:09 +00:00
|
|
|
}
|
2021-11-01 08:15:35 +00:00
|
|
|
defer func() { _ = term.Restore(int(syscall.Stdin), s) }()
|
2021-02-10 08:53:01 +00:00
|
|
|
trm = term.NewTerminal(ReadWriter{
|
|
|
|
Reader: os.Stdin,
|
|
|
|
Writer: os.Stdout,
|
|
|
|
}, "")
|
2020-08-31 09:22:09 +00:00
|
|
|
}
|
2021-02-10 08:53:01 +00:00
|
|
|
return readLine(trm, prompt)
|
2020-08-31 09:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 08:53:01 +00:00
|
|
|
func readLine(trm *term.Terminal, prompt string) (string, error) {
|
|
|
|
_, err := trm.Write([]byte(prompt))
|
2020-08-31 09:22:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-02-10 08:53:01 +00:00
|
|
|
return trm.ReadLine()
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ReadPassword reads the user's password with prompt.
|
2021-02-10 08:53:01 +00:00
|
|
|
func ReadPassword(prompt string) (string, error) {
|
|
|
|
trm := Terminal
|
2022-05-12 09:06:53 +00:00
|
|
|
if trm != nil {
|
|
|
|
return trm.ReadPassword(prompt)
|
2021-02-10 08:53:01 +00:00
|
|
|
}
|
2022-05-12 09:06:53 +00:00
|
|
|
return readSecurePassword(prompt)
|
2020-08-31 09:22:09 +00:00
|
|
|
}
|
2021-09-15 09:40:30 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ConfirmTx asks for a confirmation to send the tx.
|
2021-09-15 09:40:30 +00:00
|
|
|
func ConfirmTx(w io.Writer, tx *transaction.Transaction) error {
|
2021-10-14 08:40:51 +00:00
|
|
|
fmt.Fprintf(w, "Network fee: %s\n", fixedn.Fixed8(tx.NetworkFee))
|
|
|
|
fmt.Fprintf(w, "System fee: %s\n", fixedn.Fixed8(tx.SystemFee))
|
|
|
|
fmt.Fprintf(w, "Total fee: %s\n", fixedn.Fixed8(tx.NetworkFee+tx.SystemFee))
|
2021-09-15 09:40:30 +00:00
|
|
|
ln, err := ReadLine("Relay transaction (y|N)> ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-09 16:09:16 +00:00
|
|
|
if 0 < len(ln) && (ln[0] == 'y' || ln[0] == 'Y') {
|
2021-09-15 09:40:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("cancelled")
|
|
|
|
}
|