jeth 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aaf74976d7f6689c2ef9001ed0465b0de8f82a96
4
+ data.tar.gz: 9d0d91f68aa668bcd1abd50ebb1e5a8bcebc2d6d
5
+ SHA512:
6
+ metadata.gz: 1130904dc15fceea008ecd106014b71636c364e1e6bf130aad201169a74c13792026ccafacdadddd05f17f2f9668b0b6f8dfafd627ba1e0f5f0772b3f00138d2
7
+ data.tar.gz: a5055ceafefbe1a5f1242ff471d519fae220b79027219e9d61b839e4cc209fdfb65b29e93879679f1fb438c1f3b80c7a72dc1b06252affdacd7ccfabf9534985
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at alexander.hanhikoski@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jeth.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alexander Hanhikoski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # Jeth
2
+
3
+ Jeth is a Ethereum JSON RPC client. It supports the IPC and HTTP protocols.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'jeth'
9
+ ```
10
+
11
+ Start your Ethereum node with the desired endpoints (IPC, HTTP and/or WS). For example with Geth:
12
+
13
+ $ geth --rpc --rpcapi "admin,debug,eth,miner,net,personal,shh,txpool,web3"
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ client = Jeth.create
19
+ client = Jeth::IpcClient.create
20
+ client = Jeth::HttpClient.create('http://foo.bar:8545')
21
+
22
+ client.eth_gas_price
23
+ => {"jsonrpc"=>"2.0", "id"=>1, "result"=>"0x4a817c800"}
24
+ ```
25
+
26
+ `Jeth.create(host_or_ipcpath)` automatically detects if you want HTTP or IPC and will initiate the appropriate client. It will default to IPC if nothing is specified. You can also use protocol clients explicitly:
27
+
28
+ ### Batching
29
+
30
+ ```ruby
31
+ client.batch do
32
+ client.eth_get_balance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', 'latest')
33
+ client.eth_get_balance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', 'pending')
34
+ end
35
+
36
+ => [{"jsonrpc"=>"2.0", "id"=>1, "result"=>"0x0"}, {"jsonrpc"=>"2.0", "id"=>2, "result"=>"0x0"}]
37
+ ```
38
+
39
+ The response array is guaranteed to hold the responses in the same order as their corresponding requests were called.
40
+
41
+ ## Methods
42
+
43
+ Jeth supports all methods from https://github.com/ethereum/wiki/wiki/JSON-RPC and https://github.com/ethereum/go-ethereum/wiki/Management-APIs. Method names are downcased, so for example `eth_getBlockByNumber` becomes `eth_get_block_by_number`.
44
+
45
+ ## Parameters
46
+
47
+ Jeth will automagically encode integer parameters as hex. That is `client.eth_get_block_by_number(0, false)` will be sent as `{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x0",false],"id":1}`.
48
+
49
+ You are free to use hex encoded parameters too `client.eth_get_block_by_number('0x0', false)`.
50
+
51
+ Note that Jeth won't prefix with '0x' if the user supplied hex encoded parameter is missing the prefix. However, at least Geth seems to tolerate unprefixed hex parameters.
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alexhanh/jeth.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
66
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jeth"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jeth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jeth"
8
+ spec.version = Jeth::VERSION
9
+ spec.authors = ["Alexander Hanhikoski"]
10
+ spec.email = ["alexander.hanhikoski@gmail.com"]
11
+
12
+ spec.summary = "Simple Ethereum JSON RPC client."
13
+ spec.description = "Simple Ethereum JSON RPC client. Supports IPC/HTTP and JSON RPC 2.0."
14
+ spec.homepage = "https://github.com/alexhanh/jeth"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ end
@@ -0,0 +1,15 @@
1
+ require 'jeth/version'
2
+ require 'jeth/util'
3
+ require 'jeth/client'
4
+ require 'jeth/http_client'
5
+ require 'jeth/ipc_client'
6
+
7
+ module Jeth
8
+ def self.create(host_or_ipcpath = nil)
9
+ return IpcClient.new if host_or_ipcpath.nil?
10
+ return IpcClient.new(host_or_ipcpath) if host_or_ipcpath.end_with? '.ipc'
11
+ return HttpClient.new(host_or_ipcpath) if host_or_ipcpath.start_with? 'http'
12
+
13
+ raise ArgumentError.new('Unable to detect client type. Please use "http://host:port" or "/path/to/geth.ipc".')
14
+ end
15
+ end
@@ -0,0 +1,76 @@
1
+ require 'json'
2
+
3
+ module Jeth
4
+ class Client
5
+ # https://github.com/ethereum/wiki/wiki/JSON-RPC
6
+ RPC_COMMANDS = %w(web3_clientVersion web3_sha3 net_version net_peerCount net_listening eth_protocolVersion eth_syncing eth_coinbase eth_mining eth_hashrate eth_gasPrice eth_accounts eth_blockNumber eth_getBalance eth_getStorageAt eth_getTransactionCount eth_getBlockTransactionCountByHash eth_getBlockTransactionCountByNumber eth_getUncleCountByBlockHash eth_getUncleCountByBlockNumber eth_getCode eth_sign eth_sendTransaction eth_sendRawTransaction eth_call eth_estimateGas eth_getBlockByHash eth_getBlockByNumber eth_getTransactionByHash eth_getTransactionByBlockHashAndIndex eth_getTransactionByBlockNumberAndIndex eth_getTransactionReceipt eth_getUncleByBlockHashAndIndex eth_getUncleByBlockNumberAndIndex eth_getCompilers eth_compileLLL eth_compileSolidity eth_compileSerpent eth_newFilter eth_newBlockFilter eth_newPendingTransactionFilter eth_uninstallFilter eth_getFilterChanges eth_getFilterLogs eth_getLogs eth_getWork eth_submitWork eth_submitHashrate db_putString db_getString db_putHex db_getHex shh_post shh_version shh_newIdentity shh_hasIdentity shh_newGroup shh_addToGroup shh_newFilter shh_uninstallFilter shh_getFilterChanges shh_getMessages)
7
+
8
+ # https://github.com/ethereum/go-ethereum/wiki/Management-APIs
9
+ RPC_MANAGEMENT_COMMANDS = %w(admin_addPeer admin_datadir admin_nodeInfo admin_peers admin_setSolc admin_startRPC admin_startWS admin_stopRPC admin_stopWS debug_backtraceAt debug_blockProfile debug_cpuProfile debug_dumpBlock debug_gcStats debug_getBlockRlp debug_goTrace debug_memStats debug_seedHash debug_setHead debug_setBlockProfileRate debug_stacks debug_startCPUProfile debug_startGoTrace debug_stopCPUProfile debug_stopGoTrace debug_traceBlock debug_traceBlockByNumber debug_traceBlockByHash debug_traceBlockFromFile debug_traceTransaction debug_verbosity debug_vmodule debug_writeBlockProfile debug_writeMemProfile miner_hashrate miner_makeDAG miner_setExtra miner_setGasPrice miner_start miner_startAutoDAG miner_stop miner_stopAutoDAG personal_importRawKey personal_listAccounts personal_lockAccount personal_newAccount personal_unlockAccount personal_sendTransaction txpool_content txpool_inspect txpool_status)
10
+
11
+ def initialize
12
+ @id = 0
13
+ @batch = nil
14
+ end
15
+
16
+ def batch
17
+ @batch = []
18
+
19
+ yield
20
+ result = send_batch(@batch)
21
+
22
+ @batch = nil
23
+ reset_id
24
+
25
+ return result
26
+ end
27
+
28
+ def send_batch(batch)
29
+ result = send_single(batch.to_json)
30
+ result = JSON.parse(result)
31
+
32
+ # Make sure the order is the same as it was when batching calls
33
+ # See 6 Batch here http://www.jsonrpc.org/specification
34
+ return result.sort_by! { |c| c['id'] }
35
+ end
36
+
37
+ def get_id
38
+ return @id += 1
39
+ end
40
+
41
+ def reset_id
42
+ @id = 0
43
+ end
44
+
45
+ # https://github.com/ethereum/wiki/wiki/JSON-RPC#output-hex-values
46
+ def encode_params(params)
47
+ return params.map do |p|
48
+ if p.is_a?(Integer)
49
+ Util.int_to_hex(p)
50
+ else
51
+ p
52
+ end
53
+ end
54
+ end
55
+
56
+ (RPC_COMMANDS + RPC_MANAGEMENT_COMMANDS).each do |rpc_command|
57
+ method_name = "#{Util.underscore(rpc_command)}"
58
+ define_method method_name do |*args|
59
+ payload = { jsonrpc: "2.0", method: rpc_command, params: encode_params(args), id: get_id }
60
+
61
+ if @batch
62
+ @batch << payload
63
+ return true
64
+ else
65
+ read = send_single(payload.to_json)
66
+ output = JSON.parse(read)
67
+ reset_id
68
+ return output
69
+ end
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
@@ -0,0 +1,33 @@
1
+ require 'net/http'
2
+
3
+ module Jeth
4
+ class HttpClient < Client
5
+ def initialize(host = 'http://localhost:8545')
6
+ super()
7
+ uri = URI.parse(host)
8
+ raise ArgumentError unless ['http', 'https'].include? uri.scheme
9
+ @host = uri.host
10
+ @port = uri.port
11
+
12
+ @ssl = uri.scheme == 'https'
13
+ if @ssl
14
+ @uri = URI("https://#{@host}:#{@port}")
15
+ else
16
+ @uri = URI("http://#{@host}:#{@port}")
17
+ end
18
+ end
19
+
20
+ def send_single(payload)
21
+ http = ::Net::HTTP.new(@host, @port)
22
+ if @ssl
23
+ http.use_ssl = true
24
+ end
25
+ header = {'Content-Type' => 'application/json'}
26
+ request = ::Net::HTTP::Post.new(@uri, header)
27
+ request.body = payload
28
+ response = http.request(request)
29
+ return response.body
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,24 @@
1
+ require 'socket'
2
+
3
+ module Jeth
4
+ class IpcClient < Client
5
+ def initialize(ipcpath = default_path)
6
+ super()
7
+ @ipcpath = ipcpath
8
+ end
9
+
10
+ def send_single(payload)
11
+ socket = UNIXSocket.new(@ipcpath)
12
+ socket.puts(payload)
13
+ read = socket.gets
14
+ socket.close
15
+ return read
16
+ end
17
+
18
+ def default_path
19
+ ["#{ENV['HOME']}/.ethereum/geth.ipc", "#{ENV['HOME']}/Library/Ethereum/geth.ipc"].each do |path|
20
+ return path if File.exists?(path)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ module Jeth
2
+ class Util
3
+ def self.underscore(s)
4
+ s.gsub(/::/, '/').
5
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
6
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
7
+ tr("-", "_").
8
+ downcase
9
+ end
10
+
11
+ def self.int_to_hex(n)
12
+ "0x#{n.to_s(16)}"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Jeth
2
+ VERSION = "0.0.11"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jeth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.11
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Hanhikoski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Simple Ethereum JSON RPC client. Supports IPC/HTTP and JSON RPC 2.0.
56
+ email:
57
+ - alexander.hanhikoski@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - jeth.gemspec
72
+ - lib/jeth.rb
73
+ - lib/jeth/client.rb
74
+ - lib/jeth/http_client.rb
75
+ - lib/jeth/ipc_client.rb
76
+ - lib/jeth/util.rb
77
+ - lib/jeth/version.rb
78
+ homepage: https://github.com/alexhanh/jeth
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Simple Ethereum JSON RPC client.
102
+ test_files: []
103
+ has_rdoc: