diff --git a/core/rpc.go b/core/rpc.go index 0ab4b46..5f126db 100644 --- a/core/rpc.go +++ b/core/rpc.go @@ -9,6 +9,7 @@ import ( "io" "log" "net/http" + "net/url" "sync" "sync/atomic" "time" @@ -32,13 +33,22 @@ const ( // NewCoreRPCClient returns a Dogecoin Core Node client. // Thread-safe, can be shared across Goroutines. -func NewCoreRPCClient(rpcHost string, rpcPort int, rpcUser string, rpcPass string) spec.Blockchain { - url := fmt.Sprintf("http://%s:%d", rpcHost, rpcPort) - return &CoreRPCClient{url: url, user: rpcUser, pass: rpcPass, retryDelay: 5 * time.Second} +// If rpcHostOrUrl is a URL, it will be used directly. +// If rpcHostOrUrl is a hostname, it will be used with the port. +func NewCoreRPCClient(rpcHostOrUrl string, rpcPort int, rpcUser string, rpcPass string) spec.Blockchain { + parsedUrl, err := url.Parse(rpcHostOrUrl) + var url string + if err != nil || parsedUrl.Scheme == "" { + url = fmt.Sprintf("http://%s:%d", rpcHostOrUrl, rpcPort) + } else { + url = rpcHostOrUrl + } + + return &CoreRPCClient{URL: url, user: rpcUser, pass: rpcPass, retryDelay: 5 * time.Second} } type CoreRPCClient struct { - url string + URL string user string pass string id atomic.Uint64 // next unique request id @@ -364,7 +374,7 @@ func (c *CoreRPCClient) Request(ctx context.Context, method string, params []any if err != nil { return 0, fmt.Errorf("json-rpc marshal request: %v", err) } - req, err := http.NewRequestWithContext(ctx, "POST", c.url, bytes.NewBuffer(payload)) + req, err := http.NewRequestWithContext(ctx, "POST", c.URL, bytes.NewBuffer(payload)) if err != nil { return 0, fmt.Errorf("json-rpc request: %v", err) } diff --git a/core/rpc_test.go b/core/rpc_test.go new file mode 100644 index 0000000..46fc7e9 --- /dev/null +++ b/core/rpc_test.go @@ -0,0 +1,27 @@ +package core + +import ( + "testing" +) + +func TestNewCoreRPCClient(t *testing.T) { + rpc := NewCoreRPCClient("http://127.0.0.1:22555", 0, "dogecoin", "dogecoin") + if rpc == nil { + t.Fatal("NewCoreRPCClient returned nil") + } + + if rpc.(*CoreRPCClient).URL != "http://127.0.0.1:22555" { + t.Fatal("GetURL returned wrong URL") + } +} + +func TestNewCoreRPCClientWithHostname(t *testing.T) { + rpc := NewCoreRPCClient("hostname", 22555, "dogecoin", "dogecoin") + if rpc == nil { + t.Fatal("NewCoreRPCClient returned nil") + } + + if rpc.(*CoreRPCClient).URL != "http://hostname:22555" { + t.Fatal("GetURL returned wrong URL") + } +}