Skip to content

Commit

Permalink
feat: accept private key with or without 0x prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
iczc committed Feb 15, 2023
1 parent 5e23f23 commit 816cd5d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ func Execute() {

func getPrivateKeyFromFlags() (*ecdsa.PrivateKey, error) {
if *privKeyFlag != "" {
return crypto.HexToECDSA(*privKeyFlag)
hexkey := *privKeyFlag
if chain.Has0xPrefix(hexkey) {
hexkey = hexkey[2:]
}
return crypto.HexToECDSA(hexkey)
} else if *keyJSONFlag == "" {
return nil, errors.New("missing private key or keystore")
}
Expand Down
14 changes: 9 additions & 5 deletions internal/chain/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import (
"github.com/ethereum/go-ethereum/common"
)

func EtherToWei(amount int64) *big.Int {
ether := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
return new(big.Int).Mul(big.NewInt(amount), ether)
}

func Has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}

func IsValidAddress(address string, checksummed bool) bool {
if !common.IsHexAddress(address) {
return false
}
return !checksummed || common.HexToAddress(address).Hex() == address
}

func EtherToWei(amount int64) *big.Int {
ether := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
return new(big.Int).Mul(big.NewInt(amount), ether)
}

0 comments on commit 816cd5d

Please sign in to comment.