eth 0.5.0 → 0.5.3

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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/codeql.yml +4 -0
  3. data/.github/workflows/spec.yml +14 -3
  4. data/.yardopts +1 -0
  5. data/AUTHORS.txt +14 -1
  6. data/CHANGELOG.md +63 -13
  7. data/README.md +121 -18
  8. data/bin/console +2 -1
  9. data/bin/setup +3 -4
  10. data/codecov.yml +6 -0
  11. data/eth.gemspec +5 -7
  12. data/lib/eth/abi/event.rb +137 -0
  13. data/lib/eth/abi/type.rb +8 -7
  14. data/lib/eth/abi.rb +29 -11
  15. data/lib/eth/address.rb +12 -5
  16. data/lib/eth/api.rb +223 -0
  17. data/lib/eth/chain.rb +31 -28
  18. data/lib/eth/client/http.rb +63 -0
  19. data/lib/eth/client/ipc.rb +50 -0
  20. data/lib/eth/client.rb +468 -0
  21. data/lib/eth/constant.rb +71 -0
  22. data/lib/eth/contract/event.rb +41 -0
  23. data/lib/eth/contract/function.rb +56 -0
  24. data/lib/eth/contract/function_input.rb +36 -0
  25. data/lib/eth/contract/function_output.rb +32 -0
  26. data/lib/eth/contract/initializer.rb +46 -0
  27. data/lib/eth/contract.rb +120 -0
  28. data/lib/eth/eip712.rb +2 -2
  29. data/lib/eth/key/decrypter.rb +16 -13
  30. data/lib/eth/key/encrypter.rb +27 -25
  31. data/lib/eth/key.rb +21 -16
  32. data/lib/eth/rlp/decoder.rb +114 -0
  33. data/lib/eth/rlp/encoder.rb +78 -0
  34. data/lib/eth/rlp/sedes/big_endian_int.rb +66 -0
  35. data/lib/eth/rlp/sedes/binary.rb +97 -0
  36. data/lib/eth/rlp/sedes/list.rb +84 -0
  37. data/lib/eth/rlp/sedes.rb +74 -0
  38. data/lib/eth/rlp.rb +63 -0
  39. data/lib/eth/signature.rb +11 -8
  40. data/lib/eth/solidity.rb +75 -0
  41. data/lib/eth/tx/eip1559.rb +22 -14
  42. data/lib/eth/tx/eip2930.rb +23 -15
  43. data/lib/eth/tx/legacy.rb +14 -10
  44. data/lib/eth/tx.rb +28 -34
  45. data/lib/eth/unit.rb +1 -1
  46. data/lib/eth/util.rb +68 -11
  47. data/lib/eth/version.rb +3 -3
  48. data/lib/eth.rb +15 -2
  49. metadata +31 -23
  50. data/lib/eth/abi/constant.rb +0 -63
