From 776600fc564277d47f0155c2af4dcdf4b5dab46f Mon Sep 17 00:00:00 2001 From: wispy <129100344+wispyiwnl@users.noreply.github.com> Date: Wed, 15 Jan 2025 12:48:51 -0500 Subject: [PATCH] Add automatic 0x prefix to private key handling (#229) * feat: add auto-prefix 0x to private key handling Previously, the code would throw an error if the WALLET_PRIVATE_KEY didn't start with '0x'. This update improves the user experience by automatically adding the '0x' prefix if missing, while maintaining the validation for the correct key length. The code will work seamlessly whether users provide the private key with or without the '0x' prefix, making it more flexible and user-friendly. * Remove rebundant error --------- Co-authored-by: Agustin Armellini Fischer --- typescript/examples/vercel-ai/mode/index.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/typescript/examples/vercel-ai/mode/index.ts b/typescript/examples/vercel-ai/mode/index.ts index adf9a3156..354c6b4a1 100644 --- a/typescript/examples/vercel-ai/mode/index.ts +++ b/typescript/examples/vercel-ai/mode/index.ts @@ -17,7 +17,16 @@ import { viem } from "@goat-sdk/wallet-viem"; require("dotenv").config(); -const account = privateKeyToAccount(process.env.WALLET_PRIVATE_KEY as `0x${string}`); +let privateKey = process.env.WALLET_PRIVATE_KEY || ""; + +if (!privateKey) { + throw new Error("WALLET_PRIVATE_KEY environment variable is required"); +} + +// Normalize private key format +privateKey = privateKey.startsWith("0x") ? privateKey : `0x${privateKey}`; + +const account = privateKeyToAccount(privateKey as `0x${string}`); const walletClient = createWalletClient({ account: account,