Module: Web3::Eth::Abi::AbiCoder
- Extended by:
- AbiCoder
- Includes:
- Constant
- Included in:
- AbiCoder, Contract::ContractMethod
- Defined in:
- lib/web3/eth/abi/abi_coder.rb
Overview
Contract ABI encoding and decoding.
Defined Under Namespace
Classes: DecodingError, EncodingError, ValueOutOfBounds
Constant Summary
Constants included from Constant
Constant::BYTE_EMPTY, Constant::BYTE_ONE, Constant::BYTE_ZERO, Constant::CONTRACT_CODE_SIZE_LIMIT, Constant::HASH_ZERO, Constant::INT_MAX, Constant::INT_MIN, Constant::PRIVKEY_ZERO, Constant::PRIVKEY_ZERO_HEX, Constant::PUBKEY_ZERO, Constant::TT160, Constant::TT256, Constant::TT32, Constant::TT40, Constant::TT64M1, Constant::UINT_MAX, Constant::UINT_MIN
Instance Method Summary collapse
-
#decode_abi(types, data, raise_errors = false) ⇒ Object
(also: #decode)
Decodes multiple arguments using the head/tail mechanism.
- #decode_primitive_type(type, data) ⇒ Object
- #decode_type(type, arg) ⇒ Object
- #decode_typed_data(type_name, data) ⇒ Object
-
#encode_abi(types, args) ⇒ Object
(also: #encode)
Encodes multiple arguments using the head/tail mechanism.
- #encode_primitive_type(type, arg) ⇒ Object
-
#encode_type(type, arg) ⇒ String
Encodes a single value (static or dynamic).
- #min_data_size(types) ⇒ Object
- #zero_padding(data, pos, count, start_positions) ⇒ Object
Instance Method Details
#decode_abi(types, data, raise_errors = false) ⇒ Object Also known as: decode
Decodes multiple arguments using the head/tail mechanism.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/web3/eth/abi/abi_coder.rb', line 203 def decode_abi types, data, raise_errors = false parsed_types = types.map {|t| Type.parse(t) } outputs = [nil] * types.size start_positions = [nil] * types.size + [data.size] # TODO: refactor, a reverse iteration will be better pos = 0 parsed_types.each_with_index do |t, i| # If a type is static, grab the data directly, otherwise record its # start position if t.dynamic? if raise_errors && pos>data.size-1 raise DecodingError, "Position out of bounds #{pos}>#{data.size-1}" end start_positions[i] = Utils.big_endian_to_int(data[pos, 32]) if raise_errors && start_positions[i]>data.size-1 raise DecodingError, "Start position out of bounds #{start_positions[i]}>#{data.size-1}" end j = i - 1 while j >= 0 && start_positions[j].nil? start_positions[j] = start_positions[i] j -= 1 end pos += 32 else outputs[i] = zero_padding data, pos, t.size, start_positions pos += t.size end end # We add a start position equal to the length of the entire data for # convenience. j = types.size - 1 while j >= 0 && start_positions[j].nil? start_positions[j] = start_positions[types.size] j -= 1 end if raise_errors && pos > data.size raise DecodingError, "Not enough data for head" end parsed_types.each_with_index do |t, i| if t.dynamic? offset, next_offset = start_positions[i, 2] if offset<=data.size && next_offset<=data.size outputs[i] = data[offset...next_offset] end end end if raise_errors && outputs.include?(nil) raise DecodingError, "Not all data can be parsed" end parsed_types.zip(outputs).map {|(type, out)| decode_type(type, out) } end |
#decode_primitive_type(type, data) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/web3/eth/abi/abi_coder.rb', line 329 def decode_primitive_type(type, data) case type.base when 'address' Utils.encode_hex data[12..-1] when 'string', 'bytes' if type.sub.empty? # dynamic if data.length==32 data[0..32] else size = Utils.big_endian_to_int data[0,32] data[32..-1][0,size] end else # fixed data[0, type.sub.to_i] end when 'hash' data[(32 - type.sub.to_i), type.sub.to_i] when 'uint' Utils.big_endian_to_int data when 'int' u = Utils.big_endian_to_int data u >= 2**(type.sub.to_i-1) ? (u - 2**type.sub.to_i) : u when 'ufixed' high, low = type.sub.split('x').map(&:to_i) Utils.big_endian_to_int(data) * 1.0 / 2**low when 'fixed' high, low = type.sub.split('x').map(&:to_i) u = Utils.big_endian_to_int data i = u >= 2**(high+low-1) ? (u - 2**(high+low)) : u i * 1.0 / 2**low when 'bool' data[-1] == BYTE_ONE else raise DecodingError, "Unknown primitive type: #{type.base}" end end |
#decode_type(type, arg) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/web3/eth/abi/abi_coder.rb', line 285 def decode_type(type, arg) return nil if arg.nil? || arg.empty? if type.kind_of?(Tuple) && type.dims.empty? arg ? decode_abi(type.types, arg) : [] elsif %w(string bytes).include?(type.base) && type.sub.empty? l = Utils.big_endian_to_int arg[0,32] data = arg[32..-1] data[0, l] elsif !type.dims.empty? && (l = type.dims.last)>0 # static-sized arrays subtype = type.subtype if subtype.dynamic? start_positions = (0...l).map {|i| Utils.big_endian_to_int(arg[32*i, 32]) } start_positions.push arg.size outputs = (0...l).map {|i| arg[start_positions[i]...start_positions[i+1]] } outputs.map {|out| decode_type(subtype, out) } else (0...l).map {|i| decode_type(subtype, arg[subtype.size*i, subtype.size]) } end elsif type.dynamic? l = Utils.big_endian_to_int arg[0,32] raise DecodingError, "Too long length: #{l}" if l>100000 subtype = type.subtype if subtype.dynamic? raise DecodingError, "Not enough data for head" unless arg.size >= 32 + 32*l start_positions = (1..l).map {|i| 32+Utils.big_endian_to_int(arg[32*i, 32]) } start_positions.push arg.size outputs = (0...l).map {|i| arg[start_positions[i]...start_positions[i+1]] } outputs.map {|out| decode_type(subtype, out) } else (0...l).map {|i| decode_type(subtype, arg[32 + subtype.size*i, subtype.size]) } end else decode_primitive_type type, arg end end |
#decode_typed_data(type_name, data) ⇒ Object
281 282 283 |
# File 'lib/web3/eth/abi/abi_coder.rb', line 281 def decode_typed_data type_name, data decode_primitive_type Type.parse(type_name), data end |
#encode_abi(types, args) ⇒ Object Also known as: encode
Encodes multiple arguments using the head/tail mechanism.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/web3/eth/abi/abi_coder.rb', line 28 def encode_abi(types, args) parsed_types = types.map {|t| Type.parse(t) } head_size = (0...args.size) .map {|i| parsed_types[i].size || 32 } .reduce(0, &:+) head, tail = '', '' args.each_with_index do |arg, i| if parsed_types[i].dynamic? head += encode_type(Type.size_type, head_size + tail.size) tail += encode_type(parsed_types[i], arg) else head += encode_type(parsed_types[i], arg) end end "#{head}#{tail}" end |
#encode_primitive_type(type, arg) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/web3/eth/abi/abi_coder.rb', line 91 def encode_primitive_type(type, arg) case type.base when 'uint' begin real_size = type.sub.to_i i = get_uint arg raise ValueOutOfBounds, arg unless i >= 0 && i < 2**real_size Utils.zpad_int i rescue EncodingError raise ValueOutOfBounds, arg end when 'bool' raise ArgumentError, "arg is not bool: #{arg}" unless arg.instance_of?(TrueClass) || arg.instance_of?(FalseClass) Utils.zpad_int(arg ? 1 : 0) when 'int' begin real_size = type.sub.to_i i = get_int arg raise ValueOutOfBounds, arg unless i >= -2**(real_size-1) && i < 2**(real_size-1) Utils.zpad_int(i % 2**type.sub.to_i) rescue EncodingError raise ValueOutOfBounds, arg end when 'ufixed' high, low = type.sub.split('x').map(&:to_i) raise ValueOutOfBounds, arg unless arg >= 0 && arg < 2**high Utils.zpad_int((arg * 2**low).to_i) when 'fixed' high, low = type.sub.split('x').map(&:to_i) raise ValueOutOfBounds, arg unless arg >= -2**(high - 1) && arg < 2**(high - 1) i = (arg * 2**low).to_i Utils.zpad_int(i % 2**(high+low)) when 'string' if arg.encoding.name == 'UTF-8' arg = arg.b else begin arg.unpack('U*') rescue ArgumentError raise ValueError, "string must be UTF-8 encoded" end end if type.sub.empty? # variable length type raise ValueOutOfBounds, "Integer invalid or out of range: #{arg.size}" if arg.size >= TT256 size = Utils.zpad_int arg.size value = Utils.rpad arg, BYTE_ZERO, Utils.ceil32(arg.size) "#{size}#{value}" else # fixed length type sub = type.sub.to_i raise ValueOutOfBounds, "invalid string length #{sub}" if arg.size > sub raise ValueOutOfBounds, "invalid string length #{sub}" if sub < 0 || sub > 32 Utils.rpad(arg, BYTE_ZERO, 32) end when 'bytes' raise EncodingError, "Expecting string: #{arg}" unless arg.instance_of?(String) arg = arg.b if type.sub.empty? # variable length type raise ValueOutOfBounds, "Integer invalid or out of range: #{arg.size}" if arg.size >= TT256 size = Utils.zpad_int arg.size value = Utils.rpad arg, BYTE_ZERO, Utils.ceil32(arg.size) "#{size}#{value}" else # fixed length type sub = type.sub.to_i raise ValueOutOfBounds, "invalid bytes length #{sub}" if arg.size > sub raise ValueOutOfBounds, "invalid bytes length #{sub}" if sub < 0 || sub > 32 Utils.rpad(arg, BYTE_ZERO, 32) end when 'hash' size = type.sub.to_i raise EncodingError, "too long: #{arg}" unless size > 0 && size <= 32 if arg.is_a?(Integer) Utils.zpad_int(arg) elsif arg.size == size Utils.zpad arg, 32 elsif arg.size == size * 2 Utils.zpad_hex arg else raise EncodingError, "Could not parse hash: #{arg}" end when 'address' if arg.is_a?(Integer) Utils.zpad_int arg elsif arg.size == 20 Utils.zpad arg, 32 elsif arg.size == 40 Utils.zpad_hex arg elsif arg.size == 42 && arg[0,2] == '0x' Utils.zpad_hex arg[2..-1] else raise EncodingError, "Could not parse address: #{arg}" end else raise EncodingError, "Unhandled type: #{type.base} #{type.sub}" end end |
#encode_type(type, arg) ⇒ String
Encodes a single value (static or dynamic).
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/web3/eth/abi/abi_coder.rb', line 57 def encode_type(type, arg) if %w(string bytes).include?(type.base) && type.sub.empty? encode_primitive_type type, arg elsif type.dynamic? raise ArgumentError, "arg must be an array" unless arg.instance_of?(Array) head, tail = '', '' if type.dims.last == 0 head += encode_type(Type.size_type, arg.size) else raise ArgumentError, "Wrong array size: found #{arg.size}, expecting #{type.dims.last}" unless arg.size == type.dims.last end sub_type = type.subtype sub_size = type.subtype.size arg.size.times do |i| if sub_size.nil? head += encode_type(Type.size_type, 32*arg.size + tail.size) tail += encode_type(sub_type, arg[i]) else head += encode_type(sub_type, arg[i]) end end "#{head}#{tail}" else # static type if type.dims.empty? encode_primitive_type type, arg else arg.map {|x| encode_type(type.subtype, x) }.join end end end |
#min_data_size(types) ⇒ Object
196 197 198 |
# File 'lib/web3/eth/abi/abi_coder.rb', line 196 def min_data_size types types.size*32 end |
#zero_padding(data, pos, count, start_positions) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/web3/eth/abi/abi_coder.rb', line 269 def zero_padding data, pos, count, start_positions if pos >= data.size start_positions[start_positions.size-1] += count "\x00"*count elsif pos + count > data.size start_positions[start_positions.size-1] += ( count - (data.size-pos)) data[pos,data.size-pos] + "\x00"*( count - (data.size-pos)) else data[pos, count] end end |