@@ -0,0 +1,66 @@
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 an recursive-length prefix (RLP) encoder and decoder.
21
+ module Rlp
22
+
23
+ # Provides serializable and deserializable types (SeDes).
24
+ module Sedes
25
+
26
+ # A serializable, big-endian, unsigned integer type.
27
+ class BigEndianInt
28
+
29
+ # Create a serializable, big-endian, unsigned integer.
30
+ #
31
+ # @param size [Integer] the size of the big endian.
32
+ def initialize(size = nil)
33
+ @size = size
34
+ end
35
+
36
+ # Serialize a big-endian integer.
37
+ #
38
+ # @param obj [Integer] the integer to be serialized.
39
+ # @return [String] a serialized big-endian integer.
40
+ # @raise [SerializationError] if provided object is not an integer.
41
+ # @raise [SerializationError] if provided integer is negative.
42
+ # @raise [SerializationError] if provided integer is too big for @size.
43
+ def serialize(obj)
44
+ raise SerializationError, "Can only serialize integers" unless obj.is_a?(Integer)
45
+ raise SerializationError, "Cannot serialize negative integers" if obj < 0
46
+ raise SerializationError, "Integer too large (does not fit in #{@size} bytes)" if @size && obj >= 256 ** @size
47
+ s = obj == 0 ? Constant::BYTE_EMPTY : Util.int_to_big_endian(obj)
48
+ @size ? "#{Constant::BYTE_ZERO * [0, @size - s.size].max}#{s}" : s
49
+ end
50
+
51
+ # Deserializes an unsigned integer.
52
+ #
53
+ # @param serial [String] the serialized integer.
54
+ # @return [Integer] a number.
55
+ # @raise [DeserializationError] if provided serial is of wrong size.
56
+ # @raise [DeserializationError] if provided serial is not of minimal length.
57
+ def deserialize(serial)
58
+ raise DeserializationError, "Invalid serialization (wrong size)" if @size && serial.size != @size
59
+ raise DeserializationError, "Invalid serialization (not minimal length)" if !@size && serial.size > 0 && serial[0] == Constant::BYTE_ZERO
60
+ serial = serial || Constant::BYTE_ZERO
61
+ Util.big_endian_to_int(serial)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,97 @@
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 an recursive-length prefix (RLP) encoder and decoder.
21
+ module Rlp
22
+
23
+ # Provides serializable and deserializable types (SeDes).
24
+ module Sedes
25
+
26
+ # A sedes type for binary values.
27
+ class Binary
28
+
29
+ # A singleton class for binary values of fixed length.
30
+ class << self
31
+
32
+ # Create a serializable bianry of fixed size.
33
+ #
34
+ # @param l [Integer] the fixed size of the binary.
35
+ # @param allow_empty [Boolean] indicator wether empty binaries should be allowed.
36
+ # @return [Eth::Rlp::Sedes::Binary] a serializable binary of fixed size.
37
+ def fixed_length(l, allow_empty: false)
38
+ new(min_length: l, max_length: l, allow_empty: allow_empty)
39
+ end
40
+
41
+ # Checks wether the given object is of a valid binary type.
42
+ #
43
+ # @param obj [Object] the supposed binary item to check.
44
+ # @return [Boolean] true if valid.
45
+ def valid_type?(obj)
46
+ obj.instance_of? String
47
+ end
48
+ end
49
+
50
+ # Create a serializable bianry of variable size.
51
+ #
52
+ # @param min_length [Integer] the minimum size of the binary.
53
+ # @param max_length [Integer] the maximum size of the binary.
54
+ # @param allow_empty [Boolean] indicator wether empty binaries should be allowed.
55
+ def initialize(min_length: 0, max_length: Constant::INFINITY, allow_empty: false)
56
+ @min_length = min_length
57
+ @max_length = max_length
58
+ @allow_empty = allow_empty
59
+ end
60
+
61
+ # Serializes a binary.
62
+ #
63
+ # @param obj [String] the binary to serialize.
64
+ # @return [Object] a serialized binary.
65
+ # @raise [SerializationError] if provided object is of invalid type.
66
+ # @raise [SerializationError] if provided binary is of invalid length.
67
+ def serialize(obj)
68
+ raise SerializationError, "Object is not a serializable (#{obj.class})" unless self.class.valid_type? obj
69
+ serial = Util.str_to_bytes obj
70
+ raise SerializationError, "Object has invalid length" unless valid_length? serial.size
71
+ serial
72
+ end
73
+
74
+ # Deserializes a binary.
75
+ #
76
+ # @param serial [Object] the serialized binary.
77
+ # @return [String] a deserialized binary.
78
+ # @raise [DeserializationError] if provided serial is of wrong type.
79
+ # @raise [DeserializationError] if provided serial is of wrong length.
80
+ def deserialize(serial)
81
+ raise DeserializationError, "Objects of type #{serial.class} cannot be deserialized" unless Util.is_primitive? serial
82
+ raise DeserializationError, "#{serial.class} has invalid length" unless valid_length? serial.size
83
+ serial
84
+ end
85
+
86
+ # Checks wether the given length fits the defined size boundaries of the
87
+ # binary type.
88
+ #
89
+ # @param length [Integer] the supposed length of the binary item.
90
+ # @return [Boolean] true if valid.
91
+ def valid_length?(length)
92
+ (@min_length <= length && length <= @max_length) || (@allow_empty && length == 0)
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,84 @@
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 an recursive-length prefix (RLP) encoder and decoder.
21
+ module Rlp
22
+
23
+ # Provides serializable and deserializable types (SeDes).
24
+ module Sedes
25
+
26
+ # A sedes type for lists of fixed length.
27
+ class List < Array
28
+
29
+ # Create a serializable list of fixed size.
30
+ #
31
+ # @param elements [Array] an array indicating the structure of the list.
32
+ # @param strict [Boolean] an option to enforce the given structure.
33
+ def initialize(elements: [], strict: true)
34
+ super()
35
+ @strict = strict
36
+ elements.each do |e|
37
+ if Sedes.is_sedes?(e)
38
+ push e
39
+ elsif Util.is_list?(e)
40
+ push List.new(elements: e)
41
+ else
42
+ raise TypeError, "Instances of List must only contain sedes objects or nested sequences thereof."
43
+ end
44
+ end
45
+ end
46
+
47
+ # Serialize an array.
48
+ #
49
+ # @param obj [Array] the array to be serialized.
50
+ # @return [Array] a serialized list.
51
+ # @raise [SerializationError] if provided array is not a sequence.
52
+ # @raise [SerializationError] if provided array is of wrong length.
53
+ def serialize(obj)
54
+ raise SerializationError, "Can only serialize sequences" unless Util.is_list?(obj)
55
+ raise SerializationError, "List has wrong length" if (@strict && self.size != obj.size) || self.size < obj.size
56
+ result = []
57
+ obj.zip(self).each_with_index do |(element, sedes), i|
58
+ result.push sedes.serialize(element)
59
+ end
60
+ result
61
+ end
62
+
63
+ # Deserializes a list.
64
+ #
65
+ # @param serial [Array] the serialized list.
66
+ # @return [Array] a deserialized list.
67
+ # @raise [DeserializationError] if provided serial is not a sequence.
68
+ # @raise [DeserializationError] if provided serial is of wrong length.
69
+ def deserialize(serial)
70
+ raise DeserializationError, "Can only deserialize sequences" unless Util.is_list?(serial)
71
+ raise DeserializationError, "List has wrong length" if @strict && serial.size != self.size
72
+ result = []
73
+ len = [serial.size, self.size].min
74
+ len.times do |i|
75
+ sedes = self[i]
76
+ element = serial[i]
77
+ result.push sedes.deserialize(element)
78
+ end
79
+ result.freeze
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -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
data/lib/eth/signature.rb CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  require "rbsecp256k1"
16
16
 
