elchapo 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 027006a7542f71dd3e5a0a2b13d67f5ad61b4587
4
- data.tar.gz: 6bdcee39b88372882d96924c7a107c9fcaaaf0ea
3
+ metadata.gz: 3ef89a15a572c27a49e4066309407551be248f52
4
+ data.tar.gz: b0ecc19e544d7c0b78f11de540b9bc6b32be2e5c
5
5
  SHA512:
6
- metadata.gz: 44ebfbe6bc94a2edffadb872eb963531c2c69a1b49a3e3de85c030f96c406915fca688e2847c1c334f67ad095058b92b43e51b345dc81399c606f210fff6a137
7
- data.tar.gz: e0d5603dd8d89c22a5ca8fc32bde4f42a8019ee0a4e6877fe9b5bf6ef745936fcc783dddb54ebcb783a3dd783727bb80a40921a60af95da11e00c6e57647c540
6
+ metadata.gz: 0c00f2979a40759c7b43af5bfcbf742f7a650013a959051ee7ff63796fd49fa2835a09d6fcade15c1a694b9d6fe6dedf0877450e1a4c03837657eb02a19957f1
7
+ data.tar.gz: 73191d5eaa4a42a75583ed9aac9621e310a137108da7212f72cd12c7e3a1c3b73de995d9e8ce4628bc0b6dec0c21041a624abbe9c4802c3c2396442e752639cc
data/README.md CHANGED
@@ -2,6 +2,29 @@
2
2
 
3
3
  Ruby API for Ethereum Blockchain network
4
4
 
