Accounts

Interacting with Ethereum via Accounts

written by
Chris Cao
Date Last Updated
June 24, 2022
Download Project Files

One of the most important tools ape can provide is a way to create or import an account to interact with Ethereum.

Ape Accounts

One of the most important tools ape can provide is a way to create or import an account to interact with Ethereum.

You can think of an account like a wallet.

The purpose of an account is to interact with a contract. It has certain key features that make it useful. Let us dive into what it is and why you should use it.

An account is a wallet with a public address and a private key. It can be created through ape or imported using your private key.

An account is used to sign messages or transactions.

With the creation of an account. You will be prompted to create a private key based on password to make it encrypted. Only you know this password and it is not stored anywhere. With the a fresh account, you can view the public address and have access to certain commands.

Why do we not store api keys, account passwords in a .env

Storing your passwords or sensitive data is always up for debate. ApeWorX has decided that storing storing api keys and passwords in an .env file is nice, but you run the possibility of accidentally pushing the .env to a repository(human error).

Therefore the another layer of security to help mitigate this issue is to only store it in a ~/.bashrc file in Linux or a ~/.zshrc for Mac.

You are unlikely to accidentally push this file to a repository since it is hidden by default.

Ape Accounts private keys generated and stored as an encrypted key file with AES-128 algorithm. In summary to access your wallet, you must enter a 2 Factor Authentication

You can read how we generate the private key here in Ape Core. Note the Create method uses eth_account.

Hot Tip for Significant Funds storing:

For sign fig funds we would recommend to use hardware wallets. We have ape-ledger and ape-trezor to access your funds via a cold storage/ hardware wallet.

Lets create a wallet!

Tutorial on Accounts in action: 

Step 1: Requirements

Verify or Create a network tool API key

To access a blockchain, you should use a remote access provider such as Alchemy or Infura.

  • For the purposes of this tutorial. I will use Alchemy.
  • Follow the steps from the network provider to get an API key
  • Store your API key in an environment variable
  • Store the Environment Variable in local file ~/.bashrc (Linux) or ~/.zshrc (Mac)

Store your API KEY

Open your ~/.bashrc with a code editor or in terminal (vim)

Paste export WEB3_ALCHEMY_PROJECT_ID=ABCEFGHIJ69420BLAHBLAHBLAH

Save ~/.bashrc (escape key and :wq)

Source ~/.bashrc


$ vim ~/.bashrc
$ export WEB3_ALCHEMY_PROJECT_ID=123asld
$ source ~/.bashrc

This should load your environment variable every time you launch your bash shell!

You can check that you have your WEB3_ALCHEMY_PROJECT_ID available and returns the correct project ID. It should match your project ID on alchemy.


$ echo $WEB3_ALCHEMY_PROJECT_ID

Step 2: Create or Load an Address wallet with Eth 

This is how you create an account


$ ape accounts generate "test"
$ Add extra entropy for key generation...:
$ Create Passphrase:
$ Repeat for confirmation:
$ SUCCESS: A new account '0x1234567890ABCDEFGHiJKlmNOPQRSTUV2WXYz123456' has been added with the id 'test'

This is how you can import an account


$ ape accounts import demo_import
Enter Private Key:

Step 3: Verify it is your created account

Once you create your account you can verify your account with metamask and/or etherscan

Launch ape console and check the balance of the account. It should match the numbers on metamask or etherscan.

NOTE: The new wallet does not come with testnet eth. You need eth to sign messages, deploy contracts, or transfer tokens.


$ ape accounts list
$ test = accounts.load("test")
$ test.balance
$ test.balance / 1e18

Step 4: OPTIONAL Add Eth to your wallet via Testnet Faucet

The faucet is a web-based service that provides free tokens to users of the testnet who are running a stake pool or other node. The tokens enable users to experiment with network features without spending cryptocurrency on the mainnet.

Here are some links to added eth to your wallet:

Once you added testnet eth to your wallet. You should verify it is there.

That is it! The next steps are to deploy a contract with your testnet eth.

Hot Tip: You can automate lookup of your wallet with .eth namespace with ape-ens plugin

You can use this to find your address to import.

You don't need ape-ens to check balance locally. 


In [1]: from ape.types import AddressType
In [2]: convert("vitalik.eth", AddressType)
Out[2]: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'

The next portion of the tutorial is the explanation of the tech docs of Accounts.

Ape account CLI Commands

change-password

Change the password of an existing account


$ accounts change-password [OPTIONS]

delete

Delete an existing account


$ accounts delete [OPTIONS]

generate

Create a new keyfile account with a random private key


$ ape accounts generate "ALIAS"

import

Add a new keyfile account by entering a private key


$ accounts import [OPTIONS] ALIAS

list

List available local accounts

Options: --all


$ accounts list [OPTIONS]

Besides the CLI Commands, you are allowed to automated the interaction of the account with the python modules.

Accounts Python Modules

You can launch ape console to interact with your account.

Once you have an understanding of how to interact. You can write python scripts that use these methods.

  • dir(accounts) : List the methods
  • Dev.deploy(project_name) : deploys contract REQUIRES eth
  • dev.call : calls a transaction
  • dev.transfer("reciever_address", token_amount), sender=ALIAS: 
  • dev.balance("ALIAS")

REFERENCE

What is the difference between CLI and Python

  • CLI is how to interact with an account in a live environment via IPython
  • Python Modules are Internal classes for plugins. You can utilize them in scripts

Thank you for reading and watching!

SUBMIT YOUR PROJECTGET HELPLEAVE FEEDBACKAPE ACADEMY GITHUB
SHARE TUTORIAL