etherlite 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +49 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +10 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +41 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/etherlite.gemspec +31 -0
  14. data/lib/etherlite.rb +66 -0
  15. data/lib/etherlite/abi.rb +22 -0
  16. data/lib/etherlite/account.rb +67 -0
  17. data/lib/etherlite/address.rb +21 -0
  18. data/lib/etherlite/api/address.rb +21 -0
  19. data/lib/etherlite/api/node.rb +36 -0
  20. data/lib/etherlite/client.rb +11 -0
  21. data/lib/etherlite/commands/abi/load_contract.rb +66 -0
  22. data/lib/etherlite/commands/abi/load_function.rb +28 -0
  23. data/lib/etherlite/commands/abi/load_type.rb +55 -0
  24. data/lib/etherlite/commands/base.rb +0 -0
  25. data/lib/etherlite/commands/contract/event_base/decode_log_inputs.rb +21 -0
  26. data/lib/etherlite/commands/contract/function/encode_arguments.rb +32 -0
  27. data/lib/etherlite/commands/utils/validate_address.rb +25 -0
  28. data/lib/etherlite/configuration.rb +33 -0
  29. data/lib/etherlite/connection.rb +39 -0
  30. data/lib/etherlite/contract/base.rb +58 -0
  31. data/lib/etherlite/contract/event_base.rb +36 -0
  32. data/lib/etherlite/contract/event_input.rb +19 -0
  33. data/lib/etherlite/contract/function.rb +43 -0
  34. data/lib/etherlite/railtie.rb +29 -0
  35. data/lib/etherlite/railties/configuration_extensions.rb +9 -0
  36. data/lib/etherlite/railties/utils.rb +13 -0
  37. data/lib/etherlite/types/address.rb +20 -0
  38. data/lib/etherlite/types/array_base.rb +36 -0
  39. data/lib/etherlite/types/array_dynamic.rb +13 -0
  40. data/lib/etherlite/types/array_fixed.rb +23 -0
  41. data/lib/etherlite/types/base.rb +27 -0
  42. data/lib/etherlite/types/bool.rb +26 -0
  43. data/lib/etherlite/types/byte_string.rb +17 -0
  44. data/lib/etherlite/types/bytes.rb +19 -0
  45. data/lib/etherlite/types/fixed.rb +38 -0
  46. data/lib/etherlite/types/integer.rb +38 -0
  47. data/lib/etherlite/types/string.rb +10 -0
  48. data/lib/etherlite/utils.rb +63 -0
  49. data/lib/etherlite/version.rb +3 -0
  50. data/lib/generators/etherlite/init_generator.rb +10 -0
  51. data/lib/generators/etherlite/templates/etherlite.yml +11 -0
  52. metadata +206 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd54c39f71674af50c15e5b2c1a75c9c972083d3
