2018-03-02 15:24:09 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
2020-03-13 14:03:38 +00:00
|
|
|
"encoding/hex"
|
2018-03-02 15:24:09 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-28 09:11:19 +00:00
|
|
|
"io"
|
2018-03-02 15:24:09 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-03-03 20:09:47 +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-06-17 21:15:13 +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-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2021-01-26 15:00:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-08-27 07:50:55 +00:00
|
|
|
"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"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2018-03-02 15:24:09 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-25 07:34:02 +00:00
|
|
|
errNoPath = errors.New("target path where the wallet should be stored is mandatory and should be passed using (--wallet, -w) flags")
|
2019-01-25 11:20:35 +00:00
|
|
|
errPhraseMismatch = errors.New("the entered pass-phrases do not match. Maybe you have misspelled them")
|
2018-03-02 15:24:09 +00:00
|
|
|
)
|
|
|
|
|
2020-02-20 09:05:55 +00:00
|
|
|
var (
|
|
|
|
walletPathFlag = cli.StringFlag{
|
2020-06-25 07:34:02 +00:00
|
|
|
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",
|
|
|
|
}
|
2020-02-20 13:14:14 +00:00
|
|
|
decryptFlag = cli.BoolFlag{
|
|
|
|
Name: "decrypt, d",
|
|
|
|
Usage: "Decrypt encrypted keys.",
|
|
|
|
}
|
2020-03-03 07:35:21 +00:00
|
|
|
outFlag = cli.StringFlag{
|
|
|
|
Name: "out",
|
|
|
|
Usage: "file to put JSON transaction to",
|
|
|
|
}
|
2020-03-02 12:39:02 +00:00
|
|
|
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",
|
|
|
|
}
|
2020-03-13 13:24:33 +00:00
|
|
|
forceFlag = cli.BoolFlag{
|
|
|
|
Name: "force",
|
|
|
|
Usage: "Do not ask for a confirmation",
|
|
|
|
}
|
2020-02-20 09:05:55 +00:00
|
|
|
)
|
|
|
|
|
2019-10-19 20:58:45 +00:00
|
|
|
// NewCommands returns 'wallet' command.
|
|
|
|
func NewCommands() []cli.Command {
|
2020-06-17 21:15:13 +00:00
|
|
|
claimFlags := []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
flags.AddressFlag{
|
|
|
|
Name: "address, a",
|
|
|
|
Usage: "Address to claim GAS for",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
claimFlags = append(claimFlags, options.RPC...)
|
2021-03-03 11:47:36 +00:00
|
|
|
signFlags := []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
outFlag,
|
|
|
|
inFlag,
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "address, a",
|
|
|
|
Usage: "Address to use",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
signFlags = append(signFlags, options.RPC...)
|
2019-10-19 20:58:45 +00:00
|
|
|
return []cli.Command{{
|
2018-03-02 15:24:09 +00:00
|
|
|
Name: "wallet",
|
|
|
|
Usage: "create, open and manage a NEO wallet",
|
|
|
|
Subcommands: []cli.Command{
|
2020-02-27 13:09:19 +00:00
|
|
|
{
|
|
|
|
Name: "claim",
|
|
|
|
Usage: "claim GAS",
|
|
|
|
Action: claimGas,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: claimFlags,
|
2020-02-27 13:09:19 +00:00
|
|
|
},
|
2018-03-02 15:24:09 +00:00
|
|
|
{
|
2020-03-16 12:08:38 +00:00
|
|
|
Name: "init",
|
2018-03-02 15:24:09 +00:00
|
|
|
Usage: "create a new wallet",
|
|
|
|
Action: createWallet,
|
|
|
|
Flags: []cli.Flag{
|
2020-02-20 09:05:55 +00:00
|
|
|
walletPathFlag,
|
2018-03-02 15:24:09 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "account, a",
|
|
|
|
Usage: "Create a new account",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 11:08:47 +00:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-20 13:39:15 +00:00
|
|
|
{
|
2020-03-16 12:08:38 +00:00
|
|
|
Name: "create",
|
2020-02-20 13:39:15 +00:00
|
|
|
Usage: "add an account to the existing wallet",
|
|
|
|
Action: addAccount,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
},
|
|
|
|
},
|
2018-03-02 15:24:09 +00:00
|
|
|
{
|
2020-01-09 15:39:38 +00:00
|
|
|
Name: "dump",
|
|
|
|
Usage: "check and dump an existing NEO wallet",
|
|
|
|
Action: dumpWallet,
|
2018-03-02 15:24:09 +00:00
|
|
|
Flags: []cli.Flag{
|
2020-02-20 09:05:55 +00:00
|
|
|
walletPathFlag,
|
2020-02-20 13:14:14 +00:00
|
|
|
decryptFlag,
|
|
|
|
},
|
|
|
|
},
|
2021-03-05 09:23:25 +00:00
|
|
|
{
|
|
|
|
Name: "dump-keys",
|
|
|
|
Usage: "dump public keys for account",
|
|
|
|
Action: dumpKeys,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "address, a",
|
|
|
|
Usage: "address to print public keys for",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-20 13:14:14 +00:00
|
|
|
{
|
|
|
|
Name: "export",
|
|
|
|
Usage: "export keys for address",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "export --wallet <path> [--decrypt] [<address>]",
|
2020-02-20 13:14:14 +00:00
|
|
|
Action: exportKeys,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
decryptFlag,
|
2018-03-02 15:24:09 +00:00
|
|
|
},
|
|
|
|
},
|
2020-02-20 10:48:59 +00:00
|
|
|
{
|
|
|
|
Name: "import",
|
|
|
|
Usage: "import WIF",
|
|
|
|
Action: importWallet,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
wifFlag,
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name, n",
|
|
|
|
Usage: "Optional account name",
|
|
|
|
},
|
2020-03-13 14:03:38 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "contract",
|
|
|
|
Usage: "Verification script for custom contracts",
|
|
|
|
},
|
2020-02-20 10:48:59 +00:00
|
|
|
},
|
|
|
|
},
|
2020-02-20 12:50:17 +00:00
|
|
|
{
|
|
|
|
Name: "import-multisig",
|
|
|
|
Usage: "import multisig contract",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "import-multisig --wallet <path> --wif <wif> --min <n>" +
|
2020-02-20 12:50:17 +00:00
|
|
|
" [<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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-27 07:50:55 +00:00
|
|
|
{
|
|
|
|
Name: "import-deployed",
|
|
|
|
Usage: "import deployed contract",
|
|
|
|
UsageText: "import-multisig --wallet <path> --wif <wif> --contract <hash>",
|
|
|
|
Action: importDeployed,
|
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
wifFlag,
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "contract, c",
|
|
|
|
Usage: "Contract hash",
|
|
|
|
},
|
|
|
|
}, options.RPC...),
|
|
|
|
},
|
2020-03-13 13:24:33 +00:00
|
|
|
{
|
|
|
|
Name: "remove",
|
|
|
|
Usage: "remove an account from the wallet",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "remove --wallet <path> [--force] <addr>",
|
2020-03-13 13:24:33 +00:00
|
|
|
Action: removeAccount,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
forceFlag,
|
|
|
|
},
|
|
|
|
},
|
2020-03-02 12:39:02 +00:00
|
|
|
{
|
2021-03-03 11:47:36 +00:00
|
|
|
Name: "sign",
|
|
|
|
Usage: "cosign transaction with multisig/contract/additional account",
|
|
|
|
UsageText: "sign --wallet <path> --address <address> --in <file.in> --out <file.out>",
|
|
|
|
Action: signStoredTransaction,
|
|
|
|
Flags: signFlags,
|
2020-03-02 12:39:02 +00:00
|
|
|
},
|
2020-03-06 13:23:41 +00:00
|
|
|
{
|
2020-11-24 08:14:25 +00:00
|
|
|
Name: "nep17",
|
|
|
|
Usage: "work with NEP17 contracts",
|
|
|
|
Subcommands: newNEP17Commands(),
|
2020-03-06 13:23:41 +00:00
|
|
|
},
|
2020-08-04 09:35:04 +00:00
|
|
|
{
|
2020-08-07 09:18:38 +00:00
|
|
|
Name: "candidate",
|
|
|
|
Usage: "work with candidates",
|
|
|
|
Subcommands: newValidatorCommands(),
|
2020-08-04 09:35:04 +00:00
|
|
|
},
|
2018-03-02 15:24:09 +00:00
|
|
|
},
|
2019-10-19 20:58:45 +00:00
|
|
|
}}
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 13:09:19 +00:00
|
|
|
func claimGas(ctx *cli.Context) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-02-27 13:09:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
2020-03-03 20:09:47 +00:00
|
|
|
addrFlag := ctx.Generic("address").(*flags.Address)
|
2020-03-10 06:54:05 +00:00
|
|
|
if !addrFlag.IsSet {
|
|
|
|
return cli.NewExitError("address was not provided", 1)
|
|
|
|
}
|
2020-03-03 20:09:47 +00:00
|
|
|
scriptHash := addrFlag.Uint160()
|
2020-08-28 09:11:19 +00:00
|
|
|
acc, err := getDecryptedAccount(ctx, wall, scriptHash)
|
2020-02-27 13:09:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
2020-02-27 13:09:19 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
2020-02-27 13:09:19 +00:00
|
|
|
if err != nil {
|
2020-11-03 11:08:59 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-02-27 13:09:19 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2021-02-09 08:12:44 +00:00
|
|
|
hash, err := c.TransferNEP17(acc, scriptHash, neoContractHash, 0, 0, nil)
|
2020-04-15 06:50:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-02-27 13:09:19 +00:00
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, hash.StringLE())
|
2020-02-27 13:09:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:08:47 +00:00
|
|
|
func convertWallet(ctx *cli.Context) error {
|
2020-12-03 14:33:52 +00:00
|
|
|
wall, err := newWalletV2FromFile(ctx.String("wallet"))
|
2020-06-16 11:08:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
newWallet, err := wallet.NewWallet(ctx.String("out"))
|
|
|
|
if err != nil {
|
2020-12-03 14:33:52 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-06-16 11:08:47 +00:00
|
|
|
}
|
|
|
|
defer newWallet.Close()
|
|
|
|
|
|
|
|
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))
|
2020-06-16 11:08:47 +00:00
|
|
|
if err != nil {
|
2020-12-03 14:33:52 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-06-16 11:08:47 +00:00
|
|
|
}
|
2020-12-03 14:33:52 +00:00
|
|
|
newAcc, err := acc.convert(pass)
|
2020-06-16 11:08:47 +00:00
|
|
|
if err != nil {
|
2020-12-03 14:33:52 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-06-16 11:08:47 +00:00
|
|
|
}
|
|
|
|
newWallet.AddAccount(newAcc)
|
|
|
|
}
|
|
|
|
if err := newWallet.Save(); err != nil {
|
|
|
|
return cli.NewExitError(err, -1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 13:39:15 +00:00
|
|
|
func addAccount(ctx *cli.Context) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-02-20 13:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer wall.Close()
|
|
|
|
|
|
|
|
if err := createAccount(ctx, wall); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 13:14:14 +00:00
|
|
|
func exportKeys(ctx *cli.Context) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-02-20 13:14:14 +00:00
|
|
|
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 {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't parse address: %w", err), 1)
|
2020-02-20 13:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 > ")
|
2020-02-20 13:14:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
pk, err := keys.NEP2Decrypt(wif, pass)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
wif = pk.WIF()
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, wif)
|
2020-02-20 13:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 12:50:17 +00:00
|
|
|
func importMultisig(ctx *cli.Context) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-02-20 12:50:17 +00:00
|
|
|
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 {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't decode public key %d: %w", i, err), 1)
|
2020-02-20 12:50:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
acc, err := newAccountFromWIF(ctx.App.Writer, ctx.String("wif"))
|
2020-02-20 12:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := acc.ConvertMultisig(m, pubs); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addAccountAndSave(wall, acc); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-27 07:50:55 +00:00
|
|
|
func importDeployed(ctx *cli.Context) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
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()
|
|
|
|
|
2021-02-17 14:48:41 +00:00
|
|
|
rawHash := ctx.String("contract")
|
|
|
|
h, err := flags.ParseAddress(rawHash)
|
2020-08-27 07:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("invalid contract hash: %w", err), 1)
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
acc, err := newAccountFromWIF(ctx.App.Writer, ctx.String("wif"))
|
2020-08-27 07:50:55 +00:00
|
|
|
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)
|
2020-08-27 07:50:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 16:32:53 +00:00
|
|
|
cs, err := c.GetContractStateByHash(h)
|
2020-08-27 07:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't fetch contract info: %w", err), 1)
|
|
|
|
}
|
2021-01-26 14:37:34 +00:00
|
|
|
md := cs.Manifest.ABI.GetMethod(manifest.MethodVerify, -1)
|
2021-01-26 15:00:08 +00:00
|
|
|
if md == nil || md.ReturnType != smartcontract.BoolType {
|
|
|
|
return cli.NewExitError("contract has no `verify` method with boolean return", 1)
|
2020-08-27 07:50:55 +00:00
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
acc.Address = address.Uint160ToString(cs.Hash)
|
2021-01-13 12:34:10 +00:00
|
|
|
acc.Contract.Script = cs.NEF.Script
|
2020-09-02 09:20:42 +00:00
|
|
|
acc.Contract.Parameters = acc.Contract.Parameters[:0]
|
2020-08-27 07:50:55 +00:00
|
|
|
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 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()
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
acc, err := newAccountFromWIF(ctx.App.Writer, ctx.String("wif"))
|
2020-02-20 10:48:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2020-03-13 14:03:38 +00:00
|
|
|
if ctrFlag := ctx.String("contract"); ctrFlag != "" {
|
|
|
|
ctr, err := hex.DecodeString(ctrFlag)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError("invalid contract", 1)
|
|
|
|
}
|
|
|
|
acc.Contract.Script = ctr
|
|
|
|
}
|
|
|
|
|
2020-08-31 13:17:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-13 13:24:33 +00:00
|
|
|
func removeAccount(ctx *cli.Context) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-03-13 13:24:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
|
|
|
addrArg := ctx.Args().First()
|
|
|
|
addr, err := address.StringToUint160(addrArg)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError("valid address must be provided", 1)
|
|
|
|
}
|
|
|
|
acc := wall.GetAccount(addr)
|
|
|
|
if acc == nil {
|
|
|
|
return cli.NewExitError("account wasn't found", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.Bool("force") {
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintf(ctx.App.Writer, "Account %s will be removed. This action is irreversible.\n", addrArg)
|
|
|
|
if ok := askForConsent(ctx.App.Writer); !ok {
|
2020-03-13 13:24:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := wall.RemoveAccount(acc.Address); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("error on remove: %w", err), 1)
|
2020-03-13 13:24:33 +00:00
|
|
|
} else if err := wall.Save(); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("error while saving wallet: %w", err), 1)
|
2020-03-13 13:24:33 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
func askForConsent(w io.Writer) bool {
|
2021-02-10 08:53:01 +00:00
|
|
|
response, err := input.ReadLine("Are you sure? [y/N]: ")
|
2020-03-13 13:24:33 +00:00
|
|
|
if err == nil {
|
|
|
|
response = strings.ToLower(strings.TrimSpace(response))
|
|
|
|
if response == "y" || response == "yes" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(w, "Cancelled.")
|
2020-03-13 13:24:33 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:39:38 +00:00
|
|
|
func dumpWallet(ctx *cli.Context) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-01-09 15:39:38 +00:00
|
|
|
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 > ")
|
2020-01-09 15:39:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
for i := range wall.Accounts {
|
|
|
|
// Just testing the decryption here.
|
2020-02-20 10:34:27 +00:00
|
|
|
err := wall.Accounts[i].Decrypt(pass)
|
2020-01-09 15:39:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
fmtPrintWallet(ctx.App.Writer, wall)
|
2018-03-02 15:24:09 +00:00
|
|
|
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
|
|
|
|
addr := ctx.String("address")
|
|
|
|
if addr != "" {
|
|
|
|
u, err := flags.ParseAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1)
|
|
|
|
}
|
|
|
|
acc := wall.GetAccount(u)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if addr != "" {
|
|
|
|
return cli.NewExitError(fmt.Errorf("Unknown script type for address %s", addr), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
func createWallet(ctx *cli.Context) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
path := ctx.String("wallet")
|
2018-03-02 15:24:09 +00:00
|
|
|
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(ctx, wall); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
fmtPrintWallet(ctx.App.Writer, wall)
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "wallet successfully created, file location is %s\n", wall.Path())
|
2018-03-02 15:24:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
func readAccountInfo(w io.Writer) (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 > ")
|
2020-02-20 10:34:27 +00:00
|
|
|
if err != nil {
|
2020-02-20 10:48:59 +00:00
|
|
|
return "", "", err
|
2020-02-20 10:34:27 +00:00
|
|
|
}
|
2021-02-10 08:53:01 +00:00
|
|
|
phraseCheck, err := input.ReadPassword("Confirm passphrase > ")
|
2020-02-20 10:34:27 +00:00
|
|
|
if err != nil {
|
2020-02-20 10:48:59 +00:00
|
|
|
return "", "", err
|
2020-02-20 10:34:27 +00:00
|
|
|
}
|
2018-03-02 15:24:09 +00:00
|
|
|
|
|
|
|
if phrase != phraseCheck {
|
2020-02-20 10:48:59 +00:00
|
|
|
return "", "", errPhraseMismatch
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 10:34:27 +00:00
|
|
|
name := strings.TrimRight(string(rawName), "\n")
|
2020-02-20 10:48:59 +00:00
|
|
|
return name, phrase, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createAccount(ctx *cli.Context, wall *wallet.Wallet) error {
|
2020-08-28 09:11:19 +00:00
|
|
|
name, phrase, err := readAccountInfo(ctx.App.Writer)
|
2020-02-20 10:48:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-02 15:24:09 +00:00
|
|
|
return wall.CreateAccount(name, phrase)
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:28:49 +00:00
|
|
|
func openWallet(path string) (*wallet.Wallet, error) {
|
|
|
|
if len(path) == 0 {
|
|
|
|
return nil, errNoPath
|
|
|
|
}
|
|
|
|
return wallet.NewWalletFromFile(path)
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
func newAccountFromWIF(w io.Writer, wif string) (*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)
|
|
|
|
}
|
|
|
|
|
|
|
|
acc, err := wallet.NewAccountFromWIF(wif)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(w, "Provided WIF was unencrypted. Wallet can contain only encrypted keys.")
|
|
|
|
name, pass, err := readAccountInfo(w)
|
2020-02-20 10:48:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.Label = name
|
|
|
|
if err := acc.Encrypt(pass); err != nil {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
func fmtPrintWallet(w io.Writer, wall *wallet.Wallet) {
|
2018-03-02 15:24:09 +00:00
|
|
|
b, _ := wall.JSON()
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, string(b))
|
|
|
|
fmt.Fprintln(w, "")
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|