Class: Platon::Encoder

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

Instance Method Summary collapse

Instance Method Details

#do_encode_fixed(value, n) ⇒ Object



48
49
50
# File 'lib/platon/encoder.rb', line 48

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

#encode(type, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/platon/encoder.rb', line 5

def encode(type, value)
  is_array, arity, array_subtype = Abi::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 = Abi::parse_type(type)
    method_name = "encode_#{core}".to_sym
    self.send(method_name, value, subtype)
  end
end

#encode_address(value, _) ⇒ Object

Raises:

  • (ArgumentError)


78
79
80
81
82
83
84
# File 'lib/platon/encoder.rb', line 78

def encode_address(value, _)
  # puts "value: #{value}"  
  value = Utils.decode_bech32_address(value) if Utils.is_bech32_address?(value)
  value = "0" * 24 + value.gsub(/^0x/,'')
  raise ArgumentError if value.size != 64
  value
end

#encode_arguments(inputs, args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/platon/encoder.rb', line 90

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



39
40
41
# File 'lib/platon/encoder.rb', line 39

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

#encode_bytes(value, subtype) ⇒ Object



56
57
58
# File 'lib/platon/encoder.rb', line 56

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

#encode_dynamic_array(array_subtype, array) ⇒ Object



23
24
25
26
27
28
# File 'lib/platon/encoder.rb', line 23

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



64
65
66
67
68
69
# File 'lib/platon/encoder.rb', line 64

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



43
44
45
46
# File 'lib/platon/encoder.rb', line 43

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

#encode_int(value, _ = nil) ⇒ Object



30
31
32
# File 'lib/platon/encoder.rb', line 30

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

#encode_static_array(arity, array_subtype, array) ⇒ Object



18
19
20
21
# File 'lib/platon/encoder.rb', line 18

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



60
61
62
# File 'lib/platon/encoder.rb', line 60

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



71
72
73
74
75
76
# File 'lib/platon/encoder.rb', line 71

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)


52
53
54
# File 'lib/platon/encoder.rb', line 52

def encode_ufixed(_value, _)
  raise NotImplementedError
end

#encode_uint(value, _ = nil) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
# File 'lib/platon/encoder.rb', line 34

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

#ensure_prefix(value) ⇒ Object



86
87
88
# File 'lib/platon/encoder.rb', line 86

def ensure_prefix(value)
  value.start_with?("0x") ? value : ("0x" + value)
end