neoneo-go/cli/wallet/wallet.go

737 lines
17 KiB
Go
Raw Normal View History

package wallet
import (
"encoding/hex"
"errors"
"fmt"
"io"
"strings"
"github.com/nspcc-dev/neo-go/cli/flags"
"github.com/nspcc-dev/neo-go/cli/input"
"github.com/nspcc-dev/neo-go/cli/options"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
2021-03-05 09:23:25 +00:00
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/urfave/cli"
)
var (
errNoPath = errors.New("target path where the wallet should be stored is mandatory and should be passed using (--wallet, -w) flags")
errPhraseMismatch = errors.New("the entered pass-phrases do not match. Maybe you have misspelled them")
)
2020-02-20 09:05:55 +00:00
var (
walletPathFlag = cli.StringFlag{
Name: "wallet, w",
2020-02-20 09:05:55 +00:00
Usage: "Target location of the wallet file.",
}
2020-02-20 10:48:59 +00:00
wifFlag = cli.StringFlag{
Name: "wif",
Usage: "WIF to import",
}
decryptFlag = cli.BoolFlag{
Name: "decrypt, d",
Usage: "Decrypt encrypted keys.",
}
outFlag = cli.StringFlag{
Name: "out",
Usage: "file to put JSON transaction to",
}
inFlag = cli.StringFlag{
Name: "in",
Usage: "file with JSON transaction",
}
2020-03-06 14:22:14 +00:00
fromAddrFlag = flags.AddressFlag{
Name: "from",
Usage: "Address to send an asset from",
}
toAddrFlag = flags.AddressFlag{
Name: "to",
Usage: "Address to send an asset to",
}
forceFlag = cli.BoolFlag{
Name: "force",
Usage: "Do not ask for a confirmation",
}
2020-02-20 09:05:55 +00:00
)
// NewCommands returns 'wallet' command.
func NewCommands() []cli.Command {
claimFlags := []cli.Flag{
walletPathFlag,
flags.AddressFlag{
Name: "address, a",
Usage: "Address to claim GAS for",
},
}
claimFlags = append(claimFlags, options.RPC...)
signFlags := []cli.Flag{
walletPathFlag,
outFlag,
inFlag,
2021-04-16 08:12:51 +00:00
flags.AddressFlag{
Name: "address, a",
Usage: "Address to use",
},
}
signFlags = append(signFlags, options.RPC...)
return []cli.Command{{
Name: "wallet",
Usage: "create, open and manage a NEO wallet",
Subcommands: []cli.Command{
{
Name: "claim",
Usage: "claim GAS",
Action: claimGas,
Flags: claimFlags,
},
{
Name: "init",
Usage: "create a new wallet",
Action: createWallet,
Flags: []cli.Flag{
2020-02-20 09:05:55 +00:00
walletPathFlag,
cli.BoolFlag{
Name: "account, a",
Usage: "Create a new account",
},
},
},
{
Name: "convert",
Usage: "convert addresses from existing NEO2 NEP6-wallet to NEO3 format",
Action: convertWallet,
Flags: []cli.Flag{
walletPathFlag,
cli.StringFlag{
Name: "out, o",
Usage: "where to write converted wallet",
},
},
},
{
Name: "create",
Usage: "add an account to the existing wallet",
Action: addAccount,
Flags: []cli.Flag{
walletPathFlag,
},
},
{
Name: "dump",
Usage: "check and dump an existing NEO wallet",
Action: dumpWallet,
Flags: []cli.Flag{
2020-02-20 09:05:55 +00:00
walletPathFlag,
decryptFlag,
},
},
2021-03-05 09:23:25 +00:00
{
Name: "dump-keys",
Usage: "dump public keys for account",
Action: dumpKeys,
Flags: []cli.Flag{
walletPathFlag,
2021-04-16 09:15:00 +00:00
flags.AddressFlag{
2021-03-05 09:23:25 +00:00
Name: "address, a",
Usage: "address to print public keys for",
},
},
},
{
Name: "export",
Usage: "export keys for address",
UsageText: "export --wallet <path> [--decrypt] [<address>]",
Action: exportKeys,
Flags: []cli.Flag{
walletPathFlag,
decryptFlag,
},
},
2020-02-20 10:48:59 +00:00
{
Name: "import",
Usage: "import WIF of a standard signature contract",
UsageText: "import --wallet <path> --wif <wif> [--name <account_name>]",
Action: importWallet,
2020-02-20 10:48:59 +00:00
Flags: []cli.Flag{
walletPathFlag,
wifFlag,
cli.StringFlag{
Name: "name, n",
Usage: "Optional account name",
},
cli.StringFlag{
Name: "contract",
Usage: "Verification script for custom contracts",
},
2020-02-20 10:48:59 +00:00
},
},
{
Name: "import-multisig",
Usage: "import multisig contract",
UsageText: "import-multisig --wallet <path> --wif <wif> [--name <account_name>] --min <n>" +
" [<pubkey1> [<pubkey2> [...]]]",
Action: importMultisig,
Flags: []cli.Flag{
walletPathFlag,
wifFlag,
cli.StringFlag{
Name: "name, n",
Usage: "Optional account name",
},
cli.IntFlag{
Name: "min, m",
Usage: "Minimal number of signatures",
},
},
},
{
Name: "import-deployed",
Usage: "import deployed contract",
UsageText: "import-deployed --wallet <path> --wif <wif> --contract <hash> [--name <account_name>]",
Action: importDeployed,
Flags: append([]cli.Flag{
walletPathFlag,
wifFlag,
cli.StringFlag{
Name: "name, n",
Usage: "Optional account name",
},
flags.AddressFlag{
Name: "contract, c",
Usage: "Contract hash or address",
},
}, options.RPC...),
},
{
Name: "remove",
Usage: "remove an account from the wallet",
UsageText: "remove --wallet <path> [--force] --address <addr>",
Action: removeAccount,
Flags: []cli.Flag{
walletPathFlag,
forceFlag,
flags.AddressFlag{
Name: "address, a",
Usage: "Account address or hash in LE form to be removed",
},
},
},
{
Name: "sign",
Usage: "cosign transaction with multisig/contract/additional account",
UsageText: "sign --wallet <path> --address <address> --in <file.in> --out <file.out> [-r <endpoint>]",
Action: signStoredTransaction,
Flags: signFlags,
},
{
Name: "nep17",
Usage: "work with NEP17 contracts",
Subcommands: newNEP17Commands(),
},
2021-04-22 15:21:18 +00:00
{
Name: "nep11",
Usage: "work with NEP11 contracts",
Subcommands: newNEP11Commands(),
},
2020-08-04 09:35:04 +00:00
{
Name: "candidate",
Usage: "work with candidates",
Subcommands: newValidatorCommands(),
2020-08-04 09:35:04 +00:00
},
},
}}
}
func claimGas(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
defer wall.Close()
addrFlag := ctx.Generic("address").(*flags.Address)
if !addrFlag.IsSet {
return cli.NewExitError("address was not provided", 1)
}
scriptHash := addrFlag.Uint160()
acc, err := getDecryptedAccount(ctx, wall, scriptHash)
if err != nil {
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)
}
neoContractHash, err := c.GetNativeContractHash(nativenames.Neo)
if err != nil {
return cli.NewExitError(err, 1)
}
hash, err := c.TransferNEP17(acc, scriptHash, neoContractHash, 0, 0, nil, nil)
if err != nil {
return cli.NewExitError(err, 1)
}
fmt.Fprintln(ctx.App.Writer, hash.StringLE())
return nil
}
func convertWallet(ctx *cli.Context) error {
wall, err := newWalletV2FromFile(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
newWallet, err := wallet.NewWallet(ctx.String("out"))
if err != nil {
return cli.NewExitError(err, 1)
}
defer newWallet.Close()
newWallet.Scrypt = wall.Scrypt
for _, acc := range wall.Accounts {
2021-02-10 08:53:01 +00:00
pass, err := input.ReadPassword(fmt.Sprintf("Enter passphrase for account %s (label '%s') > ", acc.Address, acc.Label))
if err != nil {
return cli.NewExitError(err, 1)
}
newAcc, err := acc.convert(pass, wall.Scrypt)
if err != nil {
return cli.NewExitError(err, 1)
}
newWallet.AddAccount(newAcc)
}
if err := newWallet.Save(); err != nil {
return cli.NewExitError(err, -1)
}
return nil
}
func addAccount(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
defer wall.Close()
if err := createAccount(wall); err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
func exportKeys(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
var addr string
decrypt := ctx.Bool("decrypt")
if ctx.NArg() == 0 && decrypt {
return cli.NewExitError(errors.New("address must be provided if '--decrypt' flag is used"), 1)
} else if ctx.NArg() > 0 {
// check address format just to catch possible typos
addr = ctx.Args().First()
_, err := address.StringToUint160(addr)
if err != nil {
return cli.NewExitError(fmt.Errorf("can't parse address: %w", err), 1)
}
}
var wifs []string
loop:
for _, a := range wall.Accounts {
if addr != "" && a.Address != addr {
continue
}
for i := range wifs {
if a.EncryptedWIF == wifs[i] {
continue loop
}
}
wifs = append(wifs, a.EncryptedWIF)
}
for _, wif := range wifs {
if decrypt {
2021-02-10 08:53:01 +00:00
pass, err := input.ReadPassword("Enter password > ")
if err != nil {
return cli.NewExitError(err, 1)
}
pk, err := keys.NEP2Decrypt(wif, pass, wall.Scrypt)
if err != nil {
return cli.NewExitError(err, 1)
}
wif = pk.WIF()
}
fmt.Fprintln(ctx.App.Writer, wif)
}
return nil
}
func importMultisig(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
defer wall.Close()
m := ctx.Int("min")
if ctx.NArg() < m {
return cli.NewExitError(errors.New("insufficient number of public keys"), 1)
}
args := []string(ctx.Args())
pubs := make([]*keys.PublicKey, len(args))
for i := range args {
pubs[i], err = keys.NewPublicKeyFromString(args[i])
if err != nil {
return cli.NewExitError(fmt.Errorf("can't decode public key %d: %w", i, err), 1)
}
}
acc, err := newAccountFromWIF(ctx.App.Writer, ctx.String("wif"), wall.Scrypt)
if err != nil {
return cli.NewExitError(err, 1)
}
if err := acc.ConvertMultisig(m, pubs); err != nil {
return cli.NewExitError(err, 1)
}
if acc.Label == "" {
acc.Label = ctx.String("name")
}
if err := addAccountAndSave(wall, acc); err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
func importDeployed(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
2020-02-20 10:48:59 +00:00
if err != nil {
return cli.NewExitError(err, 1)
}
defer wall.Close()
rawHash := ctx.Generic("contract").(*flags.Address)
if !rawHash.IsSet {
return cli.NewExitError("contract hash was not provided", 1)
}
acc, err := newAccountFromWIF(ctx.App.Writer, ctx.String("wif"), wall.Scrypt)
if err != nil {
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)
}
cs, err := c.GetContractStateByHash(rawHash.Uint160())
if err != nil {
return cli.NewExitError(fmt.Errorf("can't fetch contract info: %w", err), 1)
}
md := cs.Manifest.ABI.GetMethod(manifest.MethodVerify, -1)
if md == nil || md.ReturnType != smartcontract.BoolType {
return cli.NewExitError("contract has no `verify` method with boolean return", 1)
}
acc.Address = address.Uint160ToString(cs.Hash)
acc.Contract.Script = cs.NEF.Script
acc.Contract.Parameters = acc.Contract.Parameters[:0]
for _, p := range md.Parameters {
acc.Contract.Parameters = append(acc.Contract.Parameters, wallet.ContractParam{
Name: p.Name,
Type: p.Type,
})
}
acc.Contract.Deployed = true
if acc.Label == "" {
acc.Label = ctx.String("name")
}
if err := addAccountAndSave(wall, acc); err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
func importWallet(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
defer wall.Close()
acc, err := newAccountFromWIF(ctx.App.Writer, ctx.String("wif"), wall.Scrypt)
2020-02-20 10:48:59 +00:00
if err != nil {
return cli.NewExitError(err, 1)
}
if ctrFlag := ctx.String("contract"); ctrFlag != "" {
ctr, err := hex.DecodeString(ctrFlag)
if err != nil {
return cli.NewExitError("invalid contract", 1)
}
acc.Contract.Script = ctr
}
if acc.Label == "" {
acc.Label = ctx.String("name")
}
2020-02-20 10:48:59 +00:00
if err := addAccountAndSave(wall, acc); err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
func removeAccount(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
defer wall.Close()
addr := ctx.Generic("address").(*flags.Address)
if !addr.IsSet {
return cli.NewExitError("valid account address must be provided", 1)
}
acc := wall.GetAccount(addr.Uint160())
if acc == nil {
return cli.NewExitError("account wasn't found", 1)
}
if !ctx.Bool("force") {
fmt.Fprintf(ctx.App.Writer, "Account %s will be removed. This action is irreversible.\n", addr.Uint160())
if ok := askForConsent(ctx.App.Writer); !ok {
return nil
}
}
if err := wall.RemoveAccount(acc.Address); err != nil {
return cli.NewExitError(fmt.Errorf("error on remove: %w", err), 1)
} else if err := wall.Save(); err != nil {
return cli.NewExitError(fmt.Errorf("error while saving wallet: %w", err), 1)
}
return nil
}
func askForConsent(w io.Writer) bool {
2021-02-10 08:53:01 +00:00
response, err := input.ReadLine("Are you sure? [y/N]: ")
if err == nil {
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
}
}
fmt.Fprintln(w, "Cancelled.")
return false
}
func dumpWallet(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
if ctx.Bool("decrypt") {
2021-02-10 08:53:01 +00:00
pass, err := input.ReadPassword("Enter wallet password > ")
if err != nil {
return cli.NewExitError(err, 1)
}
for i := range wall.Accounts {
// Just testing the decryption here.
err := wall.Accounts[i].Decrypt(pass, wall.Scrypt)
if err != nil {
return cli.NewExitError(err, 1)
}
}
}
fmtPrintWallet(ctx.App.Writer, wall)
return nil
}
2021-03-05 09:23:25 +00:00
func dumpKeys(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
accounts := wall.Accounts
2021-04-16 09:15:00 +00:00
addrFlag := ctx.Generic("address").(*flags.Address)
if addrFlag.IsSet {
acc := wall.GetAccount(addrFlag.Uint160())
2021-03-05 09:23:25 +00:00
if acc == nil {
return cli.NewExitError("account is missing", 1)
}
accounts = []*wallet.Account{acc}
}
hasPrinted := false
for _, acc := range accounts {
pub, ok := vm.ParseSignatureContract(acc.Contract.Script)
if ok {
if hasPrinted {
fmt.Fprintln(ctx.App.Writer)
}
fmt.Fprintf(ctx.App.Writer, "%s (simple signature contract):\n", acc.Address)
fmt.Fprintln(ctx.App.Writer, hex.EncodeToString(pub))
hasPrinted = true
continue
}
n, bs, ok := vm.ParseMultiSigContract(acc.Contract.Script)
if ok {
if hasPrinted {
fmt.Fprintln(ctx.App.Writer)
}
fmt.Fprintf(ctx.App.Writer, "%s (%d out of %d multisig contract):\n", acc.Address, n, len(bs))
for i := range bs {
fmt.Fprintln(ctx.App.Writer, hex.EncodeToString(bs[i]))
}
hasPrinted = true
continue
}
2021-04-16 09:15:00 +00:00
if addrFlag.IsSet {
2021-04-16 09:15:51 +00:00
return cli.NewExitError(fmt.Errorf("unknown script type for address %s", address.Uint160ToString(addrFlag.Uint160())), 1)
2021-03-05 09:23:25 +00:00
}
}
return nil
}
func createWallet(ctx *cli.Context) error {
path := ctx.String("wallet")
if len(path) == 0 {
return cli.NewExitError(errNoPath, 1)
}
wall, err := wallet.NewWallet(path)
if err != nil {
return cli.NewExitError(err, 1)
}
if err := wall.Save(); err != nil {
return cli.NewExitError(err, 1)
}
if ctx.Bool("account") {
if err := createAccount(wall); err != nil {
return cli.NewExitError(err, 1)
}
}
fmtPrintWallet(ctx.App.Writer, wall)
fmt.Fprintf(ctx.App.Writer, "wallet successfully created, file location is %s\n", wall.Path())
return nil
}
func readAccountInfo() (string, string, error) {
2021-02-10 08:53:01 +00:00
rawName, _ := input.ReadLine("Enter the name of the account > ")
phrase, err := input.ReadPassword("Enter passphrase > ")
if err != nil {
2020-02-20 10:48:59 +00:00
return "", "", err
}
2021-02-10 08:53:01 +00:00
phraseCheck, err := input.ReadPassword("Confirm passphrase > ")
if err != nil {
2020-02-20 10:48:59 +00:00
return "", "", err
}
if phrase != phraseCheck {
2020-02-20 10:48:59 +00:00
return "", "", errPhraseMismatch
}
name := strings.TrimRight(rawName, "\n")
2020-02-20 10:48:59 +00:00
return name, phrase, nil
}
func createAccount(wall *wallet.Wallet) error {
name, phrase, err := readAccountInfo()
2020-02-20 10:48:59 +00:00
if err != nil {
return err
}
return wall.CreateAccount(name, phrase)
}
func openWallet(path string) (*wallet.Wallet, error) {
if len(path) == 0 {
return nil, errNoPath
}
return wallet.NewWalletFromFile(path)
}
func newAccountFromWIF(w io.Writer, wif string, scrypt keys.ScryptParams) (*wallet.Account, error) {
2020-02-20 10:48:59 +00:00
// note: NEP2 strings always have length of 58 even though
// base58 strings can have different lengths even if slice lengths are equal
if len(wif) == 58 {
2021-02-10 08:53:01 +00:00
pass, err := input.ReadPassword("Enter password > ")
2020-02-20 10:48:59 +00:00
if err != nil {
return nil, err
}
return wallet.NewAccountFromEncryptedWIF(wif, pass, scrypt)
2020-02-20 10:48:59 +00:00
}
acc, err := wallet.NewAccountFromWIF(wif)
if err != nil {
return nil, err
}
fmt.Fprintln(w, "Provided WIF was unencrypted. Wallet can contain only encrypted keys.")
name, pass, err := readAccountInfo()
2020-02-20 10:48:59 +00:00
if err != nil {
return nil, err
}
acc.Label = name
if err := acc.Encrypt(pass, scrypt); err != nil {
2020-02-20 10:48:59 +00:00
return nil, err
}
return acc, nil
}
func addAccountAndSave(w *wallet.Wallet, acc *wallet.Account) error {
for i := range w.Accounts {
if w.Accounts[i].Address == acc.Address {
return fmt.Errorf("address '%s' is already in wallet", acc.Address)
}
}
w.AddAccount(acc)
return w.Save()
}
func fmtPrintWallet(w io.Writer, wall *wallet.Wallet) {
b, _ := wall.JSON()
fmt.Fprintln(w, "")
fmt.Fprintln(w, string(b))
fmt.Fprintln(w, "")
}