Learn EOS|EOS command-line programs and tools together with super-detailed practical exercises (with code)

image

1

wallet

Let's experience the EOS wallet first.

As mentioned in the previous article, the EOS wallet is read by the keosd process to the local wallet file, and then uses cleos to interact with the keosd process.

First, we go to the directory where keosd is located.

➜  programs git:(master) ✗ cd keosd
➜  keosd git:(master) ✗ ll
total 11576drwxr-xr-x  5 joe  staff   160B  4  8 00:32 CMakeFiles
-rw-r--r--  1 joe  staff   297B  4  8 00:29 CTestTestfile.cmake
-rw-r--r--  1 joe  staff   8.6K  4  8 00:29 Makefile
-rw-r--r--  1 joe  staff   1.8K  4  8 00:29 cmake_install.cmake
-rwxr-xr-x  1 joe  staff   5.6M  4  8 00:42 keosd
➜  keosd git:(master) ✗ pwd
/Users/joe/Workspace/eos/build/programs/keos

The executable program keosd can be seen, let's run it:

➜  keosd git:(master) ✗ ./keosd1306994ms thread-0   wallet_plugin.cpp:41          plugin_initialize    ] initializing wallet plugin1306994ms thread-0   http_plugin.cpp:141           plugin_initialize    ] host: 127.0.0.1 port: 88881306994ms thread-0   http_plugin.cpp:144           plugin_initialize    ] configured http to listen on 127.0.0.1:88881306994ms thread-0   http_plugin.cpp:213           plugin_startup       ] start listening for http requests1306995ms thread-0   wallet_api_plugin.cpp:70      plugin_startup       ] starting wallet_api_plugin1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/create1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/get_public_keys1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/import_key1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/list_keys1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/list_wallets1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/lock1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/lock_all1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/open1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/set_timeout1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/sign_transaction1306995ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/unlock

The operation was successful. We carefully observed the output date and found that keosd listened to the local port 8888, added some handlers, and printed out the api url of each handler.

So I understood that the background process of keosd provides us with access to the RESTful API. cleos also interacts with keosd through the RESTful API based on the http protocol. 

Next, let's get some intimacy with keosd.

When keosd runs, it will create an eosio-wallet folder in the current user's home directory by default. There is a config.ini file at the bottom of the folder:

➜  keosd git:(master) ✗ ls ~/eosio-wallet
config.ini
➜  keosd git:(master) ✗ cat ~/eosio-wallet/config.ini
# The local IP and port to listen for incoming http connections. (eosio::http_plugin)
http-server-address = 127.0.0.1:8888# Specify the Access-Control-Allow-Origin to be returned on each request. (eosio::http_plugin)
# access-control-allow-origin =

# Specify the Access-Control-Allow-Headers to be returned on each request. (eosio::http_plugin)
# access-control-allow-headers =

# Specify if Access-Control-Allow-Credentials: true should be returned on each request. (eosio::http_plugin)access-control-allow-credentials = false

# The path of the wallet files (absolute path or relative to application data dir) (eosio::wallet_plugin)
wallet-dir = "."# Timeout for unlocked wallet in seconds. Wallets will automatically lock after specified number of seconds of inactivity. Activity is defined as any wallet command e.g. list-wallets. (eosio::wallet_plugin)
# unlock-timeout =

# eosio key that will be imported automatically when a wallet is created. (eosio::wallet_plugin)
# eosio-key =

# Plugin(s) to enable, may be specified multiple times
# plugin =

config.ini , is the keods configuration file, which can configure the access address, wallet data storage address, etc.

See the default configuration of the configuration file above

wallet-dir = "."

That is to say, when we create a wallet, the wallet data is stored in the same directory as config.ini .

Let's create a wallet to verify it:

First create a wallet: 

➜  keosd git:(master) ✗ cd ../cleos
➜  cleos git:(master) ✗ ./cleos wallet create -n 'jc1991wallet'Creating wallet: jc1991walletSave password to use in the future to unlock this wallet.Without password imported keys will not be retrievable."PW5K9B4aHLkfB55ck69PGHFrq9ej31ZhRSWftboSW8qRhptAntV2v"

Note that cleos is used to interact with keosd

  • From the directory where keosd is located, enter the directory where cleos is located

  • Created a wallet called jc1991wallet

  • Take a closer look at the output log, except that it tells us that the jc1991wallet wallet was created successfully. It also warns us to keep the password well. If you lose this wallet password, it is impossible to retrieve the private key saved in the wallet in the future!

  • The last line of the log is the password of the wallet. It is very, very important, let me copy it and save it in a place first, so as not to lose it.

