0.6.16 • Published 4 months ago

@turnkey/viem v0.6.16

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 months ago

@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

0.6.16

4 months ago

0.6.15

4 months ago

0.6.14

4 months ago

0.6.13

4 months ago

0.6.12

5 months ago

0.6.9

6 months ago

0.6.10

5 months ago

0.6.11

5 months ago

0.6.8

6 months ago

0.6.7

7 months ago

0.6.6

7 months ago

0.6.5

7 months ago

0.6.4

8 months ago

0.6.3

8 months ago

0.6.2

9 months ago

0.6.1

10 months ago

0.4.31

10 months ago

0.4.30

10 months ago

0.5.0

10 months ago

0.4.28

11 months ago

0.4.29

11 months ago

0.6.0

10 months ago

0.4.27

11 months ago

0.4.26

11 months ago

0.4.25

12 months ago

0.4.20

1 year ago

0.4.21

1 year ago

0.4.24

12 months ago

0.4.22

1 year ago

0.4.23

1 year ago

0.4.19

1 year ago

0.4.18

1 year ago

0.4.17

1 year ago

0.4.16

1 year ago

0.4.15

1 year ago

0.4.14

1 year ago

0.4.13

1 year ago

0.4.12

1 year ago

0.4.11

1 year ago

0.4.9

1 year ago

0.4.10

1 year ago

0.4.8

1 year ago

0.4.7

2 years ago

0.4.5

2 years ago

0.4.6

2 years ago

0.4.4

2 years ago

0.4.3

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.4.2

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.2

2 years ago

0.3.0

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago