toViemAccount
Adapts a secp256k1 signing session into a viem local account. Every signing method signs through session.signDigest, so signing shows no passkey prompt. The function lives in the @category-labs/mera/viem entry point, which requires viem (^2.28.0) as an optional peer dependency; the root entry point does not use viem.
Import
Section titled “Import”import { toViemAccount } from "@category-labs/mera/viem";import { createSecp256k1SigningSession } from "@category-labs/mera";import { toViemAccount } from "@category-labs/mera/viem";import { createWalletClient, http, parseEther } from "viem";import { sepolia } from "viem/chains";
const privateKey = crypto.getRandomValues(new Uint8Array(32));const session = createSecp256k1SigningSession({ privateKey });
const client = createWalletClient({ account: toViemAccount(session), chain: sepolia, transport: http(),});
const recipient = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8";const hash = await client.sendTransaction({ to: recipient, value: parseEther("0.01"),});
session.end();Parameters
Section titled “Parameters”The session is positional; options is a ToViemAccountOptions and may be omitted.
session
Section titled “session”- Type:
Secp256k1SigningSession - Required
Live secp256k1 signing session that backs the account. createSecp256k1SigningSession produces one.
options.nonceManager
Section titled “options.nonceManager”- Type:
NonceManager, from viem - Optional; when omitted the account carries no nonce manager
viem nonce manager forwarded to the account. viem clients use it to assign transaction nonces automatically.
Returns
Section titled “Returns”A viem LocalAccount with source: "mera", accepted anywhere viem takes an account.
address
Section titled “address”The EIP-55 checksummed address of the session key, the same value getEvmAddress returns for session.publicKey.
publicKey
Section titled “publicKey”The 65-byte uncompressed secp256k1 public key as hex: the 0x04 prefix, then 128 hex characters.
signTransaction(transaction, options?)
Section titled “signTransaction(transaction, options?)”Serializes the transaction, signs its keccak-256 digest, and resolves to the signed serialized transaction. options.serializer replaces viem’s serializeTransaction for both steps. EIP-4844 transactions are hashed without their sidecars and serialized with them.
signMessage({ message })
Section titled “signMessage({ message })”Resolves to the EIP-191 personal-message signature for message, 65 bytes as hex.
signTypedData(typedData)
Section titled “signTypedData(typedData)”Resolves to the EIP-712 signature for the typed data, 65 bytes as hex.
signAuthorization(authorization)
Section titled “signAuthorization(authorization)”Signs an EIP-7702 authorization and resolves to the signed authorization object: the contract address, chain ID, and nonce together with the signature fields.
sign({ hash })
Section titled “sign({ hash })”Signs a 32-byte hash directly, with no additional hashing, and resolves to the 65-byte hex signature.
Errors
Section titled “Errors”SESSION_ENDED: any signing method rejects with this aftersession.end().INPUT_INVALID:signrejects with this whenhashis not exactly 32 bytes.
Signatures are low-S, which EVM chains require since EIP-2.
See also
Section titled “See also”- Send a transaction with viem: the recipe built on this adapter.
- createSecp256k1SigningSession: produces the session this adapter consumes.
- Signing sessions: how a session owns the key, and the lifecycle.