2016-03-17 14:09:18 +02:00
// Copyright 2016 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"io/ioutil"
"github.com/ethereum/go-ethereum/accounts"
2017-01-24 12:49:20 +03:00
"github.com/ethereum/go-ethereum/accounts/keystore"
2016-03-17 14:09:18 +02:00
"github.com/ethereum/go-ethereum/cmd/utils"
2016-05-06 12:40:23 +03:00
"github.com/ethereum/go-ethereum/console"
2016-04-05 02:08:50 +03:00
"github.com/ethereum/go-ethereum/crypto"
2017-02-22 15:10:07 +03:00
"github.com/ethereum/go-ethereum/log"
2016-06-09 12:44:42 +03:00
"gopkg.in/urfave/cli.v1"
2016-03-17 14:09:18 +02:00
)
var (
walletCommand = cli . Command {
2017-05-23 18:45:26 +03:00
Name : "wallet" ,
Usage : "Manage Ethereum presale wallets" ,
ArgsUsage : "" ,
Category : "ACCOUNT COMMANDS" ,
2016-03-17 14:09:18 +02:00
Description : `
2017-05-23 18:45:26 +03:00
geth wallet import / path / to / my / presale . wallet
will prompt for your password and imports your ether presale account .
It can be used non - interactively with the -- password option taking a
passwordfile as argument containing the wallet password in plaintext . ` ,
Subcommands : [ ] cli . Command {
{
2016-03-17 14:09:18 +02:00
2017-05-23 18:45:26 +03:00
Name : "import" ,
Usage : "Import Ethereum presale wallet" ,
ArgsUsage : "<keyFile>" ,
Action : utils . MigrateFlags ( importWallet ) ,
Category : "ACCOUNT COMMANDS" ,
Flags : [ ] cli . Flag {
utils . DataDirFlag ,
utils . KeyStoreDirFlag ,
utils . PasswordFileFlag ,
utils . LightKDFFlag ,
} ,
Description : `
geth wallet [ options ] / path / to / my / presale . wallet
2016-03-17 14:09:18 +02:00
2017-05-23 18:45:26 +03:00
will prompt for your password and imports your ether presale account .
It can be used non - interactively with the -- password option taking a
passwordfile as argument containing the wallet password in plaintext . ` ,
} ,
} ,
2016-11-10 14:00:09 +03:00
}
2017-05-23 18:45:26 +03:00
2016-03-17 14:09:18 +02:00
accountCommand = cli . Command {
2017-04-28 13:38:49 +03:00
Name : "account" ,
Usage : "Manage accounts" ,
Category : "ACCOUNT COMMANDS" ,
2016-03-17 14:09:18 +02:00
Description : `
2017-04-28 13:38:49 +03:00
Manage accounts , list all existing accounts , import a private key into a new
account , create a new account or update an existing account .
2016-03-17 14:09:18 +02:00
It supports interactive mode , when you are prompted for password as well as
non - interactive mode where passwords are supplied via a given password file .
Non - interactive mode is only meant for scripted use on test networks or known
safe environments .
Make sure you remember the password you gave when creating a new account ( with
either new or import ) . Without it you are not able to unlock your account .
Note that exporting your key in unencrypted format is NOT supported .
2016-06-08 17:00:18 +03:00
Keys are stored under < DATADIR > / keystore .
2016-03-17 14:09:18 +02:00
It is safe to transfer the entire directory or the individual keys therein
between ethereum nodes by simply copying .
2017-04-28 13:38:49 +03:00
Make sure you backup your keys regularly . ` ,
2016-03-17 14:09:18 +02:00
Subcommands : [ ] cli . Command {
{
2017-04-28 13:38:49 +03:00
Name : "list" ,
Usage : "Print summary of existing accounts" ,
Action : utils . MigrateFlags ( accountList ) ,
Flags : [ ] cli . Flag {
utils . DataDirFlag ,
utils . KeyStoreDirFlag ,
} ,
2016-11-10 14:00:09 +03:00
Description : `
2017-04-28 13:38:49 +03:00
Print a short summary of all accounts ` ,
2016-03-17 14:09:18 +02:00
} ,
{
2017-04-28 13:38:49 +03:00
Name : "new" ,
Usage : "Create a new account" ,
Action : utils . MigrateFlags ( accountCreate ) ,
Flags : [ ] cli . Flag {
utils . DataDirFlag ,
utils . KeyStoreDirFlag ,
utils . PasswordFileFlag ,
utils . LightKDFFlag ,
} ,
2016-03-17 14:09:18 +02:00
Description : `
2016-11-10 14:00:09 +03:00
geth account new
2016-03-17 14:09:18 +02:00
2017-04-28 13:38:49 +03:00
Creates a new account and prints the address .
2016-03-17 14:09:18 +02:00
The account is saved in encrypted format , you are prompted for a passphrase .
You must remember this passphrase to unlock your account in the future .
For non - interactive use the passphrase can be specified with the -- password flag :
Note , this is meant to be used for testing only , it is a bad idea to save your
password to file or expose in any other way .
2016-11-10 14:00:09 +03:00
` ,
2016-03-17 14:09:18 +02:00
} ,
{
2016-11-10 14:00:09 +03:00
Name : "update" ,
Usage : "Update an existing account" ,
2017-04-28 13:38:49 +03:00
Action : utils . MigrateFlags ( accountUpdate ) ,
2016-11-10 14:00:09 +03:00
ArgsUsage : "<address>" ,
2017-04-28 13:38:49 +03:00
Flags : [ ] cli . Flag {
utils . DataDirFlag ,
utils . KeyStoreDirFlag ,
utils . LightKDFFlag ,
} ,
2016-03-17 14:09:18 +02:00
Description : `
2016-11-10 14:00:09 +03:00
geth account update < address >
2016-03-17 14:09:18 +02:00
Update an existing account .
The account is saved in the newest version in encrypted format , you are prompted
for a passphrase to unlock the account and another to save the updated file .
This same command can therefore be used to migrate an account of a deprecated
format to the newest format or change the password for an account .
For non - interactive use the passphrase can be specified with the -- password flag :
2017-04-28 13:38:49 +03:00
geth account update [ options ] < address >
2016-03-17 14:09:18 +02:00
Since only one password can be given , only format update can be performed ,
changing your password is only possible interactively .
2016-11-10 14:00:09 +03:00
` ,
2016-03-17 14:09:18 +02:00
} ,
{
2017-04-28 13:38:49 +03:00
Name : "import" ,
Usage : "Import a private key into a new account" ,
Action : utils . MigrateFlags ( accountImport ) ,
Flags : [ ] cli . Flag {
utils . DataDirFlag ,
utils . KeyStoreDirFlag ,
utils . PasswordFileFlag ,
utils . LightKDFFlag ,
} ,
2016-11-10 14:00:09 +03:00
ArgsUsage : "<keyFile>" ,
2016-03-17 14:09:18 +02:00
Description : `
2016-11-10 14:00:09 +03:00
geth account import < keyfile >
2016-03-17 14:09:18 +02:00
Imports an unencrypted private key from < keyfile > and creates a new account .
Prints the address .
The keyfile is assumed to contain an unencrypted private key in hexadecimal format .
The account is saved in encrypted format , you are prompted for a passphrase .
You must remember this passphrase to unlock your account in the future .
For non - interactive use the passphrase can be specified with the - password flag :
2017-04-28 13:38:49 +03:00
geth account import [ options ] < keyfile >
2016-03-17 14:09:18 +02:00
Note :
As you can directly copy your encrypted accounts to another ethereum instance ,
this import mechanism is not needed when you transfer an account between
nodes .
2016-11-10 14:00:09 +03:00
` ,
2016-03-17 14:09:18 +02:00
} ,
} ,
}
)
2016-06-10 11:23:00 +03:00
func accountList ( ctx * cli . Context ) error {
2017-04-12 17:27:23 +03:00
stack , _ := makeConfigNode ( ctx )
2017-02-07 13:47:34 +03:00
var index int
for _ , wallet := range stack . AccountManager ( ) . Wallets ( ) {
for _ , account := range wallet . Accounts ( ) {
2017-02-08 16:53:02 +03:00
fmt . Printf ( "Account #%d: {%x} %s\n" , index , account . Address , & account . URL )
2017-02-07 13:47:34 +03:00
index ++
}
2016-03-17 14:09:18 +02:00
}
2016-06-10 11:23:00 +03:00
return nil
2016-03-17 14:09:18 +02:00
}
// tries unlocking the specified account a few times.
2017-01-24 12:49:20 +03:00
func unlockAccount ( ctx * cli . Context , ks * keystore . KeyStore , address string , i int , passwords [ ] string ) ( accounts . Account , string ) {
account , err := utils . MakeAddress ( ks , address )
2016-03-17 14:09:18 +02:00
if err != nil {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "Could not list accounts: %v" , err )
2016-03-17 14:09:18 +02:00
}
for trials := 0 ; trials < 3 ; trials ++ {
prompt := fmt . Sprintf ( "Unlocking account %s | Attempt %d/%d" , address , trials + 1 , 3 )
password := getPassPhrase ( prompt , false , i , passwords )
2017-01-24 12:49:20 +03:00
err = ks . Unlock ( account , password )
2016-04-01 19:10:58 +03:00
if err == nil {
2017-02-22 17:58:00 +03:00
log . Info ( "Unlocked account" , "address" , account . Address . Hex ( ) )
2016-03-17 14:09:18 +02:00
return account , password
}
2017-01-24 12:49:20 +03:00
if err , ok := err . ( * keystore . AmbiguousAddrError ) ; ok {
2017-02-22 17:58:00 +03:00
log . Info ( "Unlocked account" , "address" , account . Address . Hex ( ) )
2017-01-24 12:49:20 +03:00
return ambiguousAddrRecovery ( ks , err , password ) , password
2016-04-01 19:10:58 +03:00
}
2017-01-24 12:49:20 +03:00
if err != keystore . ErrDecrypt {
2016-04-01 19:10:58 +03:00
// No need to prompt again if the error is not decryption-related.
break
}
2016-03-17 14:09:18 +02:00
}
// All trials expended to unlock account, bail out
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "Failed to unlock account %s (%v)" , address , err )
2017-02-22 15:10:07 +03:00
2016-03-03 02:09:16 +02:00
return accounts . Account { } , ""
2016-03-17 14:09:18 +02:00
}
// getPassPhrase retrieves the passwor associated with an account, either fetched
// from a list of preloaded passphrases, or requested interactively from the user.
func getPassPhrase ( prompt string , confirmation bool , i int , passwords [ ] string ) string {
// If a list of passwords was supplied, retrieve from them
if len ( passwords ) > 0 {
if i < len ( passwords ) {
return passwords [ i ]
}
return passwords [ len ( passwords ) - 1 ]
}
// Otherwise prompt the user for the password
2016-03-18 02:35:03 +02:00
if prompt != "" {
fmt . Println ( prompt )
}
2016-05-30 17:30:17 +03:00
password , err := console . Stdin . PromptPassword ( "Passphrase: " )
2016-03-17 14:09:18 +02:00
if err != nil {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "Failed to read passphrase: %v" , err )
2016-03-17 14:09:18 +02:00
}
if confirmation {
2016-05-30 17:30:17 +03:00
confirm , err := console . Stdin . PromptPassword ( "Repeat passphrase: " )
2016-03-17 14:09:18 +02:00
if err != nil {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "Failed to read passphrase confirmation: %v" , err )
2016-03-17 14:09:18 +02:00
}
if password != confirm {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "Passphrases do not match" )
2016-03-17 14:09:18 +02:00
}
}
return password
}
2017-01-24 12:49:20 +03:00
func ambiguousAddrRecovery ( ks * keystore . KeyStore , err * keystore . AmbiguousAddrError , auth string ) accounts . Account {
2016-04-01 19:10:58 +03:00
fmt . Printf ( "Multiple key files exist for address %x:\n" , err . Addr )
for _ , a := range err . Matches {
2017-01-24 12:49:20 +03:00
fmt . Println ( " " , a . URL )
2016-04-01 19:10:58 +03:00
}
fmt . Println ( "Testing your passphrase against all of them..." )
var match * accounts . Account
for _ , a := range err . Matches {
2017-01-24 12:49:20 +03:00
if err := ks . Unlock ( a , auth ) ; err == nil {
2016-04-01 19:10:58 +03:00
match = & a
break
}
}
if match == nil {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "None of the listed files could be unlocked." )
2016-04-01 19:10:58 +03:00
}
2017-01-24 12:49:20 +03:00
fmt . Printf ( "Your passphrase unlocked %s\n" , match . URL )
2016-04-01 19:10:58 +03:00
fmt . Println ( "In order to avoid this warning, you need to remove the following duplicate key files:" )
for _ , a := range err . Matches {
if a != * match {
2017-01-24 12:49:20 +03:00
fmt . Println ( " " , a . URL )
2016-04-01 19:10:58 +03:00
}
}
return * match
}
2016-03-17 14:09:18 +02:00
// accountCreate creates a new account into the keystore defined by the CLI flags.
2016-06-10 11:23:00 +03:00
func accountCreate ( ctx * cli . Context ) error {
2017-04-12 17:27:23 +03:00
stack , _ := makeConfigNode ( ctx )
2016-03-17 14:09:18 +02:00
password := getPassPhrase ( "Your new account is locked with a password. Please give a password. Do not forget this password." , true , 0 , utils . MakePasswordList ( ctx ) )
2017-02-07 13:47:34 +03:00
ks := stack . AccountManager ( ) . Backends ( keystore . KeyStoreType ) [ 0 ] . ( * keystore . KeyStore )
2017-01-24 12:49:20 +03:00
account , err := ks . NewAccount ( password )
2016-03-17 14:09:18 +02:00
if err != nil {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "Failed to create account: %v" , err )
2016-03-17 14:09:18 +02:00
}
2016-03-03 02:15:42 +02:00
fmt . Printf ( "Address: {%x}\n" , account . Address )
2016-06-10 11:23:00 +03:00
return nil
2016-03-17 14:09:18 +02:00
}
// accountUpdate transitions an account from a previous format to the current
// one, also providing the possibility to change the pass-phrase.
2016-06-10 11:23:00 +03:00
func accountUpdate ( ctx * cli . Context ) error {
2016-03-17 14:09:18 +02:00
if len ( ctx . Args ( ) ) == 0 {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "No accounts specified to update" )
2016-03-17 14:09:18 +02:00
}
2017-04-12 17:27:23 +03:00
stack , _ := makeConfigNode ( ctx )
2017-02-07 13:47:34 +03:00
ks := stack . AccountManager ( ) . Backends ( keystore . KeyStoreType ) [ 0 ] . ( * keystore . KeyStore )
2017-01-24 12:49:20 +03:00
2017-04-28 13:38:49 +03:00
for _ , addr := range ctx . Args ( ) {
account , oldPassword := unlockAccount ( ctx , ks , addr , 0 , nil )
newPassword := getPassPhrase ( "Please give a new password. Do not forget this password." , true , 0 , nil )
if err := ks . Update ( account , oldPassword , newPassword ) ; err != nil {
utils . Fatalf ( "Could not update the account: %v" , err )
}
2016-03-17 14:09:18 +02:00
}
2016-06-10 11:23:00 +03:00
return nil
2016-03-17 14:09:18 +02:00
}
2016-06-10 11:23:00 +03:00
func importWallet ( ctx * cli . Context ) error {
2016-03-17 14:09:18 +02:00
keyfile := ctx . Args ( ) . First ( )
if len ( keyfile ) == 0 {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "keyfile must be given as argument" )
2016-03-17 14:09:18 +02:00
}
keyJson , err := ioutil . ReadFile ( keyfile )
if err != nil {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "Could not read wallet file: %v" , err )
2016-03-17 14:09:18 +02:00
}
2017-04-12 17:27:23 +03:00
stack , _ := makeConfigNode ( ctx )
2016-03-17 14:09:18 +02:00
passphrase := getPassPhrase ( "" , false , 0 , utils . MakePasswordList ( ctx ) )
2017-01-24 12:49:20 +03:00
2017-02-07 13:47:34 +03:00
ks := stack . AccountManager ( ) . Backends ( keystore . KeyStoreType ) [ 0 ] . ( * keystore . KeyStore )
2017-01-24 12:49:20 +03:00
acct , err := ks . ImportPreSaleKey ( keyJson , passphrase )
2016-03-17 14:09:18 +02:00
if err != nil {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "%v" , err )
2016-03-17 14:09:18 +02:00
}
2016-03-03 02:15:42 +02:00
fmt . Printf ( "Address: {%x}\n" , acct . Address )
2016-06-10 11:23:00 +03:00
return nil
2016-03-17 14:09:18 +02:00
}
2016-06-10 11:23:00 +03:00
func accountImport ( ctx * cli . Context ) error {
2016-03-17 14:09:18 +02:00
keyfile := ctx . Args ( ) . First ( )
if len ( keyfile ) == 0 {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "keyfile must be given as argument" )
2016-03-17 14:09:18 +02:00
}
2016-04-05 02:08:50 +03:00
key , err := crypto . LoadECDSA ( keyfile )
if err != nil {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "Failed to load the private key: %v" , err )
2016-04-05 02:08:50 +03:00
}
2017-04-12 17:27:23 +03:00
stack , _ := makeConfigNode ( ctx )
2016-03-17 14:09:18 +02:00
passphrase := getPassPhrase ( "Your new account is locked with a password. Please give a password. Do not forget this password." , true , 0 , utils . MakePasswordList ( ctx ) )
2017-01-24 12:49:20 +03:00
2017-02-07 13:47:34 +03:00
ks := stack . AccountManager ( ) . Backends ( keystore . KeyStoreType ) [ 0 ] . ( * keystore . KeyStore )
2017-01-24 12:49:20 +03:00
acct , err := ks . ImportECDSA ( key , passphrase )
2016-03-17 14:09:18 +02:00
if err != nil {
2017-02-22 18:22:50 +03:00
utils . Fatalf ( "Could not create the account: %v" , err )
2016-03-17 14:09:18 +02:00
}
2016-03-03 02:15:42 +02:00
fmt . Printf ( "Address: {%x}\n" , acct . Address )
2016-06-10 11:23:00 +03:00
return nil
2016-03-17 14:09:18 +02:00
}