17
- # Provides the `Eth` module.
17
+ # Provides the {Eth} module.
18
18
  module Eth
19
19
 
20
20
  # Defines handy tools for verifying and recovering signatures.
@@ -30,21 +30,22 @@ module Eth
30
30
  # EIP-712 version byte 0x01
31
31
  EIP712_VERSION_BYTE = "\x01".freeze
32
32
 
33
- # Prefix message as per EIP-191 with 0x19 to ensure the data is not
33
+ # Prefix message as per EIP-191 with `0x19` to ensure the data is not
34
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
35
+ # EIP-191 Version byte: `0x45` (`E`)
36
+ # Ref: https://eips.ethereum.org/EIPS/eip-191
37
37
  #
38
38
  # @param message [String] the message string to be prefixed.
39
- # @return [String] an EIP-191 prefixed string
39
+ # @return [String] an EIP-191 prefixed string.
40
40
  def prefix_message(message)
41
41
  "#{EIP191_PREFIX_BYTE}Ethereum Signed Message:\n#{message.size}#{message}"
42
42
  end
43
43
 
44
- # Dissects a signature blob of 65 bytes into its r, s, and v values.
44
+ # Dissects a signature blob of 65+ bytes into its `r`, `s`, and `v`
45
+ # values.
45
46
  #
