tron.rb 1.1.9 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 232c93f724a0aeb89150ec237af9c4a6d504b3635aabff7c833e9b71660a8752
4
- data.tar.gz: 4403e025c9d31a49e26da6cde92cfeb184df9626d97d76d662eb43ae3644728b
3
+ metadata.gz: 35bce6c89581cd9bd40fe5c0735ecc0c32967d8e009813f650b5ac72d109ba68
4
+ data.tar.gz: 3ae5de345bb422790b4d1e0088ee6fb90daa5f2bf6a5be6f0c26fef35f5bca35
5
5
  SHA512:
6
- metadata.gz: 8032ab96d92f191b7a3c6923104d656a5edb451a0498b57d339fc823084d3041b3da618ed106d015d60d17c99fb50d11a1b0228d8c1ee61ab80876feaf535a43
7
- data.tar.gz: a4bdca985bb3cf4389fd045711a2f7a3bcea9f15ddc4902a3ccb4c5e1e624d50140ae2ee1b0438fb23eb95f5e9e98e39c3316b060c877d6b22a27a45cb093df4
6
+ metadata.gz: 60866f0ac3fee060925d27bf6eebac0e5b05c5d93a44e509f8b5ed3b6119b00003a5085295cbe089d9ff0e454893ae2428272374ea1fe7d6da265d60f1cc1e5d
7
+ data.tar.gz: 722fb4af8c761a5033debf62cd09160ac355cc186ea1af598841e475a223b99a180687d20a0de479e31a7bd00f910ab5907efa8c07f36a6d55908241d0853e03
@@ -0,0 +1,177 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../type'
4
+ require_relative '../util'
5
+ require_relative '../constant'
6
+ require_relative '../../utils/address'
7
+
8
+ module Tron
9
+ module Abi
10
+ module Packed
11
+ # Provides a utility module to assist in packed encoding ABIs.
12
+ module Encoder
13
+ extend self
14
+
15
+ # Encodes a specific value in non-standard packed encoding mode.
16
+ #
17
+ # @param type [String] type to be encoded.
18
+ # @param arg [String|Number] value to be encoded.
19
+ # @return [String] the packed encoded type.
20
+ # @raise [EncodingError] if value does not match type.
21
+ # @raise [ArgumentError] if encoding fails for type.
22
+ def type(type, arg)
23
+ case type
24
+ when /^uint(\d+)$/
25
+ uint(arg, $1.to_i / 8)
26
+ when /^int(\d+)$/
27
+ int(arg, $1.to_i / 8)
28
+ when "bool"
29
+ bool(arg)
30
+ when /^ureal(\d+)x(\d+)$/, /^ufixed(\d+)x(\d+)$/
31
+ ufixed(arg, $1.to_i / 8, $2.to_i)
32
+ when /^real(\d+)x(\d+)$/, /^fixed(\d+)x(\d+)$/
33
+ fixed(arg, $1.to_i / 8, $2.to_i)
34
+ when "string"
35
+ string(arg)
36
+ when /^bytes(\d+)$/
37
+ bytes(arg, $1.to_i)
38
+ when "bytes"
39
+ string(arg)
40
+ when /^tuple\((.+)\)$/
41
+ tuple($1.split(","), arg)
42
+ when /^hash(\d+)$/
43
+ hash(arg, $1.to_i / 8)
44
+ when "address"
45
+ address(arg)
46
+ when /^(.+)\[\]$/
47
+ array($1, arg)
48
+ when /^(.+)\[(\d+)\]$/
49
+ fixed_array($1, arg, $2.to_i)
50
+ else
51
+ raise EncodingError, "Unhandled type: #{type}"
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ # Properly encodes unsigned integers.
58
+ def uint(value, byte_size)
59
+ raise ArgumentError, "Don't know how to handle this input." unless value.is_a? Numeric
60
+ raise ValueOutOfBounds, "Number out of range: #{value}" if value > Constant::UINT_MAX or value < Constant::UINT_MIN
61
+ i = value.to_i
62
+ Util.zpad_int(i, byte_size)
63
+ end
64
+
65
+ # Properly encodes signed integers.
66
+ def int(value, byte_size)
67
+ raise ArgumentError, "Don't know how to handle this input." unless value.is_a? Numeric
68
+ raise ValueOutOfBounds, "Number out of range: #{value}" if value > Constant::INT_MAX or value < Constant::INT_MIN
69
+ real_size = byte_size * 8
70
+ i = value.to_i % 2 ** real_size
71
+ Util.zpad_int(i, byte_size)
72
+ end
73
+
74
+ # Properly encodes booleans.
75
+ def bool(value)
76
+ raise EncodingError, "Argument is not bool: #{value}" unless [TrueClass, FalseClass].any? { |bool_class| value.is_a?(bool_class) }
77
+ (value ? "\x01" : "\x00").b
78
+ end
79
+
80
+ # Properly encodes unsigned fixed-point numbers.
81
+ def ufixed(value, byte_size, decimals)
82
+ raise ArgumentError, "Don't know how to handle this input." unless value.is_a? Numeric
83
+ raise ValueOutOfBounds, value unless value >= 0 and value < 2 ** decimals
84
+ scaled_value = (value * (10 ** decimals)).to_i
85
+ uint(scaled_value, byte_size)
86
+ end
87
+
88
+ # Properly encodes signed fixed-point numbers.
89
+ def fixed(value, byte_size, decimals)
90
+ raise ArgumentError, "Don't know how to handle this input." unless value.is_a? Numeric
91
+ raise ValueOutOfBounds, value unless value >= -2 ** (decimals - 1) and value < 2 ** (decimals - 1)
92
+ scaled_value = (value * (10 ** decimals)).to_i
93
+ int(scaled_value, byte_size)
94
+ end
95
+
96
+ # Properly encodes byte(-string)s.
97
+ def bytes(value, length)
98
+ raise EncodingError, "Expecting String: #{value}" unless value.is_a? String
99
+ value = handle_hex_string(value, length)
100
+ raise ArgumentError, "Value must be a string of length #{length}" unless value.is_a?(String) && value.bytesize == length
101
+ value.b
102
+ end
103
+
104
+ # Properly encodes (byte-)strings.
105
+ def string(value)
106
+ raise ArgumentError, "Value must be a string" unless value.is_a?(String)
107
+ value.b
108
+ end
109
+
110
+ # Properly encodes tuples.
111
+ def tuple(types, values)
112
+ # Call the solidity_packed function to handle tuple encoding
113
+ Tron::Abi.solidity_packed(types, values)
114
+ end
115
+
116
+ # Properly encodes hash-strings.
117
+ def hash(value, byte_size)
118
+ raise EncodingError, "Argument too long: #{value}" unless byte_size > 0 and byte_size <= 32
119
+ hash_bytes = handle_hex_string(value, byte_size)
120
+ hash_bytes.b
121
+ end
122
+
123
+ # Properly encodes addresses.
124
+ def address(value)
125
+ if value.is_a?(String) && value.start_with?('T') && value.length == 34
126
+ # TRON address in Base58 format - convert to hex
127
+ hex_addr = Utils::Address.to_hex(value)
128
+ # Remove the 0x41 prefix and return raw bytes
129
+ Util.hex_to_bin(hex_addr[2..-1])
130
+ elsif value.is_a? Integer
131
+ # address from integer
132
+ Util.zpad_int(value, 20)
133
+ elsif value.size == 20
134
+ # address from raw 20-byte address
135
+ value
136
+ elsif value.size == 40
137
+ # address from hexadecimal address (without 0x prefix)
138
+ Util.hex_to_bin(value)
139
+ elsif value.size == 42 && value[0, 2] == "0x"
140
+ # address from hexadecimal address with 0x prefix
141
+ Util.hex_to_bin(value[2..-1])
142
+ else
143
+ raise EncodingError, "Could not parse address: #{value}"
144
+ end
145
+ end
146
+
147
+ # Properly encodes dynamic-sized arrays.
148
+ def array(type, values)
149
+ # For packed encoding, we don't encode array length, just concatenate values
150
+ values.map { |value| type(type, value) }.join.b
151
+ end
152
+
153
+ # Properly encodes fixed-size arrays.
154
+ def fixed_array(type, values, size)
155
+ raise ArgumentError, "Array size does not match" unless values.size == size
156
+ # For packed encoding, concatenate values directly
157
+ values.map { |value| type(type, value) }.join.b
158
+ end
159
+
160
+ # The ABI encoder needs to be able to determine between a hex `"123"`
161
+ # and a binary `"123"` string.
162
+ def handle_hex_string(val, len)
163
+ if Util.prefixed?(val) || (len == val.size / 2 && Util.hex?(val))
164
+ # There is no way telling whether a string is hex or binary with certainty
165
+ # in Ruby. Therefore, we assume a `0x` prefix to indicate a hex string.
166
+ # Additionally, if the string size is exactly the double of the expected
167
+ # binary size, we can assume a hex value.
168
+ Util.hex_to_bin(val)
169
+ else
170
+ # Everything else will be assumed binary or raw string.
171
+ val.b
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'packed/encoder'
data/lib/tron/abi.rb CHANGED
@@ -22,6 +22,7 @@ module Tron
22
22
  require_relative 'abi/event'