I will save this wallet password here:

PW5K9B4aHLkfB55ck69PGHFrq9ej31ZhRSWftboSW8qRhptAntV2v

After creating the wallet, let's quickly see what's in the eos-wallet  directory.

➜  cleos git:(master) ✗ ls ~/eosio-wallet
config.ini          jc1991wallet.wallet
➜  cleos git:(master) ✗ cat ~/eosio-wallet/jc1991wallet.wallet
{
  "cipher_keys": "53df705d7eae43113c27af0359268a8f60f6cb246d64ced3b745a36c3549cfd08b3a9923ec346e9a8d904e81ca2a322a8b139ec6f2c709306298c9fa056bae2af13c15502971ccc779549280ffbbbb35c1854064471c899f0265a26cab3a3fdcb0c5540220a3d8d11c567ee99519746971b38581e586c5751835e87f45d4cf44e4f46a9a0afa0159042421ebb584524a"
}%

As you can see, there is an additional *jc1991wallet.wallet*  file, you can see by looking at the content inside, this is an encrypted file of the wallet content, remember to backup and save it often.

According to popular science, in the absence of a password, it  is almost impossible to crack the private key information stored in it according to the content of the file *jc1991wallet.wallet* . That's why the wallet password cannot be lost.

jc1991wallet.wallet  can be regarded as a small database that keeps your EOS private key. No one can access the private key inside without the wallet password.

Next, take a look at the wallet we created:

➜  cleos git:(master) ✗ ./cleos wallet listWallets:[  "jc1991wallet *"]

cleos wallet list can list all wallets.

Notice, what does the number after "jc1991wallet  mean?

It means that the wallet is unlocked. When the wallet is first created, it is unlocked by default. Let's try to lock the wallet:

➜  cleos git:(master) ✗ ./cleos wallet lock  -n jc1991walletLocked: jc1991wallet
➜  cleos git:(master) ✗ ./cleos wallet listWallets:
[  "jc1991wallet"]

After running cleos wallet lock -n jc1991wallet, it shows that the wallet is locked. When I checked it again, the * was gone. At this point the wallet is locked.

Next, let's unlock the wallet again: unlocking the wallet requires the password given to us when we created the wallet.

➜  cleos git:(master) ✗ ./cleos wallet unlock  -n jc1991walletpassword:

After running cleos wallet unlock -n jc1991wallet, the program waits for us to enter the wallet password, I deliberately entered the wrong password to see what happens:

➜  cleos git:(master) ✗ ./cleos wallet unlock  -n jc1991walletpassword: Error 3140005: Invalid wallet passwordAre you sure you are using the right password?Error Details:
Invalid password for wallet: "/Users/joe/eosio-wallet/./jc1991wallet.wallet"error during aes 256 cbc decrypt final

Gorgeous error, don't know the password and want to open the wallet? no way!

Next I enter the correct password:

➜  cleos git:(master) ✗ ./cleos wallet unlock  -n jc1991walletpassword: Unlocked: jc1991wallet
➜  cleos git:(master) ✗ ./cleos wallet listWallets:
[  "jc1991wallet *"]

Unlocked successfully! ! When checking the wallet list again, * comes back, and the wallet is unlocked at this time. We can perform various operations on the wallet!

Do some extra exploration:

Before going to the next actual combat, I really want to be naughty and close the keosd process to see what happens?

➜  cleos git:(master) ✗ ./cleos wallet listWallets:Failed to connect

Unsurprisingly, the prompt is not available. The keosd process has been closed by me, and the http request naturally cannot be reached.

Next I restarted the keosd process.

➜  keosd git:(master) ✗ ./keosd1036886ms thread-0   wallet_plugin.cpp:41          plugin_initialize    ] initializing wallet plugin1036886ms thread-0   http_plugin.cpp:141           plugin_initialize    ] host: 127.0.0.1 port: 88881036886ms thread-0   http_plugin.cpp:144           plugin_initialize    ] configured http to listen on 127.0.0.1:88881036887ms thread-0   http_plugin.cpp:213           plugin_startup       ] start listening for http requests1036887ms thread-0   wallet_api_plugin.cpp:70      plugin_startup       ] starting wallet_api_plugin1036887ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/create1036887ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/get_public_keys1036887ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/import_key1036887ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/list_keys1036887ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/list_wallets1036887ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/lock1036887ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/lock_all1036887ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/open1036887ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/set_timeout1036893ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/sign_transaction1036893ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/unlock

