5/5
## Encrypting a Private Key We've learned how to encrypt our keys back when we were using Python, but Mocassin has its own way to handle encryption. We can find it in the documentation under the **Wallet** section. We'll use the `wallet import ACCOUNT_NAME` command to create a keystore file in the default keystore directory. The command will then prompt us to enter our private key and password. The example command is as follows: ```bash mox wallet import my_account ``` Let's look at the different commands available to us in the `wallet` section: ```bash mox wallet --help ``` This shows a list of commands, including: * `list (ls)`: This will list all of our accounts. * `generate (g, new)`: This will create a new account with a random private key. * `import (i, add)`: This will import a private key into an encrypted keystore. * `view (v)`: This will view the JSON of a keystore file. * `decrypt (dk)`: This will decrypt a keystore file to get the private key. * `delete (d)`: This will delete a keystore file. We'll use the `import` command to encrypt our key. To do this, we'll copy the private key for the Anvil account and run this command in our terminal: ```bash mox wallet import anvil1 ``` The terminal will then ask us to enter our private key. We'll paste the copied key into the terminal and press Enter. The terminal will then ask us to enter a password to encrypt our key. We'll enter a password and confirm it. The terminal will then show: ```bash Saved account anvil1 to keystores! ``` This will save the encrypted account to our keystore. Now, if we want to view the accounts we have, we can run: ```bash mox wallet list ``` The terminal will show us all accounts we have saved in our keystore. We can also use the `view` command to see the JSON of a specific keystore. For example: ```bash mox wallet view anvil1 ``` This will show the JSON for the `anvil1` keystore. We'll see the address of the account and the encrypted key.
We've learned how to encrypt our keys back when we were using Python, but Mocassin has its own way to handle encryption.
We can find it in the documentation under the Wallet section.
We'll use the wallet import ACCOUNT_NAME
command to create a keystore file in the default keystore directory.
The command will then prompt us to enter our private key and password.
The example command is as follows:
Let's look at the different commands available to us in the wallet
section:
This shows a list of commands, including:
list (ls)
: This will list all of our accounts.
generate (g, new)
: This will create a new account with a random private key.
import (i, add)
: This will import a private key into an encrypted keystore.
view (v)
: This will view the JSON of a keystore file.
decrypt (dk)
: This will decrypt a keystore file to get the private key.
delete (d)
: This will delete a keystore file.
We'll use the import
command to encrypt our key. To do this, we'll copy the private key for the Anvil account and run this command in our terminal:
The terminal will then ask us to enter our private key. We'll paste the copied key into the terminal and press Enter.
The terminal will then ask us to enter a password to encrypt our key. We'll enter a password and confirm it. The terminal will then show:
This will save the encrypted account to our keystore.
Now, if we want to view the accounts we have, we can run:
The terminal will show us all accounts we have saved in our keystore.
We can also use the view
command to see the JSON of a specific keystore. For example:
This will show the JSON for the anvil1
keystore. We'll see the address of the account and the encrypted key.
A comprehensive guide to encrypting a private key using the Moccasin wallet command-line interface. The lesson covers importing a private key into an encrypted keystore, viewing the JSON of a keystore file, and listing the accounts in the keystore directory.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 10, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 10, 2025