eth 0.4.12 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,145 +1,221 @@
1
- # Eth [![Travis-CI](https://travis-ci.org/se3000/ruby-eth.svg?branch=master)](https://travis-ci.org/se3000/ruby-eth) [![Code Climate](https://codeclimate.com/github/se3000/ruby-eth/badges/gpa.svg)](https://codeclimate.com/github/se3000/ruby-eth) [![Gitter](https://badges.gitter.im/ruby-eth/Lobby.svg)](https://gitter.im/ruby-eth/Lobby)
2
-
3
- A simple library to build and sign Ethereum transactions. Allows separation of key and node management. Sign transactions and handle keys anywhere you can run ruby, broadcast transactions through any node.
4
-
5
- ## Installation
1
+ # Eth for Ruby
2
+
3
+ [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/q9f/eth.rb/Spec)](https://github.com/q9f/eth.rb/actions)
4
+ [![GitHub release (latest by date)](https://img.shields.io/github/v/release/q9f/eth.rb)](https://github.com/q9f/eth.rb/releases)
5
+ [![Gem](https://img.shields.io/gem/v/eth)](https://rubygems.org/gems/eth)
6
+ [![Gem](https://img.shields.io/gem/dt/eth)](https://rubygems.org/gems/eth)
7
+ [![codecov](https://codecov.io/gh/q9f/eth.rb/branch/main/graph/badge.svg?token=IK7USBPBZY)](https://codecov.io/gh/q9f/eth.rb)
8
+ [![Maintainability](https://api.codeclimate.com/v1/badges/469e6f66425198ad7614/maintainability)](https://codeclimate.com/github/q9f/eth.rb/maintainability)
9
+ [![Yard Doc API](https://img.shields.io/badge/docs-API-blue)](https://q9f.github.io/eth.rb)
10
+ [![GitHub top language](https://img.shields.io/github/languages/top/q9f/eth.rb?color=red)](https://github.com/q9f/eth.rb/pulse)
11
+ [![GitHub](https://img.shields.io/github/license/q9f/eth.rb)](LICENSE)
12
+
13
+ A simple library to build and sign Ethereum transactions. Allows separation of key and node management. Sign transactions and handle keys anywhere you can run ruby, broadcast transactions through any node. Sign messages and recover signatures for authentication.
14
+
15
+ What you get:
16
+ - [x] Secp256k1 Key-Pairs and Encrypted Ethereum Key-Stores (JSON)
17
+ - [x] EIP-55 Checksummed Ethereum Addresses
18
+ - [x] EIP-155 Replay protection with Chain IDs (with presets)
19
+ - [x] EIP-191 Ethereum Signed Messages (with prefix and type)
20
+ - [x] EIP-712 Ethereum Signed Type Data
21
+ - [x] EIP-1559 Ethereum Type-2 Transactions (with priority fee and max gas fee)
22
+ - [x] EIP-2028 Call-data intrinsic gas cost estimates (plus access lists)
23
+ - [x] EIP-2718 Ethereum Transaction Envelopes (and types)
24
+ - [x] EIP-2930 Ethereum Type-1 Transactions (with access lists)
25
+ - [x] ABI-Encoder and Decoder (including type parser)
26
+
27
+ Soon (TM):
28
+ - [ ] RLP-Encoder and Decoder (including sedes)
29
+ - [ ] RPC-Client (HTTP) for Execution APIs and Consensus APIs
30
+ - [ ] Smart Contracts and Solidity Support
31
+ - [ ] EIP-1271 Smart-Contract Authentification
32
+ - [ ] HD-Wallets (BIP-32) and Mnemonics (BIP-39)
33
+
34
+ Contents:
35
+ - [1. Installation](#1-installation)
36
+ - [2. Usage](#2-usage)
37
+ - [2.1. Ethereum Keys and Addresses (EIP-55)](#21-ethereum-keys-and-addresses-eip-55)
38
+ - [2.2. Ethereum Signatures (EIP-191, EIP-712)](#22-ethereum-signatures-eip-191-eip-712)
39
+ - [2.3. Ethereum Chains (EIP-155)](#23-ethereum-chains-eip-155)
40
+ - [2.4. Ethereum Transactions (EIP-1559, EIP-2718, EIP-2930)](#24-ethereum-transactions-eip-1559-eip-2718-eip-2930)
41
+ - [2.5. Ethereum ABI Encoder and Decoder](#25-ethereum-abi-encoder-and-decoder)
42
+ - [3. Documentation](#3-documentation)
43
+ - [4. Testing](#4-testing)
44
+ - [5. Contributing](#5-contributing)
45
+ - [6. License and Credits](#6-license-and-credits)
46
+
47
+ ## 1. Installation
6
48
 
7
49
  Add this line to your application's Gemfile:
8
50
 
9
51
  ```ruby
10
- gem 'eth'
52
+ gem "eth"
11
53
  ```
12
54
 
13
- And then execute:
14
-
15
- $ bundle
16
-
17
55
  Or install it yourself as:
18
56
 
19
- $ gem install eth
57
+ ```shell
58
+ gem install eth
59
+ ```
20
60
 
21
- ## Usage
61
+ ## 2. Usage
22
62
 
23
- ### Keys
63
+ ### 2.1. Ethereum Keys and Addresses (EIP-55)
24
64
 
25
- Create a new public/private key and get its address:
65
+ Generate a random Secp256k1 key-pair.
26
66
 
27
67
  ```ruby
28
68
  key = Eth::Key.new
29
- key.private_hex
30
- key.public_hex
31
- key.address # EIP55 checksummed address
69
+ # => #<Eth::Key:0x00005574a6ba80b8 @private_key=#<Secp256k1::PrivateKey:0x00005574a6b9a0a8 @data=")&\x86P\xB5\x16\xD9]\xFA;\x1F\xF6\xD9\xCF\xE3Vj/\xE2\x81\xC0\x9D\xE9\x05o!q\x82G\x9A\x10Q">, @public_key=#<Secp256k1::PublicKey:0x00005574a6b9bf98>>
32
70
  ```
33
71
 
34
- Import an existing key:
72
+ Create an password-encrypted Ethereum key-store.
35
73
 
36
74
  ```ruby
37
- old_key = Eth::Key.new priv: private_key
75
+ my_key = Eth::Key.new priv: "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
76
+ key_store = Eth::Key::Encrypter.perform my_key, "secret-password-1337"
77
+ # => "{\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"7e5c0fe1e27f4ea61b0f4427dd63555f\"},\"ciphertext\":\"6353653bba494cdae6bcd510febc980cdc6f7b23cfbdf950d7a909a69625c8fd\",\"kdf\":\"pbkdf2\",\"kdfparams\":{\"c\":262144,\"dklen\":32,\"prf\":\"hmac-sha256\",\"salt\":\"cce96286f3c32267fc91f756365307fe6a4c83b6b2a73c69535f721fa407736c\"},\"mac\":\"3361ffd2b158a1d7bca5a5fd86a251ba3e9d80b602c867a2e0f47023a0e17a57\"},\"id\":\"642ee9fc-72e4-4d0a-902f-247c0b59bfda\",\"version\":3}"
78
+ restored_key = Eth::Key::Decrypter.perform key_store, "secret-password-1337"
79
+ # => "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
38
80
  ```
39
81
 
40
- Or decrypt an [encrypted key](https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition):
82
+ Manage Ethereum address objects adhering to EIP-55 checksum format.
41
83
 
42
84
  ```ruby
43
- decrypted_key = Eth::Key.decrypt File.read('./some/path.json'), 'p455w0rD'
85
+ address = Eth::Address.new "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"
86
+ # => #<Eth::Address:0x00005574a6bd4fc8 @address="0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9">
87
+ address.valid?
88
+ # => true
89
+ address.checksummed # EIP 55
90
+ # => "0xD496b23D61F88A8C7758fca7560dCFac7b3b01F9"
44
91
  ```
45
92
 
46
- You can also encrypt your keys for use with other ethereum libraries:
93
+ See `/spec` or [Documentation](https://q9f.github.io/eth.rb/) for more details about key-pairs, encrypting/decrypting key-stores with a secret, and checksummed addresses.
47
94
 
48
- ```ruby
49
- encrypted_key_info = Eth::Key.encrypt key, 'p455w0rD'
50
- ```
51
-
52
- ### Transactions
53
-
54
- Build a transaction from scratch:
55
-
56
- ```ruby
57
- tx = Eth::Tx.new({
58
- data: hex_data,
59
- gas_limit: 21_000,
60
- gas_price: 3_141_592,
61
- nonce: 1,
62
- to: key2.address,
63
- value: 1_000_000_000_000,
64
- })
65
- ```
95
+ ### 2.2. Ethereum Signatures (EIP-191, EIP-712)
66
96
 
67
- Or decode an encoded raw transaction:
97
+ Manage keypairs to sign messages in EIP-191 (`personal_sign`) format or typed data in EIP-712 (`sign_typed_data`) format.
68
98
 
69
99
  ```ruby
70
- tx = Eth::Tx.decode hex
100
+ key = Eth::Key.new priv: "268be6f4a68c40f6862b7ac9aed8f701dc25a95ddb9a44d8b1f520b75f440a9a"
101
+ # => #<Eth::Key:0x00005574a699adc0 @private_key=#<Secp256k1::PrivateKey:0x00005574a6998200 @data="&\x8B\xE6\xF4\xA6\x8C@\xF6\x86+z\xC9\xAE\xD8\xF7\x01\xDC%\xA9]\xDB\x9AD\xD8\xB1\xF5 \xB7_D\n\x9A">, @public_key=#<Secp256k1::PublicKey:0x00005574a6998160>>
102
+ key.public_hex
103
+ # => "04b45200621c013a5fbab999ac33b0c836328a04afa0255ffbe6ea0f6fd97e187b02199886d942a9f50f7e279a2bc74c93b2afcbd7255489939f9b36a5eae5e281"
104
+ key.address.to_s
105
+ # => "0xD496b23D61F88A8C7758fca7560dCFac7b3b01F9"
106
+ key.personal_sign "Hello World!"
107
+ # => "ac6a59417d8688c8144f01a662384fa691636b48a071d4b7c13902bb87ca472b0bce1d7a758f39a5759ed5e937ce61f50dd1b83158371f8d0faeb9b7d81c194225"
71
108
  ```
72
109
 
73
- Then sign the transaction:
110
+ Recover and verify personal signatures respecting EIPs 155, 191, and 712.
74
111
 
75
112
  ```ruby
76
- tx.sign key
113
+ address = Eth::Address.new "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"
114
+ # => #<Eth::Address:0x00005574a6bd4fc8 @address="0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9">
115
+ signature = "ac6a59417d8688c8144f01a662384fa691636b48a071d4b7c13902bb87ca472b0bce1d7a758f39a5759ed5e937ce61f50dd1b83158371f8d0faeb9b7d81c19422d"
116
+ # => "ac6a59417d8688c8144f01a662384fa691636b48a071d4b7c13902bb87ca472b0bce1d7a758f39a5759ed5e937ce61f50dd1b83158371f8d0faeb9b7d81c19422d"
117
+ recovered_key = Eth::Signature.personal_recover "Hello World!", signature, Eth::Chain::GOERLI
118
+ # => "04b45200621c013a5fbab999ac33b0c836328a04afa0255ffbe6ea0f6fd97e187b02199886d942a9f50f7e279a2bc74c93b2afcbd7255489939f9b36a5eae5e281"
119
+ Eth::Util.public_key_to_address(recovered_key).to_s
120
+ # => "0xD496b23D61F88A8C7758fca7560dCFac7b3b01F9"
121
+ Eth::Signature.verify "Hello World!", signature, address, Eth::Chain::GOERLI
122
+ # => true
77
123
  ```
78
124
 
79
- Get the raw transaction with `tx.hex`, and broadcast it through any Ethereum node. Or, just get the TXID with `tx.hash`.
125
+ See `/spec` or [Documentation](https://q9f.github.io/eth.rb/) for signing typed data as per EIP-712.
80
126
 
81
- ### Utils
127
+ ### 2.3. Ethereum Chains (EIP-155)
82
128
 
83
- Validate an [EIP55](https://github.com/ethereum/EIPs/issues/55) checksummed address:
129
+ Manage Ethereum chain IDs for EIP-155 replay protection.
84
130
 
85
131
  ```ruby
86
- Eth::Utils.valid_address? address
132
+ chain_id = Eth::Chain::OPTIMISM
133
+ # => 10
134
+ v = Eth::Chain.to_v 0, Eth::Chain::OPTIMISM
135
+ # => 55
136
+ recovery_id = Eth::Chain.to_recovery_id v, Eth::Chain::OPTIMISM
137
+ # => 0
138
+ chain_id = Eth::Chain.to_chain_id v
139
+ # => 10
87
140
  ```
88
141
 
89
- Or add a checksum to an existing address:
142
+ ### 2.4. Ethereum Transactions (EIP-1559, EIP-2718, EIP-2930)
90
143
 
91
- ```ruby
92
- Eth::Utils.format_address "0x4bc787699093f11316e819b5692be04a712c4e69" # => "0x4bc787699093f11316e819B5692be04A712C4E69"
93
- ```
94
-
95
- ### Personal Signatures
96
-
97
- You can recover public keys and generate web3/metamask-compatible signatures:
144
+ Create an EIP-1559-conform transaction:
98
145
 
99
146
  ```ruby
100
- # Generate signature
101
- key.personal_sign('hello world')
102
-
103
- # Recover signature
104
- message = 'test'
105
- signature = '0x3eb24bd327df8c2b614c3f652ec86efe13aa721daf203820241c44861a26d37f2bffc6e03e68fc4c3d8d967054c9cb230ed34339b12ef89d512b42ae5bf8c2ae1c'
106
- Eth::Key.personal_recover(message, signature) # => 043e5b33f0080491e21f9f5f7566de59a08faabf53edbc3c32aaacc438552b25fdde531f8d1053ced090e9879cbf2b0d1c054e4b25941dab9254d2070f39418afc
147
+ payload = {
148
+ chain_id: Eth::Chain::GOERLI,
149
+ nonce: 5,
150
+ priority_fee: 3 * Eth::Unit::GWEI,
151
+ max_gas_fee: 69 * Eth::Unit::GWEI,
152
+ gas_limit: 230_420,
153
+ to: "0xCaA29806044A08E533963b2e573C1230A2cd9a2d",
154
+ value: 0.069423 * Eth::Unit::ETHER,
155
+ }
156
+ # => {:chain_id=>5, :nonce=>5, :priority_fee=>0.3e10, :max_gas_fee=>0.69e11, :gas_limit=>230420, :to=>"0xCaA29806044A08E533963b2e573C1230A2cd9a2d", :value=>0.69423e17}
157
+ tx = Eth::Tx.new payload
158
+ # => #<Eth::Tx::Eip1559:0x0000557e35fc5a68 @access_list=[], @amount=69423000000000000, @chain_id=5, @destination="CaA29806044A08E533963b2e573C1230A2cd9a2d", @gas_limit=230420, @max_fee_per_gas=69000000000, @max_priority_fee_per_gas=3000000000, @payload="", @sender="", @signature_r=0, @signature_s=0, @signature_y_parity=nil, @signer_nonce=5, @type=2>
159
+ my_key = Eth::Key.new priv: "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
160
+ # => #<Eth::Key:0x0000557e36243178 @private_key=#<Secp256k1::PrivateKey:0x0000557e36242d40 @data="0\x13vD\xB5dx]\x01B\x0F\x80C\xF0C\xD7M\xCC\xA6@\b\xE5|Y\xF8\xCEq:\x00\x05\xA5K">, @public_key=#<Secp256k1::PublicKey:0x0000557e36242cf0>>
161
+ tx.sign my_key
162
+ # => "cba302c0ebf8d0205a78ae97f560419b407e32e2426f416abc95a9bfc9dac09c"
163
+ tx.hex
164
+ # => "02f873050584b2d05e00851010b872008303841494caa29806044a08e533963b2e573c1230a2cd9a2d87f6a3d9c63df00080c080a03aa187d10b138d3e0155729adb961cd89e10f988ba2d19d6869770b9e5a23d10a04d40864600136ae214916043c7d63b849c98db757e95c86983a036982816e1af"
107
165
  ```
108
166
 
109
- ### Configure
167
+ This gem also supports access lists and ABI-encoded data payloads. See `/spec` or [Documentation](https://q9f.github.io/eth.rb/) for more details about the various supported transaction types (legacy, type-1, type-2), payload parameters, and how to estimate intrinsic gas costs.
110
168
 
111
- In order to prevent replay attacks, you must specify which Ethereum chain your transactions are created for. See [EIP 155](https://github.com/ethereum/EIPs/issues/155) for more detail.
169
+ ### 2.5. Ethereum ABI Encoder and Decoder
170
+
171
+ Encode and decode Ethereum application binary interface data (ABI).
112
172
 
113
173
  ```ruby
114
- Eth.configure do |config|
115
- config.chain_id = 1 # nil by default, meaning valid on any chain
116
- end
174
+ Eth::Util.bin_to_hex Eth::Abi.encode(["string", "address"], ["Hello, Bob!", "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"])
175
+ # => "0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000d496b23d61f88a8c7758fca7560dcfac7b3b01f9000000000000000000000000000000000000000000000000000000000000000b48656c6c6f2c20426f6221000000000000000000000000000000000000000000"
176
+ Eth::Abi.decode(["string", "address"], "0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000d496b23d61f88a8c7758fca7560dcfac7b3b01f9000000000000000000000000000000000000000000000000000000000000000b48656c6c6f2c20426f6221000000000000000000000000000000000000000000")
177
+ # => ["Hello, Bob!", "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"]
117
178
  ```
118
179
 
119
- ## Contributing
120
-
121
- Bug reports and pull requests are welcome on GitHub at https://github.com/se3000/ethereum-tx. Tests are encouraged.
180
+ ## 3. Documentation
122
181
 
123
- ### Tests
182
+ The documentation can be found at: https://q9f.github.io/eth.rb
124
183
 
125
- First install the [Ethereum common tests](https://github.com/ethereum/tests):
184
+ For any specific version, docs can be generated by `yard`:
126
185
 
127
186
  ```shell
128
- git submodule update --init
187
+ gem install bundler rdoc yard
188
+ git checkout v0.5.0
189
+ yard doc
129
190
  ```
130
191
 
131
- Then run the associated tests:
192
+ The goal is to have 100% API documentation available.
193
+
194
+ ## 4. Testing
195
+
196
+ To run tests, simply use `rspec`. Note, that the Ethereum tests fixtures are required.
132
197
 
133
198
  ```shell
199
+ git submodule update --init --recursive
200
+ bundle install
134
201
  rspec
135
202
  ```
136
203
 
137
- ## License
204
+ The goal is to have 100% specification coverage for all code inside this gem.
205
+
206
+ ## 5. Contributing
207
+
208
+ Pull requests are welcome! To contribute, please consider the following:
209
+ * Code should be fully documented. Run `yard doc` and make sure it does not yield any warnings or undocumented sets.
210
+ * Code should be fully covered by tests. Run `rspec` to make sure all tests pass. The CI has an integration that will assis you to identify uncovered lines of code and get coverage up to 100%.
211
+ * Code should be formatted properly. Try to eliminate the most common issues such as trailing white-spaces or duplicate new-lines. Usage of the `rufo` gem is recommended.
212
+ * Submit pull requests, questions, or issues to Github: https://github.com/q9f/eth.rb
138
213
 
139
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
214
+ ## 6. License and Credits
215
+ The `eth` gem is licensed under the conditions of [Apache 2.0](./LICENSE.txt). Please see [AUTHORS](./AUTHORS.txt) for contributors and copyright notices.
140
216
 
141
- ## TODO
217
+ This gem is a complete rewrite of the old `eth` gem by Steve Ellis.
218
+ * https://github.com/se3000/ruby-eth/ (MIT)
142
219
 
143
- * Better test suite.
144
- * Expose API for HD keys.
145
- * Support signing with [libsecp256k1](https://github.com/bitcoin-core/secp256k1).
220
+ It also contains a revised version the ABI gem by Jan Xie and Zhang Yaning.
221
+ * https://github.com/cryptape/ruby-ethereum-abi (MIT)
data/bin/console CHANGED
@@ -1,10 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "eth"
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.
3
+ # use the local version of the code instead of a globally installed gem
4
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
8
5
 
6
+ require "eth"
9
7
  require "pry"
8
+
10
9
  Pry.start
data/bin/setup CHANGED
@@ -1,8 +1,10 @@
1
1
  #!/usr/bin/env bash
2
+
2
3
  set -euo pipefail
3
4
  IFS=$'\n\t'
4
5
  set -vx
5
6
 
6
7
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
8
+ git submodule update --init --recursive
9
+ yard doc
10
+ rspec
data/eth.gemspec CHANGED
@@ -1,31 +1,53 @@
1
+ # frozen_string_literal: true
1
2
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'eth/version'
3
+
4
+ lib = File.expand_path("lib", __dir__).freeze
5
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
6
+
7
+ require "eth/version"
5
8
 
6
9
  Gem::Specification.new do |spec|
7
- spec.name = "eth"
8
- spec.version = Eth::VERSION
9
- spec.authors = ["Steve Ellis"]
10
- spec.email = ["email@steveell.is"]
11
-
12
- spec.summary = %q{Simple API to sign Ethereum transactions.}
13
- spec.description = %q{Library to build, parse, and sign Ethereum transactions.}
14
- spec.homepage = "https://github.com/se3000/ruby-eth"
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) }
10
+ spec.name = "eth"
11
+ spec.version = Eth::VERSION
12
+ spec.authors = ["Steve Ellis", "Afri Schoedon"]
13
+ spec.email = ["email@steveell.is", "ruby@q9f.cc"]
14
+
15
+ spec.summary = %q{Ruby Ethereum library.}
16
+ spec.description = %q{Library to handle Ethereum accounts, messages, and transactions.}
17
+ spec.homepage = "https://github.com/q9f/eth.rb"
18
+ spec.license = "Apache-2.0"
19
+
20
+ spec.metadata = {
21
+ "homepage_uri" => "https://github.com/q9f/eth.rb",
22
+ "source_code_uri" => "https://github.com/q9f/eth.rb",
23
+ "github_repo" => "https://github.com/q9f/eth.rb",
24
+ "bug_tracker_uri" => "https://github.com/q9f/eth.rb/issues",
25
+ }.freeze
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
30
  spec.require_paths = ["lib"]
31
+ spec.test_files = spec.files.grep %r{^(test|spec|features)/}
32
+
33
+ spec.platform = Gem::Platform::RUBY
34
+ spec.required_ruby_version = ">= 2.6", "< 4.0"
35
+
36
+ # keccak for hashing everything in ethereum
37
+ spec.add_dependency "keccak", "~> 1.3"
38
+
39
+ # konstructor gem for overloading constructors
40
+ spec.add_dependency "konstructor", "~> 1.0"
41
+
42
+ # rbsecp256k1 for key-pairs and signatures
43
+ spec.add_dependency "rbsecp256k1", "~> 5.1"
44
+
45
+ # rlp for transaction encoding
46
+ spec.add_dependency "rlp", "~> 0.7"
21
47
 
22
- spec.add_dependency 'digest-sha3-patched', '~> 1.1'
23
- spec.add_dependency 'ffi', '~> 1.0'
24
- spec.add_dependency 'money-tree', '~> 0.10.0'
25
- spec.add_dependency 'rlp', '~> 0.7.3'
26
- spec.add_dependency 'scrypt', '~> 3.0.6'
48
+ # openssl for encrypted key derivation
49
+ spec.add_dependency "openssl", "~> 3.0"
27
50
 
28
- spec.add_development_dependency 'pry', '~> 0.1'
29
- spec.add_development_dependency 'rake', '>= 12.3.3'
30
- spec.add_development_dependency 'rspec', '~> 3.0'
51
+ # scrypt for encrypted key derivation
52
+ spec.add_dependency "scrypt", "~> 3.0"
31
53
  end
@@ -0,0 +1,63 @@
1
+ # Copyright (c) 2016-2022 The Ruby-Eth Contributors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # -*- encoding : ascii-8bit -*-
16
+
17
+ # Provides the `Eth` module.
18
+ module Eth
19
+
20
+ # Provides a Ruby implementation of the Ethereum Applicatoin Binary Interface (ABI).
21
+ module Abi
22
+
23
+ # Provides commonly used constants, such as zero bytes or zero keys.
24
+ module Constant
25
+
26
+ # The empty byte is defined as "".
27
+ BYTE_EMPTY = "".freeze
28
+
29
+ # The zero byte is 0x00.
30
+ BYTE_ZERO = "\x00".freeze
31
+
32
+ # The byte one is 0x01.
33
+ BYTE_ONE = "\x01".freeze
34
+
35
+ # The size of a 32-bit number.
36
+ TT32 = (2 ** 32).freeze
37
+
38
+ # The size of a 256-bit number.
39
+ TT256 = (2 ** 256).freeze
40
+
41
+ # The maximum possible value of an UInt256.
42
+ UINT_MAX = (2 ** 256 - 1).freeze
43
+
44
+ # The minimum possible value of an UInt256.
45
+ UINT_MIN = 0.freeze
46
+
47
+ # The maximum possible value of an Int256.
48
+ INT_MAX = (2 ** 255 - 1).freeze
49
+
50
+ # The minimum possible value of an Int256.
51
+ INT_MIN = (-2 ** 255).freeze
52
+
53
+ # A hash containing only zeros.
54
+ HASH_ZERO = ("\x00" * 32).freeze
55
+
56
+ # A private key containing only zeros.
57
+ PRIVKEY_ZERO = ("\x00" * 32).freeze
58
+
59
+ # A private key containing only zeros (hex).
60
+ PRIVKEY_ZERO_HEX = ("0" * 64).freeze
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,177 @@
1
+ # Copyright (c) 2016-2022 The Ruby-Eth Contributors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # -*- encoding : ascii-8bit -*-
16
+
17
+ # Provides the `Eth` module.
18
+ module Eth
19
+
20
+ # Provides a Ruby implementation of the Ethereum Applicatoin Binary Interface (ABI).
21
+ module Abi
22
+
23
+ # Provides a class to handle and parse common ABI types.
24
+ class Type
25
+
26
+ # Provides a specific parser error if type cannot be determined.
27
+ class ParseError < StandardError; end
28
+
29
+ # The base attribute, e.g., `string` or `bytes`
30
+ attr :base_type
31
+
32
+ # The sub-type attribute, e.g., `256` as size of an uint256.
33
+ attr :sub_type
34
+
35
+ # The dimension attribute, e.g., `[10]` for an array of size 10.
36
+ attr :dimensions
37
+
38
+ # Create a new Type object for base types, sub types, and dimensions.
39
+ # Should use the `Type.parse` parser.
40
+ #
41
+ # @param base_type [String] the base-type attribute.
42
+ # @param sub_type [String] the sub-type attribute.
43
+ # @param dimensions [Array] the dimension attribute.
44
+ # @return [Eth::Abi::Type] an ABI type object.
45
+ def initialize(base_type, sub_type, dimensions)
46
+ sub_type = sub_type.to_s
47
+ @base_type = base_type
48
+ @sub_type = sub_type
49
+ @dimensions = dimensions
50
+ end
51
+
52
+ # Converts the self.parse method into a constructor.
53
+ konstructor :parse
54
+
55
+ # Attempts to parse a string containing a common Solidity type.
56
+ # Creates a new Type upon success (using konstructor).
57
+ #
58
+ # @param type [String] a common Solidity type.
59
+ # @return [Eth::Abi::Type] a parsed Type object.
60
+ # @raise [ParseError] if it fails to parse the type.
61
+ def parse(type)
62
+ _, base_type, sub_type, dimension = /([a-z]*)([0-9]*x?[0-9]*)((\[[0-9]*\])*)/.match(type).to_a
63
+
64
+ # type dimension can only be numeric
65
+ dims = dimension.scan(/\[[0-9]*\]/)
66
+ raise ParseError, "Unknown characters found in array declaration" if dims.join != dimension
67
+
68
+ # enforce base types
69
+ validate_base_type base_type, sub_type
70
+
71
+ # return a new Type (using konstructor)
72
+ sub_type = sub_type.to_s
73
+ @base_type = base_type
74
+ @sub_type = sub_type
75
+ @dimensions = dims.map { |x| x[1...-1].to_i }
76
+ end
77
+
78
+ # Creata new uint256 type used for size.
79
+ #
80
+ # @return [Eth::Abi::Type] a uint256 size type.
81
+ def self.size_type
82
+ @size_type ||= new("uint", 256, [])
83
+ end
84
+
85
+ # Compares two types for their attributes.
86
+ #
87
+ # @param another_type [Eth::Abi::Type] another type to be compared.
88
+ # @return [Bool] true if all attributes match.
89
+ def ==(another_type)
90
+ base_type == another_type.base_type and
91
+ sub_type == another_type.sub_type and
92
+ dimensions == another_type.dimensions
93
+ end
94
+
95
+ # Computes the size of a type if possible.
96
+ #
97
+ # @return [Integer] the size of the type; or nil if not available.
98
+ def size
99
+ s = nil
100
+ if dimensions.empty?
101
+ unless ["string", "bytes"].include?(base_type) and sub_type.empty?
102
+ s = 32
103
+ end
104
+ else
105
+ unless dimensions.last == 0
106
+ unless nested_sub.is_dynamic?
107
+ s = dimensions.last * nested_sub.size
108
+ end
109
+ end
110
+ end
111
+ @size ||= s
112
+ end
113
+
114
+ # Helper to determine whether array is of dynamic size.
115
+ #
116
+ # @return [Bool] true if array is of dynamic size.
117
+ def is_dynamic?
118
+ size.nil?
119
+ end
120
+
121
+ # Types can have nested sub-types in arrays.
122
+ #
123
+ # @return [Eth::Abi::Type] nested sub-type.
124
+ def nested_sub
125
+ @nested_sub ||= self.class.new(base_type, sub_type, dimensions[0...-1])
126
+ end
127
+
128
+ private
129
+
130
+ def validate_base_type(base_type, sub_type)
131
+ case base_type
132
+ when "string"
133
+
134
+ # string can not have any suffix
135
+ raise ParseError, "String type must have no suffix or numerical suffix" unless sub_type.empty?
136
+ when "bytes"
137
+
138
+ # bytes can be no longer than 32 bytes
139
+ raise ParseError, "Maximum 32 bytes for fixed-length string or bytes" unless sub_type.empty? || sub_type.to_i <= 32
140
+ when "uint", "int"
141
+
142
+ # integers must have a numerical suffix
143
+ raise ParseError, "Integer type must have numerical suffix" unless sub_type =~ /\A[0-9]+\z/
144
+
145
+ # integer size must be valid
146
+ size = sub_type.to_i
147
+ raise ParseError, "Integer size out of bounds" unless size >= 8 && size <= 256
148
+ raise ParseError, "Integer size must be multiple of 8" unless size % 8 == 0
149
+ when "ureal", "real", "fixed", "ufixed"
150
+
151
+ # floats must have valid dimensional suffix
152
+ raise ParseError, "Real type must have suffix of form <high>x<low>, e.g. 128x128" unless sub_type =~ /\A[0-9]+x[0-9]+\z/
153
+ high, low = sub_type.split("x").map(&:to_i)
154
+ total = high + low
155
+ raise ParseError, "Real size out of bounds (max 32 bytes)" unless total >= 8 && total <= 256
156
+ raise ParseError, "Real high/low sizes must be multiples of 8" unless high % 8 == 0 && low % 8 == 0
157
+ when "hash"
158
+
159
+ # hashs must have numerical suffix
160
+ raise ParseError, "Hash type must have numerical suffix" unless sub_type =~ /\A[0-9]+\z/
161
+ when "address"
162
+
163
+ # addresses cannot have any suffix
164
+ raise ParseError, "Address cannot have suffix" unless sub_type.empty?
165
+ when "bool"
166
+
167
+ # booleans cannot have any suffix
168
+ raise ParseError, "Bool cannot have suffix" unless sub_type.empty?
169
+ else
170
+
171
+ # we cannot parse arbitrary types such as 'decimal' or 'hex'
172
+ raise ParseError, "Unknown base type"
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end