Skip to content

Commit 347eb59

Browse files
authored
Merge pull request #664 from 0xPolygon/jihwan/ulxly-insecure-flag
feat: add --insecure flag for TLS verification skip
2 parents 99f93fd + 9d94c79 commit 347eb59

14 files changed

+161
-32
lines changed

cmd/ulxly/ulxly.go

Lines changed: 148 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"context"
77
"crypto/ecdsa"
8+
"crypto/tls"
89
_ "embed"
910
"encoding/binary"
1011
"encoding/json"
@@ -30,11 +31,11 @@ import (
3031
ethclient "github.com/ethereum/go-ethereum/ethclient"
3132
ethrpc "github.com/ethereum/go-ethereum/rpc"
3233

33-
smcerror "github.com/0xPolygon/polygon-cli/errors"
3434
"github.com/0xPolygon/polygon-cli/bindings/tokens"
3535
"github.com/0xPolygon/polygon-cli/bindings/ulxly"
3636
"github.com/0xPolygon/polygon-cli/bindings/ulxly/polygonrollupmanager"
3737
"github.com/0xPolygon/polygon-cli/cmd/flag_loader"
38+
smcerror "github.com/0xPolygon/polygon-cli/errors"
3839
"github.com/rs/zerolog/log"
3940
"github.com/spf13/cobra"
4041
)
@@ -117,13 +118,27 @@ func readDeposit(cmd *cobra.Command) error {
117118
fromBlock := getEvent.FromBlock
118119
filter := getEvent.FilterSize
119120

120-
// Dial the Ethereum RPC server.
121-
rpc, err := ethrpc.DialContext(cmd.Context(), rpcUrl)
122-
if err != nil {
123-
log.Error().Err(err).Msg("Unable to Dial RPC")
124-
return err
121+
// Use the new helper function
122+
var rpc *ethrpc.Client
123+
var err error
124+
125+
if getEvent.Insecure {
126+
client, clientErr := createInsecureEthClient(rpcUrl)
127+
if clientErr != nil {
128+
log.Error().Err(clientErr).Msg("Unable to create insecure client")
129+
return clientErr
130+
}
131+
defer client.Close()
132+
rpc = client.Client()
133+
} else {
134+
rpc, err = ethrpc.DialContext(cmd.Context(), rpcUrl)
135+
if err != nil {
136+
log.Error().Err(err).Msg("Unable to Dial RPC")
137+
return err
138+
}
139+
defer rpc.Close()
125140
}
126-
defer rpc.Close()
141+
127142
ec := ethclient.NewClient(rpc)
128143

129144
bridgeV2, err := ulxly.NewUlxly(common.HexToAddress(bridgeAddress), ec)
@@ -193,13 +208,27 @@ func readClaim(cmd *cobra.Command) error {
193208
fromBlock := getEvent.FromBlock
194209
filter := getEvent.FilterSize
195210

196-
// Dial the Ethereum RPC server.
197-
rpc, err := ethrpc.DialContext(cmd.Context(), rpcUrl)
198-
if err != nil {
199-
log.Error().Err(err).Msg("Unable to Dial RPC")
200-
return err
211+
// Use the new helper function
212+
var rpc *ethrpc.Client
213+
var err error
214+
215+
if getEvent.Insecure {
216+
client, clientErr := createInsecureEthClient(rpcUrl)
217+
if clientErr != nil {
218+
log.Error().Err(clientErr).Msg("Unable to create insecure client")
219+
return clientErr
220+
}
221+
defer client.Close()
222+
rpc = client.Client()
223+
} else {
224+
rpc, err = ethrpc.DialContext(cmd.Context(), rpcUrl)
225+
if err != nil {
226+
log.Error().Err(err).Msg("Unable to Dial RPC")
227+
return err
228+
}
229+
defer rpc.Close()
201230
}
202-
defer rpc.Close()
231+
203232
ec := ethclient.NewClient(rpc)
204233

205234
bridgeV2, err := ulxly.NewUlxly(common.HexToAddress(bridgeAddress), ec)
@@ -251,20 +280,35 @@ func readClaim(cmd *cobra.Command) error {
251280

252281
return nil
253282
}
283+
254284
func readVerifyBatches(cmd *cobra.Command) error {
255285
rollupManagerAddress := getVerifyBatchesOptions.RollupManagerAddress
256286
rpcUrl := getEvent.URL
257287
toBlock := getEvent.ToBlock
258288
fromBlock := getEvent.FromBlock
259289
filter := getEvent.FilterSize
260290

261-
// Dial the Ethereum RPC server.
262-
rpc, err := ethrpc.DialContext(cmd.Context(), rpcUrl)
263-
if err != nil {
264-
log.Error().Err(err).Msg("Unable to Dial RPC")
265-
return err
291+
// Use the new helper function
292+
var rpc *ethrpc.Client
293+
var err error
294+
295+
if getEvent.Insecure {
296+
client, clientErr := createInsecureEthClient(rpcUrl)
297+
if clientErr != nil {
298+
log.Error().Err(clientErr).Msg("Unable to create insecure client")
299+
return clientErr
300+
}
301+
defer client.Close()
302+
rpc = client.Client()
303+
} else {
304+
rpc, err = ethrpc.DialContext(cmd.Context(), rpcUrl)
305+
if err != nil {
306+
log.Error().Err(err).Msg("Unable to Dial RPC")
307+
return err
308+
}
309+
defer rpc.Close()
266310
}
267-
defer rpc.Close()
311+
268312
client := ethclient.NewClient(rpc)
269313
rm := common.HexToAddress(rollupManagerAddress)
270314
rollupManager, err := polygonrollupmanager.NewPolygonrollupmanager(rm, client)
@@ -322,10 +366,20 @@ func proof(args []string) error {
322366
func balanceTree() error {
323367
l2NetworkID := balanceTreeOptions.L2NetworkID
324368
bridgeAddress := common.HexToAddress(balanceTreeOptions.BridgeAddress)
325-
client, err := ethclient.DialContext(context.Background(), balanceTreeOptions.RpcURL)
369+
370+
var client *ethclient.Client
371+
var err error
372+
373+
if balanceTreeOptions.Insecure {
374+
client, err = createInsecureEthClient(balanceTreeOptions.RpcURL)
375+
} else {
376+
client, err = ethclient.DialContext(context.Background(), balanceTreeOptions.RpcURL)
377+
}
378+
326379
if err != nil {
327380
return err
328381
}
382+
defer client.Close()
329383
l2RawClaimsData, l2RawDepositsData, err := getBalanceTreeData()
330384
if err != nil {
331385
return err
@@ -362,10 +416,20 @@ func nullifierTree(args []string) error {
362416
func nullifierAndBalanceTree(args []string) error {
363417
l2NetworkID := balanceTreeOptions.L2NetworkID
364418
bridgeAddress := common.HexToAddress(balanceTreeOptions.BridgeAddress)
365-
client, err := ethclient.DialContext(context.Background(), balanceTreeOptions.RpcURL)
419+
420+
var client *ethclient.Client
421+
var err error
422+
423+
if balanceTreeOptions.Insecure {
424+
client, err = createInsecureEthClient(balanceTreeOptions.RpcURL)
425+
} else {
426+
client, err = ethclient.DialContext(context.Background(), balanceTreeOptions.RpcURL)
427+
}
428+
366429
if err != nil {
367430
return err
368431
}
432+
defer client.Close()
369433
l2RawClaimsData, l2RawDepositsData, err := getBalanceTreeData()
370434
if err != nil {
371435
return err
@@ -616,7 +680,7 @@ func logAndReturnJsonError(ctx context.Context, client *ethclient.Client, tx *ty
616680
errLog = errLog.Err(callErr)
617681
}
618682

619-
customErr := errors.New(err.Error()+": " + reason)
683+
customErr := errors.New(err.Error() + ": " + reason)
620684
if errCode, isValid := jsonError.Data.(string); isValid && errCode == "0x646cf558" {
621685
// I don't want to bother with the additional error logging for previously claimed deposits
622686
return customErr
@@ -685,8 +749,7 @@ func bridgeAsset(cmd *cobra.Command) error {
685749
timeoutTxnReceipt := *inputUlxlyArgs.timeout
686750
RPCURL := *inputUlxlyArgs.rpcURL
687751

688-
// Dial the Ethereum RPC server.
689-
client, err := ethclient.DialContext(cmd.Context(), RPCURL)
752+
client, err := createEthClient(cmd.Context(), RPCURL)
690753
if err != nil {
691754
log.Error().Err(err).Msg("Unable to Dial RPC")
692755
return err
@@ -775,7 +838,7 @@ func bridgeMessage(cmd *cobra.Command) error {
775838
RPCURL := *inputUlxlyArgs.rpcURL
776839

777840
// Dial the Ethereum RPC server.
778-
client, err := ethclient.DialContext(cmd.Context(), RPCURL)
841+
client, err := createEthClient(cmd.Context(), RPCURL)
779842
if err != nil {
780843
log.Error().Err(err).Msg("Unable to Dial RPC")
781844
return err
@@ -828,7 +891,7 @@ func bridgeWETHMessage(cmd *cobra.Command) error {
828891
RPCURL := *inputUlxlyArgs.rpcURL
829892

830893
// Dial the Ethereum RPC server.
831-
client, err := ethclient.DialContext(cmd.Context(), RPCURL)
894+
client, err := createEthClient(cmd.Context(), RPCURL)
832895
if err != nil {
833896
log.Error().Err(err).Msg("Unable to Dial RPC")
834897
return err
@@ -886,7 +949,7 @@ func claimAsset(cmd *cobra.Command) error {
886949
wait := *inputUlxlyArgs.wait
887950

888951
// Dial Ethereum client
889-
client, err := ethclient.DialContext(cmd.Context(), RPCURL)
952+
client, err := createEthClient(cmd.Context(), RPCURL)
890953
if err != nil {
891954
log.Error().Err(err).Msg("Unable to Dial RPC")
892955
return err
@@ -940,7 +1003,7 @@ func claimMessage(cmd *cobra.Command) error {
9401003
wait := *inputUlxlyArgs.wait
9411004

9421005
// Dial Ethereum client
943-
client, err := ethclient.DialContext(cmd.Context(), RPCURL)
1006+
client, err := createEthClient(cmd.Context(), RPCURL)
9441007
if err != nil {
9451008
log.Error().Err(err).Msg("Unable to Dial RPC")
9461009
return err
@@ -1078,7 +1141,7 @@ func claimEverything(cmd *cobra.Command) error {
10781141
}
10791142
}
10801143

1081-
client, err := ethclient.DialContext(cmd.Context(), RPCURL)
1144+
client, err := createEthClient(cmd.Context(), RPCURL)
10821145
if err != nil {
10831146
log.Error().Err(err).Msg("Unable to Dial RPC")
10841147
return err
@@ -1802,8 +1865,25 @@ func generateTransactionPayload(ctx context.Context, client *ethclient.Client, u
18021865
return bridgeV2, toAddress, opts, err
18031866
}
18041867

1868+
// Helper function to get the appropriate HTTP client
1869+
func getHTTPClient() *http.Client {
1870+
if *inputUlxlyArgs.insecure {
1871+
log.Warn().Msg("WARNING: Using insecure HTTP client for bridge service requests")
1872+
return &http.Client{
1873+
Timeout: 30 * time.Second,
1874+
Transport: &http.Transport{
1875+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
1876+
},
1877+
}
1878+
}
1879+
return &http.Client{
1880+
Timeout: 30 * time.Second,
1881+
}
1882+
}
1883+
18051884
func getMerkleProofsExitRoots(bridgeServiceProofEndpoint string) (merkleProofArray [32][32]byte, rollupMerkleProofArray [32][32]byte, mainExitRoot []byte, rollupExitRoot []byte) {
1806-
reqBridgeProof, err := http.Get(bridgeServiceProofEndpoint)
1885+
client := getHTTPClient()
1886+
reqBridgeProof, err := client.Get(bridgeServiceProofEndpoint)
18071887
if err != nil {
18081888
log.Error().Err(err)
18091889
return
@@ -1851,7 +1931,8 @@ func getMerkleProofsExitRoots(bridgeServiceProofEndpoint string) (merkleProofArr
18511931
}
18521932

18531933
func getDeposit(bridgeServiceDepositsEndpoint string) (globalIndex *big.Int, originAddress common.Address, amount *big.Int, metadata []byte, leafType uint8, claimDestNetwork, claimOriginalNetwork uint32, err error) {
1854-
reqBridgeDeposit, err := http.Get(bridgeServiceDepositsEndpoint)
1934+
client := getHTTPClient()
1935+
reqBridgeDeposit, err := client.Get(bridgeServiceDepositsEndpoint)
18551936
if err != nil {
18561937
log.Error().Err(err)
18571938
return
@@ -1909,7 +1990,8 @@ func getDepositsForAddress(bridgeRequestUrl string) ([]BridgeDeposit, error) {
19091990
Deposits []BridgeDeposit `json:"deposits"`
19101991
Total int `json:"total_cnt,string"`
19111992
}
1912-
httpResp, err := http.Get(bridgeRequestUrl)
1993+
client := getHTTPClient()
1994+
httpResp, err := client.Get(bridgeRequestUrl)
19131995
if err != nil {
19141996
return nil, err
19151997
}
@@ -1929,6 +2011,33 @@ func getDepositsForAddress(bridgeRequestUrl string) ([]BridgeDeposit, error) {
19292011
return resp.Deposits, nil
19302012
}
19312013

2014+
// Add the helper function to create an insecure client
2015+
func createInsecureEthClient(rpcURL string) (*ethclient.Client, error) {
2016+
// WARNING: This disables TLS certificate verification
2017+
log.Warn().Msg("WARNING: TLS certificate verification is disabled. This is unsafe for production use.")
2018+
2019+
httpClient := &http.Client{
2020+
Transport: &http.Transport{
2021+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
2022+
},
2023+
}
2024+
2025+
rpcClient, err := ethrpc.DialOptions(context.Background(), rpcURL, ethrpc.WithHTTPClient(httpClient))
2026+
if err != nil {
2027+
return nil, err
2028+
}
2029+
2030+
return ethclient.NewClient(rpcClient), nil
2031+
}
2032+
2033+
// Add helper function to create either secure or insecure client based on flag
2034+
func createEthClient(ctx context.Context, rpcURL string) (*ethclient.Client, error) {
2035+
if *inputUlxlyArgs.insecure {
2036+
return createInsecureEthClient(rpcURL)
2037+
}
2038+
return ethclient.DialContext(ctx, rpcURL)
2039+
}
2040+
19322041
//go:embed BridgeAssetUsage.md
19332042
var bridgeAssetUsage string
19342043

@@ -2040,6 +2149,7 @@ type ulxlyArgs struct {
20402149
bridgeOffset *int
20412150
wait *time.Duration
20422151
concurrency *uint
2152+
insecure *bool
20432153
}
20442154

20452155
var inputUlxlyArgs = ulxlyArgs{}
@@ -2106,6 +2216,7 @@ const (
21062216
ArgBridgeOffset = "bridge-offset"
21072217
ArgWait = "wait"
21082218
ArgConcurrency = "concurrency"
2219+
ArgInsecure = "insecure"
21092220
)
21102221

21112222
func prepInputs(cmd *cobra.Command, args []string) error {
@@ -2164,6 +2275,7 @@ func (o *FileOptions) AddFlags(cmd *cobra.Command) {
21642275
type BalanceTreeOptions struct {
21652276
L2ClaimsFile, L2DepositsFile, BridgeAddress, RpcURL string
21662277
L2NetworkID uint32
2278+
Insecure bool
21672279
}
21682280

21692281
func (o *BalanceTreeOptions) AddFlags(cmd *cobra.Command) {
@@ -2172,6 +2284,7 @@ func (o *BalanceTreeOptions) AddFlags(cmd *cobra.Command) {
21722284
cmd.Flags().StringVarP(&o.BridgeAddress, ArgBridgeAddress, "", "", "Bridge Address")
21732285
cmd.Flags().StringVarP(&o.RpcURL, ArgRPCURL, "r", "", "RPC URL")
21742286
cmd.Flags().Uint32VarP(&o.L2NetworkID, ArgL2NetworkID, "", 0, "The L2 networkID")
2287+
cmd.Flags().BoolVarP(&o.Insecure, ArgInsecure, "", false, "skip TLS certificate verification")
21752288
}
21762289

21772290
type ProofOptions struct {
@@ -2195,13 +2308,15 @@ func (o *RollupsProofOptions) AddFlags(cmd *cobra.Command) {
21952308
type GetEvent struct {
21962309
URL string
21972310
FromBlock, ToBlock, FilterSize uint64
2311+
Insecure bool
21982312
}
21992313

22002314
func (o *GetEvent) AddFlags(cmd *cobra.Command) {
22012315
cmd.Flags().StringVarP(&o.URL, ArgRPCURL, "u", "", "The RPC URL to read the events data")
22022316
cmd.Flags().Uint64VarP(&o.FromBlock, ArgFromBlock, "f", 0, "The start of the range of blocks to retrieve")
22032317
cmd.Flags().Uint64VarP(&o.ToBlock, ArgToBlock, "t", 0, "The end of the range of blocks to retrieve")
22042318
cmd.Flags().Uint64VarP(&o.FilterSize, ArgFilterSize, "i", 1000, "The batch size for individual filter queries")
2319+
cmd.Flags().BoolVarP(&o.Insecure, ArgInsecure, "", false, "skip TLS certificate verification")
22052320
fatalIfError(cmd.MarkFlagRequired(ArgFromBlock))
22062321
fatalIfError(cmd.MarkFlagRequired(ArgToBlock))
22072322
fatalIfError(cmd.MarkFlagRequired(ArgRPCURL))
@@ -2440,6 +2555,7 @@ or if it's actually an intermediate hash.`,
24402555
inputUlxlyArgs.timeout = ulxlyBridgeAndClaimCmd.PersistentFlags().Uint64(ArgTimeout, 60, "the amount of time to wait while trying to confirm a transaction receipt")
24412556
inputUlxlyArgs.gasPrice = ulxlyBridgeAndClaimCmd.PersistentFlags().String(ArgGasPrice, "", "the gas price to be used")
24422557
inputUlxlyArgs.dryRun = ulxlyBridgeAndClaimCmd.PersistentFlags().Bool(ArgDryRun, false, "do all of the transaction steps but do not send the transaction")
2558+
inputUlxlyArgs.insecure = ulxlyBridgeAndClaimCmd.PersistentFlags().Bool(ArgInsecure, false, "skip TLS certificate verification")
24432559
fatalIfError(ulxlyBridgeAndClaimCmd.MarkPersistentFlagRequired(ArgBridgeAddress))
24442560

24452561
// bridge specific args

doc/polycli_ulxly_bridge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The command also inherits flags from parent commands.
3535
--dry-run do all of the transaction steps but do not send the transaction
3636
--gas-limit uint force a gas limit when sending a transaction
3737
--gas-price string the gas price to be used
38+
--insecure skip TLS certificate verification
3839
--pretty-logs Should logs be in pretty format or JSON (default true)
3940
--private-key string the hex encoded private key to be used when sending the tx
4041
--rpc-url string the URL of the RPC to send the transaction

doc/polycli_ulxly_bridge_asset.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ The command also inherits flags from parent commands.
109109
--force-update-root indicates if the new global exit root is updated or not (default true)
110110
--gas-limit uint force a gas limit when sending a transaction
111111
--gas-price string the gas price to be used
112+
--insecure skip TLS certificate verification
112113
--pretty-logs Should logs be in pretty format or JSON (default true)
113114
--private-key string the hex encoded private key to be used when sending the tx
114115
--rpc-url string the URL of the RPC to send the transaction

doc/polycli_ulxly_bridge_message.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ The command also inherits flags from parent commands.
106106
--force-update-root indicates if the new global exit root is updated or not (default true)
107107
--gas-limit uint force a gas limit when sending a transaction
108108
--gas-price string the gas price to be used
109+
--insecure skip TLS certificate verification
109110
--pretty-logs Should logs be in pretty format or JSON (default true)
110111
--private-key string the hex encoded private key to be used when sending the tx
111112
--rpc-url string the URL of the RPC to send the transaction

0 commit comments

Comments
 (0)