Skip to content

Commit 8db4bcc

Browse files
fkondejcanercidam
andauthored
feat: add --insecure to test cmd (#375)
Co-authored-by: Caner Çıdam <canercidam01@gmail.com>
1 parent a8f7669 commit 8db4bcc

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log"
88
"log/slog"
9+
"net/url"
910
"os"
1011
"sort"
1112
"strings"
@@ -59,6 +60,7 @@ var (
5960
testRPCURL string
6061
testELRPCURL string
6162
testTimeout time.Duration
63+
testInsecure bool
6264
portListFlag bool
6365
)
6466

@@ -507,11 +509,22 @@ var testCmd = &cobra.Command{
507509
Short: "Send a test transaction to the local EL node",
508510
RunE: func(cmd *cobra.Command, args []string) error {
509511
cmd.SilenceUsage = true
512+
510513
ctx := mainctx.Get()
511514
cfg := playground.DefaultTestTxConfig()
512515
cfg.RPCURL = testRPCURL
513516
cfg.ELRPCURL = testELRPCURL
514517
cfg.Timeout = testTimeout
518+
cfg.Insecure = testInsecure
519+
520+
// Suggest --insecure flag if any RPC URL uses https without insecure mode
521+
for _, rpcURL := range []string{cfg.RPCURL, cfg.ELRPCURL} {
522+
if parsed, err := url.Parse(rpcURL); err == nil && parsed.Scheme == "https" && !cfg.Insecure {
523+
color.Yellow("Tip: If using self-signed certificates, consider adding --insecure flag")
524+
break
525+
}
526+
}
527+
515528
return playground.SendTestTransaction(ctx, cfg)
516529
},
517530
}
@@ -621,6 +634,7 @@ func main() {
621634
testCmd.Flags().StringVar(&testRPCURL, "rpc", "http://localhost:8545", "Target RPC URL for sending transactions")
622635
testCmd.Flags().StringVar(&testELRPCURL, "el-rpc", "", "EL RPC URL for chain queries (default: same as --rpc)")
623636
testCmd.Flags().DurationVar(&testTimeout, "timeout", 2*time.Minute, "Timeout for waiting for transaction receipt")
637+
testCmd.Flags().BoolVar(&testInsecure, "insecure", false, "Skip TLS certificate verification (for self-signed certs)")
624638
rootCmd.AddCommand(testCmd)
625639

626640
if err := rootCmd.Execute(); err != nil {

playground/test_tx.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package playground
33
import (
44
"context"
55
"crypto/ecdsa"
6+
"crypto/tls"
67
"fmt"
78
"math/big"
9+
"net/http"
810
"time"
911

1012
"github.com/ethereum/go-ethereum/common"
1113
"github.com/ethereum/go-ethereum/core/types"
1214
"github.com/ethereum/go-ethereum/crypto"
1315
"github.com/ethereum/go-ethereum/ethclient"
16+
"github.com/ethereum/go-ethereum/rpc"
1417
)
1518

1619
// TestTxConfig holds configuration for the test transaction
@@ -23,6 +26,7 @@ type TestTxConfig struct {
2326
GasLimit uint64
2427
GasPrice *big.Int
2528
Timeout time.Duration // Timeout for waiting for receipt. If 0, defaults to 2 minutes
29+
Insecure bool // Skip TLS certificate verification
2630
}
2731

2832
// DefaultTestTxConfig returns the default test transaction configuration
@@ -69,8 +73,25 @@ func SendTestTransaction(ctx context.Context, cfg *TestTxConfig) error {
6973
elRPCURL = cfg.RPCURL
7074
}
7175

76+
// dialRPC connects to an RPC endpoint, optionally skipping TLS verification
77+
dialRPC := func(url string) (*ethclient.Client, error) {
78+
if cfg.Insecure {
79+
httpClient := &http.Client{
80+
Transport: &http.Transport{
81+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
82+
},
83+
}
84+
rpcClient, err := rpc.DialOptions(ctx, url, rpc.WithHTTPClient(httpClient))
85+
if err != nil {
86+
return nil, err
87+
}
88+
return ethclient.NewClient(rpcClient), nil
89+
}
90+
return ethclient.Dial(url)
91+
}
92+
7293
// Connect to the EL RPC endpoint (for chain queries)
73-
elClient, err := ethclient.Dial(elRPCURL)
94+
elClient, err := dialRPC(elRPCURL)
7495
if err != nil {
7596
return fmt.Errorf("failed to connect to EL RPC: %w", err)
7697
}
@@ -79,7 +100,7 @@ func SendTestTransaction(ctx context.Context, cfg *TestTxConfig) error {
79100
// Connect to the target RPC endpoint (for sending transactions)
80101
var targetClient *ethclient.Client
81102
if cfg.RPCURL != elRPCURL {
82-
targetClient, err = ethclient.Dial(cfg.RPCURL)
103+
targetClient, err = dialRPC(cfg.RPCURL)
83104
if err != nil {
84105
return fmt.Errorf("failed to connect to target RPC: %w", err)
85106
}

0 commit comments

Comments
 (0)