Skip to content

Commit

Permalink
fix: automatically add 0x prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoyang committed Dec 28, 2023
1 parent 5e30a68 commit 4d02ce4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/lib/PhatBaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}

0 comments on commit 4d02ce4

Please sign in to comment.