Skip to content
On this page

Private Key Account โ€‹

A Private Key Account is an interface that has the ability to sign transactions and messages with a given private key.

INFO

viem internally uses @noble/curves, an audited implementation of secp256k1, for our private key & signing implementation.

Import โ€‹

ts
import { privateKeyToAccount } from 'viem/accounts'

Usage โ€‹

To initialize a Private Key Account, you will need to pass a private key to privateKeyToAccount:

ts
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'

const account = privateKeyToAccount('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80') 

const client = createWalletClient({
  account,
  chain: mainnet,
  transport: http()
})

Note: the above is a valid private key, but it is not a "real" private key. Please do not use it for anything other than testing.

Generating Private Keys โ€‹

You can generate a random private key using the generatePrivateKey function:

ts
import { generatePrivateKey } from 'viem/accounts'

const privateKey = generatePrivateKey()

Parameters โ€‹

privateKey โ€‹

  • Type: Hex

The private key to use for the Account.

Released under the MIT License.