eth 0.5.14 → 0.5.16
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.
- checksums.yaml +4 -4
 - data/.github/workflows/codeql.yml +4 -4
 - data/.github/workflows/docs.yml +1 -1
 - data/.github/workflows/spec.yml +31 -13
 - data/CHANGELOG.md +53 -0
 - data/CODE_OF_CONDUCT.md +3 -5
 - data/Gemfile +3 -3
 - data/README.md +8 -6
 - data/SECURITY.md +2 -2
 - data/eth.gemspec +10 -1
 - data/lib/eth/abi/decoder.rb +94 -39
 - data/lib/eth/abi/encoder.rb +85 -58
 - data/lib/eth/abi/function.rb +124 -0
 - data/lib/eth/abi/type.rb +69 -5
 - data/lib/eth/abi.rb +2 -0
 - data/lib/eth/bls.rb +68 -0
 - data/lib/eth/chain.rb +3 -0
 - data/lib/eth/client/http.rb +5 -8
 - data/lib/eth/client/ws.rb +323 -0
 - data/lib/eth/client.rb +44 -40
 - data/lib/eth/contract/error.rb +62 -0
 - data/lib/eth/contract/function.rb +21 -0
 - data/lib/eth/contract/function_output.rb +11 -3
 - data/lib/eth/contract.rb +55 -4
 - data/lib/eth/eip712.rb +48 -13
 - data/lib/eth/key.rb +1 -1
 - data/lib/eth/tx/eip1559.rb +32 -7
 - data/lib/eth/tx/eip2930.rb +31 -6
 - data/lib/eth/tx/eip4844.rb +401 -0
 - data/lib/eth/tx/eip7702.rb +34 -9
 - data/lib/eth/tx/legacy.rb +30 -6
 - data/lib/eth/tx.rb +45 -4
 - data/lib/eth/util.rb +19 -7
 - data/lib/eth/version.rb +1 -1
 - data/lib/eth.rb +1 -0
 - metadata +53 -15
 
    
        data/lib/eth/eip712.rb
    CHANGED
    
    | 
         @@ -47,7 +47,12 @@ module Eth 
     | 
|
| 
       47 
47 
     | 
    
         | 
| 
       48 
48 
     | 
    
         
             
                    # recursively look for further nested dependencies
         
     | 
| 
       49 
49 
     | 
    
         
             
                    types[primary_type.to_sym].each do |t|
         
     | 
| 
       50 
     | 
    
         
            -
                       
     | 
| 
      
 50 
     | 
    
         
            +
                      nested_type = t[:type]
         
     | 
| 
      
 51 
     | 
    
         
            +
                      # unpack arrays to their inner types to resolve dependencies
         
     | 
| 
      
 52 
     | 
    
         
            +
                      if nested_type.end_with?("]")
         
     | 
| 
      
 53 
     | 
    
         
            +
                        nested_type = nested_type.partition("[").first
         
     | 
| 
      
 54 
     | 
    
         
            +
                      end
         
     | 
| 
      
 55 
     | 
    
         
            +
                      dependency = type_dependencies nested_type, types, result
         
     | 
| 
       51 
56 
     | 
    
         
             
                    end
         
     | 
| 
       52 
57 
     | 
    
         
             
                    return result
         
     | 
| 
       53 
58 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -113,19 +118,12 @@ module Eth 
     | 
|
| 
       113 
118 
     | 
    
         
             
                  types[primary_type.to_sym].each do |field|
         
     | 
| 
       114 
119 
     | 
    
         
             
                    value = data[field[:name].to_sym]
         
     | 
| 
       115 
120 
     | 
    
         
             
                    type = field[:type]
         
     | 
| 
       116 
     | 
    
         
            -
                    if type 
     | 
| 
       117 
     | 
    
         
            -
                      encoded_types.push  
     | 
| 
       118 
     | 
    
         
            -
                      encoded_values.push  
     | 
| 
       119 
     | 
    
         
            -
                    elsif type == "bytes"
         
     | 
| 
       120 
     | 
    
         
            -
                      encoded_types.push "bytes32"
         
     | 
| 
       121 
     | 
    
         
            -
                      value = Util.hex_to_bin value
         
     | 
| 
       122 
     | 
    
         
            -
                      encoded_values.push Util.keccak256 value
         
     | 
| 
       123 
     | 
    
         
            -
                    elsif !types[type.to_sym].nil?
         
     | 
| 
      
 121 
     | 
    
         
            +
                    if type.end_with?("]")
         
     | 
| 
      
 122 
     | 
    
         
            +
                      encoded_types.push type
         
     | 
| 
      
 123 
     | 
    
         
            +
                      encoded_values.push encode_array(type, value, types)
         
     | 
| 
      
 124 
     | 
    
         
            +
                    elsif type == "string" || type == "bytes" || !types[type.to_sym].nil?
         
     | 
| 
       124 
125 
     | 
    
         
             
                      encoded_types.push "bytes32"
         
     | 
| 
       125 
     | 
    
         
            -
                       
     | 
| 
       126 
     | 
    
         
            -
                      encoded_values.push Util.keccak256 value
         
     | 
| 
       127 
     | 
    
         
            -
                    elsif type.end_with? "]"
         
     | 
| 
       128 
     | 
    
         
            -
                      raise NotImplementedError, "Arrays currently unimplemented for EIP-712."
         
     | 
| 
      
 126 
     | 
    
         
            +
                      encoded_values.push encode_value(type, value, types)
         
     | 
| 
       129 
127 
     | 
    
         
             
                    else
         
     | 
| 
       130 
128 
     | 
    
         
             
                      encoded_types.push type
         
     | 
| 
       131 
129 
     | 
    
         
             
                      encoded_values.push value
         
     | 
| 
         @@ -136,6 +134,43 @@ module Eth 
     | 
|
| 
       136 
134 
     | 
    
         
             
                  return Abi.encode encoded_types, encoded_values
         
     | 
| 
       137 
135 
     | 
    
         
             
                end
         
     | 
| 
       138 
136 
     | 
    
         | 
| 
      
 137 
     | 
    
         
            +
                # Encodes a single value according to its type following EIP-712 rules.
         
     | 
| 
      
 138 
     | 
    
         
            +
                # Returns a 32-byte binary string.
         
     | 
| 
      
 139 
     | 
    
         
            +
                def encode_value(type, value, types)
         
     | 
| 
      
 140 
     | 
    
         
            +
                  if type == "string"
         
     | 
| 
      
 141 
     | 
    
         
            +
                    return Util.keccak256 value
         
     | 
| 
      
 142 
     | 
    
         
            +
                  elsif type == "bytes"
         
     | 
| 
      
 143 
     | 
    
         
            +
                    value = Util.hex_to_bin value
         
     | 
| 
      
 144 
     | 
    
         
            +
                    return Util.keccak256 value
         
     | 
| 
      
 145 
     | 
    
         
            +
                  elsif !types[type.to_sym].nil?
         
     | 
| 
      
 146 
     | 
    
         
            +
                    nested = encode_data type, value, types
         
     | 
| 
      
 147 
     | 
    
         
            +
                    return Util.keccak256 nested
         
     | 
| 
      
 148 
     | 
    
         
            +
                  else
         
     | 
| 
      
 149 
     | 
    
         
            +
                    # encode basic types via ABI to get 32-byte representation
         
     | 
| 
      
 150 
     | 
    
         
            +
                    return Abi.encode([type], [value])
         
     | 
| 
      
 151 
     | 
    
         
            +
                  end
         
     | 
| 
      
 152 
     | 
    
         
            +
                end
         
     | 
| 
      
 153 
     | 
    
         
            +
             
     | 
| 
      
 154 
     | 
    
         
            +
                # Prepares array values by encoding each element according to its
         
     | 
| 
      
 155 
     | 
    
         
            +
                # base type. Returns an array compatible with Abi.encode.
         
     | 
| 
      
 156 
     | 
    
         
            +
                def encode_array(type, value, types)
         
     | 
| 
      
 157 
     | 
    
         
            +
                  inner_type = type.slice(0, type.rindex("["))
         
     | 
| 
      
 158 
     | 
    
         
            +
                  return [] if value.nil?
         
     | 
| 
      
 159 
     | 
    
         
            +
                  value.map do |v|
         
     | 