46
- # @param signature [String] a Secp256k1 signature.
47
- # @return [String, String, String] the r, s, and v values.
47
+ # @param signature [String] a concatenated Secp256k1 signature string.
48
+ # @return [String, String, String] the `r`, `s`, and `v` values.
48
49
  # @raise [SignatureError] if signature is of unknown size.
49
50
  def dissect(signature)
50
51
  signature = Util.bin_to_hex signature unless Util.is_hex? signature
@@ -79,6 +80,7 @@ module Eth
79
80
 
80
81
  # Recovers a public key from a prefixed, personal message and
81
82
  # a signature on a given chain. (EIP-191)
83
+ # Ref: https://eips.ethereum.org/EIPS/eip-191
82
84
  #
83
85
  # @param message [String] the message string.
84
86
  # @param signature [String] the hex string containing the signature.
@@ -92,6 +94,7 @@ module Eth
92
94
 
93
95
  # Recovers a public key from a typed data structure and a signature
94
96
  # on a given chain. (EIP-712)
97
+ # Ref: https://eips.ethereum.org/EIPS/eip-712
95
98
  #
96
99
  # @param typed_data [Array] all the data in the typed data structure to be recovered.
97
100
  # @param signature [String] the hex string containing the signature.
@@ -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
@@ -12,7 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- # Provides the `Eth` module.
15
+ # Provides the {Eth} module.
16
16
  module Eth
17
17
 
18
18
  # Provides the `Tx` module supporting various transaction types.
@@ -20,9 +20,11 @@ module Eth
20
20
 
21
21
  # Provides support for EIP-1559 transactions utilizing EIP-2718
22
22
  # types and envelopes.
23
+ # Ref: https://eips.ethereum.org/EIPS/eip-1559
23
24
  class Eip1559
24
25
 
25
26
  # The EIP-155 Chain ID.
27
+ # Ref: https://eips.ethereum.org/EIPS/eip-155
26
28
  attr_reader :chain_id
27
29
 
28
30
  # The transaction nonce provided by the signer.
@@ -47,6 +49,7 @@ module Eth
47
49
  attr_reader :payload
48
50
 
49
51
  # An optional EIP-2930 access list.
52
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
50
53
  attr_reader :access_list
51
54
 
52
55
  # The signature's y-parity byte (not v).
@@ -66,8 +69,10 @@ module Eth
66
69
 
67
70
  # Create a type-2 (EIP-1559) transaction payload object that
68
71
  # can be prepared for envelope, signature and broadcast.
72
+ # Ref: https://eips.ethereum.org/EIPS/eip-1559
69
73
  #
70
74
  # @param params [Hash] all necessary transaction fields.
75
+ # @option params [Integer] :chain_id the chain ID.
71
76
  # @option params [Integer] :nonce the signer nonce.
72
77
  # @option params [Integer] :priority_fee the max priority fee per gas.
73
78
  # @option params [Integer] :max_gas_fee the max transaction fee per gas.
@@ -77,6 +82,7 @@ module Eth
77
82
  # @option params [Integer] :value the transaction value.
78
83
  # @option params [String] :data the transaction data payload.
79
84
  # @option params [Array] :access_list an optional access list.
85
+ # @raise [ParameterError] if gas limit is too low.
80
86
  def initialize(params)
81
87
  fields = { recovery_id: nil, r: 0, s: 0 }.merge params
82
88
 
@@ -89,6 +95,7 @@ module Eth
89
95
 
90
96
  # ensure sane values for all mandatory fields
91
97
  fields = Tx.validate_params fields
98
+ fields = Tx.validate_eip1559_params fields
92
99
  fields[:access_list] = Tx.sanitize_list fields[:access_list]
93
100
 
94
101
  # ensure gas limit is not too low
@@ -121,7 +128,7 @@ module Eth
121
128
  # Overloads the constructor for decoding raw transactions and creating unsigned copies.
122
129
  konstructor :decode, :unsigned_copy
123
130
 