23
23
  require_relative 'abi/util'
24
24
  require_relative 'abi/constant'
25
+ require_relative 'abi/packed'
25
26
 
26
27
  # For address handling functionality
27
28
  require_relative 'utils/address'
@@ -139,6 +140,21 @@ module Tron
139
140
  types
140
141
  end
141
142
 
143
+ # Encodes values using Solidity packed encoding (no padding to 32 bytes)
144
+ #
145
+ # @param types [Array<String>] array of type strings
146
+ # @param values [Array] array of values to encode
147
+ # @return [String] binary string of packed encoded values
148
+ def self.solidity_packed(types, values)
149
+ raise ArgumentError, "Types and values must be the same length" if types.length != values.length
150
+
151
+ packed = types.zip(values).map do |type, value|
152
+ Tron::Abi::Packed::Encoder.type(type, value)
153
+ end.join
154
+
155
+ packed.force_encoding(Encoding::ASCII_8BIT)
156
+ end
157
+
142
158
  # Decode output from a contract call
143
159
  #
144
160
  # @param type_str [String] the output type
data/lib/tron/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  # lib/tron/version.rb
4
4
  module Tron
5
- VERSION = "1.1.9".freeze
5
+ VERSION = "1.2.1".freeze
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tron.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.9
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bolo Michelin
@@ -141,6 +141,8 @@ files:
141
141
  - lib/tron/abi/encoder.rb
142
142
  - lib/tron/abi/event.rb
143
143
  - lib/tron/abi/function.rb
144
+ - lib/tron/abi/packed.rb
145
+ - lib/tron/abi/packed/encoder.rb
144
146
  - lib/tron/abi/type.rb
145
147
  - lib/tron/abi/util.rb
146
148
  - lib/tron/cache.rb