Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

eth/client: implement websockets client #285

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions eth.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ Gem::Specification.new do |spec|

# scrypt for encrypted key derivation
spec.add_dependency "scrypt", "~> 3.0"

# websocket api for clients
spec.add_dependency "websocket", "~> 1.2"
end
3 changes: 3 additions & 0 deletions lib/eth/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ContractExecutionError < StandardError; end
def self.create(host)
return Client::Ipc.new host if host.end_with? ".ipc"
return Client::Http.new host if host.start_with? "http"
return Client::Ws.new host if host.start_with? "ws"
raise ArgumentError, "Unable to detect client type!"
end

Expand Down Expand Up @@ -487,6 +488,7 @@ def send_command(command, args)

# Increments the request id.
def next_id
@id ||= 0
@id += 1
end

Expand Down Expand Up @@ -523,3 +525,4 @@ def marshal(params)
# Load the client/* libraries
require "eth/client/http"
require "eth/client/ipc"
require "eth/client/ws"
89 changes: 89 additions & 0 deletions lib/eth/client/ws.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright (c) 2016-2023 The Ruby-Eth Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "websocket"
require "socket"

# Provides the {Eth} module.
module Eth

# Provides a Websocket-RPC client.
class Client::Ws < Client

# The host of the Websocket endpoint.
attr_reader :host

# The port of the Websocket endpoint.
attr_reader :port

# The full URI of the Websocket endpoint, including path.
attr_reader :uri

# Attribute indicator for SSL.
attr_reader :ssl

# Constructor for the Websocket Client. Should not be used; use
# {Client.create} instead.
#
# @param host [String] an URI pointing to an Websocket RPC-API.
def initialize(host)
super
uri = URI.parse(host)
raise ArgumentError, "Unable to parse the WS-URI!" unless ["ws", "wss"].include? uri.scheme
@host = uri.host
@port = uri.port
@ssl = uri.scheme == "wss"
@uri = URI("#{uri.scheme}://#{@host}:#{@port}#{uri.path}")
@ws = WebSocket::Handshake::Client.new(
url: @uri.to_s,
)
end

# Sends an RPC request to the connected Websocket client.
#
# @param payload [Hash] the RPC request parameters.
# @return [String] a JSON-encoded response.
def send_request(payload)

socket = TCPSocket.new @host, @port
if ssl
socket = OpenSSL::SSL::SSLSocket.new(socket, OpenSSL::SSL::SSLContext.new)
socket.sync_close = true
socket.connect
end

frame = WebSocket::Frame::Outgoing::Client.new(
version: @ws.version,
data: payload,
type: 'text'
)

socket.write(frame.to_s)
socket.flush

# to do
# response = socket.read

socket.close

# to do remove
return '{"jsonrpc":"2.0","id":1,"result":"0x539"}'
end

private

attr_reader :ws

end
end
14 changes: 14 additions & 0 deletions spec/eth/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
# to provide both http and ipc to pass these tests.
let(:geth_ipc_path) { "/tmp/geth.ipc" }
let(:geth_http_path) { "http://127.0.0.1:8545" }
let(:geth_ws_path) { "ws://127.0.0.1:8546" }
let(:geth_http_authed_path) { "http://username:password@127.0.0.1:8545" }
let(:geth_http_query_path) { "http://127.0.0.1:8545?foo=bar&asdf=qwer" }
subject(:geth_ipc) { Client.create geth_ipc_path }
subject(:geth_http) { Client.create geth_http_path }
subject(:geth_ws) { Client.create geth_ws_path }
subject(:geth_http_authed) { Client.create geth_http_authed_path }
subject(:geth_http_query) { Client.create geth_http_query_path }

Expand All @@ -26,6 +28,18 @@
expect(geth_ipc.path).to eq geth_ipc_path
end

it "creates an ws client" do
expect(geth_ws).to be
expect(geth_ws).to be_instance_of Client::Ws
expect(geth_ws.host).to eq "127.0.0.1"
expect(geth_ws.port).to eq 8546
expect(geth_ws.uri.to_s).to eq geth_ws_path
expect(geth_ws.ssl).to be_falsy

### to do remove
geth_ws.chain_id
end

it "creates an http client" do
expect(geth_http).to be
expect(geth_http).to be_instance_of Client::Http
Expand Down
Loading