From 816cd5d580d233a30a475e2dd7636269ab57da05 Mon Sep 17 00:00:00 2001 From: iczc Date: Thu, 16 Feb 2023 02:45:12 +0800 Subject: [PATCH] feat: accept private key with or without 0x prefix --- cmd/server.go | 6 +++++- internal/chain/util.go | 14 +++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 91488a01..6914a343 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -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") } diff --git a/internal/chain/util.go b/internal/chain/util.go index 61c55f01..1c57e4f2 100644 --- a/internal/chain/util.go +++ b/internal/chain/util.go @@ -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) -}