| 
      
 160 
     | 
    
         
            +
                    if inner_type.end_with?("]")
         
     | 
| 
      
 161 
     | 
    
         
            +
                      encode_array inner_type, v, types
         
     | 
| 
      
 162 
     | 
    
         
            +
                    elsif inner_type == "string"
         
     | 
| 
      
 163 
     | 
    
         
            +
                      Util.keccak256 v
         
     | 
| 
      
 164 
     | 
    
         
            +
                    elsif inner_type == "bytes"
         
     | 
| 
      
 165 
     | 
    
         
            +
                      Util.keccak256 Util.hex_to_bin(v)
         
     | 
| 
      
 166 
     | 
    
         
            +
                    elsif !types[inner_type.to_sym].nil?
         
     | 
| 
      
 167 
     | 
    
         
            +
                      Util.keccak256 encode_data(inner_type, v, types)
         
     | 
| 
      
 168 
     | 
    
         
            +
                    else
         
     | 
| 
      
 169 
     | 
    
         
            +
                      v
         
     | 
| 
      
 170 
     | 
    
         
            +
                    end
         
     | 
| 
      
 171 
     | 
    
         
            +
                  end
         
     | 
| 
      
 172 
     | 
    
         
            +
                end
         
     | 
| 
      
 173 
     | 
    
         
            +
             
     | 
| 
       139 
174 
     | 
    
         
             
                # Recursively ABI-encodes and hashes all data and types.
         
     | 
| 
       140 
175 
     | 
    
         
             
                #
         
     | 
| 
       141 
176 
     | 
    
         
             
                # @param primary_type [String] the primary type which we want to hash.
         
     | 
    
        data/lib/eth/key.rb
    CHANGED
    
    | 
         @@ -75,7 +75,7 @@ module Eth 
     | 
|
| 
       75 
75 
     | 
    
         
             
                  signature = compact.bytes
         
     | 
| 
       76 
76 
     | 
    
         
             
                  v = Chain.to_v recovery_id, chain_id
         
     | 
| 
       77 
77 
     | 
    
         
             
                  leading_zero = true
         
     | 
