K
K
Klaytn Docs
Search…
Managing Accounts

List Your Accounts

This will return the list of all accounts created under the data directory.

ken

From the command line, call the CLI with:
1
$ ken account list --datadir <DATADIR>
2
$ ken account list --datadir ~/kend_home
3
Account #0: {bfc22a57999459b0c2ce6337deb9287e7a970e02} keystore:///Users/username/kend_home/keystore/UTC--2019-03-26T07-02-58.524962000Z--bfc22a57999459b0c2ce6337deb9287e7a970e02
4
Account #1: {47bd2e9565cbe1789454718d6cf1778d7ea557aa} keystore:///Users/username/kend_home/keystore/UTC--2019-03-26T07-04-44.840061000Z--47bd2e9565cbe1789454718d6cf1778d7ea557aa
Copied!
NOTE: This order of returned account list can change if you copy keystore files from other nodes or remove the files. Therefore, make sure you either do not rely on the index or make sure if you copy or remove keystore files you check and update your account indexes in your scripts.

JavaScript Console

When using the console:
1
> klay.accounts
2
["bfc22a57999459b0c2ce6337deb9287e7a970e02", "47bd2e9565cbe1789454718d6cf1778d7ea557aa"]
Copied!

Unlock Accounts

If you want to use an account non-interactively, you need to unlock it.

ken

You can unlock accounts and start the EN on the command line with the --unlock "{address},{address}" option which takes a comma-separated list of accounts (in hex or index) as an argument so you can unlock the accounts programmatically for one session. This is useful if you want to use your account from BApps via RPC. --unlock will unlock the first account in the list. This is useful when you created your account programmatically, you do not need to know the actual account to unlock it.
Create an account and start a node with the account unlocked:
1
$ ken account new --password <(echo this is not secret) --datadir <DATADIR>
2
$ ken --password <(echo "this is not secret") --unlock primary --datadir <DATADIR> --rpccorsdomain localhost --verbosity 6 2>> log.log
Copied!
If you want to start a node with a specific account unlocked, you can use an address or an index which refers to the address position in the account list (and corresponds to the order of creation).
1
$ ken --unlock "0" --datadir <DATADIR>
2
$ ken --unlock "2" --datadir <DATADIR>
3
$ ken --unlock "bfc22a57999459b0c2ce6337deb9287e7a970e02" --datadir <DATADIR>
Copied!
The command line allows you to unlock multiple accounts. In this case, the argument to unlock is a comma-separated list of account addresses or indexes.
1
$ ken --unlock "0x407d73d8a49eeb85d32cf465507dd71d507100c1,0,5,e470b1a7d2c9c5c6f03bbaa8fa20db6d404a0c32" --datadir <DATADIR>
Copied!
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.

JavaScript Console

On the console you can also unlock accounts (one at a time) for a duration (in seconds).
1
> personal.unlockAccount(address, "password", 300)
Copied!
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.

Check Account Balance

ken

n/a

JavaScript Console

To check your account balance:
1
> klay.fromPeb(klay.getBalance("{account}"), "KLAY")
2
6.5
Copied!
Print all balances with a JavaScript function:
1
function checkAllBalances() {
2
var totalBal = 0;
3
for (var acctNum in klay.accounts) {
4
var acct = klay.accounts[acctNum];
5
6
var acctBal = klay.fromPeb(klay.getBalance(acct), "KLAY");
7
totalBal += parseFloat(acctBal);
8
9
console.log("klay.accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + "KLAY");
10
11
}
12
13
console.log("Total balance: " + totalBal + " KLAY");
14
};
Copied!
That can then be executed with:
1
> checkAllBalances();
2
klay.accounts[0]: 0xd1ade25ccd3d550a7eb532ac759cac7be09c2719 balance: 63.11848 KLAY
3
klay.accounts[1]: 0xda65665fc30803cb1fb7e6d86691e20b1826dee0 balance: 0 KLAY
4
klay.accounts[2]: 0xe470b1a7d2c9c5c6f03bbaa8fa20db6d404a0c32 balance: 1 KLAY
5
klay.accounts[3]: 0xf4dd5c3794f1fd0cdc0327a83aa472609c806e99 balance: 6 KLAY
Copied!
Since this function will disappear after restarting ken, it can be helpful to store commonly used functions to be called later.
First, save the checkAllBalances() function definition to a file on your computer. For example, /Users/username/klayload.js. Then load the file from the interactive console:
1
> loadScript("/Users/username/klayload.js")
2
true
Copied!
The file will modify your JavaScript environment as if you have typed the commands manually. Feel free to experiment!
Last modified 1mo ago