# Accounts

## What is account?

An account in POTOS is an entity interacting with the blockchain network. The main operations involved in an account include deploying smart contracts, calling deployed smart contracts and transferring funds to other accounts.

## Account data structure

An account consists of the following fields:

- **nonce:** The sequential counter that records the number of transactions created by an **externally-owned account(EOA)** or the number of contracts created by a contract account. The nonce is crucial for preventing transaction replay attacks, ensuring that each transaction is unique and can only be executed once per account.
- **balance:** The number of native utility token owned by this account.
- **code:** Each account has a code field that contains the EVM bytecode of the account. For contract accounts, this is the code that gets executed when the account receives a message call. For externally owned accounts, this field is empty.
- **codeHash:** The cryptographic hash that references the `code` field.

## Types of Accounts

POTOS mainly includes two types of accounts:

- **Externally-Owned Account (EOA):** These accounts are controlled by individuals holding the corresponding private keys.
- **Contract Account:** A smart contract deployed to the blockchain network, controlled by the contract code.

Both account types have the ability to:

- Receive, hold and send tokens
- Interact with deployed smart contracts

Here's a concise table summarizing the differences between Externally-Owned Accounts (EOAs) and Contract Accounts:

|                            | EOA                                                  | Contract Account                                          |
|----------------------------|------------------------------------------------------|-----------------------------------------------------------|
| **Controller**                | Controlled by private key holders                    | Controlled by smart contract code                         |
| **Creation Cost**          | No cost                                              | Incurs cost caused by utilizing the blockchain resources                  |
| **Transaction Initiation** | Can initiate transactions                            | Can not initiate transactions                             |
| **Key Pair**               | Consists of public and private keys                  | Does not possess private keys; governed by contract logic |
| **Example Use Case**       | Personal or organizational accounts for transactions | Deploying and interacting with smart contracts            |

## EOA and key pairs

EOA accounts hold key pairs consisting of a public keys and a private keys.

The private key is used to sign transactions, so it grants you custody over the funds associated with your account. It is important to note that you do not own cryptocurrency; instead, you own private keys, with the funds residing on the POTOS's ledger.

The public key is generated from the private key using the [Elliptic Curve Digital Signature Algorithm (ECDSA)](https://wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm).

The address for the account is generated by taking the last 20 bytes of the Keccak-256 hash of the public key, prefixed with 0x.

This results in an EOA having a 42-character address (a 20-byte segment, which translates to 40 hexadecimal characters, plus the 0x prefix).

It is important to note that while new public keys can be derived from a private key, the reverse is not possible. Therefore, it is imperative to safeguard private keys and maintain their confidentiality.

This cryptographic mechanism prevents malicious entities from broadcasting counterfeit transactions, as the sender of a transaction can always be authenticated.

## EOA Account creation

When creating an account, most libraries will generate a random private key for you. A private key consists of 64 hexadecimal characters and can be encrypted with a passphrase for enhanced security.

### Manually creating an account

There are various ways to create an account, such as using the `openssl` command or a JavaScript library. The following is an example of how to create a key pair using the `openssl` command:

```bash
$ openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout

read EC key
Private-Key: (256 bit)
priv:
    ed:41:ac:9a:4a:2f:43:57:89:92:c7:52:40:9d:a2:
    8f:05:95:fe:26:62:6c:70:60:e1:03:30:c0:d7:65:
    1d:4c
pub:
    04:ed:b5:47:a9:a1:75:2a:ec:93:17:3d:1b:dc:e9:
    33:d1:08:9e:7e:92:24:cb:91:83:0c:2c:fd:d8:83:
    ad:39:1d:38:97:c0:78:8f:a2:0c:88:10:31:f9:20:
    57:5d:56:ea:9c:c1:d2:92:db:d1:07:00:7f:6f:58:
    90:90:80:90:e1
ASN1 OID: secp256k1
```

And the `ed41ac9a4a2f43578992c752409da28f0595fe26626c7060e10330c0d7651d4c` is your private key.

For more details, you can refer to the [Generating the EC private key](https://kobl.one/blog/create-full-ethereum-keypair-and-address)

### Using crypto wallets

You can also create an account using crypto wallets like MetaMask. These wallets generate a private key for you, which you can use to access your account.

You can check this [link](../developer/wallet_usage.md) to use MetaMask to create an account.

## Contract accounts creation

Contract accounts also have a 42 character hexadecimal address:

The contract address is usually given when a contract is deployed to the POTOS Blockchain. It is generated from the creator's address and the number of transactions initiated from that address (namely the "nonce").

## Accout usage

### Sending transactions

To send a transaction, you need to sign it with your private key. The transaction is then broadcasted to the network, where it is validated and included in a block.

### Interacting with smart contracts

You can interact with smart contracts by sending transactions. These transactions invoke methods in the smart contract, these methods modify the contract state and return the results as needed.

### Account balance Management

Your account balance is the number of native utility tokens you own. This balance can be used to pay for transaction fees. 
