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-12-13 18:25:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
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-07-21 19:39:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2022-07-25 19:07:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
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 {
|
2021-07-21 09:25:42 +00:00
|
|
|
return handleCandidate(ctx, "registerCandidate", 100000000) // 1 additional GAS.
|
2020-08-07 09:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleUnregister(ctx *cli.Context) error {
|
2021-01-21 20:02:59 +00:00
|
|
|
return handleCandidate(ctx, "unregisterCandidate", -1)
|
2020-08-07 09:24:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 20:02:59 +00:00
|
|
|
func handleCandidate(ctx *cli.Context, method string, sysGas int64) 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-21 09:25:42 +00:00
|
|
|
if sysGas >= 0 {
|
|
|
|
regPrice, err := c.GetCandidateRegisterPrice()
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
sysGas += regPrice
|
|
|
|
}
|
|
|
|
|
2020-08-07 09:24:54 +00:00
|
|
|
gas := flags.Fixed8FromContext(ctx, "gas")
|
2020-12-13 18:25:04 +00:00
|
|
|
neoContractHash, err := c.GetNativeContractHash(nativenames.Neo)
|
2020-09-25 16:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-25 19:07:13 +00:00
|
|
|
script, err := smartcontract.CreateCallWithAssertScript(neoContractHash, method, acc.PrivateKey().PublicKey().Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
res, err := c.SignAndPushInvocationTx(script, acc, sysGas, gas, []rpcclient.SignerAccount{{
|
2021-03-02 12:43:09 +00:00
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: acc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
|
|
|
Account: acc,
|
2021-04-21 14:21:54 +00:00
|
|
|
}})
|
2020-08-07 09:24:54 +00:00
|
|
|
if err != nil {
|
2021-04-21 14:21:54 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to push 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-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:18:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
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:18:38 +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 {
|
|
|
|
return cli.NewExitError(err, 1)
|
2020-08-07 09:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pub *keys.PublicKey
|
|
|
|
pubStr := ctx.String("candidate")
|
|
|
|
if pubStr != "" {
|
|
|
|
pub, err = keys.NewPublicKeyFromString(pubStr)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("invalid public key: '%s'", pubStr), 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:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pubArg interface{}
|
|
|
|
if pub != nil {
|
|
|
|
pubArg = pub.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
gas := flags.Fixed8FromContext(ctx, "gas")
|
2020-12-13 18:25:04 +00:00
|
|
|
neoContractHash, err := c.GetNativeContractHash(nativenames.Neo)
|
2020-09-25 16:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2022-07-25 19:07:13 +00:00
|
|
|
script, err := smartcontract.CreateCallWithAssertScript(neoContractHash, "vote", addr.BytesBE(), pubArg)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
res, err := c.SignAndPushInvocationTx(script, acc, -1, gas, []rpcclient.SignerAccount{{
|
2021-03-02 12:43:09 +00:00
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: acc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
2021-04-21 14:25:25 +00:00
|
|
|
Account: acc}})
|
2020-08-07 09:18:38 +00:00
|
|
|
if err != nil {
|
2021-04-21 14:25:25 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to push invocation transaction: %w", err), 1)
|
2020-08-07 09:18:38 +00:00
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, res.StringLE())
|
2020-08-07 09:18:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-07 09:34:54 +00:00
|
|
|
|
2022-06-23 13:50:21 +00:00
|
|
|
// getDecryptedAccount tries to unlock the specified account. If password is nil, it will be requested via terminal.
|
|
|
|
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-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
|
|
|
|
}
|