diff --git a/src/lib/PhatBaseCommand.ts b/src/lib/PhatBaseCommand.ts index 609ee3f..b8e2775 100644 --- a/src/lib/PhatBaseCommand.ts +++ b/src/lib/PhatBaseCommand.ts @@ -35,6 +35,7 @@ import { } from '../lib/runWebpack' import { formatWebpackMessages } from '../lib/formatWebpackMessages' import BaseCommand from '../lib/BaseCommand' +import { add0xPrefix } from '../lib/utils' export interface ParsedFlags { readonly build: boolean @@ -503,8 +504,8 @@ export default abstract class PhatBaseCommand extends BaseCommand { apiPromise: ApiPromise }) { if (this.parsedFlags.privateKey || (process.env.PRIVATE_KEY && !this.parsedFlags.suri && !this.parsedFlags.accountFilePath)) { - const privateKey = this.parsedFlags.privateKey || process.env.PRIVATE_KEY - const account = privateKeyToAccount(privateKey as `0x${string}`) + const privateKey = add0xPrefix(this.parsedFlags.privateKey || process.env.PRIVATE_KEY!) + const account = privateKeyToAccount(privateKey) const client = createWalletClient({ account, chain: mainnet, diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 62d3e78..9fecd38 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -5,3 +5,10 @@ export function resolveToAbsolutePath(inputPath: string): string { const regex = /^~(?=$|[/\\])/ return upath.resolve(inputPath.replace(regex, os.homedir())) } + +export function add0xPrefix(inputStr: string): `0x${string}` { + if (!inputStr.startsWith('0x')) { + inputStr = `0x${inputStr}` + } + return inputStr as `0x${string}` +}