2018-03-02 15:24:09 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-02-27 13:09:19 +00:00
|
|
|
"context"
|
2020-03-03 07:35:21 +00:00
|
|
|
"encoding/json"
|
2018-03-02 15:24:09 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-03-03 07:35:21 +00:00
|
|
|
"io/ioutil"
|
2018-03-02 15:24:09 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2019-12-03 17:04:52 +00:00
|
|
|
"syscall"
|
2018-03-02 15:24:09 +00:00
|
|
|
|
2020-03-03 20:09:47 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"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/rpc/client"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
|
2020-03-03 07:35:21 +00:00
|
|
|
context2 "github.com/nspcc-dev/neo-go/pkg/smartcontract/context"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2018-03-02 15:24:09 +00:00
|
|
|
"github.com/urfave/cli"
|
2019-12-03 17:04:52 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2018-03-02 15:24:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-01-25 11:20:35 +00:00
|
|
|
errNoPath = errors.New("target path where the wallet should be stored is mandatory and should be passed using (--path, -p) flags")
|
|
|
|
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{
|
|
|
|
Name: "path, p",
|
|
|
|
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-02-27 13:09:19 +00:00
|
|
|
rpcFlag = cli.StringFlag{
|
|
|
|
Name: "rpc, r",
|
|
|
|
Usage: "RPC node address",
|
|
|
|
}
|
|
|
|
timeoutFlag = cli.DurationFlag{
|
|
|
|
Name: "timeout, t",
|
|
|
|
Usage: "Timeout for the operation",
|
|
|
|
}
|
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-02-20 09:05:55 +00:00
|
|
|
)
|
|
|
|
|
2019-10-19 20:58:45 +00:00
|
|
|
// NewCommands returns 'wallet' command.
|
|
|
|
func NewCommands() []cli.Command {
|
|
|
|
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,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
rpcFlag,
|
|
|
|
timeoutFlag,
|
2020-03-03 20:09:47 +00:00
|
|
|
flags.AddressFlag{
|
2020-02-27 13:09:19 +00:00
|
|
|
Name: "address, a",
|
|
|
|
Usage: "Address to claim GAS for",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-03-02 15:24:09 +00:00
|
|
|
{
|
|
|
|
Name: "create",
|
|
|
|
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-02-20 13:39:15 +00:00
|
|
|
{
|
|
|
|
Name: "create-account",
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "export",
|
|
|
|
Usage: "export keys for address",
|
|
|
|
UsageText: "export --path <path> [--decrypt] [<address>]",
|
|
|
|
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-02-20 12:50:17 +00:00
|
|
|
{
|
|
|
|
Name: "import-multisig",
|
|
|
|
Usage: "import multisig contract",
|
|
|
|
UsageText: "import-multisig --path <path> --wif <wif> --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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-28 07:25:03 +00:00
|
|
|
{
|
|
|
|
Name: "transfer",
|
|
|
|
Usage: "transfer NEO/GAS",
|
|
|
|
UsageText: "transfer --path <path> --from <addr> --to <addr>" +
|
2020-03-03 07:35:21 +00:00
|
|
|
" --amount <amount> --asset [NEO|GAS|<hex-id>] [--out <path>]",
|
2020-02-28 07:25:03 +00:00
|
|
|
Action: transferAsset,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
rpcFlag,
|
|
|
|
timeoutFlag,
|
2020-03-03 07:35:21 +00:00
|
|
|
outFlag,
|
2020-03-06 14:22:14 +00:00
|
|
|
fromAddrFlag,
|
|
|
|
toAddrFlag,
|
2020-02-28 07:25:03 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "amount",
|
|
|
|
Usage: "Amount of asset to send",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "asset",
|
|
|
|
Usage: "Asset ID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-02 12:39:02 +00:00
|
|
|
{
|
|
|
|
Name: "multisig",
|
|
|
|
Usage: "work with multisig address",
|
|
|
|
Subcommands: newMultisigCommands(),
|
|
|
|
},
|
2020-03-06 13:23:41 +00:00
|
|
|
{
|
|
|
|
Name: "nep5",
|
|
|
|
Usage: "work with NEP5 contracts",
|
|
|
|
Subcommands: newNEP5Commands(),
|
|
|
|
},
|
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 {
|
|
|
|
wall, err := openWallet(ctx.String("path"))
|
|
|
|
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-02-27 13:09:19 +00:00
|
|
|
acc := wall.GetAccount(scriptHash)
|
|
|
|
if acc == nil {
|
2020-03-03 20:09:47 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("wallet contains no account for '%s'", addrFlag), 1)
|
2020-02-27 13:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pass, err := readPassword("Enter password > ")
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
} else if err := acc.Decrypt(pass); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
gctx, cancel := getGoContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := client.New(gctx, ctx.String("rpc"), client.Options{})
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-03-10 07:56:46 +00:00
|
|
|
info, err := c.GetClaimable(addrFlag.String())
|
2020-02-27 13:09:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
} else if info.Unclaimed == 0 || len(info.Spents) == 0 {
|
|
|
|
fmt.Println("Nothing to claim")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var claim transaction.ClaimTX
|
|
|
|
for i := range info.Spents {
|
|
|
|
claim.Claims = append(claim.Claims, transaction.Input{
|
|
|
|
PrevHash: info.Spents[i].Tx,
|
|
|
|
PrevIndex: uint16(info.Spents[i].N),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := &transaction.Transaction{
|
|
|
|
Type: transaction.ClaimType,
|
|
|
|
Data: &claim,
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.AddOutput(&transaction.Output{
|
|
|
|
AssetID: core.UtilityTokenID(),
|
|
|
|
Amount: info.Unclaimed,
|
|
|
|
ScriptHash: scriptHash,
|
|
|
|
})
|
|
|
|
|
2020-02-28 16:01:07 +00:00
|
|
|
_ = acc.SignTx(tx)
|
2020-02-27 13:09:19 +00:00
|
|
|
if err := c.SendRawTransaction(tx); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(tx.Hash().StringLE())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 13:39:15 +00:00
|
|
|
func addAccount(ctx *cli.Context) error {
|
2020-02-21 08:28:49 +00:00
|
|
|
wall, err := openWallet(ctx.String("path"))
|
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-02-21 08:28:49 +00:00
|
|
|
wall, err := openWallet(ctx.String("path"))
|
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 {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't parse address: %v", 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 {
|
|
|
|
pass, err := readPassword("Enter password > ")
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(wif)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 12:50:17 +00:00
|
|
|
func importMultisig(ctx *cli.Context) error {
|
2020-02-21 08:28:49 +00:00
|
|
|
wall, err := openWallet(ctx.String("path"))
|
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 {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't decode public key %d: %v", i, err), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acc, err := newAccountFromWIF(ctx.String("wif"))
|
|
|
|
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-02-20 10:48:59 +00:00
|
|
|
func importWallet(ctx *cli.Context) error {
|
2020-02-21 08:28:49 +00:00
|
|
|
wall, err := openWallet(ctx.String("path"))
|
2020-02-20 10:48:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer wall.Close()
|
|
|
|
|
|
|
|
acc, err := newAccountFromWIF(ctx.String("wif"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.Label = ctx.String("name")
|
|
|
|
if err := addAccountAndSave(wall, acc); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:25:03 +00:00
|
|
|
func transferAsset(ctx *cli.Context) error {
|
|
|
|
wall, err := openWallet(ctx.String("path"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
2020-03-03 20:09:47 +00:00
|
|
|
fromFlag := ctx.Generic("from").(*flags.Address)
|
2020-03-10 06:54:05 +00:00
|
|
|
if !fromFlag.IsSet {
|
|
|
|
return cli.NewExitError("'from' address was not provided", 1)
|
|
|
|
}
|
2020-03-03 20:09:47 +00:00
|
|
|
from := fromFlag.Uint160()
|
|
|
|
acc := wall.GetAccount(from)
|
2020-02-28 07:25:03 +00:00
|
|
|
if acc == nil {
|
2020-03-03 20:09:47 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("wallet contains no account for '%s'", from), 1)
|
2020-02-28 07:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
asset, err := getAssetID(ctx.String("asset"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("invalid asset id: %v", err), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
amount, err := util.Fixed8FromString(ctx.String("amount"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("invalid amount: %v", err), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
pass, err := readPassword("Enter wallet password > ")
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
} else if err := acc.Decrypt(pass); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
gctx, cancel := getGoContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := client.New(gctx, ctx.String("rpc"), client.Options{})
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := transaction.NewContractTX()
|
2020-03-03 20:09:47 +00:00
|
|
|
if err := request.AddInputsAndUnspentsToTx(tx, fromFlag.String(), asset, amount, c); err != nil {
|
2020-02-28 07:25:03 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2020-03-10 06:54:05 +00:00
|
|
|
toFlag := ctx.Generic("to").(*flags.Address)
|
|
|
|
if !toFlag.IsSet {
|
|
|
|
return cli.NewExitError("'to' address was not provided", 1)
|
|
|
|
}
|
|
|
|
toAddr := toFlag.Uint160()
|
2020-02-28 07:25:03 +00:00
|
|
|
tx.AddOutput(&transaction.Output{
|
|
|
|
AssetID: asset,
|
|
|
|
Amount: amount,
|
|
|
|
ScriptHash: toAddr,
|
|
|
|
Position: 1,
|
|
|
|
})
|
|
|
|
|
2020-03-03 07:35:21 +00:00
|
|
|
if outFile := ctx.String("out"); outFile != "" {
|
|
|
|
priv := acc.PrivateKey()
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
sign := priv.Sign(tx.GetSignedPart())
|
|
|
|
c := context2.NewParameterContext("Neo.Core.ContractTransaction", tx)
|
|
|
|
if err := c.AddSignature(acc.Contract, pub, sign); err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't add signature: %v", err), 1)
|
|
|
|
} else if data, err := json.Marshal(c); err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't marshal tx to JSON: %v", err), 1)
|
|
|
|
} else if err := ioutil.WriteFile(outFile, data, 0644); err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't write tx to file: %v", err), 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_ = acc.SignTx(tx)
|
|
|
|
if err := c.SendRawTransaction(tx); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-02-28 07:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(tx.Hash().StringLE())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-27 13:09:19 +00:00
|
|
|
func getGoContext(ctx *cli.Context) (context.Context, func()) {
|
|
|
|
if dur := ctx.Duration("timeout"); dur != 0 {
|
|
|
|
return context.WithTimeout(context.Background(), dur)
|
|
|
|
}
|
|
|
|
return context.Background(), func() {}
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:39:38 +00:00
|
|
|
func dumpWallet(ctx *cli.Context) error {
|
2020-02-21 08:28:49 +00:00
|
|
|
wall, err := openWallet(ctx.String("path"))
|
2020-01-09 15:39:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
if ctx.Bool("decrypt") {
|
2020-02-20 10:34:27 +00:00
|
|
|
pass, err := 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmtPrintWallet(wall)
|
2018-03-02 15:24:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createWallet(ctx *cli.Context) error {
|
|
|
|
path := ctx.String("path")
|
|
|
|
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-01-09 15:39:38 +00:00
|
|
|
fmtPrintWallet(wall)
|
2019-02-13 18:01:10 +00:00
|
|
|
fmt.Printf("wallet successfully created, file location is %s\n", wall.Path())
|
2018-03-02 15:24:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 10:48:59 +00:00
|
|
|
func readAccountInfo() (string, string, error) {
|
2018-03-02 15:24:09 +00:00
|
|
|
buf := bufio.NewReader(os.Stdin)
|
|
|
|
fmt.Print("Enter the name of the account > ")
|
2020-02-20 10:34:27 +00:00
|
|
|
rawName, _ := buf.ReadBytes('\n')
|
|
|
|
phrase, err := readPassword("Enter passphrase > ")
|
|
|
|
if err != nil {
|
2020-02-20 10:48:59 +00:00
|
|
|
return "", "", err
|
2020-02-20 10:34:27 +00:00
|
|
|
}
|
|
|
|
phraseCheck, err := readPassword("Confirm passphrase > ")
|
|
|
|
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 {
|
|
|
|
name, phrase, err := readAccountInfo()
|
|
|
|
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-02-28 07:25:03 +00:00
|
|
|
func getAssetID(s string) (util.Uint256, error) {
|
|
|
|
s = strings.ToLower(s)
|
|
|
|
switch {
|
|
|
|
case s == "neo":
|
|
|
|
return core.GoverningTokenID(), nil
|
|
|
|
case s == "gas":
|
|
|
|
return core.UtilityTokenID(), nil
|
|
|
|
default:
|
|
|
|
return util.Uint256DecodeStringLE(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 10:48:59 +00:00
|
|
|
func newAccountFromWIF(wif string) (*wallet.Account, error) {
|
|
|
|
// 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 {
|
|
|
|
pass, err := readPassword("Enter password > ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return wallet.NewAccountFromEncryptedWIF(wif, pass)
|
|
|
|
}
|
|
|
|
|
|
|
|
acc, err := wallet.NewAccountFromWIF(wif)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Provided WIF was unencrypted. Wallet can contain only encrypted keys.")
|
|
|
|
name, pass, err := readAccountInfo()
|
|
|
|
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-02-20 10:34:27 +00:00
|
|
|
func readPassword(prompt string) (string, error) {
|
|
|
|
fmt.Print(prompt)
|
|
|
|
rawPass, err := terminal.ReadPassword(syscall.Stdin)
|
|
|
|
fmt.Println()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.TrimRight(string(rawPass), "\n"), nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:39:38 +00:00
|
|
|
func fmtPrintWallet(wall *wallet.Wallet) {
|
2018-03-02 15:24:09 +00:00
|
|
|
b, _ := wall.JSON()
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println(string(b))
|
|
|
|
fmt.Println("")
|
|
|
|
}
|