eth 0.4.16 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/codeql.yml +15 -5
- data/.github/workflows/docs.yml +26 -0
- data/.github/workflows/spec.yml +52 -0
- data/.gitignore +24 -24
- data/.gitmodules +3 -3
- data/.yardopts +1 -0
- data/AUTHORS.txt +21 -0
- data/CHANGELOG.md +65 -10
- data/Gemfile +13 -2
- data/LICENSE.txt +202 -22
- data/README.md +199 -74
- data/bin/console +4 -4
- data/bin/setup +5 -4
- data/eth.gemspec +34 -29
- data/lib/eth/abi/type.rb +178 -0
- data/lib/eth/abi.rb +396 -0
- data/lib/eth/address.rb +55 -11
- data/lib/eth/api.rb +223 -0
- data/lib/eth/chain.rb +151 -0
- data/lib/eth/client/http.rb +63 -0
- data/lib/eth/client/ipc.rb +47 -0
- data/lib/eth/client.rb +232 -0
- data/lib/eth/constant.rb +71 -0
- data/lib/eth/eip712.rb +184 -0
- data/lib/eth/key/decrypter.rb +121 -88
- data/lib/eth/key/encrypter.rb +178 -99
- data/lib/eth/key.rb +136 -48
- data/lib/eth/rlp/decoder.rb +109 -0
- data/lib/eth/rlp/encoder.rb +78 -0
- data/lib/eth/rlp/sedes/big_endian_int.rb +66 -0
- data/lib/eth/rlp/sedes/binary.rb +97 -0
- data/lib/eth/rlp/sedes/list.rb +84 -0
- data/lib/eth/rlp/sedes.rb +74 -0
- data/lib/eth/rlp.rb +63 -0
- data/lib/eth/signature.rb +163 -0
- data/lib/eth/tx/eip1559.rb +336 -0
- data/lib/eth/tx/eip2930.rb +328 -0
- data/lib/eth/tx/legacy.rb +296 -0
- data/lib/eth/tx.rb +273 -143
- data/lib/eth/unit.rb +49 -0
- data/lib/eth/util.rb +235 -0
- data/lib/eth/version.rb +18 -1
- data/lib/eth.rb +33 -67
- metadata +50 -85
- data/.github/workflows/build.yml +0 -26
- data/lib/eth/gas.rb +0 -9
- data/lib/eth/open_ssl.rb +0 -264
- data/lib/eth/secp256k1.rb +0 -7
- data/lib/eth/sedes.rb +0 -40
- data/lib/eth/utils.rb +0 -130
data/README.md
CHANGED
@@ -1,151 +1,276 @@
|
|
1
1
|
# Eth for Ruby
|
2
|
-
|
3
|
-
[![GitHub
|
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)
|
4
5
|
[![Gem](https://img.shields.io/gem/v/eth)](https://rubygems.org/gems/eth)
|
5
6
|
[![Gem](https://img.shields.io/gem/dt/eth)](https://rubygems.org/gems/eth)
|
6
|
-
[![
|
7
|
-
[![
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
+
- [x] RLP-Encoder and Decoder (including sedes)
|
27
|
+
- [x] RPC-Client (IPC/HTTP) for Execution-Layer APIs
|
28
|
+
|
29
|
+
Soon (TM):
|
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
|
+
- [2.6. Ethereum RLP Encoder and Decoder](#26-ethereum-rlp-encoder-and-decoder)
|
43
|
+
- [2.7. Ethereum RPC-Client](#27-ethereum-rpc-client)
|
44
|
+
- [3. Documentation](#3-documentation)
|
45
|
+
- [4. Testing](#4-testing)
|
46
|
+
- [5. Contributing](#5-contributing)
|
47
|
+
- [6. License and Credits](#6-license-and-credits)
|
48
|
+
|
49
|
+
## 1. Installation
|
12
50
|
|
13
51
|
Add this line to your application's Gemfile:
|
14
52
|
|
15
53
|
```ruby
|
16
|
-
gem
|
54
|
+
gem "eth"
|
17
55
|
```
|
18
56
|
|
19
|
-
And then execute:
|
20
|
-
|
21
|
-
$ bundle install
|
22
|
-
|
23
57
|
Or install it yourself as:
|
24
58
|
|
25
|
-
|
59
|
+
```shell
|
60
|
+
gem install eth
|
61
|
+
```
|
26
62
|
|
27
|
-
## Usage
|
63
|
+
## 2. Usage
|
28
64
|
|
29
|
-
### Keys
|
65
|
+
### 2.1. Ethereum Keys and Addresses (EIP-55)
|
30
66
|
|
31
|
-
|
67
|
+
Generate a random Secp256k1 key-pair.
|
32
68
|
|
33
69
|
```ruby
|
34
70
|
key = Eth::Key.new
|
35
|
-
|
36
|
-
key.public_hex
|
37
|
-
key.address # EIP55 checksummed address
|
71
|
+
# => #<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>>
|
38
72
|
```
|
39
73
|
|
40
|
-
|
74
|
+
Create an password-encrypted Ethereum key-store.
|
41
75
|
|
42
76
|
```ruby
|
43
|
-
|
77
|
+
my_key = Eth::Key.new priv: "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
|
78
|
+
key_store = Eth::Key::Encrypter.perform my_key, "secret-password-1337"
|
79
|
+
# => "{\"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}"
|
80
|
+
restored_key = Eth::Key::Decrypter.perform key_store, "secret-password-1337"
|
81
|
+
# => "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
|
44
82
|
```
|
45
83
|
|
46
|
-
|
84
|
+
Manage Ethereum address objects adhering to EIP-55 checksum format.
|
47
85
|
|
48
86
|
```ruby
|
49
|
-
|
87
|
+
address = Eth::Address.new "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"
|
88
|
+
# => #<Eth::Address:0x00005574a6bd4fc8 @address="0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9">
|
89
|
+
address.valid?
|
90
|
+
# => true
|
91
|
+
address.checksummed # EIP 55
|
92
|
+
# => "0xD496b23D61F88A8C7758fca7560dCFac7b3b01F9"
|
50
93
|
```
|
51
94
|
|
52
|
-
|
95
|
+
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.
|
96
|
+
|
97
|
+
### 2.2. Ethereum Signatures (EIP-191, EIP-712)
|
98
|
+
|
99
|
+
Manage keypairs to sign messages in EIP-191 (`personal_sign`) format or typed data in EIP-712 (`sign_typed_data`) format.
|
53
100
|
|
54
101
|
```ruby
|
55
|
-
|
102
|
+
key = Eth::Key.new priv: "268be6f4a68c40f6862b7ac9aed8f701dc25a95ddb9a44d8b1f520b75f440a9a"
|
103
|
+
# => #<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>>
|
104
|
+
key.public_hex
|
105
|
+
# => "04b45200621c013a5fbab999ac33b0c836328a04afa0255ffbe6ea0f6fd97e187b02199886d942a9f50f7e279a2bc74c93b2afcbd7255489939f9b36a5eae5e281"
|
106
|
+
key.address.to_s
|
107
|
+
# => "0xD496b23D61F88A8C7758fca7560dCFac7b3b01F9"
|
108
|
+
key.personal_sign "Hello World!"
|
109
|
+
# => "ac6a59417d8688c8144f01a662384fa691636b48a071d4b7c13902bb87ca472b0bce1d7a758f39a5759ed5e937ce61f50dd1b83158371f8d0faeb9b7d81c194225"
|
56
110
|
```
|
57
111
|
|
58
|
-
|
59
|
-
|
60
|
-
Build a transaction from scratch:
|
112
|
+
Recover and verify personal signatures respecting EIPs 155, 191, and 712.
|
61
113
|
|
62
114
|
```ruby
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
115
|
+
address = Eth::Address.new "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"
|
116
|
+
# => #<Eth::Address:0x00005574a6bd4fc8 @address="0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9">
|
117
|
+
signature = "ac6a59417d8688c8144f01a662384fa691636b48a071d4b7c13902bb87ca472b0bce1d7a758f39a5759ed5e937ce61f50dd1b83158371f8d0faeb9b7d81c19422d"
|
118
|
+
# => "ac6a59417d8688c8144f01a662384fa691636b48a071d4b7c13902bb87ca472b0bce1d7a758f39a5759ed5e937ce61f50dd1b83158371f8d0faeb9b7d81c19422d"
|
119
|
+
recovered_key = Eth::Signature.personal_recover "Hello World!", signature, Eth::Chain::GOERLI
|
120
|
+
# => "04b45200621c013a5fbab999ac33b0c836328a04afa0255ffbe6ea0f6fd97e187b02199886d942a9f50f7e279a2bc74c93b2afcbd7255489939f9b36a5eae5e281"
|
121
|
+
Eth::Util.public_key_to_address(recovered_key).to_s
|
122
|
+
# => "0xD496b23D61F88A8C7758fca7560dCFac7b3b01F9"
|
123
|
+
Eth::Signature.verify "Hello World!", signature, address, Eth::Chain::GOERLI
|
124
|
+
# => true
|
71
125
|
```
|
72
126
|
|
73
|
-
|
127
|
+
See `/spec` or [Documentation](https://q9f.github.io/eth.rb/) for signing typed data as per EIP-712.
|
128
|
+
|
129
|
+
### 2.3. Ethereum Chains (EIP-155)
|
130
|
+
|
131
|
+
Manage Ethereum chain IDs for EIP-155 replay protection.
|
74
132
|
|
75
133
|
```ruby
|
76
|
-
|
134
|
+
chain_id = Eth::Chain::OPTIMISM
|
135
|
+
# => 10
|
136
|
+
v = Eth::Chain.to_v 0, Eth::Chain::OPTIMISM
|
137
|
+
# => 55
|
138
|
+
recovery_id = Eth::Chain.to_recovery_id v, Eth::Chain::OPTIMISM
|
139
|
+
# => 0
|
140
|
+
chain_id = Eth::Chain.to_chain_id v
|
141
|
+
# => 10
|
77
142
|
```
|
78
143
|
|
79
|
-
|
144
|
+
### 2.4. Ethereum Transactions (EIP-1559, EIP-2718, EIP-2930)
|
145
|
+
|
146
|
+
Create an EIP-1559-conform transaction:
|
80
147
|
|
81
148
|
```ruby
|
82
|
-
|
149
|
+
payload = {
|
150
|
+
chain_id: Eth::Chain::GOERLI,
|
151
|
+
nonce: 5,
|
152
|
+
priority_fee: 3 * Eth::Unit::GWEI,
|
153
|
+
max_gas_fee: 69 * Eth::Unit::GWEI,
|
154
|
+
gas_limit: 230_420,
|
155
|
+
to: "0xCaA29806044A08E533963b2e573C1230A2cd9a2d",
|
156
|
+
value: 0.069423 * Eth::Unit::ETHER,
|
157
|
+
}
|
158
|
+
# => {:chain_id=>5, :nonce=>5, :priority_fee=>0.3e10, :max_gas_fee=>0.69e11, :gas_limit=>230420, :to=>"0xCaA29806044A08E533963b2e573C1230A2cd9a2d", :value=>0.69423e17}
|
159
|
+
tx = Eth::Tx.new payload
|
160
|
+
# => #<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>
|
161
|
+
my_key = Eth::Key.new priv: "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
|
162
|
+
# => #<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>>
|
163
|
+
tx.sign my_key
|
164
|
+
# => "cba302c0ebf8d0205a78ae97f560419b407e32e2426f416abc95a9bfc9dac09c"
|
165
|
+
tx.hex
|
166
|
+
# => "02f873050584b2d05e00851010b872008303841494caa29806044a08e533963b2e573c1230a2cd9a2d87f6a3d9c63df00080c080a03aa187d10b138d3e0155729adb961cd89e10f988ba2d19d6869770b9e5a23d10a04d40864600136ae214916043c7d63b849c98db757e95c86983a036982816e1af"
|
83
167
|
```
|
84
168
|
|
85
|
-
|
169
|
+
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.
|
86
170
|
|
87
|
-
###
|
171
|
+
### 2.5. Ethereum ABI Encoder and Decoder
|
88
172
|
|
89
|
-
|
173
|
+
Encode and decode Ethereum application binary interface data (ABI).
|
90
174
|
|
91
175
|
```ruby
|
92
|
-
Eth::
|
176
|
+
Eth::Util.bin_to_hex Eth::Abi.encode(["string", "address"], ["Hello, Bob!", "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"])
|
177
|
+
# => "0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000d496b23d61f88a8c7758fca7560dcfac7b3b01f9000000000000000000000000000000000000000000000000000000000000000b48656c6c6f2c20426f6221000000000000000000000000000000000000000000"
|
178
|
+
Eth::Abi.decode(["string", "address"], "0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000d496b23d61f88a8c7758fca7560dcfac7b3b01f9000000000000000000000000000000000000000000000000000000000000000b48656c6c6f2c20426f6221000000000000000000000000000000000000000000")
|
179
|
+
# => ["Hello, Bob!", "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"]
|
93
180
|
```
|
94
181
|
|
95
|
-
|
182
|
+
### 2.6. Ethereum RLP Encoder and Decoder
|
183
|
+
|
184
|
+
Serialize and deserialize Ethereum recursive-length prefix data (RLP).
|
96
185
|
|
97
186
|
```ruby
|
98
|
-
Eth::
|
187
|
+
Eth::Util.bin_to_hex Eth::Rlp.encode ["Hello, Bob!", "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"]
|
188
|
+
# => "f78b48656c6c6f2c20426f6221aa307864343936623233643631663838613863373735386663613735363064636661633762336230316639"
|
189
|
+
Eth::Rlp.decode "f78b48656c6c6f2c20426f6221aa307864343936623233643631663838613863373735386663613735363064636661633762336230316639"
|
190
|
+
# => ["Hello, Bob!", "0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9"]
|
99
191
|
```
|
100
192
|
|
101
|
-
|
102
|
-
|
103
|
-
You can recover public keys and generate web3/metamask-compatible signatures:
|
193
|
+
Or ;-)
|
104
194
|
|
105
195
|
```ruby
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
# Recover signature
|
110
|
-
message = 'test'
|
111
|
-
signature = '0x3eb24bd327df8c2b614c3f652ec86efe13aa721daf203820241c44861a26d37f2bffc6e03e68fc4c3d8d967054c9cb230ed34339b12ef89d512b42ae5bf8c2ae1c'
|
112
|
-
Eth::Key.personal_recover(message, signature) # => 043e5b33f0080491e21f9f5f7566de59a08faabf53edbc3c32aaacc438552b25fdde531f8d1053ced090e9879cbf2b0d1c054e4b25941dab9254d2070f39418afc
|
196
|
+
Eth::Rlp.decode "c7c0c1c0c3c0c1c0"
|
197
|
+
# => [[], [[]], [[], [[]]]]
|
113
198
|
```
|
114
199
|
|
115
|
-
###
|
200
|
+
### 2.7. Ethereum RPC-Client
|
116
201
|
|
117
|
-
|
202
|
+
Create an IPC- or HTTP-RPC-API client to seamlessly query the chain state, e.g., Infura over HTTPS with access token:
|
118
203
|
|
119
204
|
```ruby
|
120
|
-
Eth.
|
121
|
-
|
122
|
-
|
205
|
+
infura = Eth::Client.create "https://mainnet.infura.io/v3/#{access_token}"
|
206
|
+
# => #<Eth::Client::Http:0x000055d43f3ca460 @gas_limit=21000, @host="mainnet.infura.io", @id=0, @max_fee_per_gas=0.2e11, @max_priority_fee_per_gas=0, @port=443, @ssl=true, @uri=#<URI::HTTPS https://mainnet.infura.io/v3/31b...d93>>
|
207
|
+
deposit_contract = Eth::Address.new "0x00000000219ab540356cBB839Cbe05303d7705Fa"
|
208
|
+
# => #<Eth::Address:0x000055d43f381738 @address="0x00000000219ab540356cBB839Cbe05303d7705Fa">
|
209
|
+
infura.get_balance deposit_contract
|
210
|
+
# => 9087314000069000000000069
|
123
211
|
```
|
124
212
|
|
125
|
-
|
213
|
+
Or set up a local development environment with `geth --dev`:
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
cli = Eth::Client.create "/tmp/geth.ipc"
|
217
|
+
# => #<Eth::Client::Ipc:0x000055d43f51c390 @gas_limit=21000, @id=0, @max_fee_per_gas=0.2e11, @max_priority_fee_per_gas=0, @path="/tmp/geth.ipc">
|
218
|
+
cli.eth_coinbase
|
219
|
+
# => {"jsonrpc"=>"2.0", "id"=>1, "result"=>"0x6868074fb21c48dfad0c448fbabd99383a6598e4"}
|
220
|
+
tx = cli.transfer_and_wait(Eth::Key.new.address, 1337 * Eth::Unit::ETHER)
|
221
|
+
# => "0x141c6dff40df34fe4fce5a65588d2161dab3e0e977fb8049ff7d79bc901034f7"
|
222
|
+
cli.eth_get_transaction_by_hash tx
|
223
|
+
# => {"jsonrpc"=>"2.0", "id"=>8, "result"=> {"blockHash"=>"0x47e742038c75851348dbda87b15fde044d54c442c371f43bea881a44d5589de3", "blockNumber"=>"0x1", "from"=>"0x6868074fb21c48dfad0c448fbabd99383a6598e4", "gas"=>"0x5208", "gasPrice"=>"0x342770c1", "maxFeePerGas"=>"0x77359401", "maxPriorityFeePerGas"=>"0x1", "hash"=>"0x141c6dff40df34fe4fce5a65588d2161dab3e0e977fb8049ff7d79bc901034f7", "input"=>"0x", "nonce"=>"0x0", "to"=>"0x311c61e5dc6123ad016bb7fd687d283c327bcd5f", "transactionIndex"=>"0x0", "value"=>"0x487a9a304539440000", "type"=>"0x2", "accessList"=>[], "chainId"=>"0x539", "v"=>"0x0", "r"=>"0xb42477d69eae65a3a3d91d9cb173e4a45a403fb0a15fa729dbfdc9d13211d7b5", "s"=>"0x4a2f98fc2b61c2d7c907520bc8c6ebe42ea6fe1cb6824f95e4b30e9464395100"}}
|
224
|
+
cli.get_balance "0x311c61e5dc6123ad016bb7fd687d283c327bcd5f"
|
225
|
+
# => 1337000000000000000000
|
226
|
+
cli.get_nonce cli.eth_coinbase["result"]
|
227
|
+
# => 1
|
228
|
+
```
|
126
229
|
|
127
|
-
|
230
|
+
Check out `Eth::Api` for a list of supported RPC-APIs or consult the [Documentation](https://q9f.github.io/eth.rb/) for more details.
|
128
231
|
|
129
|
-
|
232
|
+
## 3. Documentation
|
130
233
|
|
131
|
-
|
234
|
+
The documentation can be found at: https://q9f.github.io/eth.rb
|
235
|
+
|
236
|
+
For any specific version, docs can be generated by `yard`:
|
132
237
|
|
133
238
|
```shell
|
134
|
-
|
239
|
+
gem install bundler rdoc yard
|
240
|
+
git checkout v0.5.0
|
241
|
+
yard doc
|
135
242
|
```
|
136
243
|
|
137
|
-
|
244
|
+
The goal is to have 100% API documentation available.
|
245
|
+
|
246
|
+
## 4. Testing
|
247
|
+
|
248
|
+
To run tests, simply use `rspec`. Note, that the Ethereum tests fixtures are required.
|
138
249
|
|
139
250
|
```shell
|
251
|
+
git submodule update --init --recursive
|
252
|
+
bundle install
|
140
253
|
rspec
|
141
254
|
```
|
142
255
|
|
143
|
-
|
256
|
+
The goal is to have 100% specification coverage for all code inside this gem.
|
257
|
+
|
258
|
+
## 5. Contributing
|
259
|
+
|
260
|
+
Pull requests are welcome! To contribute, please consider the following:
|
261
|
+
* Code should be fully documented. Run `yard doc` and make sure it does not yield any warnings or undocumented sets.
|
262
|
+
* 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%.
|
263
|
+
* 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.
|
264
|
+
* Submit pull requests, questions, or issues to Github: https://github.com/q9f/eth.rb
|
265
|
+
|
266
|
+
## 6. License and Credits
|
267
|
+
The `eth` gem is licensed under the conditions of [Apache 2.0](./LICENSE.txt). Please see [AUTHORS](./AUTHORS.txt) for contributors and copyright notices.
|
144
268
|
|
145
|
-
|
269
|
+
This gem is a complete rewrite of the old `eth` gem by Steve Ellis.
|
270
|
+
* https://github.com/se3000/ruby-eth/ (MIT)
|
146
271
|
|
147
|
-
|
272
|
+
It also contains a revised version the ABI gem by Jan Xie and Zhang Yaning.
|
273
|
+
* https://github.com/cryptape/ruby-ethereum-abi (MIT)
|
148
274
|
|
149
|
-
|
150
|
-
*
|
151
|
-
* Support signing with [libsecp256k1](https://github.com/bitcoin-core/secp256k1).
|
275
|
+
It also contains a revised version the RLP gem by Jan Xie and Zhang Yaning.
|
276
|
+
* https://github.com/cryptape/ruby-rlp (MIT)
|
data/bin/console
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
# use the local version of the code instead of a globally installed gem
|
4
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
require "eth"
|
7
|
+
include Eth
|
8
8
|
|
9
9
|
require "pry"
|
10
10
|
Pry.start
|
data/bin/setup
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
#!/usr/bin/env bash
|
2
|
-
set -euo pipefail
|
3
|
-
IFS=$'\n\t'
|
4
|
-
set -vx
|
5
2
|
|
6
3
|
bundle install
|
4
|
+
git submodule update --init --recursive
|
5
|
+
rufo .
|
6
|
+
yard doc
|
7
|
+
rspec
|
7
8
|
|
8
|
-
|
9
|
+
echo "Tests fail? Run \`geth --dev --http --ipcpath /tmp/geth.ipc\` in background and try again."
|
data/eth.gemspec
CHANGED
@@ -1,46 +1,51 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
# coding: utf-8
|
3
3
|
|
4
|
-
lib = File.expand_path(
|
4
|
+
lib = File.expand_path("lib", __dir__).freeze
|
5
5
|
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
|
6
6
|
|
7
|
-
require
|
7
|
+
require "eth/version"
|
8
8
|
|
9
9
|
Gem::Specification.new do |spec|
|
10
|
-
spec.name
|
11
|
-
spec.version
|
12
|
-
spec.authors
|
13
|
-
spec.email
|
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
14
|
|
15
|
-
spec.summary
|
16
|
-
spec.description
|
17
|
-
spec.homepage
|
18
|
-
spec.license
|
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
19
|
|
20
20
|
spec.metadata = {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
"bug_tracker_uri" => "https://github.com/q9f/eth.rb/issues",
|
22
|
+
"changelog_uri" => "https://github.com/q9f/eth.rb/blob/main/CHANGELOG.md",
|
23
|
+
"documentation_uri" => "https://q9f.github.io/eth.rb/",
|
24
|
+
"github_repo" => "https://github.com/q9f/eth.rb",
|
25
|
+
"source_code_uri" => "https://github.com/q9f/eth.rb",
|
25
26
|
}.freeze
|
26
27
|
|
27
|
-
spec.files
|
28
|
-
spec.bindir
|
29
|
-
spec.executables
|
28
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
31
|
spec.require_paths = ["lib"]
|
31
|
-
spec.test_files
|
32
|
-
|
33
|
-
spec.add_dependency 'keccak', '~> 1.2'
|
34
|
-
spec.add_dependency 'ffi', '~> 1.15'
|
35
|
-
spec.add_dependency 'money-tree', '~> 0.10'
|
36
|
-
spec.add_dependency 'rlp', '~> 0.7'
|
37
|
-
spec.add_dependency 'scrypt', '~> 3.0'
|
32
|
+
spec.test_files = spec.files.grep %r{^(test|spec|features)/}
|
38
33
|
|
39
34
|
spec.platform = Gem::Platform::RUBY
|
40
|
-
spec.required_ruby_version = ">= 2.
|
35
|
+
spec.required_ruby_version = ">= 2.6", "< 4.0"
|
36
|
+
|
37
|
+
# keccak for hashing everything in ethereum
|
38
|
+
spec.add_dependency "keccak", "~> 1.3"
|
39
|
+
|
40
|
+
# konstructor gem for overloading constructors
|
41
|
+
spec.add_dependency "konstructor", "~> 1.0"
|
42
|
+
|
43
|
+
# rbsecp256k1 for key-pairs and signatures
|
44
|
+
spec.add_dependency "rbsecp256k1", "~> 5.1"
|
45
|
+
|
46
|
+
# openssl for encrypted key derivation
|
47
|
+
spec.add_dependency "openssl", "~> 2.2"
|
41
48
|
|
42
|
-
|
43
|
-
spec.
|
44
|
-
spec.add_development_dependency 'rake', '~> 13.0'
|
45
|
-
spec.add_development_dependency 'rspec', '~> 3.10'
|
49
|
+
# scrypt for encrypted key derivation
|
50
|
+
spec.add_dependency "scrypt", "~> 3.0"
|
46
51
|
end
|
data/lib/eth/abi/type.rb
ADDED
@@ -0,0 +1,178 @@
|
|
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 not be used; use {Type.parse} instead.
|
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
|
+
# Creates a 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 [Boolean] 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
|
+
# Helpes to determine whether array is of dynamic size.
|
115
|
+
#
|
116
|
+
# @return [Boolean] 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
|
+
# Validates all known base types and raises if an issue occurs.
|
131
|
+
def validate_base_type(base_type, sub_type)
|
132
|
+
case base_type
|
133
|
+
when "string"
|
134
|
+
|
135
|
+
# string can not have any suffix
|
136
|
+
raise ParseError, "String type must have no suffix or numerical suffix" unless sub_type.empty?
|
137
|
+
when "bytes"
|
138
|
+
|
139
|
+
# bytes can be no longer than 32 bytes
|
140
|
+
raise ParseError, "Maximum 32 bytes for fixed-length string or bytes" unless sub_type.empty? || sub_type.to_i <= 32
|
141
|
+
when "uint", "int"
|
142
|
+
|
143
|
+
# integers must have a numerical suffix
|
144
|
+
raise ParseError, "Integer type must have numerical suffix" unless sub_type =~ /\A[0-9]+\z/
|
145
|
+
|
146
|
+
# integer size must be valid
|
147
|
+
size = sub_type.to_i
|
148
|
+
raise ParseError, "Integer size out of bounds" unless size >= 8 && size <= 256
|
149
|
+
raise ParseError, "Integer size must be multiple of 8" unless size % 8 == 0
|
150
|
+
when "ureal", "real", "fixed", "ufixed"
|
151
|
+
|
152
|
+
# floats must have valid dimensional suffix
|
153
|
+
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/
|
154
|
+
high, low = sub_type.split("x").map(&:to_i)
|
155
|
+
total = high + low
|
156
|
+
raise ParseError, "Real size out of bounds (max 32 bytes)" unless total >= 8 && total <= 256
|
157
|
+
raise ParseError, "Real high/low sizes must be multiples of 8" unless high % 8 == 0 && low % 8 == 0
|
158
|
+
when "hash"
|
159
|
+
|
160
|
+
# hashs must have numerical suffix
|
161
|
+
raise ParseError, "Hash type must have numerical suffix" unless sub_type =~ /\A[0-9]+\z/
|
162
|
+
when "address"
|
163
|
+
|
164
|
+
# addresses cannot have any suffix
|
165
|
+
raise ParseError, "Address cannot have suffix" unless sub_type.empty?
|
166
|
+
when "bool"
|
167
|
+
|
168
|
+
# booleans cannot have any suffix
|
169
|
+
raise ParseError, "Bool cannot have suffix" unless sub_type.empty?
|
170
|
+
else
|
171
|
+
|
172
|
+
# we cannot parse arbitrary types such as 'decimal' or 'hex'
|
173
|
+
raise ParseError, "Unknown base type"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|