# Keystore

Utilities & types for working with [Keystores](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage).

## Examples

Below are some examples demonstrating common usages of the `Keystore` module:

* [Encrypting Private Keys](#encrypting-private-keys)

* [Decrypting Private Keys](#decrypting-private-keys)

### Encrypting Private Keys

Private keys can be encrypted into a JSON keystore using [`Keystore.encrypt`](/api/Keystore/encrypt):

```ts twoslash
import { Keystore, Secp256k1 } from 'ox'

// Generate a random private key.
const privateKey = Secp256k1.randomPrivateKey()

// Derive a key from a password.
const [key, opts] = Keystore.pbkdf2({
  password: 'testpassword'
})

// Encrypt the private key.
const keystore = Keystore.encrypt(privateKey, key, opts)
// @log: {
// @log:   "crypto": {
// @log:     "cipher": "aes-128-ctr",
// @log:     "ciphertext": "...",
// @log:     "cipherparams": {
// @log:       "iv": "...",
// @log:     },
// @log:     "kdf": "pbkdf2",
// @log:     "kdfparams": {
// @log:       "salt": "...",
// @log:       "dklen": 32,
// @log:       "prf": "hmac-sha256",
// @log:       "c": 262144,
// @log:     },
// @log:     "mac": "...",
// @log:   },
// @log:   "id": "...",
// @log:   "version": 3,
// @log: }
```

### Decrypting Private Keys

Private keys can be decrypted from a JSON keystore using [`Keystore.decrypt`](/api/Keystore/decrypt):

```ts twoslash
// @noErrors
import { Keystore, Secp256k1 } from 'ox'

const keystore = { crypto: { ... }, id: '...', version: 3 }

// Derive the key.
const key = Keystore.toKey(keystore, { password: 'testpassword' })

// Decrypt the private key.
const decrypted = Keystore.decrypt(keystore, key)
// @log: "0x..."

```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Keystore.decrypt`](/api/Keystore/decrypt) | Decrypts a [JSON keystore](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage/) into a private key. |
| [`Keystore.encrypt`](/api/Keystore/encrypt) | Encrypts a private key as a [JSON keystore](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage/) using a derived key. |
| [`Keystore.pbkdf2`](/api/Keystore/pbkdf2) | Derives a key from a password using [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2). |
| [`Keystore.pbkdf2Async`](/api/Keystore/pbkdf2Async) | Derives a key from a password using [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2). |
| [`Keystore.scrypt`](/api/Keystore/scrypt) | Derives a key from a password using [scrypt](https://en.wikipedia.org/wiki/Scrypt). |
| [`Keystore.scryptAsync`](/api/Keystore/scryptAsync) | Derives a key from a password using [scrypt](https://en.wikipedia.org/wiki/Scrypt). |
| [`Keystore.toKey`](/api/Keystore/toKey) | Extracts a Key from a JSON Keystore to use for decryption. |
| [`Keystore.toKeyAsync`](/api/Keystore/toKeyAsync) | Extracts a Key asynchronously from a JSON Keystore to use for decryption. |

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Keystore.DeriveOpts`](/api/Keystore/types#keystorederiveopts) | Derivation Options. |
| [`Keystore.Key`](/api/Keystore/types#keystorekey) | Key. |
| [`Keystore.Keystore`](/api/Keystore/types#keystorekeystore) | Keystore. |
| [`Keystore.Pbkdf2DeriveOpts`](/api/Keystore/types#keystorepbkdf2deriveopts) | PBKDF2 Derivation Options. |
| [`Keystore.ScryptDeriveOpts`](/api/Keystore/types#keystorescryptderiveopts) | Scrypt Derivation Options. |