Start the keosd process, and how about the wallet list?

➜  cleos git:(master) ✗ ./cleos wallet listWallets:[]

I don't know if I don't see it, I'm startled when I see it, where is my wallet? What about the wallet? What about the bag? ! ! Why is the array empty?

Don't panic, it turns out that before using the wallet, there is an additional operation: then open the wallet (it can also be understood as loading the wallet file)

➜  cleos git:(master) ✗ ./cleos wallet open -n jc1991walletOpened: jc1991wallet
➜  cleos git:(master) ✗ ./cleos wallet listWallets:[  "jc1991wallet"]

After running cleos wallet open -n jc1991wallet, check the jc1991wallet wallet again, but the * sign is gone. It means that the wallet is locked at this time. Before proceeding further, you need to unlock:

➜  cleos git:(master) ✗ ./cleos wallet unlock  -n jc1991walletpassword: Unlocked: jc1991wallet

With an unlocked wallet, let's store some private keys in the wallet!

2

Pour the EOS private key into the wallet

First look at the key in the wallet:

➜  cleos git:(master) ✗ ./cleos wallet keys[[    "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",    "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
  ]
]

After running cleos wallet keys, the keys of all currently unlocked wallets are listed.

Some people may have doubts, obviously the key has not been created, nor has any key been stored in the wallet. Where did this list come from?

Good question, this key is the key of the super account dedicated to our developers in the EOS version (super intimate, the previous version is to apply for it by yourself, trouble to die.). Later we will create the account, which will be used when deploying the smart contract . Here we skip it for now and get back to the point.

Before pouring the private key into the wallet, let's create one,

➜  cleos git:(master) ✗ ./cleos create keyPrivate key: 5JwmoZrb9zTEQSt6xS3oJzk2ihho9W8bjZda6ZASN2NTUzHxt43Public key: EOS5KEYfYrQQHbqx2GqqGMFLeqywSj2TWig4dQLJMhyHHEmWSK7jt

After running cleos create key, a key pair is created. Look at the output. The above line is the private key. The private key is carefully stored and not leaked. We will import it into our wallet.

The next line is the public key. The public key can be made public. The public key is prefixed with "EOS" don't get me wrong.

Next, import the private key into the wallet:

➜  cleos git:(master) ✗ ./cleos wallet import -n jc1991wallet  5JwmoZrb9zTEQSt6xS3oJzk2ihho9W8bjZda6ZASN2NTUzHxt43
imported private key for: EOS5KEYfYrQQHbqx2GqqGMFLeqywSj2TWig4dQLJMhyHHEmWSK7jt

Run cleos wallet import -n jc1991wallet , we imported the private key into the jc1991wallet wallet, the import was successful, and the public key address was printed.

The public key can be deduced from the private key  . This needs to be remembered, and the exam needs to be tested. So we just imported the private key into the wallet. The wallet can deduce the public key of this private key through an algorithm: Let's take a look:

➜  cleos git:(master) ✗ ./cleos wallet keys
[[    "EOS5KEYfYrQQHbqx2GqqGMFLeqywSj2TWig4dQLJMhyHHEmWSK7jt",    "5JwmoZrb9zTEQSt6xS3oJzk2ihho9W8bjZda6ZASN2NTUzHxt43"
  ],[    "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",    "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
  ]
]

In the keys list, a new pair of keys is added, which is the pair of keys we imported into the wallet above. The wallet only saves the private key, and the printed public key is calculated from the private key.

3

Summarize

In this article, we did the following:

  • Start the keosd wallet process

  • browsed the ~/eos-wallet  directory created by the keosd wallet process 

  • Use the cleos command line tool to interact with the keosd wallet process.

  • Created the first wallet, named jc1991wallet, and blatantly hid the wallet's password in this article. (Don't learn anything, you must keep your wallet password well.)

  • Next, I tried to unlock and lock the jc1991wallet wallet.

  • Close the keosd wallet process, and then open it again. It is found that jc1991wallet is missing, and it is handed over. Before using the wallet, you need to open it first.

  • A pair of keys is created.

  • Import the private key in keys into the jc1991wallet wallet.

The author of this article: HiBlock blockchain community "write notes together" small partner - jc1991

The original text was first published on Bihu

The following is the introduction of our community, all kinds of cooperation, exchanges and learning are welcome:)

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325922699&siteId=291194637