# Accounts

{% hint style="warning" %}
The following functions and commands are subject to further testing and potential changed.
{% endhint %}

### BlockX Accounts <a href="#evmos-accounts" id="evmos-accounts"></a>

BlockX defines its own custom `Account` type that uses Ethereum's ECDSA secp256k1 curve for keys. This satisfies the [EIP84 ](https://github.com/ethereum/EIPs/issues/84)for full [BIP44 ](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)paths. The root HD path for BlockXs-based accounts is `m/44'/60'/0'/0`.

```go
// EthAccount implements the authtypes.AccountI interface and embeds an
// authtypes.BaseAccount type. It is compatible with the auth AccountKeeper.
type EthAccount struct {
	*types.BaseAccount `protobuf:"bytes,1,opt,name=base_account,json=baseAccount,proto3,embedded=base_account" json:"base_account,omitempty" yaml:"base_account"`
	CodeHash           string `protobuf:"bytes,2,opt,name=code_hash,json=codeHash,proto3" json:"code_hash,omitempty" yaml:"code_hash"`
}
```

### Addresses and Public Keys <a href="#addresses-and-public-keys" id="addresses-and-public-keys"></a>

[BIP-0173 ](https://github.com/satoshilabs/slips/blob/master/slip-0173.md)defines a new format for segregated witness output addresses that contains a human-readable part that identifies the Bech32 usage. BlockX uses the following HRP (human readable prefix) as the base HRP:

| Network | Testnet | Mainnet |
| :-----: | :-----: | :-----: |
|         |         |         |

There are 3 main types of HRP for the `Addresses`/`PubKeys` available by default on BlockX:

* Addresses and Keys for **accounts**, which identify users (e.g. the sender of a `message`). They are derived using the **`eth_secp256k1`** curve.
* Addresses and Keys for **validator operators**, which identify the operators of validators. They are derived using the **`eth_secp256k1`** curve.
* Addresses and Keys for **consensus nodes**, which identify the validator nodes participating in consensus. They are derived using the **`ed25519`** curve.

{% hint style="info" %}
Data items are coming soon
{% endhint %}

| -                  | Address bech32 Prefix | Pubkey bech32 Prefix | Curve | Address byte length | Pubkey byte length |
| ------------------ | --------------------- | -------------------- | ----- | ------------------- | ------------------ |
| Accounts           |                       |                      |       |                     |                    |
| Validator Operator |                       |                      |       |                     |                    |
| Consensus Nodes    |                       |                      |       |                     |                    |

### Address formats for clients <a href="#address-formats-for-clients" id="address-formats-for-clients"></a>

`EthAccount` can be represented in both [Bech32](https://en.bitcoin.it/wiki/Bech32) and hex (`0x...`) formats for Ethereum's Web3 tooling compatibility.

The Bech32 format is the default format for Cosmos-SDK queries and transactions through CLI and REST clients. The hex format on the other hand, is the Ethereum `common.Address` representation of a Cosmos `sdk.AccAddress`.

* **Address (Bech32)**: `[]`
* **Address (**[**EIP55**](https://eips.ethereum.org/EIPS/eip-55) **Hex)**: `[]`
* **Compressed Public Key**: `[]`

#### Address conversion <a href="#address-conversion" id="address-conversion"></a>

The `[]` can be used to convert an address between hex and bech32 formats. For example:

:::: tabs ::: tab Bech32

```bash
blockxd debug addr blockx1z3t55m0l9h0eupuz3dp5t5cypyv674jj7mz2jw
  Address: [20 87 74 109 255 45 223 158 7 130 139 67 69 211 4 9 25 175 86 82]
  Address (hex): 14574A6DFF2DDF9E07828B4345D3040919AF5652
  Bech32 Acc: blockx1z3t55m0l9h0eupuz3dp5t5cypyv674jj7mz2jw
  Bech32 Val: blockxvaloper1z3t55m0l9h0eupuz3dp5t5cypyv674jjn4d6nn
```

::: ::: tab Hex

```bash
blockxd debug addr 14574A6DFF2DDF9E07828B4345D3040919AF5652
  Address: [20 87 74 109 255 45 223 158 7 130 139 67 69 211 4 9 25 175 86 82]
  Address (hex): 14574A6DFF2DDF9E07828B4345D3040919AF5652
  Bech32 Acc: blockx1z3t55m0l9h0eupuz3dp5t5cypyv674jj7mz2jw
  Bech32 Val: blockxvaloper1z3t55m0l9h0eupuz3dp5t5cypyv674jjn4d6nn
```

::: ::::

#### Key output <a href="#key-output" id="key-output"></a>

{% hint style="info" %}
The Cosmos SDK Keyring output (i.e blockxd`keys`) only supports addresses and public keys in Bech32 format.
{% endhint %}

We can use the `keys show` command of `blockxd` with the flag `--bech <type> (acc|val|cons)` to obtain the addresses and keys as mentioned above,

:::: tabs ::: tab Account

```bash
blockxd keys show mykey --bech acc
- name: mykey
  type: local
  address: blockx1z3t55m0l9h0eupuz3dp5t5cypyv674jj7mz2jw
  pubkey: '{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"AsV5oddeB+hkByIJo/4lZiVUgXTzNfBPKC73cZ4K1YD2"}'
  mnemonic: ""
```

::: ::: tab Validator

```bash
blockxd keys show mykey --bech val
- name: mykey
  type: local
  address: blockxvaloper1z3t55m0l9h0eupuz3dp5t5cypyv674jjn4d6nn
  pubkey: '{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"AsV5oddeB+hkByIJo/4lZiVUgXTzNfBPKC73cZ4K1YD2"}'
  mnemonic: ""
```

::: ::: tab Consensus

```bash
blockxd keys show mykey --bech cons
- name: mykey
  type: local
  address: blockxvalcons1rllqa5d97n6zyjhy6cnscc7zu30zjn3f7wyj2n
  pubkey: '{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"A/fVLgIqiLykFQxum96JkSOoTemrXD0tFaFQ1B0cpB2c"}'
  mnemonic: ""
```

::: ::::

### Querying an Account <a href="#querying-an-account" id="querying-an-account"></a>

You can query an account address using the CLI, gRPC or

#### Command Line Interface <a href="#command-line-interface" id="command-line-interface"></a>

```bash
# NOTE: the --output (-o) flag will define the output format in JSON or YAML (text)
blockxd q auth account $(blockxd keys show mykey -a) -o text
|
  '@type': /ethermint.types.v1.EthAccount
  base_account:
    account_number: "0"
    address: blockx1z3t55m0l9h0eupuz3dp5t5cypyv674jj7mz2jw
    pub_key:
      '@type': /ethermint.crypto.v1.ethsecp256k1.PubKey
      key: AsV5oddeB+hkByIJo/4lZiVUgXTzNfBPKC73cZ4K1YD2
    sequence: "1"
  code_hash: 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
```

#### Cosmos gRPC and REST <a href="#cosmos-grpc-and-rest" id="cosmos-grpc-and-rest"></a>

```bash
# GET /cosmos/auth/v1beta1/accounts/{address}
curl -X GET "http://localhost:10337/cosmos/auth/v1beta1/accounts/blockx14au322k9munkmx5wrchz9q30juf5wjgz2cfqku" -H "accept: application/json"
```

#### JSON-RPC <a href="#json-rpc" id="json-rpc"></a>

To retrieve the Ethereum hex address using Web3, use the JSON-RPC `eth_accounts` or `personal_listAccounts` endpoints:

```bash
# query against a local node
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}' -H "Content-Type: application/json" http://localhost:8545

curl -X POST --data '{"jsonrpc":"2.0","method":"personal_listAccounts","params":[],"id":1}' -H "Content-Type: application/json" http://localhost:8545
```
