Skip to content

Commit

Permalink
add private key rendering from env
Browse files Browse the repository at this point in the history
  • Loading branch information
RnkSngh committed Jun 26, 2024
1 parent 8ae2265 commit 9eb14fd
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/evm/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from "ethers";
import fs from "fs";
import path from "path";
import { Registry } from "../utils/registry";
import { parseZodSchema } from "../utils/io";
import { parseZodSchema, renderString } from "../utils/io";

const privateKey = z.object({
name: z.string().min(1),
Expand Down Expand Up @@ -109,7 +109,16 @@ export function createWallet(opt: {
index?: number;
}): Wallet {
if (opt.privateKey && typeof opt.privateKey === "string") {
return new ethers.Wallet(opt.privateKey);
let renderedPrivatekey = opt.privateKey;
if (!ethers.isHexString(renderedPrivatekey, 32)) {
// check if is a valid private key - if not, see if it represents an env variable that represents a private key.
try {
renderedPrivatekey = renderString(opt.privateKey, process.env); // look up in env if not.
} catch (e) {
console.log("no valid private key found for account spec", e);
}
}
return new ethers.Wallet(renderedPrivatekey);
}
if (opt.mnemonic && typeof opt.mnemonic === "string") {
let wallet = ethers.Wallet.fromPhrase(opt.mnemonic);
Expand Down

0 comments on commit 9eb14fd

Please sign in to comment.