4
+ data.tar.gz: 3d7afe1cdf25b667e2d6365b3c786458fd1a37c2
5
+ SHA512:
6
+ metadata.gz: 523fd7b998e5ce21dee58c5bbd1290e5fe5f73c26cab92ac3f5fe67935d65a45e025a7cc813bc0f829b63bf1d516d461f62f3e74d2dd4ce037cdf75f80a4c00c
7
+ data.tar.gz: 0cfaf81870210ac15218299f99602312828e1f5a8b758667567f4a07d0ff7bc976d62ef992990a6b80194cdafb952ae5d26a1254e61b76cbda5b6b91b0d59bf4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.4
@@ -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 iobaixas@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 etherlite.gemspec
4
+ gemspec
@@ -0,0 +1,10 @@
1
+ watch ("Guardfile") do
2
+ UI.info "Exiting because Guard must be restarted for changes to take effect"
3
+ exit 0
4
+ end
5
+
6
+ guard :rspec, cmd: 'bundle exec rspec' do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/etherlite/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ignacio Baixas
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,41 @@
1
+ # Etherlite
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/etherlite`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'etherlite'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install etherlite
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/etherlite. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "etherlite"
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,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'etherlite/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "etherlite"
8
+ spec.version = Etherlite::VERSION
9
+ spec.authors = ["Ignacio Baixas"]
10
+ spec.email = ["ignacio@surbtc.com"]
11
+
12
+ spec.summary = %q{Ethereum integration for ruby on rails}
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/SurBTC/etherlite"
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_dependency "digest-sha3", "~> 1.1"
23
+ spec.add_dependency "power-types", "~> 0.1"
24
+ spec.add_dependency "activesupport"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.12"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "guard", "~> 2.14"
30
+ spec.add_development_dependency "guard-rspec", "~> 4.7"
31
+ end
@@ -0,0 +1,66 @@
1
+ require "digest/sha3"
2
+ require "active_support/all"
3
+ require "power-types"
4
+
5
+ require "etherlite/version"
6
+
7
+ require "etherlite/api/address"
8
+ require "etherlite/api/node"
9
+
10
+ require "etherlite/types/base"
11
+ require "etherlite/types/address"
12
+ require "etherlite/types/array_base"
13
+ require "etherlite/types/array_dynamic"
14
+ require "etherlite/types/array_fixed"
15
+ require "etherlite/types/bool"
16
+ require "etherlite/types/byte_string"
17
+ require "etherlite/types/bytes"
18
+ require "etherlite/types/fixed"
19
+ require "etherlite/types/integer"
20
+ require "etherlite/types/string"
21
+
22
+ require "etherlite/contract/base"
23
+ require "etherlite/contract/event_base"
24
+ require "etherlite/contract/event_input"
25
+ require "etherlite/contract/function"
26
+
27
+ require "etherlite/configuration"
28
+ require "etherlite/abi"
29
+ require "etherlite/utils"
30
+ require "etherlite/connection"
31
+ require "etherlite/account"
32
+ require "etherlite/address"
33
+ require "etherlite/client"
34
+
35
+ module Etherlite
36
+ extend Api::Node
37
+
38
+ def self.valid_address?(_value)
39
+ Utils.valid_address? _value
40
+ end
41
+
42
+ def self.connect(_url)
43
+ _url = URI(_url) unless _url.is_a? URI
44
+
45
+ Client.new Connection.new(_url)
46
+ end
47
+
48
+ def self.config
49
+ @config ||= Configuration.new
50
+ end
51
+
52
+ def self.logger
53
+ config.logger
54
+ end
55
+
56
+ def self.configure(_options = nil, &_block)
57
+ config.assign_attributes(_options) unless _options.nil?
58
+ _block.call(config) unless _block.nil?
59
+ end
60
+
61
+ def self.connection
62
+ @connection ||= Connection.new URI(config.url)
63
+ end
64
+ end
65
+
66
+ require "etherlite/railtie" if defined? Rails
@@ -0,0 +1,22 @@
1
+ require 'etherlite/commands/abi/load_type'
2
+ require 'etherlite/commands/abi/load_contract'
3
+ require 'etherlite/commands/abi/load_function'
4
+
5
+ module Etherlite
6
+ module Abi
7
+ extend self
8
+
9
+ def load_contract_at(_path)
10
+ json = JSON.parse File.read(_path)
11
+ LoadContract.for json: json
12
+ end
13
+
14
+ def load_contract(_json)
15
+ LoadContract.for json: _json
16
+ end
17
+
18
+ def load_function(_signature)
19
+ LoadFunction.for signature: _signature
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,67 @@
1
+ module Etherlite
2
+ class Account
3
+ include Etherlite::Api::Address
4
+
5
+ attr_reader :connection, :normalized_address
6
+
7
+ def initialize(_connection, _normalized_address)
8
+ @connection = _connection
9
+ @normalized_address = _normalized_address
10
+ end
11
+
12
+ def unlock(_passphrase)
13
+ @passphrase = _passphrase
14
+ end
15
+
16
+ def lock
17
+ @passphrase = nil
18
+ end
19
+
20
+ def send_to(_target, _options = {})
21
+ params = {
22
+ from: '0x' + @normalized_address,
23
+ to: Utils.encode_address_param(_target),
24
+ value: Utils.encode_quantity_param(_options[:amount] || 0)
25
+ }
26
+
27
+ send_transaction params, _options
28
+ end
29
+
30
+ def call(_target, _function, *_params)
31
+ _function = Utils.parse_function(_function) unless _function.is_a? Contract::Function
32
+ options = _params.last.is_a?(Hash) ? _params.pop : {}
33
+
34
+ params = {
35
+ from: '0x' + @normalized_address,
36
+ to: Utils.encode_address_param(_target),
37
+ data: _function.encode(_params)
38
+ }
39
+
40
+ if _function.constant?
41
+ _function.decode @connection, @connection.ipc_call(:eth_call, params, "latest")
42
+ else
43
+ if _function.payable? && options.key?(:pay)
44
+ params[:value] = Utils.encode_quantity_param options[:pay]
45
+ end
46
+
47
+ send_transaction params, options
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ attr_reader :normalized_address
54
+
55
+ def send_transaction(_params, _opt)
56
+ _params[:gas] = Utils.encode_quantity_param(_opt[:gas]) if _opt.key? :gas
57
+ _params[:gasPrice] = Utils.encode_quantity_param(_opt[:gas_price]) if _opt.key? :gas_price
58
+
59
+ passphrase = _opt.fetch(:passphrase, @passphrase)
60
+ if passphrase.nil?
61
+ @connection.ipc_call(:eth_sendTransaction, _params)
62
+ else
63
+ @connection.ipc_call(:personal_sendTransaction, _params, passphrase)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,21 @@
1
+ module Etherlite
2
+ class Address
3
+ include Etherlite::Api::Address
4
+
5
+ attr_reader :connection, :address
6
+
7
+ def initialize(_connection, _normalized_address)
8
+ @connection = _connection
9
+ @address = _normalized_address
10
+ end
11
+
12
+ def to_s
13
+ # TODO: format address using case-chechsum
14
+ address
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :normalized_address
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Etherlite
2
+ module Api
3
+ module Address
4
+ extend Forwardable
5
+
6
+ def address
7
+ normalized_address
8
+ end
9
+
10
+ def get_balance(block: :latest)
11
+ Etherlite::Utils.hex_to_uint(
12
+ connection.ipc_call(
13
+ :eth_getBalance,
14
+ '0x' + normalized_address,
15
+ Etherlite::Utils.encode_block_param(block)
16
+ )
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ module Etherlite
2
+ module Api
3
+ module Node
4
+ extend Forwardable
5
+
6
+ def get_block_number
7
+ Etherlite::Utils.hex_to_uint connection.ipc_call(:eth_blockNumber)
8
+ end
9
+
10
+ def get_gas_price
11
+ Etherlite::Utils.hex_to_uint connection.ipc_call(:eth_gasPrice)
12
+ end
13
+
14
+ def get_transaction_receipt(_tx_hash)
15
+ connection.ipc_call(:eth_getTransactionReceipt, _tx_hash)
16
+ end
17
+
18
+ def register_account(_passphrase)
19
+ address = connection.ipc_call(:personal_newAccount, _passphrase)
20
+ Etherlite::Account.new @connection, Etherlite::Utils.normalize_address(address)
21
+ end
22
+
23
+ def accounts
24
+ connection.ipc_call(:eth_accounts).map do |address|
25
+ Etherlite::Account.new @connection, Etherlite::Utils.normalize_address(address)
26
+ end
27
+ end
28
+
29
+ def first_account
30
+ @first_account ||= accounts.first
31
+ end
32
+
33
+ def_delegators :first_account, :unlock, :lock, :get_balance, :send_to, :call
34
+ end
35
+ end
36
+ end