5
+ ## IPC Connection
6
+
7
+ ```ruby
8
+ require 'elchapo'
9
+
10
+ client = Ethereum::Connection.new("/root/.ethereum/rinkeby/geth.ipc")
11
+
12
+ ```
13
+
14
+ After you create the connection client, you can use all these Ethereum `eth` commands & call those methods.
15
+
16
+ [https://github.com/ethereum/wiki/wiki/JSON-RPC](https://github.com/ethereum/wiki/wiki/JSON-RPC)
17
+
18
+ [https://github.com/ethereum/go-ethereum/wiki/Management-APIs](https://github.com/ethereum/go-ethereum/wiki/Management-APIs)
19
+
20
+ `client.eth_accounts`
21
+
22
+ Make sure you are using underscore instead of capitals. Eg: `getBalance` => `get_balance`
23
+
24
+ `client.get_balance("ACCOUNT NUMBER")`
25
+
26
+ This gem is inspired & forked from [ethereum.rb](https://github.com/EthWorks/ethereum.rb)
27
+
5
28
  ## License
6
29
 
7
30
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/elchapo.gemspec CHANGED
@@ -21,4 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.15"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_dependency "activesupport", "~> 5.0"
25
+ spec.add_dependency "digest-sha3", "~> 1.1"
24
26
  end
@@ -0,0 +1,69 @@
1
+ module Ethereum
2
+ class Client
3
+
4
+ DEFAULT_GAS_LIMIT = 4_000_000
5
+
6
+ DEFAULT_GAS_PRICE = 22_000_000_000
7
+
8
+ # https://github.com/ethereum/wiki/wiki/JSON-RPC
9
+ 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)
10
+
11
+ # https://github.com/ethereum/go-ethereum/wiki/Management-APIs
12
+ 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)
13
+
14
+ attr_accessor :command, :id, :log, :logger, :default_account, :gas_price, :gas_limit
15
+
16
+ def initialize
17
+ @id = 0
18
+ @log = log
19
+ @batch = nil
20
+ # @formatter = Ethereum::Formatter.new
21
+ @gas_price = DEFAULT_GAS_PRICE
22
+ @gas_limit = DEFAULT_GAS_LIMIT
23
+ if @log == true
24
+ @logger = Logger.new("/tmp/ethereum_ruby_http.log")
25
+ end
26
+ end
27
+
28
+ def get_id
29
+ @id += 1
30
+ return @id
31
+ end
32
+
33
+ def reset_id
34
+ @id = 0
35
+ end
36
+
37
+ def int_to_hex(p)
38
+ p.is_a?(Integer) ? "0x#{p.to_s(16)}" : p
39
+ end
40
+
41
+ def encode_params(params)
42
+ params.map(&method(:int_to_hex))
43
+ end
44
+
45
+ def send_command(command,args)
46
+ if ["eth_getBalance", "eth_call"].include?(command)
47
+ args << "latest"
48
+ end
49
+
50
+ payload = {jsonrpc: "2.0", method: command, params: encode_params(args), id: get_id}
51
+ @logger.info("Sending #{payload.to_json}") if @log
52
+
53
+ output = JSON.parse(send_single(payload.to_json))
54
+ @logger.info("Received #{output.to_json}") if @log
55
+ reset_id
56
+ raise IOError, output["error"]["message"] if output["error"]
57
+ return output["result"]
58
+ end
59
+
60
+ (RPC_COMMANDS + RPC_MANAGEMENT_COMMANDS).each do |rpc_command|
61
+ method_name = rpc_command.underscore
62
+ define_method method_name do |*args|
63
+ send_command(rpc_command, args)
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,19 @@
1
+ require 'socket'
2
+ module Ethereum
3
+ class Connection < Client
4
+ attr_accessor :ipcpath
5
+
6
+ def initialize(ipcpath)
7
+ super
8
+ @ipcpath = ipcpath
9
+ end
10
+
11
+ def send_single(payload)
12
+ socket = UNIXSocket.new(@ipcpath)
13
+ socket.puts(payload)
14
+ read = socket.recvmsg(nil)[0]
15
+ socket.close
16
+ return read
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ module Ethereum
4
+ class HttpConnection < Client
5
+ attr_accessor :host, :port, :uri, :ssl
6
+
7
+ def initialize(host, log = false)
8
+ super(log)
9
+ uri = URI.parse(host)
10
+ raise ArgumentError unless ['http', 'https'].include? uri.scheme
11
+ @host = uri.host
12
+ @port = uri.port
13
+
14
+ @ssl = uri.scheme == 'https'
15
+ if ssl
16
+ @uri = URI("https://#{@host}:#{@port}")
17
+ else
18
+ @uri = URI("http://#{@host}:#{@port}")
19
+ end
20
+ end
21
+
22
+ def send_single(payload)
23
+ http = ::Net::HTTP.new(@host, @port)
24
+ if @ssl
25
+ http.use_ssl = true
26
+ end
27
+ header = {'Content-Type' => 'application/json'}
28
+ request = ::Net::HTTP::Post.new(uri, header)
29
+ request.body = payload
30
+ response = http.request(request)
31
+ return response.body
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -1,3 +1,3 @@
1
1
  module Elchapo
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/elchapo.rb CHANGED
@@ -1,5 +1,11 @@
1
1
  require "elchapo/version"
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+ require 'digest/sha3'
2
5
 
3
6
  module Elchapo
4
- # Your code goes here...
7
+
8
+ require 'elchapo/ethereum/client'
9
+ require 'elchapo/ethereum/connection'
10
+ require 'elchapo/ethereum/http_connection'
5
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elchapo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - DilumN
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-16 00:00:00.000000000 Z
11
+ date: 2017-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: digest-sha3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.1'
55
83
  description: This will enable your Ruby application to connect to Ethereum Blockchain
56
84
  email:
57
85
  - dilumnavanjana@gmail.com
@@ -67,6 +95,9 @@ files:
67
95
  - bin/setup
68
96
  - elchapo.gemspec
69
97
  - lib/elchapo.rb
98
+ - lib/elchapo/ethereum/client.rb
99
+ - lib/elchapo/ethereum/connection.rb
100
+ - lib/elchapo/ethereum/http_connection.rb
70
101
  - lib/elchapo/version.rb
71
102
  homepage: http://dilumn.github.io
72
103
  licenses: