# Key management

### Generate new key

To generate new key, use the following command:

```bash
swisstronikcli keys add <key_name>
```

{% hint style="info" %}
If you got an error "Error: provided algorithm "eth\_secp256k1" is not supported", specify an additional parameter for keyring `--keyring-backend test` or `--keyring-backend file`
{% endhint %}

**NOTE**: This command will generate a mnemonic for a new key, therefore, you should save it in a secure place.

The command above will output the following:

```
- address: swtr156pzc2rj2xka9cu0gpgkmz3vmal544nlu9mwj7
  name: debug
  pubkey: '{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"AyJHdeTNCnjV4v5ZJEbyFS5UtzBQk1ThkZB4G7QR3Cb6"}'
  type: local


**Important** write this mnemonic phrase in a safe place.
It is the only way to recover your account if you ever forget your password.

picnic explain flower snack lava town marriage search approve increase display angry express peanut peanut mouse noise knock address situate hope pelican advance rather
```

### Restore key from mnemonic

{% hint style="info" %}
If you got error "Error: provided algorithm "eth\_secp256k1" is not supported", specify an additional parameter for keyring `--keyring-backend test` or `--keyring-backend file`
{% endhint %}

If you already have a mnemonic and want to restore a key from it, use the following command:

```bash
swisstronikcli keys add <key_name> --recover
```

It will prompt you for your mnemonic. Once you input your mnemonic, your output should look like:

```bash
- address: swtr156pzc2rj2xka9cu0gpgkmz3vmal544nlu9mwj7
  name: debug
  pubkey: '{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"AyJHdeTNCnjV4v5ZJEbyFS5UtzBQk1ThkZB4G7QR3Cb6"}'
  type: local
```

### List all accounts

To show all stored accounts, use the following command:

```bash
swisstronikcli keys list
```

This command should output the following:

```bash
- address: swtr156pzc2rj2xka9cu0gpgkmz3vmal544nlu9mwj7
  name: debug
  pubkey: '{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"AyJHdeTNCnjV4v5ZJEbyFS5UtzBQk1ThkZB4G7QR3Cb6"}'
  type: local
```

{% hint style="info" %}
If you cannot find your key in the output, try to specify `--keyring-backend test` or `--keyring-backend file`
{% endhint %}

### Export ETH private key

Since `swisstronikcli` operates with `eth_secp256k1` private keys, all keys stored in the keyring are Ethereum-compatible private keys. You can export Ethereum private key (for example, to use it in MetaMask) by using the following command:

```bash
swisstronikcli keys unsafe-export-eth-key <key_name>
```

{% hint style="info" %}
If you encounter the error "Error: \<wallet\_name>.info: key not found", try specifying either `--keyring-backend test` or `--keyring-backend file`
{% endhint %}

The command above should output your Ethereum private key, for example:

```bash
C66DB5D865EEF076B638AEA46445775989C7B0B88805CB6A70AF37523DC2672D
```

### Convert address

`swisstronikcli` can work only with bech32-encoded (swtr1) addresses, so if you want to convert your Ethereum address to bech32 format (and vice versa), you can use the following command:

```bash
swisstronikcli debug convert-address <bech32-or-eth-address>
```

It should output the converted address:

```bash
Address bytes: [166 130 44 40 114 81 173 210 227 143 64 81 109 138 44 223 127 74 214 127]
Address (hex): A6822C287251ADD2E38F40516D8A2CDF7F4AD67F
Address (EIP-55): 0xA6822c287251ADd2E38F40516D8a2cDF7F4aD67f
Bech32 Acc: swtr156pzc2rj2xka9cu0gpgkmz3vmal544nlu9mwj7
Bech32 Val: swtrvaloper156pzc2rj2xka9cu0gpgkmz3vmal544nlhg5v7r
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.swisstronik.com/swisstronik-docs/development/swisstronik-cli/key-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
