Listing accounts and checking balances

Listing your current accounts

From the command line, call the CLI with:

$ geth account list
Account #0: {d1ade25ccd3d550a7eb532ac759cac7be09c2719}
Account #1: {da65665fc30803cb1fb7e6d86691e20b1826dee0}
Account #2: {e470b1a7d2c9c5c6f03bbaa8fa20db6d404a0c32}
Account #3: {f4dd5c3794f1fd0cdc0327a83aa472609c806e99}

to list your accounts in order of creation.

Note: This order can change if you copy keyfiles from other nodes, so make sure you either do not rely on indexes or make sure if you copy keys you check and update your account indexes in your scripts.

When using the console:

> eth.accounts
['0x407d73d8a49eeb85d32cf465507dd71d507100c1']

or via RPC:

# Request
$ curl -X POST --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1} http://127.0.0.1:8545'

# Result
{
  "id":1,
  "jsonrpc": "2.0",
  "result": ["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]
}

If you want to use an account non-interactively, you need to unlock it. You can do this on the command line with the --unlock option which takes a whitespace separated list of accounts (in hex or index) as argument so you can unlock the accounts programmatically for one session. This is useful if you want to use your account from Dapps via RPC. --unlock will unlock the first account. This is useful when you created your account programmatically, you do not need to know the actual account to unlock it.

Unlocking one account:

geth --password <(echo this is not secret!) account new 
geth --password <(echo this is not secret!) --unlock primary --rpccorsdomain localhost --verbosity 6 2>> geth.log

Instead of the account address, you can use integer indexes which refers to the address position in the account listing (and corresponds to order of creation)

The command line allows you to unlock multiple accounts. In this case the argument to unlock is a whitespace delimited list of accounts addresses or indexes.

geth --unlock "0x407d73d8a49eeb85d32cf465507dd71d507100c1 0 5 e470b1a7d2c9c5c6f03bbaa8fa20db6d404a0c32"

If this construction is used non-interactively, your password file will need to contain the respective passwords for the accounts in question, one per line.

On the console you can also unlock accounts (one at a time).

personal.unlockAccount(address, "password")

Note that we do NOT recommend using the password argument here, since the console history is logged, so you may compromise your account. You have been warned.

Checking account balances

To check your the etherbase account balance:

> web3.fromWei(eth.getBalance(eth.coinbase), "ether")
6.5

Print all balances with a JavaScript function:

function checkAllBalances() { 
var i =0; 
eth.accounts.forEach( function(e){
     console.log("  eth.accounts["+i+"]: " +  e + " \tbalance: " + web3.fromWei(eth.getBalance(e), "ether") + " ether"); 
i++; 
})
};

That can then be executed with:

> checkAllBalances();
  eth.accounts[0]: 0xd1ade25ccd3d550a7eb532ac759cac7be09c2719     balance: 63.11848 ether
  eth.accounts[1]: 0xda65665fc30803cb1fb7e6d86691e20b1826dee0     balance: 0 ether
  eth.accounts[2]: 0xe470b1a7d2c9c5c6f03bbaa8fa20db6d404a0c32     balance: 1 ether
  eth.accounts[3]: 0xf4dd5c3794f1fd0cdc0327a83aa472609c806e99     balance: 6 ether

Since this function will disappear after restarting geth, it can be helpful to store commonly used functions to be recalled later. The loadScript function makes this very easy.

First, save the checkAllBalances() function definition to a file on your computer. For example, /Users/username/gethload.js. Then load the file from the interactive console:

> loadScript("/Users/username/gethload.js")
true

The file will modify your JavaScript environment as if you has typed the commands manually. Feel free to experiment!