Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@turnkey/viem

tkhq146.3kApache-2.00.10.5TypeScript support: included

Turnkey Helpers to work with Viem

Turnkey, Viem, custom account, account, wallet, signer

readme

@turnkey/viem

npm

This package contains helpers to use Viem with Turnkey.

We provide a Turnkey Custom Account (signer) which implements the signing APIs expected by Viem clients.

If you need a lower-level, fully typed HTTP client for interacting with Turnkey API, check out @turnkey/http.

Getting started

$ npm install viem @turnkey/viem
import { createAccount } from "@turnkey/viem";
import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { createWalletClient, http } from "viem";
import { sepolia } from "viem/chains";

async function main() {
  // Create a Turnkey HTTP client with API key credentials
  const httpClient = new TurnkeyClient(
    {
      baseUrl: "https://api.turnkey.com",
    },
    // This uses API key credentials.
    // If you're using passkeys, use `@turnkey/webauthn-stamper` to collect webauthn signatures:
    // new WebauthnStamper({...options...})
    new ApiKeyStamper({
      apiPublicKey: "...",
      apiPrivateKey: "...",
    }),
  );

  // Create the Viem custom account
  const turnkeyAccount = await createAccount({
    client: httpClient,
    organizationId: "...",
    signWith: "...",
    // optional; will be fetched from Turnkey if not provided
    ethereumAddress: "...",
  });

  // Below: standard Viem APIs are used, nothing special!

  const client = createWalletClient({
    account: turnkeyAccount,
    chain: sepolia,
    transport: http(`https://sepolia.infura.io/v3/$(YOUR_INFURA_API_KEY)`),
  });

  const transactionRequest = {
    to: "0x08d2b0a37F869FF76BACB5Bab3278E26ab7067B7" as `0x${string}`,
    value: 1000000000000000n, // 0.001 ETH
  };

  const txHash = await client.sendTransaction(transactionRequest);
  console.log(`Success! Transaction broadcast with hash ${txHash}`);
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Testing (Local)

  1. Copy .env.example to .env

    $ cp .env.example .env
  2. Start the Anvil node in one shell:

    • Install Foundry & Anvil if you haven't done so already
    • Add Foundry to your $PATH
      $ export PATH="$PATH:$HOME/.foundry/bin"
    • Source your env e.g.
      $ source ~/.zshrc
    • Run foundryup to install Anvil
      $ foundryup
    • Start Anvil
      $ pnpm anvil
  3. Run the tests in a new shell:

    $ pnpm test

See also

Transaction types supported

  • Turnkey's Viem implementation now supports all transaction types: legacy, EIP-2930 (Type 1), EIP-1559 (Type 2), EIP-4844 (Type 3), and EIP-7702 (Type 4). See with-viem for examples of scripts that sign using those various types.

changelog

@turnkey/viem

0.10.5

Patch Changes

0.10.4

Patch Changes

0.10.3

Patch Changes

0.10.2

Patch Changes

0.10.1

Patch Changes

0.10.0

Minor Changes

  • #733 cc463d3 Author @besler613 - Typed data hashing is now performed server-side using the new PAYLOAD_ENCODING_EIP712 encoding, and EIP-712 Policies are supported via the eth.eip_712 namespace.

0.9.12

Patch Changes

0.9.11

Patch Changes

0.9.10

Patch Changes

0.9.9

Patch Changes

0.9.8

Patch Changes

0.9.7

Patch Changes

0.9.6

Patch Changes

0.9.5

Patch Changes

0.9.4

Patch Changes

0.9.3

Patch Changes

0.9.2

Patch Changes

  • d440e7b: Update signAuthorization implementation to explicitly include yParity in the response

0.9.1

Patch Changes

0.9.0

Minor Changes

  • 2f75cf1: Add support for signing Type 3 (EIP-4844) transactions
    • Note the inline comments on the signTransaction implementation: when signing Type 3 transactions, our Viem implementation will extract the transaction payload (not including blobs, commitments, or proofs), sign it, extract the signature, and then reassemble the entire transaction payload.
    • See with-viem for examples.

Patch Changes

0.8.0

Minor Changes

  • 1d709ce: - Add support for EIP 7702 (Type 4) transactions by way of a new signAuthorization method
    • Update upstream viem version to ^2.24.2 (required for 7702)
    • Introduce new to parameter, used for indicating the result shape of signMessage (and related) requests
      • Affects signTypedData as well
      • Is used by signAuthorization
      • As a result, serializeSignature is updated as well

0.7.2

Patch Changes

0.7.1

Patch Changes

  • 123406b: The organizationId parameter is ignored when using a client other than TurnkeyClient (e.g., passkeyClient). Consequently, the SDK calls the client without the specified organizationId, which is unintended. This patch resolves the issue
  • Updated dependencies [e501690]
  • Updated dependencies [d1083bd]
  • Updated dependencies [f94d36e]

0.7.0

Minor Changes

  • d99fe40: Upgrade upstream viem dependency

Patch Changes

0.6.18

Patch Changes

0.6.17

Patch Changes

0.6.16

Patch Changes

0.6.15

Patch Changes

0.6.14

Patch Changes

0.6.13

Patch Changes

0.6.12

Patch Changes

0.6.11

Patch Changes

0.6.10

Patch Changes

0.6.9

Patch Changes

0.6.8

Patch Changes

0.6.7

Patch Changes

0.6.6

Patch Changes

0.6.5

Patch Changes

0.6.4

Patch Changes

0.6.3

Patch Changes

0.6.2

Patch Changes

0.6.1

Patch Changes

0.6.0

Minor Changes

Patch Changes

0.5.0

Minor Changes

  • 848f8d3: Support awaiting consensus and improve error handling

    • Add new error types that extend BaseError (and thus implement error.walk)
      • TurnkeyConsensusNeededError wraps consensus-related errors
      • TurnkeyActivityError wraps base Turnkey errors
    • Add a few new helper functions:
      • serializeSignature serializes a raw signature
      • isTurnkeyActivityConsensusNeededError and isTurnkeyActivityError use error.walk to check the type of a Viem error

Patch Changes

0.4.31

Patch Changes

0.4.30

Patch Changes

0.4.29

Patch Changes

0.4.28

Patch Changes

0.4.27

Patch Changes

0.4.26

Patch Changes

0.4.25

Patch Changes

0.4.24

Patch Changes

0.4.23

Patch Changes

0.4.22

Patch Changes

0.4.21

Patch Changes

0.4.20

Patch Changes

0.4.19

Patch Changes

0.4.18

Patch Changes

0.4.17

Patch Changes

0.4.16

Patch Changes

0.4.15

Patch Changes

  • a6502e6: Add support for new Turnkey Client types

0.4.14

Patch Changes

0.4.13

Patch Changes

0.4.12

Patch Changes

0.4.11

Patch Changes

0.4.10

Patch Changes

0.4.9

Patch Changes

0.4.8

Patch Changes

  • 4794c64: Updated dependencies

0.4.7

Patch Changes

0.4.6

Patch Changes

0.4.5

Patch Changes

0.4.4

Patch Changes

0.4.3

Patch Changes

0.4.2

Patch Changes

0.4.1

Patch Changes

0.4.0

Minor Changes

  • Use rollup to build ESM and CommonJS, fix ESM support (#174)

Patch Changes

0.3.4

Patch Changes

0.3.3

Patch Changes

0.3.2

Patch Changes

0.3.0

Minor Changes

  • cf8631a: Update interface to support signWith

This change supports signing with wallet account addresses, private key addresses, or private key IDs. See below for an example:

const httpClient = new TurnkeyClient(
  {
    baseUrl: "https://api.turnkey.com",
  },
  // This uses API key credentials.
  // If you're using passkeys, use `@turnkey/webauthn-stamper` to collect webauthn signatures:
  // new WebauthnStamper({...options...})
  new ApiKeyStamper({
    apiPublicKey: "...",
    apiPrivateKey: "...",
  }),
);

// Create the Viem custom account
const turnkeyAccount = await createAccount({
  client: httpClient,
  organizationId: "...",
  signWith: "...",
  // optional; will be fetched from Turnkey if not provided
  ethereumAddress: "...",
});

0.2.7

Patch Changes

0.2.6

Patch Changes

  • 59dcd2f: Unpin typescript
  • da7c960: Bump Viem dependency to fix getAddresses() for LocalAccount
  • Updated dependencies
  • Updated the shape of signing

0.2.5

Patch Changes

0.2.4

Patch Changes

0.2.3

Patch Changes

0.2.2

Patch Changes

0.2.1

Patch Changes

  • Fix code sample in the README; add more details and links

0.2.0

Minor Changes

  • Add new createAccount method and deprecates the existing createApiAccount. createAccount offers a superset of functionality and works with stampers (@turnkey/api-key-stamper / @turnkey/webauthn-stamper) to integrate with API keys or passkeys.

Patch Changes

0.1.1

Patch Changes

  • README updates

0.1.0

Initial release!