Module: SafeEthRuby::Util

Extended by:
AbiCoderRb
Defined in:
lib/safe_eth_ruby/util.rb

Class Method Summary collapse

Class Method Details

.adjust_v_in_signature(signature) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/safe_eth_ruby/util.rb', line 19

def adjust_v_in_signature(signature)
  # We expect the V value to be '1c', '1d', '1e', or '1f' and want to set it to '1f'
  signature[-2..].to_i(16)
  new_v = 0x1f # Explicitly setting V to '1f'

  # Convert new V back to hexadecimal and ensure it's two characters long
  new_v_hex = new_v.to_s(16).rjust(2, "0")

  # Replace the last two characters (original V) with the new V value
  signature[0...-2] + new_v_hex
end

.encode_function_data(function_name:, abi:, data:) ⇒ Object

Encodes function call data for smart contract interactions



43
44
45
46
47
48
49
50
51
# File 'lib/safe_eth_ruby/util.rb', line 43

def encode_function_data(function_name:, abi:, data:)
  # Generate the function signature hash
  hash = Eth::Util.keccak256("#{function_name}(#{abi})")
  signature = slice_hex(Eth::Util.bin_to_hex(hash), 0, 4)

  # Encode the data
  encoded_data = Eth::Util.bin_to_hex(Eth::Abi.encode([abi], [Eth::Util.hex_to_bin(data)]))
  "0x#{signature}#{encoded_data}"
end

.encode_meta_transaction(tx) ⇒ Object



35
36
37
38
39
40
# File 'lib/safe_eth_ruby/util.rb', line 35

def encode_meta_transaction(tx)
  types = ["uint8", "address", "uint256", "uint256", "bytes"]
  values = [tx[:operation], tx[:to], tx[:value], tx[:data].bytesize, tx[:data]]
  bin_data = encode(types, values, true)
  Eth::Util.bin_to_hex(bin_data)
end

.encode_multi_send_data(txs) ⇒ Object



31
32
33
# File 'lib/safe_eth_ruby/util.rb', line 31

def encode_multi_send_data(txs)
  "0x" + txs.map { |tx| encode_meta_transaction(tx) }.join
end

.slice_hex(value, start_byte = 0, end_byte = nil) ⇒ Object

Slices a hex string to extract a specific byte range



9
10
11
12
13
# File 'lib/safe_eth_ruby/util.rb', line 9

def slice_hex(value, start_byte = 0, end_byte = nil)
  start_index = start_byte * 2
  end_index = end_byte.nil? ? value.length : end_byte * 2
  value[start_index...end_index]
end