eth 0.4.18 → 0.5.2
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 +6 -2
- data/.github/workflows/docs.yml +1 -1
- data/.github/workflows/spec.yml +52 -0
- data/.gitignore +24 -24
- data/.gitmodules +3 -3
- data/.yardopts +1 -0
- data/AUTHORS.txt +27 -0
- data/CHANGELOG.md +63 -13
- data/Gemfile +12 -4
- data/LICENSE.txt +202 -22
- data/README.md +231 -76
- data/bin/console +4 -4
- data/bin/setup +5 -4
- data/codecov.yml +6 -0
- data/eth.gemspec +23 -19
- data/lib/eth/abi/type.rb +178 -0
- data/lib/eth/abi.rb +396 -0
- data/lib/eth/address.rb +57 -10
- 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 +50 -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 -85
- data/lib/eth/key/encrypter.rb +180 -99
- data/lib/eth/key.rb +134 -45
- data/lib/eth/rlp/decoder.rb +114 -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/solidity.rb +75 -0
- data/lib/eth/tx/eip1559.rb +337 -0
- data/lib/eth/tx/eip2930.rb +329 -0
- data/lib/eth/tx/legacy.rb +297 -0
- data/lib/eth/tx.rb +269 -146
- 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 +34 -67
- metadata +47 -95
- data/.github/workflows/build.yml +0 -36
- data/lib/eth/gas.rb +0 -7
- data/lib/eth/open_ssl.rb +0 -395
- data/lib/eth/secp256k1.rb +0 -5
- data/lib/eth/sedes.rb +0 -39
- data/lib/eth/utils.rb +0 -126
@@ -0,0 +1,74 @@
|
|
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
|
+
require "eth/rlp/sedes/big_endian_int"
|
18
|
+
require "eth/rlp/sedes/binary"
|
19
|
+
require "eth/rlp/sedes/list"
|
20
|
+
|
21
|
+
# Provides the {Eth} module.
|
22
|
+
module Eth
|
23
|
+
|
24
|
+
# Provides an recursive-length prefix (RLP) encoder and decoder.
|
25
|
+
module Rlp
|
26
|
+
|
27
|
+
# Provides serializable and deserializable types (SeDes).
|
28
|
+
module Sedes
|
29
|
+
|
30
|
+
# Provides a singleton {Eth::Rlp::Sedes} class to infer objects and types.
|
31
|
+
class << self
|
32
|
+
|
33
|
+
# Tries to find a sedes objects suitable for a given Ruby object.
|
34
|
+
#
|
35
|
+
# The sedes objects considered are `obj`'s class, {big_endian_int} and
|
36
|
+
# {binary}. If `obj` is a list, an {Eth::Rlp::Sedes::List} will be
|
37
|
+
# constructed recursively.
|
38
|
+
#
|
39
|
+
# @param obj [Object] the Ruby object for which to find a sedes object.
|
40
|
+
# @raise [TypeError] if no appropriate sedes could be found.
|
41
|
+
def infer(obj)
|
42
|
+
return obj.class if is_sedes? obj.class
|
43
|
+
return big_endian_int if obj.is_a?(Integer) && obj >= 0
|
44
|
+
return binary if Binary.valid_type? obj
|
45
|
+
return List.new(elements: obj.map { |item| infer item }) if Util.is_list? obj
|
46
|
+
raise TypeError, "Did not find sedes handling type #{obj.class.name}"
|
47
|
+
end
|
48
|
+
|
49
|
+
# Determines if an object is a sedes object.
|
50
|
+
#
|
51
|
+
# @param obj [Object] the object to check.
|
52
|
+
# @return [Boolean] true if it's serializable and deserializable.
|
53
|
+
def is_sedes?(obj)
|
54
|
+
obj.respond_to?(:serialize) && obj.respond_to?(:deserialize)
|
55
|
+
end
|
56
|
+
|
57
|
+
# A utility to use a big-endian, unsigned integer sedes type with
|
58
|
+
# unspecified length.
|
59
|
+
#
|
60
|
+
# @return [Eth::Rlp::Sedes::BigEndianInt] a big-endian, unsigned integer sedes.
|
61
|
+
def big_endian_int
|
62
|
+
@big_endian_int ||= BigEndianInt.new
|
63
|
+
end
|
64
|
+
|
65
|
+
# A utility to use a binary sedes type.
|
66
|
+
#
|
67
|
+
# @return [Eth::Rlp::Sedes::Binary] a binary sedes.
|
68
|
+
def binary
|
69
|
+
@binary ||= Binary.new
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/eth/rlp.rb
ADDED
@@ -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
|
+
require "eth/rlp/decoder"
|
18
|
+
require "eth/rlp/encoder"
|
19
|
+
require "eth/rlp/sedes"
|
20
|
+
require "eth/util"
|
21
|
+
|
22
|
+
# Provides the {Eth} module.
|
23
|
+
module Eth
|
24
|
+
|
25
|
+
# Provides an recursive-length prefix (RLP) encoder and decoder.
|
26
|
+
module Rlp
|
27
|
+
extend self
|
28
|
+
|
29
|
+
# The Rlp module exposes a variety of exceptions grouped as {RlpException}.
|
30
|
+
class RlpException < StandardError; end
|
31
|
+
|
32
|
+
# An error-type to point out RLP-encoding errors.
|
33
|
+
class EncodingError < RlpException; end
|
34
|
+
|
35
|
+
# An error-type to point out RLP-decoding errors.
|
36
|
+
class DecodingError < RlpException; end
|
37
|
+
|
38
|
+
# An error-type to point out RLP-type serialization errors.
|
39
|
+
class SerializationError < RlpException; end
|
40
|
+
|
41
|
+
# An error-type to point out RLP-type serialization errors.
|
42
|
+
class DeserializationError < RlpException; end
|
43
|
+
|
44
|
+
# A wrapper to represent already RLP-encoded data.
|
45
|
+
class Data < String; end
|
46
|
+
|
47
|
+
# Performes an {Eth::Rlp::Encoder} on any ruby object.
|
48
|
+
#
|
49
|
+
# @param obj [Object] any ruby object.
|
50
|
+
# @return [String] a packed, RLP-encoded item.
|
51
|
+
def encode(obj)
|
52
|
+
Rlp::Encoder.perform obj
|
53
|
+
end
|
54
|
+
|
55
|
+
# Performes an {Eth::Rlp::Decoder} on any RLP-encoded item.
|
56
|
+
#
|
57
|
+
# @param rlp [String] a packed, RLP-encoded item.
|
58
|
+
# @return [Object] a decoded ruby object.
|
59
|
+
def decode(rlp)
|
60
|
+
Rlp::Decoder.perform rlp
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,163 @@
|
|
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
|
+
require "rbsecp256k1"
|
16
|
+
|
17
|
+
# Provides the {Eth} module.
|
18
|
+
module Eth
|
19
|
+
|
20
|
+
# Defines handy tools for verifying and recovering signatures.
|
21
|
+
module Signature
|
22
|
+
extend self
|
23
|
+
|
24
|
+
# Provides a special signature error if signature is invalid.
|
25
|
+
class SignatureError < StandardError; end
|
26
|
+
|
27
|
+
# EIP-191 prefix byte 0x19
|
28
|
+
EIP191_PREFIX_BYTE = "\x19".freeze
|
29
|
+
|
30
|
+
# EIP-712 version byte 0x01
|
31
|
+
EIP712_VERSION_BYTE = "\x01".freeze
|
32
|
+
|
33
|
+
# Prefix message as per EIP-191 with `0x19` to ensure the data is not
|
34
|
+
# valid RLP and thus not mistaken for a transaction.
|
35
|
+
# EIP-191 Version byte: `0x45` (`E`)
|
36
|
+
# Ref: https://eips.ethereum.org/EIPS/eip-191
|
37
|
+
#
|
38
|
+
# @param message [String] the message string to be prefixed.
|
39
|
+
# @return [String] an EIP-191 prefixed string.
|
40
|
+
def prefix_message(message)
|
41
|
+
"#{EIP191_PREFIX_BYTE}Ethereum Signed Message:\n#{message.size}#{message}"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Dissects a signature blob of 65+ bytes into its `r`, `s`, and `v`
|
45
|
+
# values.
|
46
|
+
#
|
47
|
+
# @param signature [String] a concatenated Secp256k1 signature string.
|
48
|
+
# @return [String, String, String] the `r`, `s`, and `v` values.
|
49
|
+
# @raise [SignatureError] if signature is of unknown size.
|
50
|
+
def dissect(signature)
|
51
|
+
signature = Util.bin_to_hex signature unless Util.is_hex? signature
|
52
|
+
signature = Util.remove_hex_prefix signature
|
53
|
+
if signature.size < 130
|
54
|
+
raise SignatureError, "Unknown signature length #{signature.size}!"
|
55
|
+
end
|
56
|
+
r = signature[0, 64]
|
57
|
+
s = signature[64, 64]
|
58
|
+
v = signature[128..]
|
59
|
+
return r, s, v
|
60
|
+
end
|
61
|
+
|
62
|
+
# Recovers a signature from arbitrary data without validation on a given chain.
|
63
|
+
#
|
64
|
+
# @param blob [String] that arbitrary data to be recovered.
|
65
|
+
# @param signature [String] the hex string containing the signature.
|
66
|
+
# @param chain_id [Integer] the chain ID the signature should be recovered from.
|
67
|
+
# @return [String] a hexa-decimal, uncompressed public key.
|
68
|
+
# @raise [SignatureError] if signature is of invalid size or invalid v.
|
69
|
+
def recover(blob, signature, chain_id = Chain::ETHEREUM)
|
70
|
+
context = Secp256k1::Context.new
|
71
|
+
r, s, v = dissect signature
|
72
|
+
v = v.to_i(16)
|
73
|
+
raise SignatureError, "Invalid signature v byte #{v} for chain ID #{chain_id}!" if v < chain_id
|
74
|
+
recovery_id = Chain.to_recovery_id v, chain_id
|
75
|
+
signature_rs = Util.hex_to_bin "#{r}#{s}"
|
76
|
+
recoverable_signature = context.recoverable_signature_from_compact signature_rs, recovery_id
|
77
|
+
public_key = recoverable_signature.recover_public_key blob
|
78
|
+
Util.bin_to_hex public_key.uncompressed
|
79
|
+
end
|
80
|
+
|
81
|
+
# Recovers a public key from a prefixed, personal message and
|
82
|
+
# a signature on a given chain. (EIP-191)
|
83
|
+
# Ref: https://eips.ethereum.org/EIPS/eip-191
|
84
|
+
#
|
85
|
+
# @param message [String] the message string.
|
86
|
+
# @param signature [String] the hex string containing the signature.
|
87
|
+
# @param chain_id [Integer] the chain ID the signature should be recovered from.
|
88
|
+
# @return [String] a hexa-decimal, uncompressed public key.
|
89
|
+
def personal_recover(message, signature, chain_id = Chain::ETHEREUM)
|
90
|
+
prefixed_message = prefix_message message
|
91
|
+
hashed_message = Util.keccak256 prefixed_message
|
92
|
+
recover hashed_message, signature, chain_id
|
93
|
+
end
|
94
|
+
|
95
|
+
# Recovers a public key from a typed data structure and a signature
|
96
|
+
# on a given chain. (EIP-712)
|
97
|
+
# Ref: https://eips.ethereum.org/EIPS/eip-712
|
98
|
+
#
|
99
|
+
# @param typed_data [Array] all the data in the typed data structure to be recovered.
|
100
|
+
# @param signature [String] the hex string containing the signature.
|
101
|
+
# @param chain_id [Integer] the chain ID the signature should be recovered from.
|
102
|
+
# @return [String] a hexa-decimal, uncompressed public key.
|
103
|
+
def recover_typed_data(typed_data, signature, chain_id = Chain::ETHEREUM)
|
104
|
+
hash_to_sign = Eip712.hash typed_data
|
105
|
+
recover hash_to_sign, signature, chain_id
|
106
|
+
end
|
107
|
+
|
108
|
+
# Verifies a signature for a given public key or address.
|
109
|
+
#
|
110
|
+
# @param blob [String] that arbitrary data to be verified.
|
111
|
+
# @param signature [String] the hex string containing the signature.
|
112
|
+
# @param public_key [String] either a public key or an Ethereum address.
|
113
|
+
# @param chain_id [Integer] the chain ID used to sign.
|
114
|
+
# @return [Boolean] true if signature matches provided public key.
|
115
|
+
# @raise [SignatureError] if it cannot determine the type of data or public key.
|
116
|
+
def verify(blob, signature, public_key, chain_id = Chain::ETHEREUM)
|
117
|
+
recovered_key = nil
|
118
|
+
if blob.instance_of? Array or blob.instance_of? Hash
|
119
|
+
|
120
|
+
# recover Array from sign_typed_data
|
121
|
+
recovered_key = recover_typed_data blob, signature, chain_id
|
122
|
+
elsif blob.instance_of? String and blob.encoding != Encoding::ASCII_8BIT
|
123
|
+
|
124
|
+
# recover message from personal_sign
|
125
|
+
recovered_key = personal_recover blob, signature, chain_id
|
126
|
+
elsif blob.instance_of? String and (Util.is_hex? blob or blob.encoding == Encoding::ASCII_8BIT)
|
127
|
+
|
128
|
+
# if nothing else, recover from arbitrary signature
|
129
|
+
recovered_key = recover blob, signature, chain_id
|
130
|
+
end
|
131
|
+
|
132
|
+
# raise if we cannot determine the data format
|
133
|
+
raise SignatureError, "Unknown data format to verify: #{blob}" if recovered_key.nil?
|
134
|
+
|
135
|
+
if public_key.instance_of? Address
|
136
|
+
|
137
|
+
# recovering using an Eth::Address
|
138
|
+
address = public_key.to_s
|
139
|
+
recovered_address = Util.public_key_to_address(recovered_key).to_s
|
140
|
+
return address == recovered_address
|
141
|
+
elsif public_key.instance_of? Secp256k1::PublicKey
|
142
|
+
|
143
|
+
# recovering using an Secp256k1::PublicKey
|
144
|
+
public_hex = Util.bin_to_hex public_key.uncompressed
|
145
|
+
return public_hex == recovered_key
|
146
|
+
elsif public_key.size == 42
|
147
|
+
|
148
|
+
# recovering using an address String
|
149
|
+
address = Address.new(public_key).to_s
|
150
|
+
recovered_address = Util.public_key_to_address(recovered_key).to_s
|
151
|
+
return address == recovered_address
|
152
|
+
elsif public_key.size == 130
|
153
|
+
|
154
|
+
# recovering using an uncompressed public key String
|
155
|
+
return public_key == recovered_key
|
156
|
+
else
|
157
|
+
|
158
|
+
# raise if we cannot determine the public key format used
|
159
|
+
raise SignatureError, "Invalid public key or address supplied #{public_key}!"
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
data/lib/eth/solidity.rb
ADDED
@@ -0,0 +1,75 @@
|
|
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
|
+
require "open3"
|
16
|
+
|
17
|
+
# Provides the {Eth} module.
|
18
|
+
module Eth
|
19
|
+
|
20
|
+
# Class to create {Solidity} compiler bingings for Ruby.
|
21
|
+
class Solidity
|
22
|
+
|
23
|
+
# Provides a Compiler Error in case the contract does not compile.
|
24
|
+
class CompilerError < StandardError; end
|
25
|
+
|
26
|
+
# Solidity compiler binary path.
|
27
|
+
attr_reader :compiler
|
28
|
+
|
29
|
+
# Instantiates a Solidity `solc` system compiler binding that can be
|
30
|
+
# used to compile Solidity contracts.
|
31
|
+
def initialize
|
32
|
+
|
33
|
+
# Currently only supports `solc`.
|
34
|
+
solc = get_compiler_path
|
35
|
+
raise SystemCallError, "Unable to find the solc compiler path!" if solc.nil?
|
36
|
+
@compiler = solc
|
37
|
+
end
|
38
|
+
|
39
|
+
# Use the bound Solidity executable to compile the given contract.
|
40
|
+
#
|
41
|
+
# @param contract [String] path of the contract to compile.
|
42
|
+
# @return [Array] JSON containing the compiled contract and ABI for all contracts.
|
43
|
+
def compile(contract)
|
44
|
+
raise Errno::ENOENT, "Contract file not found: #{contract}" unless File.exist? contract
|
45
|
+
command = "#{@compiler} --optimize --combined-json bin,abi #{contract}"
|
46
|
+
output, error, status = Open3.capture3 command
|
47
|
+
raise SystemCallError, "Unable to run solc compiler!" if status.exitstatus === 127
|
48
|
+
raise CompilerError, error unless status.success?
|
49
|
+
json = JSON.parse output
|
50
|
+
result = {}
|
51
|
+
json["contracts"].each do |key, value|
|
52
|
+
_file, name = key.split ":"
|
53
|
+
result[name] = {}
|
54
|
+
result[name]["abi"] = value["abi"]
|
55
|
+
result[name]["bin"] = value["bin"]
|
56
|
+
end
|
57
|
+
return result
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# Tries to find a system executable path for the given compiler binary name.
|
63
|
+
def get_compiler_path(name = "solc")
|
64
|
+
extensions = [""]
|
65
|
+
extensions = ENV["PATHEXT"].split(";") unless ENV["PATHEXT"].nil?
|
66
|
+
ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
|
67
|
+
extensions.each do |ext|
|
68
|
+
executable = File.join path, "#{name}#{ext}"
|
69
|
+
return executable if File.executable? executable and !File.directory? executable
|
70
|
+
end
|
71
|
+
end
|
72
|
+
return nil
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|