Class: Klaytn::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/klaytn/encoder.rb

Instance Method Summary collapse

Instance Method Details

#do_encode_fixed(value, n) ⇒ Object



87
88
89
# File 'lib/klaytn/encoder.rb', line 87

def do_encode_fixed(value, n)
  encode_uint((value * 2**n).to_i)
end

#encode(type, value) ⇒ Object

code below this point was adopted from ethereum.rb gem in order to avoid dependency issues with Digest requiring older Ruby versions



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

def encode(type, value)
  is_array, arity, array_subtype = parse_array_type(type)
  if is_array && arity
    encode_static_array(arity, array_subtype, value)
  elsif is_array
    encode_dynamic_array(array_subtype, value)
  else
    core, subtype = parse_type(type)
    method_name = "encode_#{core}".to_sym
    self.send(method_name, value, subtype)
  end
end

#encode_address(value, _) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
120
121
# File 'lib/klaytn/encoder.rb', line 117

def encode_address(value, _)
  value = "0" * 24 + value.gsub(/^0x/,'')
  raise ArgumentError if value.size != 64
  value
end

#encode_arguments(inputs, args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/klaytn/encoder.rb', line 61

def encode_arguments(inputs, args)
  raise "Wrong number of arguments" if inputs.length != args.length
  @head = ""
  @tail = ""
  @inputs = inputs
  inputs.each.with_index do |input, index|
    encoded = encode(input.type, args[index])
    if encoded.is_a? Array
      @head << encoded[0]
      @tail << encoded[1]
    else
      @head << encoded
    end
  end
  @head + @tail
end

#encode_bool(value, _) ⇒ Object



78
79
80
# File 'lib/klaytn/encoder.rb', line 78

def encode_bool(value, _)
  (value ? "1" : "0").rjust(64, '0')
end

#encode_bytes(value, subtype) ⇒ Object



95
96
97
# File 'lib/klaytn/encoder.rb', line 95

def encode_bytes(value, subtype)
  subtype.nil? ? encode_dynamic_bytes(value) : encode_static_bytes(value)
end

#encode_dynamic_array(array_subtype, array) ⇒ Object



37
38
39
40
41
42
# File 'lib/klaytn/encoder.rb', line 37

def encode_dynamic_array(array_subtype, array)
  location = encode_uint(@inputs ? size_of_inputs(@inputs) + @tail.size/2 : 32)
  size = encode_uint(array.size)
  data = array.inject("") { |a, e| a << encode(array_subtype, e) }
  [location, size + data]
end

#encode_dynamic_bytes(value) ⇒ Object



103
104
105
106
107
108
# File 'lib/klaytn/encoder.rb', line 103

def encode_dynamic_bytes(value)
  location = encode_uint(@inputs ? size_of_inputs(@inputs) + @tail.size/2 : 32)
  size = encode_uint(value.size)
  content = encode_static_bytes(value)
  [location, size + content]
end

#encode_fixed(value, subtype) ⇒ Object



82
83
84
85
# File 'lib/klaytn/encoder.rb', line 82

def encode_fixed(value, subtype)
  n = subtype.nil? ? 128 : /(\d+)x(\d+)/.match(subtype)[2].to_i
  do_encode_fixed(value, n)
end

#encode_function(definition, inputs, params) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/klaytn/encoder.rb', line 3

def encode_function(definition, inputs, params)
  prefix = '0x'
  str = Digest::Keccak.hexdigest(definition, 256)[0..7] # ex: "getEarnout(uint256)"
  num = encode_arguments(inputs, params)

  "#{prefix}#{str}#{num}"
end

#encode_int(value, _ = nil) ⇒ Object



128
129
130
# File 'lib/klaytn/encoder.rb', line 128

def encode_int(value, _ = nil)
  to_twos_complement(value).to_s(16).rjust(64, '0')
end

#encode_integer(integer) ⇒ Object

ex: 5000000000000000000 (5 KLAY) => 0x4563918244f40000



11
12
13
14
15
# File 'lib/klaytn/encoder.rb', line 11

def encode_integer(integer) # ex: 5000000000000000000 (5 KLAY) => 0x4563918244f40000
  return '0x0' if integer.zero?
  long = encode_uint(integer)
  '0x' + long.sub!(/^[0]+/,'')
end

#encode_static_array(arity, array_subtype, array) ⇒ Object



32
33
34
35
# File 'lib/klaytn/encoder.rb', line 32

def encode_static_array(arity, array_subtype, array)
  raise "Wrong number of arguments" if arity != array.size
  array.inject("") { |a, e| a << encode(array_subtype, e) }
end

#encode_static_bytes(value) ⇒ Object



99
100
101
# File 'lib/klaytn/encoder.rb', line 99

def encode_static_bytes(value)
  value.bytes.map {|x| x.to_s(16).rjust(2, '0')}.join("").ljust(64, '0')
end

#encode_string(value, _) ⇒ Object



110
111
112
113
114
115
# File 'lib/klaytn/encoder.rb', line 110

def encode_string(value, _)
  location = encode_uint(@inputs ? size_of_inputs(@inputs) + @tail.size/2 : 32)
  size = encode_uint(value.bytes.size)
  content = value.bytes.map {|x| x.to_s(16).rjust(2, '0')}.join("").ljust(64, '0')
  [location, size + content]
end

#encode_ufixed(_value, _) ⇒ Object

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/klaytn/encoder.rb', line 91

def encode_ufixed(_value, _)
  raise NotImplementedError
end

#encode_uint(value, _ = nil) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
# File 'lib/klaytn/encoder.rb', line 123

def encode_uint(value, _ = nil)
  raise ArgumentError if value < 0
  encode_int(value)
end

#parse_array_type(type) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/klaytn/encoder.rb', line 44

def parse_array_type(type)
  match = /(.+)\[(\d*)\]\z/.match(type)
  if match
    [true, match[2].present? ? match[2].to_i : nil, match[1]]
  else
    [false, nil, nil]
  end
end

#parse_type(type) ⇒ Object

Raises:

  • (NotImplementedError)


53
54
55
56
57
58
59
# File 'lib/klaytn/encoder.rb', line 53

def parse_type(type)
  puts "TYPE: #{type}"
  # raise NotImplementedError if type.ends_with?("]")
  raise NotImplementedError if type[-1] == "]"
  match = /(\D+)(\d.*)?/.match(type)
  [match[1], match[2]]
end

#to_twos_complement(number) ⇒ Object



132
133
134
# File 'lib/klaytn/encoder.rb', line 132

def to_twos_complement(number)
  (number & ((1 << 256) - 1))
end