From 6737cf0edf4b04997ba80fa2d94116b2536c45d3 Mon Sep 17 00:00:00 2001 From: Ljubisa Gacevic Date: Tue, 23 Sep 2025 16:46:55 +0200 Subject: [PATCH] fix(node): support basic auth in blockchain RPC endpoint URLs --- cmd/bee/cmd/db.go | 1 - pkg/node/chain.go | 28 ++++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cmd/bee/cmd/db.go b/cmd/bee/cmd/db.go index 62eee2591c2..8bca2ed31cc 100644 --- a/cmd/bee/cmd/db.go +++ b/cmd/bee/cmd/db.go @@ -31,7 +31,6 @@ import ( const ( optionNameValidation = "validate" - optionNameValidationPin = "validate-pin" optionNameCollectionPin = "pin" optionNameOutputLocation = "output" ) diff --git a/pkg/node/chain.go b/pkg/node/chain.go index c1f00dd61bc..aa1ef49749f 100644 --- a/pkg/node/chain.go +++ b/pkg/node/chain.go @@ -6,10 +6,13 @@ package node import ( "context" + "encoding/base64" "encoding/hex" "errors" "fmt" "math/big" + "net/http" + "net/url" "strings" "time" @@ -59,15 +62,32 @@ func InitChain( } if chainEnabled { - // connect to the real one - rpcClient, err := rpc.DialContext(ctx, endpoint) + parsedURL, err := url.Parse(endpoint) + if err != nil { + return nil, common.Address{}, 0, nil, nil, fmt.Errorf("parse blockchain endpoint: %w", err) + } + + opts := []rpc.ClientOption{} + + if parsedURL.User != nil { + authOpt := rpc.WithHTTPAuth(func(h http.Header) error { + username := parsedURL.User.Username() + password, _ := parsedURL.User.Password() + h.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password))) + return nil + }) + parsedURL.User = nil + endpoint = parsedURL.String() + opts = append(opts, authOpt) + } + + rpcClient, err := rpc.DialOptions(ctx, endpoint, opts...) if err != nil { return nil, common.Address{}, 0, nil, nil, fmt.Errorf("dial blockchain client: %w", err) } var versionString string - err = rpcClient.CallContext(ctx, &versionString, "web3_clientVersion") - if err != nil { + if err = rpcClient.CallContext(ctx, &versionString, "web3_clientVersion"); err != nil { logger.Info("could not connect to backend; in a swap-enabled network a working blockchain node (for xdai network in production, sepolia in testnet) is required; check your node or specify another node using --blockchain-rpc-endpoint.", "backend_endpoint", endpoint) return nil, common.Address{}, 0, nil, nil, fmt.Errorf("blockchain client get version: %w", err) }