| 
       78 
     | 
    
         
            -
                  [v].pack(" 
     | 
| 
      
 78 
     | 
    
         
            +
                  [v].pack("Q>").unpack("C*").each do |byte|
         
     | 
| 
       79 
79 
     | 
    
         
             
                    leading_zero = false if byte > 0 and leading_zero
         
     | 
| 
       80 
80 
     | 
    
         
             
                    signature.append byte unless leading_zero and byte === 0
         
     | 
| 
       81 
81 
     | 
    
         
             
                  end
         
     | 
    
        data/lib/eth/tx/eip1559.rb
    CHANGED
    
    | 
         @@ -78,7 +78,7 @@ module Eth 
     | 
|
| 
       78 
78 
     | 
    
         
             
                  # @option params [Integer] :max_gas_fee the max transaction fee per gas.
         
     | 
| 
       79 
79 
     | 
    
         
             
                  # @option params [Integer] :gas_limit the gas limit.
         
     | 
| 
       80 
80 
     | 
    
         
             
                  # @option params [Eth::Address] :from the sender address.
         
     | 
| 
       81 
     | 
    
         
            -
                  # @option params [Eth::Address] :to the  
     | 
| 
      
 81 
     | 
    
         
            +
                  # @option params [Eth::Address] :to the receiver address.
         
     | 
| 
       82 
82 
     | 
    
         
             
                  # @option params [Integer] :value the transaction value.
         
     | 
| 
       83 
83 
     | 
    
         
             
                  # @option params [String] :data the transaction data payload.
         
     | 
| 
       84 
84 
     | 
    
         
             
                  # @option params [Array] :access_list an optional access list.
         
     | 
| 
         @@ -148,13 +148,13 @@ module Eth 
     | 
|
| 
       148 
148 
     | 
    
         
             
                    raise ParameterError, "Transaction missing fields!" if tx.size < 9
         
     | 
| 
       149 
149 
     | 
    
         | 
| 
       150 
150 
     | 
    
         
             
                    # populate the 9 payload fields
         
     | 
| 
       151 
     | 
    
         
            -
                    chain_id = Util. 
     | 
| 
       152 
     | 
    
         
            -
                    nonce = Util. 
     | 
| 
       153 
     | 
    
         
            -
                    priority_fee = Util. 
     | 
| 
       154 
     | 
    
         
            -
                    max_gas_fee = Util. 
     | 
| 
       155 
     | 
    
         
            -
                    gas_limit = Util. 
     | 
| 
      
 151 
     | 
    
         
            +
                    chain_id = Util.deserialize_rlp_int tx[0]
         
     | 
| 
      
 152 
     | 
    
         
            +
                    nonce = Util.deserialize_rlp_int tx[1]
         
     | 
| 
      
 153 
     | 
    
         
            +
                    priority_fee = Util.deserialize_rlp_int tx[2]
         
     | 
| 
      
 154 
     | 
    
         
            +
                    max_gas_fee = Util.deserialize_rlp_int tx[3]
         
     | 
| 
      
 155 
     | 
    
         
            +
                    gas_limit = Util.deserialize_rlp_int tx[4]
         
     | 
| 
       156 
156 
     | 
    
         
             
                    to = Util.bin_to_hex tx[5]
         
     | 
| 
       157 
     | 
    
         
            -
                    value = Util. 
     | 
| 
      
 157 
     | 
    
         
            +
                    value = Util.deserialize_rlp_int tx[6]
         
     | 
| 
       158 
158 
     | 
    
         
             
                    data = tx[7]
         
     | 
| 
       159 
159 
     | 
    
         
             
                    access_list = tx[8]
         
     | 
| 
       160 
160 
     | 
    
         | 
| 
         @@ -257,6 +257,31 @@ module Eth 
     | 
|
| 
       257 
257 
     | 
    
         
             
                    return hash
         
     | 
| 
       258 
258 
     | 
    
         
             
                  end
         
     | 
| 
       259 
259 
     | 
    
         | 
| 
      
 260 
     | 
    
         
            +
                  # Signs the transaction with a provided signature blob.
         
     | 
| 
      
 261 
     | 
    
         
            +
                  #
         
     | 
| 
      
 262 
     | 
    
         
            +
                  # @param signature [String] the concatenated `r`, `s`, and `v` values.
         
     | 
| 
      
 263 
     | 
    
         
            +
                  # @return [String] a transaction hash.
         
     | 
| 
      
 264 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if transaction is already signed.
         
     | 
| 
      
 265 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if sender address does not match signer.
         
     | 
| 
      
 266 
     | 
    
         
            +
                  def sign_with(signature)
         
     | 
| 
      
 267 
     | 
    
         
            +
                    if Tx.signed? self
         
     | 
| 
      
 268 
     | 
    
         
            +
                      raise Signature::SignatureError, "Transaction is already signed!"
         
     | 
| 
      
 269 
     | 
    
         
            +
                    end
         
     | 
| 
      
 270 
     | 
    
         
            +
             
     | 
| 
      
 271 
     | 
    
         
            +
                    # ensure the sender address matches the signature
         
     | 
| 
      
 272 
     | 
    
         
            +
                    unless @sender.nil? or sender.empty?
         
     | 
| 
      
 273 
     | 
    
         
            +
                      public_key = Signature.recover(unsigned_hash, signature, @chain_id)
         
     | 
| 
      
 274 
     | 
    
         
            +
                      signer_address = Tx.sanitize_address Util.public_key_to_address(public_key).to_s
         
     | 
| 
      
 275 
     | 
    
         
            +
                      from_address = Tx.sanitize_address @sender
         
     | 
| 
      
 276 
     | 
    
         
            +
                      raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
         
     | 
| 
      
 277 
     | 
    
         
            +
                    end
         
     | 
| 
      
 278 
     | 
    
         
            +
             
     | 
| 
      
 279 
     | 
    
         
            +
                    r, s, v = Signature.dissect signature
         
     | 
| 
      
 280 
     | 
    
         
            +
                    recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
         
     | 
| 
      
 281 
     | 
    
         
            +
                    send :_set_signature, recovery_id, r, s
         
     | 
| 
      
 282 
     | 
    
         
            +
                    return hash
         
     | 
| 
      
 283 
     | 
    
         
            +
                  end
         
     | 
| 
      
 284 
     | 
    
         
            +
             
     | 
| 
       260 
285 
     | 
    
         
             
                  # Encodes a raw transaction object, wraps it in an EIP-2718 envelope
         
     | 
| 
       261 
286 
     | 
    
         
             
                  # with an EIP-1559 type prefix.
         
     | 
| 
       262 
287 
     | 
    
         
             
                  #
         
     | 
    
        data/lib/eth/tx/eip2930.rb
    CHANGED
    
    | 
         @@ -76,7 +76,7 @@ module Eth 
     | 
|
| 
       76 
76 
     | 
    
         
             
                  # @option params [Integer] :gas_price the gas price.
         
     | 
| 
       77 
77 
     | 
    
         
             
                  # @option params [Integer] :gas_limit the gas limit.
         
     | 
| 
       78 
78 
     | 
    
         
             
                  # @option params [Eth::Address] :from the sender address.
         
     | 
| 
       79 
     | 
    
         
            -
                  # @option params [Eth::Address] :to the  
     | 
| 
      
 79 
     | 
    
         
            +
                  # @option params [Eth::Address] :to the receiver address.
         
     | 
| 
       80 
80 
     | 
    
         
             
                  # @option params [Integer] :value the transaction value.
         
     | 
| 
       81 
81 
     | 
    
         
             
                  # @option params [String] :data the transaction data payload.
         
     | 
| 
       82 
82 
     | 
    
         
             
                  # @option params [Array] :access_list an optional access list.
         
     | 
| 
         @@ -145,12 +145,12 @@ module Eth 
     | 
|
| 
       145 
145 
     | 
    
         
             
                    raise ParameterError, "Transaction missing fields!" if tx.size < 8
         
     | 
| 
       146 
146 
     | 
    
         | 
| 
       147 
147 
     | 
    
         
             
                    # populate the 8 payload fields
         
     | 
| 
       148 
     | 
    
         
            -
                    chain_id = Util. 
     | 
| 
       149 
     | 
    
         
            -
                    nonce = Util. 
     | 
| 
       150 
     | 
    
         
            -
                    gas_price = Util. 
     | 
| 
       151 
     | 
    
         
            -
                    gas_limit = Util. 
     | 
| 
      
 148 
     | 
    
         
            +
                    chain_id = Util.deserialize_rlp_int tx[0]
         
     | 
| 
      
 149 
     | 
    
         
            +
                    nonce = Util.deserialize_rlp_int tx[1]
         
     | 
| 
      
 150 
     | 
    
         
            +
                    gas_price = Util.deserialize_rlp_int tx[2]
         
     | 
| 
      
 151 
     | 
    
         
            +
                    gas_limit = Util.deserialize_rlp_int tx[3]
         
     | 
| 
       152 
152 
     | 
    
         
             
                    to = Util.bin_to_hex tx[4]
         
     | 
| 
       153 
     | 
    
         
            -
                    value = Util. 
     | 
| 
      
 153 
     | 
    
         
            +
                    value = Util.deserialize_rlp_int tx[5]
         
     | 
| 
       154 
154 
     | 
    
         
             
                    data = tx[6]
         
     | 
| 
       155 
155 
     | 
    
         
             
                    access_list = tx[7]
         
     | 
| 
       156 
156 
     | 
    
         | 
| 
         @@ -251,6 +251,31 @@ module Eth 
     | 
|
| 
       251 
251 
     | 
    
         
             
                    return hash
         
     | 
| 
       252 
252 
     | 
    
         
             
                  end
         
     | 
| 
       253 
253 
     | 
    
         | 
| 
      
 254 
     | 
    
         
            +
                  # Signs the transaction with a provided signature blob.
         
     | 
| 
      
 255 
     | 
    
         
            +
                  #
         
     | 
| 
      
 256 
     | 
    
         
            +
                  # @param signature [String] the concatenated `r`, `s`, and `v` values.
         
     | 
| 
      
 257 
     | 
    
         
            +
                  # @return [String] a transaction hash.
         
     | 
| 
      
 258 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if transaction is already signed.
         
     | 
| 
      
 259 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if sender address does not match signer.
         
     | 
| 
      
 260 
     | 
    
         
            +
                  def sign_with(signature)
         
     | 
| 
      
 261 
     | 
    
         
            +
                    if Tx.signed? self
         
     | 
| 
      
 262 
     | 
    
         
            +
                      raise Signature::SignatureError, "Transaction is already signed!"
         
     | 
| 
      
 263 
     | 
    
         
            +
                    end
         
     | 
| 
      
 264 
     | 
    
         
            +
             
     | 
| 
      
 265 
     | 
    
         
            +
                    # ensure the sender address matches the signature
         
     | 
| 
      
 266 
     | 
    
         
            +
                    unless @sender.nil? or sender.empty?
         
     | 
| 
      
 267 
     | 
    
         
            +
                      public_key = Signature.recover(unsigned_hash, signature, @chain_id)
         
     | 
| 
      
 268 
     | 
    
         
            +
                      signer_address = Tx.sanitize_address Util.public_key_to_address(public_key).to_s
         
     | 
| 
      
 269 
     | 
    
         
            +
                      from_address = Tx.sanitize_address @sender
         
     | 
| 
      
 270 
     | 
    
         
            +
                      raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
         
     | 
| 
      
 271 
     | 
    
         
            +
                    end
         
     | 
| 
      
 272 
     | 
    
         
            +
             
     | 
| 
      
 273 
     | 
    
         
            +
                    r, s, v = Signature.dissect signature
         
     | 
| 
      
 274 
     | 
    
         
            +
                    recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
         
     | 
| 
      
 275 
     | 
    
         
            +
                    send :_set_signature, recovery_id, r, s
         
     | 
| 
      
 276 
     | 
    
         
            +
                    return hash
         
     | 
| 
      
 277 
     | 
    
         
            +
                  end
         
     | 
| 
      
 278 
     | 
    
         
            +
             
     | 
| 
       254 
279 
     | 
    
         
             
                  # Encodes a raw transaction object, wraps it in an EIP-2718 envelope
         
     | 
| 
       255 
280 
     | 
    
         
             
                  # with an EIP-2930 type prefix.
         
     | 
| 
       256 
281 
     | 
    
         
             
                  #
         
     | 
| 
         @@ -0,0 +1,401 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # Copyright (c) 2016-2025 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 
     | 
    
         
            +
            # Provides the {Eth} module.
         
     | 
| 
      
 16 
     | 
    
         
            +
            module Eth
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
              # Provides the `Tx` module supporting various transaction types.
         
     | 
| 
      
 19 
     | 
    
         
            +
              module Tx
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                # Provides support for EIP-4844 transactions utilizing EIP-2718
         
     | 
| 
      
 22 
     | 
    
         
            +
                # types and envelopes.
         
     | 
| 
      
 23 
     | 
    
         
            +
                # Ref: https://eips.ethereum.org/EIPS/eip-4844
         
     | 
| 
      
 24 
     | 
    
         
            +
                class Eip4844
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                  # The blob gas consumed by a single blob.
         
     | 
| 
      
 27 
     | 
    
         
            +
                  GAS_PER_BLOB = (2 ** 17).freeze
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                  # The target blob gas per block as per EIP-7691.
         
     | 
| 
      
 30 
     | 
    
         
            +
                  TARGET_BLOB_GAS_PER_BLOCK = 786_432.freeze
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                  # The maximum blob gas allowed in a block as per EIP-7691.
         
     | 
| 
      
 33 
     | 
    
         
            +
                  MAX_BLOB_GAS_PER_BLOCK = 1_179_648.freeze
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                  # The maximum number of blobs permitted in a single block.
         
     | 
| 
      
 36 
     | 
    
         
            +
                  MAX_BLOBS_PER_BLOCK = (MAX_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB).freeze
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                  # The EIP-155 Chain ID.
         
     | 
| 
      
 39 
     | 
    
         
            +
                  # Ref: https://eips.ethereum.org/EIPS/eip-155
         
     | 
| 
      
 40 
     | 
    
         
            +
                  attr_reader :chain_id
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                  # The transaction nonce provided by the signer.
         
     | 
| 
      
 43 
     | 
    
         
            +
                  attr_reader :signer_nonce
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                  # The transaction max priority fee per gas in Wei.
         
     | 
| 
      
 46 
     | 
    
         
            +
                  attr_reader :max_priority_fee_per_gas
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                  # The transaction max fee per gas in Wei.
         
     | 
| 
      
 49 
     | 
    
         
            +
                  attr_reader :max_fee_per_gas
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                  # The gas limit for the transaction.
         
     | 
| 
      
 52 
     | 
    
         
            +
                  attr_reader :gas_limit
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
                  # The recipient address.
         
     | 
| 
      
 55 
     | 
    
         
            +
                  attr_reader :destination
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                  # The transaction amount in Wei.
         
     | 
| 
      
 58 
     | 
    
         
            +
                  attr_reader :amount
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
                  # The transaction data payload.
         
     | 
| 
      
 61 
     | 
    
         
            +
                  attr_reader :payload
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                  # An optional EIP-2930 access list.
         
     | 
| 
      
 64 
     | 
    
         
            +
                  # Ref: https://eips.ethereum.org/EIPS/eip-2930
         
     | 
| 
      
 65 
     | 
    
         
            +
                  attr_reader :access_list
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
                  # The transaction max fee per blob gas in Wei.
         
     | 
| 
      
 68 
     | 
    
         
            +
                  attr_reader :max_fee_per_blob_gas
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
                  # The list of KZG commitment versioned hashes.
         
     | 
| 
      
 71 
     | 
    
         
            +
                  attr_reader :blob_versioned_hashes
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
                  # The signature's y-parity byte (not v).
         
     | 
| 
      
 74 
     | 
    
         
            +
                  attr_reader :signature_y_parity
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                  # The signature `r` value.
         
     | 
| 
      
 77 
     | 
    
         
            +
                  attr_reader :signature_r
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
                  # The signature `s` value.
         
     | 
| 
      
 80 
     | 
    
         
            +
                  attr_reader :signature_s
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
                  # The sender address.
         
     | 
| 
      
 83 
     | 
    
         
            +
                  attr_reader :sender
         
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
                  # The transaction type.
         
     | 
| 
      
 86 
     | 
    
         
            +
                  attr_reader :type
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
      
 88 
     | 
    
         
            +
                  # Create a type-3 (EIP-4844) transaction payload object that
         
     | 
| 
      
 89 
     | 
    
         
            +
                  # can be prepared for envelope, signature and broadcast.
         
     | 
| 
      
 90 
     | 
    
         
            +
                  # Ref: https://eips.ethereum.org/EIPS/eip-4844
         
     | 
| 
      
 91 
     | 
    
         
            +
                  #
         
     | 
| 
      
 92 
     | 
    
         
            +
                  # @param params [Hash] all necessary transaction fields.
         
     | 
| 
      
 93 
     | 
    
         
            +
                  # @option params [Integer] :chain_id the chain ID.
         
     | 
| 
      
 94 
     | 
    
         
            +
                  # @option params [Integer] :nonce the signer nonce.
         
     | 
| 
      
 95 
     | 
    
         
            +
                  # @option params [Integer] :priority_fee the max priority fee per gas.
         
     | 
| 
      
 96 
     | 
    
         
            +
                  # @option params [Integer] :max_gas_fee the max transaction fee per gas.
         
     | 
| 
      
 97 
     | 
    
         
            +
                  # @option params [Integer] :gas_limit the gas limit.
         
     | 
| 
      
 98 
     | 
    
         
            +
                  # @option params [Eth::Address] :from the sender address.
         
     | 
| 
      
 99 
     | 
    
         
            +
                  # @option params [Eth::Address] :to the receiver address.
         
     | 
| 
      
 100 
     | 
    
         
            +
                  # @option params [Integer] :value the transaction value.
         
     | 
| 
      
 101 
     | 
    
         
            +
                  # @option params [String] :data the transaction data payload.
         
     | 
| 
      
 102 
     | 
    
         
            +
                  # @option params [Array] :access_list an optional access list.
         
     | 
| 
      
 103 
     | 
    
         
            +
                  # @option params [Integer] :max_fee_per_blob_gas the max blob fee per gas.
         
     | 
| 
      
 104 
     | 
    
         
            +
                  # @option params [Array] :blob_versioned_hashes the blob versioned hashes (max 9).
         
     | 
| 
      
 105 
     | 
    
         
            +
                  # @raise [ParameterError] if gas limit is too low.
         
     | 
| 
      
 106 
     | 
    
         
            +
                  def initialize(params)
         
     | 
| 
      
 107 
     | 
    
         
            +
                    fields = { recovery_id: nil, r: 0, s: 0 }.merge params
         
     | 
| 
      
 108 
     | 
    
         
            +
             
     | 
| 
      
 109 
     | 
    
         
            +
                    # populate optional fields with serializable empty values
         
     | 
| 
      
 110 
     | 
    
         
            +
                    fields[:chain_id] = Tx.sanitize_chain fields[:chain_id]
         
     | 
| 
      
 111 
     | 
    
         
            +
                    fields[:from] = Tx.sanitize_address fields[:from]
         
     | 
| 
      
 112 
     | 
    
         
            +
                    fields[:to] = Tx.sanitize_address fields[:to]
         
     | 
| 
      
 113 
     | 
    
         
            +
                    fields[:value] = Tx.sanitize_amount fields[:value]
         
     | 
| 
      
 114 
     | 
    
         
            +
                    fields[:data] = Tx.sanitize_data fields[:data]
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
                    # ensure sane values for all mandatory fields
         
     | 
| 
      
 117 
     | 
    
         
            +
                    fields = Tx.validate_params fields
         
     | 
| 
      
 118 
     | 
    
         
            +
                    fields = Tx.validate_eip1559_params fields
         
     | 
| 
      
 119 
     | 
    
         
            +
                    fields = Tx.validate_eip4844_params fields
         
     | 
| 
      
 120 
     | 
    
         
            +
                    fields[:access_list] = Tx.sanitize_list fields[:access_list]
         
     | 
| 
      
 121 
     | 
    
         
            +
                    fields[:blob_versioned_hashes] = Tx.sanitize_hashes fields[:blob_versioned_hashes]
         
     | 
| 
      
 122 
     | 
    
         
            +
             
     | 
| 
      
 123 
     | 
    
         
            +
                    # ensure gas limit is not too low
         
     | 
| 
      
 124 
     | 
    
         
            +
                    minimum_cost = Tx.estimate_intrinsic_gas fields[:data], fields[:access_list]
         
     | 
| 
      
 125 
     | 
    
         
            +
                    raise ParameterError, "Transaction gas limit is too low, try #{minimum_cost}!" if fields[:gas_limit].to_i < minimum_cost
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                    # populate class attributes
         
     | 
| 
      
 128 
     | 
    
         
            +
                    @signer_nonce = fields[:nonce].to_i
         
     | 
| 
      
 129 
     | 
    
         
            +
                    @max_priority_fee_per_gas = fields[:priority_fee].to_i
         
     | 
| 
      
 130 
     | 
    
         
            +
                    @max_fee_per_gas = fields[:max_gas_fee].to_i
         
     | 
| 
      
 131 
     | 
    
         
            +
                    @gas_limit = fields[:gas_limit].to_i
         
     | 
| 
      
 132 
     | 
    
         
            +
                    @sender = fields[:from].to_s
         
     | 
| 
      
 133 
     | 
    
         
            +
                    @destination = fields[:to].to_s
         
     | 
| 
      
 134 
     | 
    
         
            +
                    @amount = fields[:value].to_i
         
     | 
| 
      
 135 
     | 
    
         
            +
                    @payload = fields[:data]
         
     | 
| 
      
 136 
     | 
    
         
            +
                    @access_list = fields[:access_list]
         
     | 
| 
      
 137 
     | 
    
         
            +
                    @max_fee_per_blob_gas = fields[:max_fee_per_blob_gas].to_i
         
     | 
| 
      
 138 
     | 
    
         
            +
                    @blob_versioned_hashes = fields[:blob_versioned_hashes]
         
     | 
| 
      
 139 
     | 
    
         
            +
             
     | 
| 
      
 140 
     | 
    
         
            +
                    # the signature v is set to the chain id for unsigned transactions
         
     | 
| 
      
 141 
     | 
    
         
            +
                    @signature_y_parity = fields[:recovery_id]
         
     | 
| 
      
 142 
     | 
    
         
            +
                    @chain_id = fields[:chain_id]
         
     | 
| 
      
 143 
     | 
    
         
            +
             
     | 
| 
      
 144 
     | 
    
         
            +
                    # the signature fields are empty for unsigned transactions.
         
     | 
| 
      
 145 
     | 
    
         
            +
                    @signature_r = fields[:r]
         
     | 
| 
      
 146 
     | 
    
         
            +
                    @signature_s = fields[:s]
         
     | 
| 
      
 147 
     | 
    
         
            +
             
     | 
| 
      
 148 
     | 
    
         
            +
                    # last but not least, set the type.
         
     | 
| 
      
 149 
     | 
    
         
            +
                    @type = TYPE_4844
         
     | 
| 
      
 150 
     | 
    
         
            +
                  end
         
     | 
| 
      
 151 
     | 
    
         
            +
             
     | 
| 
      
 152 
     | 
    
         
            +
                  # Overloads the constructor for decoding raw transactions and creating unsigned copies.
         
     | 
| 
      
 153 
     | 
    
         
            +
                  konstructor :decode, :unsigned_copy
         
     | 
| 
      
 154 
     | 
    
         
            +
             
     | 
| 
      
 155 
     | 
    
         
            +
                  # Decodes a raw transaction hex into an {Eth::Tx::Eip4844}
         
     | 
| 
      
 156 
     | 
    
         
            +
                  # transaction object.
         
     | 
| 
      
 157 
     | 
    
         
            +
                  #
         
     | 
| 
      
 158 
     | 
    
         
            +
                  # @param hex [String] the raw transaction hex-string.
         
     | 
| 
      
 159 
     | 
    
         
            +
                  # @return [Eth::Tx::Eip4844] transaction payload.
         
     | 
| 
      
 160 
     | 
    
         
            +
                  # @raise [TransactionTypeError] if transaction type is invalid.
         
     | 
| 
      
 161 
     | 
    
         
            +
                  # @raise [ParameterError] if transaction is missing fields.
         
     | 
| 
      
 162 
     | 
    
         
            +
                  # @raise [DecoderError] if transaction decoding fails.
         
     | 
| 
      
 163 
     | 
    
         
            +
                  def decode(hex)
         
     | 
| 
      
 164 
     | 
    
         
            +
                    hex = Util.remove_hex_prefix hex
         
     | 
| 
      
 165 
     | 
    
         
            +
                    type = hex[0, 2]
         
     | 
| 
      
 166 
     | 
    
         
            +
                    raise TransactionTypeError, "Invalid transaction type #{type}!" if type.to_i(16) != TYPE_4844
         
     | 
| 
      
 167 
     | 
    
         
            +
             
     | 
| 
      
 168 
     | 
    
         
            +
                    bin = Util.hex_to_bin hex[2..]
         
     | 
| 
      
 169 
     | 
    
         
            +
                    tx = Rlp.decode bin
         
     | 
| 
      
 170 
     | 
    
         
            +
             
     | 
| 
      
 171 
     | 
    
         
            +
                    # decoded transactions always have 11 + 3 fields, even if they are empty or zero
         
     | 
| 
      
 172 
     | 
    
         
            +
                    raise ParameterError, "Transaction missing fields!" if tx.size < 11
         
     | 
| 
      
 173 
     | 
    
         
            +
             
     | 
| 
      
 174 
     | 
    
         
            +
                    # populate the 11 payload fields
         
     | 
| 
      
 175 
     | 
    
         
            +
                    chain_id = Util.deserialize_rlp_int tx[0]
         
     | 
| 
      
 176 
     | 
    
         
            +
                    nonce = Util.deserialize_rlp_int tx[1]
         
     | 
| 
      
 177 
     | 
    
         
            +
                    priority_fee = Util.deserialize_rlp_int tx[2]
         
     | 
| 
      
 178 
     | 
    
         
            +
                    max_gas_fee = Util.deserialize_rlp_int tx[3]
         
     | 
| 
      
 179 
     | 
    
         
            +
                    gas_limit = Util.deserialize_rlp_int tx[4]
         
     | 
| 
      
 180 
     | 
    
         
            +
                    to = Util.bin_to_hex tx[5]
         
     | 
| 
      
 181 
     | 
    
         
            +
                    value = Util.deserialize_rlp_int tx[6]
         
     | 
| 
      
 182 
     | 
    
         
            +
                    data = tx[7]
         
     | 
| 
      
 183 
     | 
    
         
            +
                    access_list = tx[8]
         
     | 
| 
      
 184 
     | 
    
         
            +
                    max_fee_per_blob_gas = Util.deserialize_rlp_int tx[9]
         
     | 
| 
      
 185 
     | 
    
         
            +
                    blob_versioned_hashes = tx[10]
         
     | 
| 
      
 186 
     | 
    
         
            +
             
     | 
| 
      
 187 
     | 
    
         
            +
                    # populate class attributes
         
     | 
| 
      
 188 
     | 
    
         
            +
                    @chain_id = chain_id.to_i
         
     | 
| 
      
 189 
     | 
    
         
            +
                    @signer_nonce = nonce.to_i
         
     | 
| 
      
 190 
     | 
    
         
            +
                    @max_priority_fee_per_gas = priority_fee.to_i
         
     | 
| 
      
 191 
     | 
    
         
            +
                    @max_fee_per_gas = max_gas_fee.to_i
         
     | 
| 
      
 192 
     | 
    
         
            +
                    @gas_limit = gas_limit.to_i
         
     | 
| 
      
 193 
     | 
    
         
            +
                    @destination = to.to_s
         
     | 
| 
      
 194 
     | 
    
         
            +
                    @amount = value.to_i
         
     | 
| 
      
 195 
     | 
    
         
            +
                    @payload = data
         
     | 
| 
      
 196 
     | 
    
         
            +
                    @access_list = access_list
         
     | 
| 
      
 197 
     | 
    
         
            +
                    @max_fee_per_blob_gas = max_fee_per_blob_gas.to_i
         
     | 
| 
      
 198 
     | 
    
         
            +
                    @blob_versioned_hashes = blob_versioned_hashes
         
     | 
| 
      
 199 
     | 
    
         
            +
             
     | 
| 
      
 200 
     | 
    
         
            +
                    # populate the 3 signature fields
         
     | 
| 
      
 201 
     | 
    
         
            +
                    if tx.size == 11
         
     | 
| 
      
 202 
     | 
    
         
            +
                      _set_signature(nil, 0, 0)
         
     | 
| 
      
 203 
     | 
    
         
            +
                    elsif tx.size == 14
         
     | 
| 
      
 204 
     | 
    
         
            +
                      recovery_id = Util.bin_to_hex(tx[11]).to_i(16)
         
     | 
| 
      
 205 
     | 
    
         
            +
                      r = Util.bin_to_hex tx[12]
         
     | 
| 
      
 206 
     | 
    
         
            +
                      s = Util.bin_to_hex tx[13]
         
     | 
| 
      
 207 
     | 
    
         
            +
             
     | 
| 
      
 208 
     | 
    
         
            +
                      # allows us to force-setting a signature if the transaction is signed already
         
     | 
| 
      
 209 
     | 
    
         
            +
                      _set_signature(recovery_id, r, s)
         
     | 
| 
      
 210 
     | 
    
         
            +
                    else
         
     | 
| 
      
 211 
     | 
    
         
            +
                      raise DecoderError, "Cannot decode EIP-4844 payload!"
         
     | 
| 
      
 212 
     | 
    
         
            +
                    end
         
     | 
| 
      
 213 
     | 
    
         
            +
             
     | 
| 
      
 214 
     | 
    
         
            +
                    # last but not least, set the type.
         
     | 
| 
      
 215 
     | 
    
         
            +
                    @type = TYPE_4844
         
     | 
| 
      
 216 
     | 
    
         
            +
             
     | 
| 
      
 217 
     | 
    
         
            +
                    unless recovery_id.nil?
         
     | 
| 
      
 218 
     | 
    
         
            +
                      # recover sender address
         
     | 
| 
      
 219 
     | 
    
         
            +
                      v = Chain.to_v recovery_id, chain_id
         
     | 
| 
      
 220 
     | 
    
         
            +
                      public_key = Signature.recover(unsigned_hash, "#{r.rjust(64, "0")}#{s.rjust(64, "0")}#{v.to_s(16)}", chain_id)
         
     | 
| 
      
 221 
     | 
    
         
            +
                      address = Util.public_key_to_address(public_key).to_s
         
     | 
| 
      
 222 
     | 
    
         
            +
                      @sender = Tx.sanitize_address address
         
     | 
| 
      
 223 
     | 
    
         
            +
                    else
         
     | 
| 
      
 224 
     | 
    
         
            +
                      # keep the 'from' field blank
         
     | 
| 
      
 225 
     | 
    
         
            +
                      @sender = Tx.sanitize_address nil
         
     | 
| 
      
 226 
     | 
    
         
            +
                    end
         
     | 
| 
      
 227 
     | 
    
         
            +
                  end
         
     | 
| 
      
 228 
     | 
    
         
            +
             
     | 
| 
      
 229 
     | 
    
         
            +
                  # Creates an unsigned copy of a transaction payload.
         
     | 
| 
      
 230 
     | 
    
         
            +
                  #
         
     | 
| 
      
 231 
     | 
    
         
            +
                  # @param tx [Eth::Tx::Eip4844] an EIP-4844 transaction payload.
         
     | 
| 
      
 232 
     | 
    
         
            +
                  # @return [Eth::Tx::Eip4844] an unsigned EIP-4844 transaction payload.
         
     | 
| 
      
 233 
     | 
    
         
            +
                  # @raise [TransactionTypeError] if transaction type does not match.
         
     | 
| 
      
 234 
     | 
    
         
            +
                  def unsigned_copy(tx)
         
     | 
| 
      
 235 
     | 
    
         
            +
             
     | 
| 
      
 236 
     | 
    
         
            +
                    # not checking transaction validity unless it's of a different class
         
     | 
| 
      
 237 
     | 
    
         
            +
                    raise TransactionTypeError, "Cannot copy transaction of different payload type!" unless tx.instance_of? Tx::Eip4844
         
     | 
| 
      
 238 
     | 
    
         
            +
             
     | 
| 
      
 239 
     | 
    
         
            +
                    # populate class attributes
         
     | 
| 
      
 240 
     | 
    
         
            +
                    @signer_nonce = tx.signer_nonce
         
     | 
| 
      
 241 
     | 
    
         
            +
                    @max_priority_fee_per_gas = tx.max_priority_fee_per_gas
         
     | 
| 
      
 242 
     | 
    
         
            +
                    @max_fee_per_gas = tx.max_fee_per_gas
         
     | 
| 
      
 243 
     | 
    
         
            +
                    @gas_limit = tx.gas_limit
         
     | 
| 
      
 244 
     | 
    
         
            +
                    @destination = tx.destination
         
     | 
| 
      
 245 
     | 
    
         
            +
                    @amount = tx.amount
         
     | 
| 
      
 246 
     | 
    
         
            +
                    @payload = tx.payload
         
     | 
| 
      
 247 
     | 
    
         
            +
                    @access_list = tx.access_list
         
     | 
| 
      
 248 
     | 
    
         
            +
                    @max_fee_per_blob_gas = tx.max_fee_per_blob_gas
         
     | 
| 
      
 249 
     | 
    
         
            +
                    @blob_versioned_hashes = tx.blob_versioned_hashes
         
     | 
| 
      
 250 
     | 
    
         
            +
                    @chain_id = tx.chain_id
         
     | 
| 
      
 251 
     | 
    
         
            +
             
     | 
| 
      
 252 
     | 
    
         
            +
                    # force-set signature to unsigned
         
     | 
| 
      
 253 
     | 
    
         
            +
                    _set_signature(nil, 0, 0)
         
     | 
| 
      
 254 
     | 
    
         
            +
             
     | 
| 
      
 255 
     | 
    
         
            +
                    # keep the 'from' field blank
         
     | 
| 
      
 256 
     | 
    
         
            +
                    @sender = Tx.sanitize_address nil
         
     | 
| 
      
 257 
     | 
    
         
            +
             
     | 
| 
      
 258 
     | 
    
         
            +
                    # last but not least, set the type.
         
     | 
| 
      
 259 
     | 
    
         
            +
                    @type = TYPE_4844
         
     | 
| 
      
 260 
     | 
    
         
            +
                  end
         
     | 
| 
      
 261 
     | 
    
         
            +
             
     | 
| 
      
 262 
     | 
    
         
            +
                  # Sign the transaction with a given key.
         
     | 
| 
      
 263 
     | 
    
         
            +
                  #
         
     | 
| 
      
 264 
     | 
    
         
            +
                  # @param key [Eth::Key] the key-pair to use for signing.
         
     | 
| 
      
 265 
     | 
    
         
            +
                  # @return [String] a transaction hash.
         
     | 
| 
      
 266 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if transaction is already signed.
         
     | 
| 
      
 267 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if sender address does not match signing key.
         
     | 
| 
      
 268 
     | 
    
         
            +
                  def sign(key)
         
     | 
| 
      
 269 
     | 
    
         
            +
                    if Tx.signed? self
         
     | 
| 
      
 270 
     | 
    
         
            +
                      raise Signature::SignatureError, "Transaction is already signed!"
         
     | 
| 
      
 271 
     | 
    
         
            +
                    end
         
     | 
| 
      
 272 
     | 
    
         
            +
             
     | 
| 
      
 273 
     | 
    
         
            +
                    # ensure the sender address matches the given key
         
     | 
| 
      
 274 
     | 
    
         
            +
                    unless @sender.nil? or sender.empty?
         
     | 
| 
      
 275 
     | 
    
         
            +
                      signer_address = Tx.sanitize_address key.address.to_s
         
     | 
| 
      
 276 
     | 
    
         
            +
                      from_address = Tx.sanitize_address @sender
         
     | 
| 
      
 277 
     | 
    
         
            +
                      raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
         
     | 
| 
      
 278 
     | 
    
         
            +
                    end
         
     | 
| 
      
 279 
     | 
    
         
            +
             
     | 
| 
      
 280 
     | 
    
         
            +
                    # sign a keccak hash of the unsigned, encoded transaction
         
     | 
| 
      
 281 
     | 
    
         
            +
                    signature = key.sign(unsigned_hash, @chain_id)
         
     | 
| 
      
 282 
     | 
    
         
            +
                    r, s, v = Signature.dissect signature
         
     | 
| 
      
 283 
     | 
    
         
            +
                    recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
         
     | 
| 
      
 284 
     | 
    
         
            +
                    @signature_y_parity = recovery_id
         
     | 
| 
      
 285 
     | 
    
         
            +
                    @signature_r = r
         
     | 
| 
      
 286 
     | 
    
         
            +
                    @signature_s = s
         
     | 
| 
      
 287 
     | 
    
         
            +
                    return hash
         
     | 
| 
      
 288 
     | 
    
         
            +
                  end
         
     | 
| 
      
 289 
     | 
    
         
            +
             
     | 
| 
      
 290 
     | 
    
         
            +
                  # Signs the transaction with a provided signature blob.
         
     | 
| 
      
 291 
     | 
    
         
            +
                  #
         
     | 
| 
      
 292 
     | 
    
         
            +
                  # @param signature [String] the concatenated `r`, `s`, and `v` values.
         
     | 
| 
      
 293 
     | 
    
         
            +
                  # @return [String] a transaction hash.
         
     | 
| 
      
 294 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if transaction is already signed.
         
     | 
| 
      
 295 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if sender address does not match signer.
         
     | 
| 
      
 296 
     | 
    
         
            +
                  def sign_with(signature)
         
     | 
| 
      
 297 
     | 
    
         
            +
                    if Tx.signed? self
         
     | 
| 
      
 298 
     | 
    
         
            +
                      raise Signature::SignatureError, "Transaction is already signed!"
         
     | 
| 
      
 299 
     | 
    
         
            +
                    end
         
     | 
| 
      
 300 
     | 
    
         
            +
             
     | 
| 
      
 301 
     | 
    
         
            +
                    # ensure the sender address matches the signature
         
     | 
| 
      
 302 
     | 
    
         
            +
                    unless @sender.nil? or sender.empty?
         
     | 
| 
      
 303 
     | 
    
         
            +
                      public_key = Signature.recover(unsigned_hash, signature, @chain_id)
         
     | 
| 
      
 304 
     | 
    
         
            +
                      signer_address = Tx.sanitize_address Util.public_key_to_address(public_key).to_s
         
     | 
| 
      
 305 
     | 
    
         
            +
                      from_address = Tx.sanitize_address @sender
         
     | 
| 
      
 306 
     | 
    
         
            +
                      raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
         
     | 
| 
      
 307 
     | 
    
         
            +
                    end
         
     | 
| 
      
 308 
     | 
    
         
            +
             
     | 
| 
      
 309 
     | 
    
         
            +
                    r, s, v = Signature.dissect signature
         
     | 
| 
      
 310 
     | 
    
         
            +
                    recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
         
     | 
| 
      
 311 
     | 
    
         
            +
                    send :_set_signature, recovery_id, r, s
         
     | 
| 
      
 312 
     | 
    
         
            +
                    return hash
         
     | 
| 
      
 313 
     | 
    
         
            +
                  end
         
     | 
| 
      
 314 
     | 
    
         
            +
             
     | 
| 
      
 315 
     | 
    
         
            +
                  # Encodes a raw transaction object, wraps it in an EIP-2718 envelope
         
     | 
| 
      
 316 
     | 
    
         
            +
                  # with an EIP-4844 type prefix.
         
     | 
| 
      
 317 
     | 
    
         
            +
                  #
         
     | 
| 
      
 318 
     | 
    
         
            +
                  # @return [String] a raw, RLP-encoded EIP-4844 type transaction object.
         
     | 
| 
      
 319 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if the transaction is not yet signed.
         
     | 
| 
      
 320 
     | 
    
         
            +
                  def encoded
         
     | 
| 
      
 321 
     | 
    
         
            +
                    unless Tx.signed? self
         
     | 
| 
      
 322 
     | 
    
         
            +
                      raise Signature::SignatureError, "Transaction is not signed!"
         
     | 
| 
      
 323 
     | 
    
         
            +
                    end
         
     | 
| 
      
 324 
     | 
    
         
            +
                    tx_data = []
         
     | 
| 
      
 325 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @chain_id
         
     | 
| 
      
 326 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @signer_nonce
         
     | 
| 
      
 327 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
         
     | 
| 
      
 328 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
         
     | 
| 
      
 329 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @gas_limit
         
     | 
| 
      
 330 
     | 
    
         
            +
                    tx_data.push Util.hex_to_bin @destination
         
     | 
| 
      
 331 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @amount
         
     | 
| 
      
 332 
     | 
    
         
            +
                    tx_data.push Rlp::Sedes.binary.serialize @payload
         
     | 
| 
      
 333 
     | 
    
         
            +
                    tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
         
     | 
| 
      
 334 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @max_fee_per_blob_gas
         
     | 
| 
      
 335 
     | 
    
         
            +
                    tx_data.push Rlp::Sedes.infer(@blob_versioned_hashes).serialize @blob_versioned_hashes
         
     | 
| 
      
 336 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @signature_y_parity
         
     | 
| 
      
 337 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @signature_r
         
     | 
| 
      
 338 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @signature_s
         
     | 
| 
      
 339 
     | 
    
         
            +
                    tx_encoded = Rlp.encode tx_data
         
     | 
| 
      
 340 
     | 
    
         
            +
             
     | 
| 
      
 341 
     | 
    
         
            +
                    # create an EIP-2718 envelope with EIP-4844 type payload
         
     | 
| 
      
 342 
     | 
    
         
            +
                    tx_type = Util.serialize_int_to_big_endian @type
         
     | 
| 
      
 343 
     | 
    
         
            +
                    return "#{tx_type}#{tx_encoded}"
         
     | 
| 
      
 344 
     | 
    
         
            +
                  end
         
     | 
| 
      
 345 
     | 
    
         
            +
             
     | 
| 
      
 346 
     | 
    
         
            +
                  # Gets the encoded, enveloped, raw transaction hex.
         
     | 
| 
      
 347 
     | 
    
         
            +
                  #
         
     | 
| 
      
 348 
     | 
    
         
            +
                  # @return [String] the raw transaction hex.
         
     | 
| 
      
 349 
     | 
    
         
            +
                  def hex
         
     | 
| 
      
 350 
     | 
    
         
            +
                    Util.bin_to_hex encoded
         
     | 
| 
      
 351 
     | 
    
         
            +
                  end
         
     | 
| 
      
 352 
     | 
    
         
            +
             
     | 
| 
      
 353 
     | 
    
         
            +
                  # Gets the transaction hash.
         
     | 
| 
      
 354 
     | 
    
         
            +
                  #
         
     | 
| 
      
 355 
     | 
    
         
            +
                  # @return [String] the transaction hash.
         
     | 
| 
      
 356 
     | 
    
         
            +
                  def hash
         
     | 
| 
      
 357 
     | 
    
         
            +
                    Util.bin_to_hex Util.keccak256 encoded
         
     | 
| 
      
 358 
     | 
    
         
            +
                  end
         
     | 
| 
      
 359 
     | 
    
         
            +
             
     | 
| 
      
 360 
     | 
    
         
            +
                  # Encodes the unsigned transaction payload in an EIP-4844 envelope,
         
     | 
| 
      
 361 
     | 
    
         
            +
                  # required for signing.
         
     | 
| 
      
 362 
     | 
    
         
            +
                  #
         
     | 
| 
      
 363 
     | 
    
         
            +
                  # @return [String] an RLP-encoded, unsigned, enveloped EIP-4844 transaction.
         
     | 
| 
      
 364 
     | 
    
         
            +
                  def unsigned_encoded
         
     | 
| 
      
 365 
     | 
    
         
            +
                    tx_data = []
         
     | 
| 
      
 366 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @chain_id
         
     | 
| 
      
 367 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @signer_nonce
         
     | 
| 
      
 368 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
         
     | 
| 
      
 369 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
         
     | 
| 
      
 370 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @gas_limit
         
     | 
| 
      
 371 
     | 
    
         
            +
                    tx_data.push Util.hex_to_bin @destination
         
     | 
| 
      
 372 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @amount
         
     | 
| 
      
 373 
     | 
    
         
            +
                    tx_data.push Rlp::Sedes.binary.serialize @payload
         
     | 
| 
      
 374 
     | 
    
         
            +
                    tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
         
     | 
| 
      
 375 
     | 
    
         
            +
                    tx_data.push Util.serialize_int_to_big_endian @max_fee_per_blob_gas
         
     | 
| 
      
 376 
     | 
    
         
            +
                    tx_data.push Rlp::Sedes.infer(@blob_versioned_hashes).serialize @blob_versioned_hashes
         
     | 
| 
      
 377 
     | 
    
         
            +
                    tx_encoded = Rlp.encode tx_data
         
     | 
| 
      
 378 
     | 
    
         
            +
             
     | 
| 
      
 379 
     | 
    
         
            +
                    # create an EIP-2718 envelope with EIP-4844 type payload (unsigned)
         
     | 
| 
      
 380 
     | 
    
         
            +
                    tx_type = Util.serialize_int_to_big_endian @type
         
     | 
| 
      
 381 
     | 
    
         
            +
                    return "#{tx_type}#{tx_encoded}"
         
     | 
| 
      
 382 
     | 
    
         
            +
                  end
         
     | 
| 
      
 383 
     | 
    
         
            +
             
     | 
| 
      
 384 
     | 
    
         
            +
                  # Gets the sign-hash required to sign a raw transaction.
         
     | 
| 
      
 385 
     | 
    
         
            +
                  #
         
     | 
| 
      
 386 
     | 
    
         
            +
                  # @return [String] a Keccak-256 hash of an unsigned transaction.
         
     | 
| 
      
 387 
     | 
    
         
            +
                  def unsigned_hash
         
     | 
| 
      
 388 
     | 
    
         
            +
                    Util.keccak256 unsigned_encoded
         
     | 
| 
      
 389 
     | 
    
         
            +
                  end
         
     | 
| 
      
 390 
     | 
    
         
            +
             
     | 
| 
      
 391 
     | 
    
         
            +
                  private
         
     | 
| 
      
 392 
     | 
    
         
            +
             
     | 
| 
      
 393 
     | 
    
         
            +
                  # Force-sets an existing signature of a decoded transaction.
         
     | 
| 
      
 394 
     | 
    
         
            +
                  def _set_signature(recovery_id, r, s)
         
     | 
| 
      
 395 
     | 
    
         
            +
                    @signature_y_parity = recovery_id
         
     | 
| 
      
 396 
     | 
    
         
            +
                    @signature_r = r
         
     | 
| 
      
 397 
     | 
    
         
            +
                    @signature_s = s
         
     | 
| 
      
 398 
     | 
    
         
            +
                  end
         
     | 
| 
      
 399 
     | 
    
         
            +
                end
         
     | 
| 
      
 400 
     | 
    
         
            +
              end
         
     | 
| 
      
 401 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/eth/tx/eip7702.rb
    CHANGED
    
    | 
         @@ -196,7 +196,7 @@ module Eth 
     | 
|
| 
       196 
196 
     | 
    
         
             
                  # @option params [Integer] :max_gas_fee the max transaction fee per gas.
         
     | 
| 
       197 
197 
     | 
    
         
             
                  # @option params [Integer] :gas_limit the gas limit.
         
     | 
| 
       198 
198 
     | 
    
         
             
                  # @option params [Eth::Address] :from the sender address.
         
     | 
| 
       199 
     | 
    
         
            -
                  # @option params [Eth::Address] :to the  
     | 
| 
      
 199 
     | 
    
         
            +
                  # @option params [Eth::Address] :to the receiver address.
         
     | 
| 
       200 
200 
     | 
    
         
             
                  # @option params [Integer] :value the transaction value.
         
     | 
| 
       201 
201 
     | 
    
         
             
                  # @option params [String] :data the transaction data payload.
         
     | 
| 
       202 
202 
     | 
    
         
             
                  # @option params [Array] :access_list an optional access list.
         
     | 
| 
         @@ -269,13 +269,13 @@ module Eth 
     | 
|
| 
       269 
269 
     | 
    
         
             
                    raise ParameterError, "Transaction missing fields!" if tx.size < 10
         
     | 
| 
       270 
270 
     | 
    
         | 
| 
       271 
271 
     | 
    
         
             
                    # populate the 10 payload fields
         
     | 
| 
       272 
     | 
    
         
            -
                    chain_id = Util. 
     | 
| 
       273 
     | 
    
         
            -
                    nonce = Util. 
     | 
| 
       274 
     | 
    
         
            -
                    priority_fee = Util. 
     | 
| 
       275 
     | 
    
         
            -
                    max_gas_fee = Util. 
     | 
| 
       276 
     | 
    
         
            -
                    gas_limit = Util. 
     | 
| 
      
 272 
     | 
    
         
            +
                    chain_id = Util.deserialize_rlp_int tx[0]
         
     | 
| 
      
 273 
     | 
    
         
            +
                    nonce = Util.deserialize_rlp_int tx[1]
         
     | 
| 
      
 274 
     | 
    
         
            +
                    priority_fee = Util.deserialize_rlp_int tx[2]
         
     | 
| 
      
 275 
     | 
    
         
            +
                    max_gas_fee = Util.deserialize_rlp_int tx[3]
         
     | 
| 
      
 276 
     | 
    
         
            +
                    gas_limit = Util.deserialize_rlp_int tx[4]
         
     | 
| 
       277 
277 
     | 
    
         
             
                    to = Util.bin_to_hex tx[5]
         
     | 
| 
       278 
     | 
    
         
            -
                    value = Util. 
     | 
| 
      
 278 
     | 
    
         
            +
                    value = Util.deserialize_rlp_int tx[6]
         
     | 
| 
       279 
279 
     | 
    
         
             
                    data = tx[7]
         
     | 
| 
       280 
280 
     | 
    
         
             
                    access_list = tx[8]
         
     | 
| 
       281 
281 
     | 
    
         
             
                    authorization_list = tx[9]
         
     | 
| 
         @@ -389,6 +389,31 @@ module Eth 
     | 
|
| 
       389 
389 
     | 
    
         
             
                    return hash
         
     | 
| 
       390 
390 
     | 
    
         
             
                  end
         
     | 
| 
       391 
391 
     | 
    
         | 
| 
      
 392 
     | 
    
         
            +
                  # Signs the transaction with a provided signature blob.
         
     | 
| 
      
 393 
     | 
    
         
            +
                  #
         
     | 
| 
      
 394 
     | 
    
         
            +
                  # @param signature [String] the concatenated `r`, `s`, and `v` values.
         
     | 
| 
      
 395 
     | 
    
         
            +
                  # @return [String] a transaction hash.
         
     | 
| 
      
 396 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if transaction is already signed.
         
     | 
| 
      
 397 
     | 
    
         
            +
                  # @raise [Signature::SignatureError] if sender address does not match signer.
         
     | 
| 
      
 398 
     | 
    
         
            +
                  def sign_with(signature)
         
     | 
| 
      
 399 
     | 
    
         
            +
                    if Tx.signed? self
         
     | 
| 
      
 400 
     | 
    
         
            +
                      raise Signature::SignatureError, "Transaction is already signed!"
         
     | 
| 
      
 401 
     | 
    
         
            +
                    end
         
     | 
| 
      
 402 
     | 
    
         
            +
             
     | 
| 
      
 403 
     | 
    
         
            +
                    # ensure the sender address matches the signature
         
     | 
| 
      
 404 
     | 
    
         
            +
                    unless @sender.nil? or sender.empty?
         
     | 
| 
      
 405 
     | 
    
         
            +
                      public_key = Signature.recover(unsigned_hash, signature, @chain_id)
         
     | 
| 
      
 406 
     | 
    
         
            +
                      signer_address = Tx.sanitize_address Util.public_key_to_address(public_key).to_s
         
     | 
| 
      
 407 
     | 
    
         
            +
                      from_address = Tx.sanitize_address @sender
         
     | 
| 
      
 408 
     | 
    
         
            +
                      raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
         
     | 
| 
      
 409 
     | 
    
         
            +
                    end
         
     | 
| 
      
 410 
     | 
    
         
            +
             
     | 
| 
      
 411 
     | 
    
         
            +
                    r, s, v = Signature.dissect signature
         
     | 
| 
      
 412 
     | 
    
         
            +
                    recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
         
     | 
| 
      
 413 
     | 
    
         
            +
                    send :_set_signature, recovery_id, r, s
         
     | 
| 
      
 414 
     | 
    
         
            +
                    return hash
         
     | 
| 
      
 415 
     | 
    
         
            +
                  end
         
     | 
| 
      
 416 
     | 
    
         
            +
             
     | 
| 
       392 
417 
     | 
    
         
             
                  # Encodes a raw transaction object, wraps it in an EIP-2718 envelope
         
     | 
| 
       393 
418 
     | 
    
         
             
                  # with an EIP-7702 type prefix.
         
     | 
| 
       394 
419 
     | 
    
         
             
                  #
         
     | 
| 
         @@ -474,9 +499,9 @@ module Eth 
     | 
|
| 
       474 
499 
     | 
    
         | 
| 
       475 
500 
     | 
    
         
             
                  def deserialize_authorizations(authorization_list)
         
     | 
| 
       476 
501 
     | 
    
         
             
                    authorization_list.map do |authorization_tuple|
         
     | 
| 
       477 
     | 
    
         
            -
                      chain_id = Util. 
     | 
| 
      
 502 
     | 
    
         
            +
                      chain_id = Util.deserialize_rlp_int authorization_tuple[0]
         
     | 
| 
       478 
503 
     | 
    
         
             
                      address = Util.bin_to_hex authorization_tuple[1]
         
     | 
| 
       479 
     | 
    
         
            -
                      nonce = Util. 
     | 
| 
      
 504 
     | 
    
         
            +
                      nonce = Util.deserialize_rlp_int authorization_tuple[2]
         
     | 
| 
       480 
505 
     | 
    
         
             
                      recovery_id = Util.bin_to_hex(authorization_tuple[3]).to_i(16)
         
     | 
| 
       481 
506 
     | 
    
         
             
                      r = Util.bin_to_hex authorization_tuple[4]
         
     | 
| 
       482 
507 
     | 
    
         
             
                      s = Util.bin_to_hex authorization_tuple[5]
         
     |