github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/cmd/geth/accountcmd.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  
    23  	"github.com/ethereum/go-ethereum/accounts"
    24  	"github.com/ethereum/go-ethereum/accounts/keystore"
    25  	"github.com/ethereum/go-ethereum/cmd/utils"
    26  	"github.com/ethereum/go-ethereum/console"
    27  	"github.com/ethereum/go-ethereum/crypto"
    28  	"github.com/ethereum/go-ethereum/log"
    29  	"gopkg.in/urfave/cli.v1"
    30  )
    31  
    32  var (
    33  	walletCommand = cli.Command{
    34  		Name:      "wallet",
    35  		Usage:     "Manage Ethereum presale wallets",
    36  		ArgsUsage: "",
    37  		Category:  "ACCOUNT COMMANDS",
    38  		Description: `
    39      geth wallet import /path/to/my/presale.wallet
    40  
    41  will prompt for your password and imports your ether presale account.
    42  It can be used non-interactively with the --password option taking a
    43  passwordfile as argument containing the wallet password in plaintext.
    44  
    45  `,
    46  		Subcommands: []cli.Command{
    47  			{
    48  				Action:    importWallet,
    49  				Name:      "import",
    50  				Usage:     "Import Ethereum presale wallet",
    51  				ArgsUsage: "<keyFile>",
    52  				Description: `
    53  TODO: Please write this
    54  `,
    55  			},
    56  		},
    57  	}
    58  	accountCommand = cli.Command{
    59  		Action:    accountList,
    60  		Name:      "account",
    61  		Usage:     "Manage accounts",
    62  		ArgsUsage: "",
    63  		Category:  "ACCOUNT COMMANDS",
    64  		Description: `
    65  Manage accounts lets you create new accounts, list all existing accounts,
    66  import a private key into a new account.
    67  
    68  '            help' shows a list of subcommands or help for one subcommand.
    69  
    70  It supports interactive mode, when you are prompted for password as well as
    71  non-interactive mode where passwords are supplied via a given password file.
    72  Non-interactive mode is only meant for scripted use on test networks or known
    73  safe environments.
    74  
    75  Make sure you remember the password you gave when creating a new account (with
    76  either new or import). Without it you are not able to unlock your account.
    77  
    78  Note that exporting your key in unencrypted format is NOT supported.
    79  
    80  Keys are stored under <DATADIR>/keystore.
    81  It is safe to transfer the entire directory or the individual keys therein
    82  between ethereum nodes by simply copying.
    83  Make sure you backup your keys regularly.
    84  
    85  In order to use your account to send transactions, you need to unlock them using
    86  the '--unlock' option. The argument is a space separated list of addresses or
    87  indexes. If used non-interactively with a passwordfile, the file should contain
    88  the respective passwords one per line. If you unlock n accounts and the password
    89  file contains less than n entries, then the last password is meant to apply to
    90  all remaining accounts.
    91  
    92  And finally. DO NOT FORGET YOUR PASSWORD.
    93  `,
    94  		Subcommands: []cli.Command{
    95  			{
    96  				Action:    accountList,
    97  				Name:      "list",
    98  				Usage:     "Print account addresses",
    99  				ArgsUsage: " ",
   100  				Description: `
   101  TODO: Please write this
   102  `,
   103  			},
   104  			{
   105  				Action:    accountCreate,
   106  				Name:      "new",
   107  				Usage:     "Create a new account",
   108  				ArgsUsage: " ",
   109  				Description: `
   110      geth account new
   111  
   112  Creates a new account. Prints the address.
   113  
   114  The account is saved in encrypted format, you are prompted for a passphrase.
   115  
   116  You must remember this passphrase to unlock your account in the future.
   117  
   118  For non-interactive use the passphrase can be specified with the --password flag:
   119  
   120      geth --password <passwordfile> account new
   121  
   122  Note, this is meant to be used for testing only, it is a bad idea to save your
   123  password to file or expose in any other way.
   124  `,
   125  			},
   126  			{
   127  				Action:    accountUpdate,
   128  				Name:      "update",
   129  				Usage:     "Update an existing account",
   130  				ArgsUsage: "<address>",
   131  				Description: `
   132      geth account update <address>
   133  
   134  Update an existing account.
   135  
   136  The account is saved in the newest version in encrypted format, you are prompted
   137  for a passphrase to unlock the account and another to save the updated file.
   138  
   139  This same command can therefore be used to migrate an account of a deprecated
   140  format to the newest format or change the password for an account.
   141  
   142  For non-interactive use the passphrase can be specified with the --password flag:
   143  
   144      geth --password <passwordfile> account update <address>
   145  
   146  Since only one password can be given, only format update can be performed,
   147  changing your password is only possible interactively.
   148  `,
   149  			},
   150  			{
   151  				Action:    accountImport,
   152  				Name:      "import",
   153  				Usage:     "Import a private key into a new account",
   154  				ArgsUsage: "<keyFile>",
   155  				Description: `
   156      geth account import <keyfile>
   157  
   158  Imports an unencrypted private key from <keyfile> and creates a new account.
   159  Prints the address.
   160  
   161  The keyfile is assumed to contain an unencrypted private key in hexadecimal format.
   162  
   163  The account is saved in encrypted format, you are prompted for a passphrase.
   164  
   165  You must remember this passphrase to unlock your account in the future.
   166  
   167  For non-interactive use the passphrase can be specified with the -password flag:
   168  
   169      geth --password <passwordfile> account import <keyfile>
   170  
   171  Note:
   172  As you can directly copy your encrypted accounts to another ethereum instance,
   173  this import mechanism is not needed when you transfer an account between
   174  nodes.
   175  `,
   176  			},
   177  		},
   178  	}
   179  )
   180  
   181  func accountList(ctx *cli.Context) error {
   182  	stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
   183  
   184  	var index int
   185  	for _, wallet := range stack.AccountManager().Wallets() {
   186  		for _, account := range wallet.Accounts() {
   187  			fmt.Printf("Account #%d: {%x} %s\n", index, account.Address, &account.URL)
   188  			index++
   189  		}
   190  	}
   191  	return nil
   192  }
   193  
   194  // tries unlocking the specified account a few times.
   195  func unlockAccount(ctx *cli.Context, ks *keystore.KeyStore, address string, i int, passwords []string) (accounts.Account, string) {
   196  	account, err := utils.MakeAddress(ks, address)
   197  	if err != nil {
   198  		utils.Fatalf("Could not list accounts: %v", err)
   199  	}
   200  	for trials := 0; trials < 3; trials++ {
   201  		prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3)
   202  		password := getPassPhrase(prompt, false, i, passwords)
   203  		err = ks.Unlock(account, password)
   204  		if err == nil {
   205  			log.Info("Unlocked account", "address", account.Address.Hex())
   206  			return account, password
   207  		}
   208  		if err, ok := err.(*keystore.AmbiguousAddrError); ok {
   209  			log.Info("Unlocked account", "address", account.Address.Hex())
   210  			return ambiguousAddrRecovery(ks, err, password), password
   211  		}
   212  		if err != keystore.ErrDecrypt {
   213  			// No need to prompt again if the error is not decryption-related.
   214  			break
   215  		}
   216  	}
   217  	// All trials expended to unlock account, bail out
   218  	utils.Fatalf("Failed to unlock account %s (%v)", address, err)
   219  
   220  	return accounts.Account{}, ""
   221  }
   222  
   223  // getPassPhrase retrieves the passwor associated with an account, either fetched
   224  // from a list of preloaded passphrases, or requested interactively from the user.
   225  func getPassPhrase(prompt string, confirmation bool, i int, passwords []string) string {
   226  	// If a list of passwords was supplied, retrieve from them
   227  	if len(passwords) > 0 {
   228  		if i < len(passwords) {
   229  			return passwords[i]
   230  		}
   231  		return passwords[len(passwords)-1]
   232  	}
   233  	// Otherwise prompt the user for the password
   234  	if prompt != "" {
   235  		fmt.Println(prompt)
   236  	}
   237  	password, err := console.Stdin.PromptPassword("Passphrase: ")
   238  	if err != nil {
   239  		utils.Fatalf("Failed to read passphrase: %v", err)
   240  	}
   241  	if confirmation {
   242  		confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ")
   243  		if err != nil {
   244  			utils.Fatalf("Failed to read passphrase confirmation: %v", err)
   245  		}
   246  		if password != confirm {
   247  			utils.Fatalf("Passphrases do not match")
   248  		}
   249  	}
   250  	return password
   251  }
   252  
   253  func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrError, auth string) accounts.Account {
   254  	fmt.Printf("Multiple key files exist for address %x:\n", err.Addr)
   255  	for _, a := range err.Matches {
   256  		fmt.Println("  ", a.URL)
   257  	}
   258  	fmt.Println("Testing your passphrase against all of them...")
   259  	var match *accounts.Account
   260  	for _, a := range err.Matches {
   261  		if err := ks.Unlock(a, auth); err == nil {
   262  			match = &a
   263  			break
   264  		}
   265  	}
   266  	if match == nil {
   267  		utils.Fatalf("None of the listed files could be unlocked.")
   268  	}
   269  	fmt.Printf("Your passphrase unlocked %s\n", match.URL)
   270  	fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:")
   271  	for _, a := range err.Matches {
   272  		if a != *match {
   273  			fmt.Println("  ", a.URL)
   274  		}
   275  	}
   276  	return *match
   277  }
   278  
   279  // accountCreate creates a new account into the keystore defined by the CLI flags.
   280  func accountCreate(ctx *cli.Context) error {
   281  	stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
   282  	password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
   283  
   284  	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
   285  	account, err := ks.NewAccount(password)
   286  	if err != nil {
   287  		utils.Fatalf("Failed to create account: %v", err)
   288  	}
   289  	fmt.Printf("Address: {%x}\n", account.Address)
   290  	return nil
   291  }
   292  
   293  // accountUpdate transitions an account from a previous format to the current
   294  // one, also providing the possibility to change the pass-phrase.
   295  func accountUpdate(ctx *cli.Context) error {
   296  	if len(ctx.Args()) == 0 {
   297  		utils.Fatalf("No accounts specified to update")
   298  	}
   299  	stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
   300  	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
   301  
   302  	account, oldPassword := unlockAccount(ctx, ks, ctx.Args().First(), 0, nil)
   303  	newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true, 0, nil)
   304  	if err := ks.Update(account, oldPassword, newPassword); err != nil {
   305  		utils.Fatalf("Could not update the account: %v", err)
   306  	}
   307  	return nil
   308  }
   309  
   310  func importWallet(ctx *cli.Context) error {
   311  	keyfile := ctx.Args().First()
   312  	if len(keyfile) == 0 {
   313  		utils.Fatalf("keyfile must be given as argument")
   314  	}
   315  	keyJson, err := ioutil.ReadFile(keyfile)
   316  	if err != nil {
   317  		utils.Fatalf("Could not read wallet file: %v", err)
   318  	}
   319  
   320  	stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
   321  	passphrase := getPassPhrase("", false, 0, utils.MakePasswordList(ctx))
   322  
   323  	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
   324  	acct, err := ks.ImportPreSaleKey(keyJson, passphrase)
   325  	if err != nil {
   326  		utils.Fatalf("%v", err)
   327  	}
   328  	fmt.Printf("Address: {%x}\n", acct.Address)
   329  	return nil
   330  }
   331  
   332  func accountImport(ctx *cli.Context) error {
   333  	keyfile := ctx.Args().First()
   334  	if len(keyfile) == 0 {
   335  		utils.Fatalf("keyfile must be given as argument")
   336  	}
   337  	key, err := crypto.LoadECDSA(keyfile)
   338  	if err != nil {
   339  		utils.Fatalf("Failed to load the private key: %v", err)
   340  	}
   341  	stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
   342  	passphrase := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
   343  
   344  	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
   345  	acct, err := ks.ImportECDSA(key, passphrase)
   346  	if err != nil {
   347  		utils.Fatalf("Could not create the account: %v", err)
   348  	}
   349  	fmt.Printf("Address: {%x}\n", acct.Address)
   350  	return nil
   351  }