2020-08-07 09:18:38 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-08-05 10:32:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/cmdargs"
|
2020-08-07 09:18:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
2020-08-31 09:22:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
2020-08-07 09:18:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/options"
|
2020-09-01 13:55:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-08-07 09:18:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-08-07 09:34:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2022-08-08 15:19:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2022-08-17 18:55:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neo"
|
2020-08-07 09:34:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-08-07 09:18:38 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newValidatorCommands() []cli.Command {
|
|
|
|
return []cli.Command{
|
2020-08-07 09:24:54 +00:00
|
|
|
{
|
|
|
|
Name: "register",
|
|
|
|
Usage: "register as a new candidate",
|
|
|
|
UsageText: "register -w <path> -r <rpc> -a <addr>",
|
|
|
|
Action: handleRegister,
|
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
walletPathFlag,
|
2022-06-23 13:50:21 +00:00
|
|
|
walletConfigFlag,
|
2020-08-07 09:24:54 +00:00
|
|
|
gasFlag,
|
|
|
|
flags.AddressFlag{
|
|
|
|
Name: "address, a",
|
|
|
|
Usage: "Address to register",
|
|
|
|
},
|
|
|
|
}, options.RPC...),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "unregister",
|
|
|
|
Usage: "unregister self as a candidate",
|
|
|
|
UsageText: "unregister -w <path> -r <rpc> -a <addr>",
|
|
|
|
Action: handleUnregister,
|
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
walletPathFlag,
|
2022-06-23 13:50:21 +00:00
|
|
|
walletConfigFlag,
|
2020-08-07 09:24:54 +00:00
|
|
|
gasFlag,
|
|
|
|
flags.AddressFlag{
|
|
|
|
Name: "address, a",
|
|
|
|
Usage: "Address to unregister",
|
|
|
|
},
|
|
|
|
}, options.RPC...),
|
|
|
|
},
|
2020-08-07 09:18:38 +00:00
|
|
|
{
|
|
|
|
Name: "vote",
|
|
|
|
Usage: "vote for a validator",
|
2021-05-28 12:15:37 +00:00
|
|
|
UsageText: "vote -w <path> -r <rpc> [-s <timeout>] [-g gas] -a <addr> [-c <public key>]",
|
|
|
|
Description: `Votes for a validator by calling "vote" method of a NEO native
|
|
|
|
contract. Do not provide candidate argument to perform unvoting.
|
|
|
|
`,
|
|
|
|
Action: handleVote,
|
2020-08-07 09:18:38 +00:00
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
walletPathFlag,
|
2022-06-23 13:50:21 +00:00
|
|
|
walletConfigFlag,
|
2020-08-07 09:18:38 +00:00
|
|
|
gasFlag,
|
|
|
|
flags.AddressFlag{
|
|
|
|
Name: "address, a",
|
|
|
|
Usage: "Address to vote from",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "candidate, c",
|
|
|
|
Usage: "Public key of candidate to vote for",
|
|
|
|
},
|
|
|
|
}, options.RPC...),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 09:24:54 +00:00
|
|
|
func handleRegister(ctx *cli.Context) error {
|
2022-08-17 18:55:30 +00:00
|
|
|
return handleNeoAction(ctx, func(contract *neo.Contract, _ util.Uint160, acc *wallet.Account) (*transaction.Transaction, error) {
|
2022-09-01 14:52:44 +00:00
|
|
|
return contract.RegisterCandidateUnsigned(acc.PublicKey())
|
2022-08-17 18:55:30 +00:00
|
|
|
})
|
2020-08-07 09:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleUnregister(ctx *cli.Context) error {
|
2022-08-17 18:55:30 +00:00
|
|
|
return handleNeoAction(ctx, func(contract *neo.Contract, _ util.Uint160, acc *wallet.Account) (*transaction.Transaction, error) {
|
2022-09-01 14:52:44 +00:00
|
|
|
return contract.UnregisterCandidateUnsigned(acc.PublicKey())
|
2022-08-17 18:55:30 +00:00
|
|
|
})
|
2020-08-07 09:24:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 18:55:30 +00:00
|
|
|
func handleNeoAction(ctx *cli.Context, mkTx func(*neo.Contract, util.Uint160, *wallet.Account) (*transaction.Transaction, error)) error {
|
2022-08-05 10:32:37 +00:00
|
|
|
if err := cmdargs.EnsureNone(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-23 13:50:21 +00:00
|
|
|
wall, pass, err := readWallet(ctx)
|
2020-08-07 09:24:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2022-09-01 18:44:49 +00:00
|
|
|
defer wall.Close()
|
2020-08-07 09:24:54 +00:00
|
|
|
|
|
|
|
addrFlag := ctx.Generic("address").(*flags.Address)
|
2021-04-16 09:03:03 +00:00
|
|
|
if !addrFlag.IsSet {
|
|
|
|
return cli.NewExitError("address was not provided", 1)
|
|
|
|
}
|
2020-08-07 09:24:54 +00:00
|
|
|
addr := addrFlag.Uint160()
|
2022-06-23 13:50:21 +00:00
|
|
|
acc, err := getDecryptedAccount(wall, addr, pass)
|
2020-08-07 09:34:54 +00:00
|
|
|
if err != nil {
|
2020-08-07 09:24:54 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
2020-11-03 11:08:59 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-08-07 09:24:54 +00:00
|
|
|
}
|
2022-08-08 15:19:33 +00:00
|
|
|
act, err := actor.NewSimple(c, acc)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("RPC actor issue: %w", err), 1)
|
2021-07-21 09:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 09:24:54 +00:00
|
|
|
gas := flags.Fixed8FromContext(ctx, "gas")
|
2022-08-17 18:55:30 +00:00
|
|
|
contract := neo.New(act)
|
|
|
|
tx, err := mkTx(contract, addr, acc)
|
2022-07-25 19:07:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2022-08-17 18:55:30 +00:00
|
|
|
tx.NetworkFee += int64(gas)
|
|
|
|
res, _, err := act.SignAndSend(tx)
|
2020-08-07 09:24:54 +00:00
|
|
|
if err != nil {
|
2022-08-17 18:55:30 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to sign/send transaction: %w", err), 1)
|
2020-08-07 09:24:54 +00:00
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, res.StringLE())
|
2020-08-07 09:24:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 09:18:38 +00:00
|
|
|
func handleVote(ctx *cli.Context) error {
|
2022-08-17 18:55:30 +00:00
|
|
|
return handleNeoAction(ctx, func(contract *neo.Contract, addr util.Uint160, acc *wallet.Account) (*transaction.Transaction, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
pub *keys.PublicKey
|
|
|
|
)
|
|
|
|
pubStr := ctx.String("candidate")
|
|
|
|
if pubStr != "" {
|
|
|
|
pub, err = keys.NewPublicKeyFromString(pubStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid public key: '%s'", pubStr)
|
|
|
|
}
|
2020-08-07 09:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 18:55:30 +00:00
|
|
|
return contract.VoteUnsigned(addr, pub)
|
2022-08-08 15:26:32 +00:00
|
|
|
})
|
2020-08-07 09:18:38 +00:00
|
|
|
}
|
2020-08-07 09:34:54 +00:00
|
|
|
|
2022-08-31 19:38:35 +00:00
|
|
|
// getDecryptedAccount tries to get and unlock the specified account if it has a
|
|
|
|
// key inside (otherwise it's returned as is, without an ability to sign). If
|
|
|
|
// password is nil, it will be requested via terminal.
|
2022-06-23 13:50:21 +00:00
|
|
|
func getDecryptedAccount(wall *wallet.Wallet, addr util.Uint160, password *string) (*wallet.Account, error) {
|
2020-08-07 09:34:54 +00:00
|
|
|
acc := wall.GetAccount(addr)
|
|
|
|
if acc == nil {
|
|
|
|
return nil, fmt.Errorf("can't find account for the address: %s", address.Uint160ToString(addr))
|
|
|
|
}
|
|
|
|
|
2022-08-31 19:38:35 +00:00
|
|
|
// No private key available, nothing to decrypt, but it's still a useful account for many purposes.
|
|
|
|
if acc.EncryptedWIF == "" {
|
|
|
|
return acc, nil
|
|
|
|
}
|
|
|
|
|
2022-06-23 13:50:21 +00:00
|
|
|
if password == nil {
|
|
|
|
pass, err := input.ReadPassword(EnterPasswordPrompt)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reading password", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
password = &pass
|
|
|
|
}
|
|
|
|
err := acc.Decrypt(*password, wall.Scrypt)
|
|
|
|
if err != nil {
|
2020-08-07 09:34:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return acc, nil
|
|
|
|
}
|