@turnkey/viem
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() {
const httpClient = new TurnkeyClient(
{
baseUrl: "https://api.turnkey.com",
},
new ApiKeyStamper({
apiPublicKey: "...",
apiPrivateKey: "...",
})
);
const turnkeyAccount = await createAccount({
client: httpClient,
organizationId: "...",
signWith: "...",
ethereumAddress: "...",
});
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,
};
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)
See .env.example
to get your local testing environment right. In order to run the tests, run an Anvil node in one shell (via pnpm anvil
), and run pnpm jest
in another.
See also