Class: Trx::Abi::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/trx/abi/decoder.rb

Instance Method Summary collapse

Instance Method Details

#decode(type, value, start = 0) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/trx/abi/decoder.rb', line 5

def decode(type, value, start = 0)
  is_array, arity, array_subtype = Abi::parse_array_type(type)
  if is_array && arity
    decode_static_array(arity, array_subtype, value, start)
  elsif is_array
    decode_dynamic_array(array_subtype, value, start)
  else
    value = value.gsub(/^0x/,'')
    core, subtype = Abi::parse_type(type)
    method_name = "decode_#{core}".to_sym
    self.send(method_name, value, subtype, start)
  end
end

#decode_address(value, _ = nil, start) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
# File 'lib/trx/abi/decoder.rb', line 51

def decode_address(value, _ = nil, start)
  raise ArgumentError if value.size-start < 64
  value[start+24..start+63]
end

#decode_arguments(arguments, data) ⇒ Object



74
75
76
77
78
# File 'lib/trx/abi/decoder.rb', line 74

def decode_arguments(arguments, data)
  data = data.gsub(/^0x/,'')
  types = arguments.map { |o| o.type }
  types.each.with_index.map { |t , i| decode(t, data, i*64) }
end

#decode_bool(value, _, start) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
# File 'lib/trx/abi/decoder.rb', line 44

def decode_bool(value, _, start)
  value = trim(value, start, 4)
  return true if value == "1"
  return false if value == "0"
  raise ArgumentError
end

#decode_bytes(value, subtype, start) ⇒ Object



56
57
58
# File 'lib/trx/abi/decoder.rb', line 56

def decode_bytes(value, subtype, start)
  subtype.present? ? decode_static_bytes(value, subtype, start) : decode_dynamic_bytes(value, start)
end

#decode_dynamic_array(array_subtype, value, start) ⇒ Object



23
24
25
26
27
# File 'lib/trx/abi/decoder.rb', line 23

def decode_dynamic_array(array_subtype, value, start)
  location = decode_uint(value[start..(start+63)]) * 2
  size = decode_uint(value[location..location+63])
  (0..size-1).map { |i| decode(array_subtype, value, location + (i+1) * 64) }
end

#decode_dynamic_bytes(value, start = 0) ⇒ Object



64
65
66
67
68
# File 'lib/trx/abi/decoder.rb', line 64

def decode_dynamic_bytes(value, start = 0)
  location = decode_uint(value[start..(start+63)]) * 2
  size = decode_uint(value[location..location+63]) * 2
  value[location+64..location+63+size].scan(/.{2}/).map {|x| x.hex}.pack('C*')
end

#decode_fixed(value, subtype = "128x128", start = 0) ⇒ Object



29
30
31
# File 'lib/trx/abi/decoder.rb', line 29

def decode_fixed(value, subtype = "128x128", start = 0)
  decode_int(trim(value, start, fixed_bitsize(subtype))).to_f / 2**exponent(subtype)
end

#decode_int(value, subtype = "256", start = 0) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
# File 'lib/trx/abi/decoder.rb', line 37

def decode_int(value, subtype = "256", start = 0)
  raise ArgumentError if value.nil?
  size = bitsize(subtype)
  value = trim(value, start, size)
  (value[0..1] == "ff") ? (value.hex - (2 ** size)) : value.hex
end

#decode_static_array(arity, array_subtype, value, start) ⇒ Object



19
20
21
# File 'lib/trx/abi/decoder.rb', line 19

def decode_static_array(arity, array_subtype, value, start)
  (0..arity-1).map { |i| decode(array_subtype, value, start + i * 64) }
end

#decode_static_bytes(value, subtype = nil, start = 0) ⇒ Object



60
61
62
# File 'lib/trx/abi/decoder.rb', line 60

def decode_static_bytes(value, subtype = nil, start = 0)
  trim(value, start, subtype.to_i*8).scan(/.{2}/).map {|x| x.hex}.pack('C*').strip
end

#decode_string(value, _ = nil, start = 0) ⇒ Object



70
71
72
# File 'lib/trx/abi/decoder.rb', line 70

def decode_string(value, _ = nil, start = 0)
  decode_dynamic_bytes(value, start).force_encoding('utf-8')
end

#decode_uint(value, subtype = "256", start = 0) ⇒ Object



33
34
35
# File 'lib/trx/abi/decoder.rb', line 33

def decode_uint(value, subtype = "256", start = 0)
  trim(value, start, bitsize(subtype)).hex
end