Skip to content

Commit

Permalink
fix(cli): add support for 0x prefixed keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Dec 18, 2023
1 parent e0583b0 commit 9fde24b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
13 changes: 12 additions & 1 deletion cli/cmd/credit/credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package credit

import (
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand All @@ -38,6 +39,7 @@ import (
"github.com/deepsquare-io/grid/cli/internal/ether"
"github.com/deepsquare-io/grid/cli/metascheduler"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -198,7 +200,16 @@ var Command = cli.Command{
}

func initClient(ctx context.Context) (*metascheduler.RPCClientSet, error) {
pk, err := crypto.HexToECDSA(ethHexPK)
kb, err := hexutil.Decode(ethHexPK)
if errors.Is(err, hexutil.ErrMissingPrefix) {
kb, err = hex.DecodeString(ethHexPK)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
pk, err := crypto.ToECDSA(kb)
if err != nil {
return nil, err
}
Expand Down
13 changes: 12 additions & 1 deletion cli/cmd/dps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ package main
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"os"
Expand All @@ -83,6 +84,7 @@ import (
"github.com/deepsquare-io/grid/cli/tui/nav"
versionpkg "github.com/deepsquare-io/grid/cli/version"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -191,7 +193,16 @@ See the GNU General Public License for more details.`,
ctx := cCtx.Context
var pk *ecdsa.PrivateKey
if ethHexPK != "" {
pk, err = crypto.HexToECDSA(ethHexPK)
kb, err := hexutil.Decode(ethHexPK)
if errors.Is(err, hexutil.ErrMissingPrefix) {
kb, err = hex.DecodeString(ethHexPK)
if err != nil {
return err
}
} else if err != nil {
return err
}
pk, err = crypto.ToECDSA(kb)
if err != nil {
return err
}
Expand Down
46 changes: 42 additions & 4 deletions cli/cmd/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ package job
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand All @@ -49,6 +50,7 @@ import (
"github.com/erikgeiser/promptkit/confirmation"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -213,7 +215,16 @@ var Command = cli.Command{
if cCtx.NArg() < 1 {
return errors.New("missing arguments")
}
pk, err := crypto.HexToECDSA(ethHexPK)
kb, err := hexutil.Decode(ethHexPK)
if errors.Is(err, hexutil.ErrMissingPrefix) {
kb, err = hex.DecodeString(ethHexPK)
if err != nil {
return err
}
} else if err != nil {
return err
}
pk, err := crypto.ToECDSA(kb)
if err != nil {
return err
}
Expand Down Expand Up @@ -322,7 +333,16 @@ var Command = cli.Command{
if cCtx.NArg() < 1 {
return errors.New("missing arguments")
}
pk, err := crypto.HexToECDSA(ethHexPK)
kb, err := hexutil.Decode(ethHexPK)
if errors.Is(err, hexutil.ErrMissingPrefix) {
kb, err = hex.DecodeString(ethHexPK)
if err != nil {
return err
}
} else if err != nil {
return err
}
pk, err := crypto.ToECDSA(kb)
if err != nil {
return err
}
Expand Down Expand Up @@ -369,7 +389,16 @@ var Command = cli.Command{
if cCtx.NArg() < 1 {
return errors.New("missing arguments")
}
pk, err := crypto.HexToECDSA(ethHexPK)
kb, err := hexutil.Decode(ethHexPK)
if errors.Is(err, hexutil.ErrMissingPrefix) {
kb, err = hex.DecodeString(ethHexPK)
if err != nil {
return err
}
} else if err != nil {
return err
}
pk, err := crypto.ToECDSA(kb)
if err != nil {
return err
}
Expand Down Expand Up @@ -416,7 +445,16 @@ var Command = cli.Command{
if cCtx.NArg() < 2 {
return errors.New("missing arguments")
}
pk, err := crypto.HexToECDSA(ethHexPK)
kb, err := hexutil.Decode(ethHexPK)
if errors.Is(err, hexutil.ErrMissingPrefix) {
kb, err = hex.DecodeString(ethHexPK)
if err != nil {
return err
}
} else if err != nil {
return err
}
pk, err := crypto.ToECDSA(kb)
if err != nil {
return err
}
Expand Down
24 changes: 22 additions & 2 deletions cli/cmd/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ COMMANDS:
package provider

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand All @@ -39,6 +40,7 @@ import (
"github.com/deepsquare-io/grid/cli/metascheduler"
"github.com/deepsquare-io/grid/cli/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -146,7 +148,16 @@ var Command = cli.Command{
return errors.New("missing arguments")
}
ctx := cCtx.Context
pk, err := crypto.HexToECDSA(ethHexPK)
kb, err := hexutil.Decode(ethHexPK)
if errors.Is(err, hexutil.ErrMissingPrefix) {
kb, err = hex.DecodeString(ethHexPK)
if err != nil {
return err
}
} else if err != nil {
return err
}
pk, err := crypto.ToECDSA(kb)
if err != nil {
return err
}
Expand Down Expand Up @@ -184,7 +195,16 @@ var Command = cli.Command{
return errors.New("missing arguments")
}
ctx := cCtx.Context
pk, err := crypto.HexToECDSA(ethHexPK)
kb, err := hexutil.Decode(ethHexPK)
if errors.Is(err, hexutil.ErrMissingPrefix) {
kb, err = hex.DecodeString(ethHexPK)
if err != nil {
return err
}
} else if err != nil {
return err
}
pk, err := crypto.ToECDSA(kb)
if err != nil {
return err
}
Expand Down
13 changes: 12 additions & 1 deletion cli/cmd/submit/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ package submit
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
Expand All @@ -67,6 +68,7 @@ import (
metaschedulerabi "github.com/deepsquare-io/grid/cli/types/abi/metascheduler"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
Expand Down Expand Up @@ -256,7 +258,16 @@ var Command = cli.Command{
}
jobPath := cCtx.Args().First()
ctx := cCtx.Context
pk, err := crypto.HexToECDSA(ethHexPK)
kb, err := hexutil.Decode(ethHexPK)
if errors.Is(err, hexutil.ErrMissingPrefix) {
kb, err = hex.DecodeString(ethHexPK)
if err != nil {
return err
}
} else if err != nil {
return err
}
pk, err := crypto.ToECDSA(kb)
if err != nil {
return err
}
Expand Down

0 comments on commit 9fde24b

Please sign in to comment.