Sha256: 96c2894b8cad6c2aa9946e2967b2ead575f0c1d321e67d061880acfca0905598

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

require 'eth'

module Etherlite
  module Account
    class PrivateKey < Base
      def initialize(_connection, _pk)
        @key = Eth::Key.new priv: _pk
        super _connection, Etherlite::Utils.normalize_address(@key.address)
      end

      def send_transaction(_options = {})
        nonce_options = _options.slice(:replace, :nonce)
        
        nonce_manager.with_next_nonce_for(normalized_address, nonce_options) do |nonce|
          tx = Eth::Tx.new(
            value: _options.fetch(:value, 0),
            data: _options.fetch(:data, ''),
            gas_limit: _options.fetch(:gas, 90_000),
            gas_price: _options.fetch(:gas_price, gas_price),
            to: (Etherlite::Utils.encode_address_param(_options[:to]) if _options.key?(:to)),
            nonce: nonce
          )

          # Since eth gem does not allow configuration of chains for every tx, we need
          # to globally configure it before signing. This is not thread safe so a mutex is needed.
          sign_with_connection_chain tx

          Etherlite::Transaction.new @connection, @connection.eth_send_raw_transaction(tx.hex)
        end
      end

      private

      @@eth_mutex = Mutex.new

      def gas_price
        # TODO: improve on this
        @gas_price ||= connection.eth_gas_price
      end

      def nonce_manager
        Etherlite::NonceManager.new @connection
      end

      def sign_with_connection_chain(_tx)
        @@eth_mutex.synchronize do
          Eth.configure { |c| c.chain_id = @connection.chain_id }
          _tx.sign @key
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
etherlite-0.4.2 lib/etherlite/account/private_key.rb
etherlite-0.4.1 lib/etherlite/account/private_key.rb
etherlite-0.4.0 lib/etherlite/account/private_key.rb