124
- # Decodes a raw transaction hex into an Eth::Tx::Eip1559
131
+ # Decodes a raw transaction hex into an {Eth::Tx::Eip1559}
125
132
  # transaction object.
126
133
  #
127
134
  # @param hex [String] the raw transaction hex-string.
@@ -135,7 +142,7 @@ module Eth
135
142
  raise TransactionTypeError, "Invalid transaction type #{type}!" if type.to_i(16) != TYPE_1559
136
143
 
137
144
  bin = Util.hex_to_bin hex[2..]
138
- tx = RLP.decode(bin)
145
+ tx = Rlp.decode bin
139
146
 
140
147
  # decoded transactions always have 9 + 3 fields, even if they are empty or zero
141
148
  raise ParameterError, "Transaction missing fields!" if tx.size < 9
@@ -221,8 +228,8 @@ module Eth
221
228
  #
222
229
  # @param key [Eth::Key] the key-pair to use for signing.
223
230
  # @return [String] a transaction hash.
224
- # @raise [SignatureError] if transaction is already signed.
225
- # @raise [SignatureError] if sender address does not match signing key.
231
+ # @raise [Signature::SignatureError] if transaction is already signed.
232
+ # @raise [Signature::SignatureError] if sender address does not match signing key.
226
233
  def sign(key)
227
234
  if Tx.is_signed? self
228
235
  raise Signature::SignatureError, "Transaction is already signed!"
@@ -249,7 +256,7 @@ module Eth
249
256
  # with an EIP-1559 type prefix.
250
257
  #
251
258
  # @return [String] a raw, RLP-encoded EIP-1559 type transaction object.
252
- # @raise [SignatureError] if the transaction is not yet signed.
259
+ # @raise [Signature::SignatureError] if the transaction is not yet signed.
253
260
  def encoded
254
261
  unless Tx.is_signed? self
255
262
  raise Signature::SignatureError, "Transaction is not signed!"
@@ -262,12 +269,12 @@ module Eth
262
269
  tx_data.push Util.serialize_int_to_big_endian @gas_limit
263
270
  tx_data.push Util.hex_to_bin @destination
264
271
  tx_data.push Util.serialize_int_to_big_endian @amount
265
- tx_data.push @payload
266
- tx_data.push @access_list
272
+ tx_data.push Rlp::Sedes.binary.serialize @payload
273
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
267
274
  tx_data.push Util.serialize_int_to_big_endian @signature_y_parity
268
- tx_data.push Util.hex_to_bin @signature_r
269
- tx_data.push Util.hex_to_bin @signature_s
270
- tx_encoded = RLP.encode tx_data
275
+ tx_data.push Util.serialize_int_to_big_endian @signature_r
276
+ tx_data.push Util.serialize_int_to_big_endian @signature_s
277
+ tx_encoded = Rlp.encode tx_data
271
278
 
272
279
  # create an EIP-2718 envelope with EIP-1559 type payload
273
280
  tx_type = Util.serialize_int_to_big_endian @type
@@ -301,9 +308,9 @@ module Eth
301
308
  tx_data.push Util.serialize_int_to_big_endian @gas_limit
302
309
  tx_data.push Util.hex_to_bin @destination
303
310
  tx_data.push Util.serialize_int_to_big_endian @amount
304
- tx_data.push @payload
305
- tx_data.push @access_list
306
- tx_encoded = RLP.encode tx_data
311
+ tx_data.push Rlp::Sedes.binary.serialize @payload
312
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
313
+ tx_encoded = Rlp.encode tx_data
307
314
 
308
315
  # create an EIP-2718 envelope with EIP-1559 type payload (unsigned)
309
316
  tx_type = Util.serialize_int_to_big_endian @type
@@ -319,6 +326,7 @@ module Eth
319
326
 
320
327
  private
321
328
 
329
+ # Force-sets an existing signature of a decoded transaction.
322
330
  def _set_signature(recovery_id, r, s)
323
331
  @signature_y_parity = recovery_id
324
332
  @signature_r = r