Skip to content

Account.fromSecp256k1

Instantiates an Account from a secp256k1 private key (standard Ethereum signature scheme).

Usage

import { Account } from 'viem/tempo'
 
const account = Account.fromSecp256k1(
  '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
)
 
console.log('Address:', account.address)
Address: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

Access Keys

Create an account that acts as an access key for a parent account:

example.ts
import { Account, Secp256k1 } from 'viem/tempo'
import { client } from './viem.config'
 
// Create root account
const account = Account.fromSecp256k1(
  '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
)
 
// Create access key 
const accessKey = Account.fromSecp256k1(
  Secp256k1.randomPrivateKey(),
  { access: account }
)
 
// Sign a key authorization
const keyAuthorization = await account.signKeyAuthorization(accessKey, {
  expiry: Math.floor(Date.now() / 1000) + 86400, // 24 hour expiry
})
 
// Attach to next transaction
const receipt = await client.sendTransactionSync({
  account: accessKey,
  keyAuthorization,
  to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
})
 
// Now any subsequent transaction can be used with `accessKey`
// WITHOUT the `keyAuthorization` parameter!

Return Type

The return type of Account.fromSecp256k1 is backwards compatible with Viem's Account type.

type ReturnType = Account
 
type Account = {
  /** Account address */
  address: Address
  /** Key type */
  keyType: string
  /** Public key (hex) */
  publicKey: Hex
  /** Account source */
  source: string
  /** Account type */
  type: 'local'
  
  /** Assigns a key authorization to the next transaction */
  assignKeyAuthorization: (keyAuthorization: KeyAuthorization) => Promise<void>
  /** Signs a raw digest */
  sign: (parameters: { hash: Hex }) => Promise<Hex>
  /** Signs an EIP-7702 authorization */
  signAuthorization: (parameters: SignAuthorizationParameters) => Promise<Authorization>
  /** Signs a key authorization */
  signKeyAuthorization: (
    key: { accessKeyAddress: Address; keyType: string },
    parameters?: { expiry?: bigint; limits?: Limits }
  ) => Promise<KeyAuthorization>
  /** Signs a EIP-191 `personal_sign` message */
  signMessage: (parameters: { message: string | { raw: Hex } }) => Promise<Hex>
  /** Signs a transaction */
  signTransaction: (transaction: TransactionRequest) => Promise<Hex>
  /** Signs a EIP-712 typed data */
  signTypedData: (typedData: TypedData) => Promise<Hex>
}

Parameters

privateKey

  • Type: Hex

The secp256k1 private key as a hex string.

options (optional)

options.access

  • Type: Address | Account

Parent account to access.