Skip to content

Commit

Permalink
Add automatic 0x prefix to private key handling (#229)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
wispyiwnl and 0xaguspunk authored Jan 15, 2025
1 parent cd74a87 commit 776600f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion typescript/examples/vercel-ai/mode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 776600f

Please sign in to comment.