Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Adds a few improvements and makes the client work again #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 1 deletion ethereum.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "pry"

spec.add_dependency "activesupport"
spec.add_dependency "digest-sha3", "~> 1.1"
spec.add_dependency "sha3", "~> 0.2.6"
# spec.add_dependency "sha3-pure-ruby", "~>0.1.1"
end
3 changes: 2 additions & 1 deletion lib/ethereum.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "ethereum/version"
require 'active_support'
require 'active_support/core_ext'
require 'digest/sha3'
require 'sha3'
# require 'sha3-pure-ruby'

module Ethereum
require 'ethereum/client'
Expand Down
24 changes: 23 additions & 1 deletion lib/ethereum/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def initialize(log = false)
end
end

def self.create(host_or_ipcpath, log = false)
return IpcClient.new(host_or_ipcpath, log) if host_or_ipcpath.end_with? '.ipc'
return HttpClient.new(host_or_ipcpath, log) if host_or_ipcpath.start_with? 'http'
raise ArgumentError.new('Unable to detect client type')
end

def batch
@batch = []

Expand All @@ -38,14 +44,30 @@ def reset_id
@id = 0
end

def int_to_hex(n)
return "0x#{n.to_s(16)}"
end

# https://github.com/ethereum/wiki/wiki/JSON-RPC#output-hex-values
def encode_params(params)
return params.map do |p|
if p.is_a?(Integer)
int_to_hex(p)
else
p
end
end
end

(RPC_COMMANDS + RPC_MANAGEMENT_COMMANDS).each do |rpc_command|
method_name = "#{rpc_command.underscore}"
define_method method_name do |*args|
command = rpc_command
if command == "eth_call"
args << "latest"
end
payload = {jsonrpc: "2.0", method: command, params: args, id: get_id}
payload = {jsonrpc: "2.0", method: command, params: encode_params(args), id: get_id}

if @log == true
@logger.info("Sending #{payload.to_json}")
end
Expand Down
3 changes: 2 additions & 1 deletion lib/ethereum/contract_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def initialize(data)
@input_types = data["inputs"].collect {|x| x["type"]}
@inputs = data["inputs"].collect {|x| x["name"]}
@event_string = "#{@name}(#{@input_types.join(",")})"
@signature = Digest::SHA3.hexdigest(@event_string, 256)
@signature = SHA3::Digest::SHA256.hexdigest(@event_string)
# @signature = Digest::SHA3.hexdigest @event_string, 256
end

def set_address(address)
Expand Down
3 changes: 2 additions & 1 deletion lib/ethereum/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def initialize(data)
Ethereum::FunctionOutput.new(output)
end
@function_string = "#{@name}(#{@inputs.collect {|x| x.type }.join(",")})"
@signature = Digest::SHA3.hexdigest(@function_string, 256)[0..7]
@signature = SHA3::Digest::SHA256.hexdigest(@function_string)[0..7]
# @signature = Digest::SHA3.hexdigest(@function_string, 256)[0..7]
end

end
Expand Down
11 changes: 7 additions & 4 deletions lib/ethereum/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ module Ethereum
class HttpClient < Client
attr_accessor :host, :port, :uri, :ssl

def initialize(host, port, ssl = false, log = false)
def initialize(host, log = false)
super(log)
@host = host
@port = port
@ssl = ssl
uri = URI.parse(host)
raise ArgumentError unless ['http', 'https'].include? uri.scheme
@host = uri.host
@port = uri.port

@ssl = uri.scheme == 'https'
if ssl
@uri = URI("https://#{@host}:#{@port}")
else
Expand Down
9 changes: 9 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require 'spec_helper'

describe Ethereum do
describe 'Client' do
it 'should encode parameters' do
params = [true, false, 0, 12345, '0x7d84abf0f241b10927b567bd636d95fa9f66ae34', '0x4d5e07d4057dd0c3849c2295d20ee1778fc29d69150e8d75a07207347dce17fa', '7d84abf0f241b10927b567bd636d95fa9f66ae34']
client = Ethereum::Client.new
encoded_params = client.encode_params(params)
expect(encoded_params).to eq([true, false, '0x0', '0x3039', '0x7d84abf0f241b10927b567bd636d95fa9f66ae34', '0x4d5e07d4057dd0c3849c2295d20ee1778fc29d69150e8d75a07207347dce17fa', '0x7d84abf0f241b10927b567bd636d95fa9f66ae34'])
end
end

describe 'HttpClient' do
it 'should work' do
client = Ethereum::HttpClient.new('localhost', '8545')
Expand Down