> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://v3.viem.sh/api/mcp` to find what you need.

# Private Key Accounts

## Overview

A **Private Key Account** (`Account.PrivateKey`) is a [local account](/docs/accounts) backed by a
secp256k1 private key.

The account derives its public key and checksummed address from the private key. It signs hashes,
messages, transactions, typed data, and
[EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) authorizations locally.

Create one from an existing private key with
[`Account.fromPrivateKey`](#accountfromprivatekey), or generate one with
[`Account.random`](#accountrandom).

```ts twoslash
import { Account } from 'viem'

const account = Account.fromPrivateKey('0x...')

account.address
// '0x...'
account.publicKey
// '0x...'
```

## Recipes

### Set a Default Client Account

Pass the account to [`Client.create`](/docs/clients/create). Wallet Actions use this account when
you do not pass an `account` option.

```ts twoslash
import { Account, Client, http, walletActions } from 'viem'
import { mainnet } from 'viem/chains'

const account = Account.fromPrivateKey('0x...')

const client = Client.create({
  account, // [!code focus]
  chain: mainnet,
  transport: http(),
}).extend(walletActions())

const signature = await client.signMessage({ message: 'hello world' })
```

## `Account.fromPrivateKey`

Creates a private-key-backed local account from a private key.

### Usage

```ts twoslash
import { Account } from 'viem'

const account = Account.fromPrivateKey('0x...')

account.address
// '0x...'
account.publicKey
// '0x...'
```

### Parameters

#### privateKey

* **Type:** `Hex.Hex`

The private key to derive the account from.

```ts twoslash
import { Account } from 'viem'
// ---cut---
const account = Account.fromPrivateKey('0x...') // [!code focus]
```

#### options.nonceManager

* **Type:** `NonceManager.NonceManager`

The [nonce manager](/docs/accounts/nonce-manager) attached to the account.

```ts twoslash
import { Account, NonceManager } from 'viem'
// ---cut---
const account = Account.fromPrivateKey('0x...', {
  nonceManager: NonceManager.jsonRpc(), // [!code focus]
})
```

### Return Value

`Account.PrivateKey`

A private-key-backed local account (`keyType: 'secp256k1'`) exposing `address`, `publicKey`, and the signing methods.

### Errors

| Error | Description |
| --- | --- |
| `PublicKey.InvalidError` | The private key is invalid or produces an invalid public key. |

## `Account.random`

Creates a random private-key-backed local account.

### Usage

```ts twoslash
import { Account } from 'viem'

const account = Account.random()
```

### Return Value

`Account.PrivateKey`

A private-key-backed local account (`keyType: 'secp256k1'`) generated from a random private key.

### Errors

| Error | Description |
| --- | --- |
| `PublicKey.InvalidError` | The generated private key produces an invalid public key. |
