Module: Web3::Eth::Abi::Utils

Extended by:
Utils
Includes:
Constant
Included in:
Utils, Contract::ContractMethod
Defined in:
lib/web3ethereum/abi/utils.rb

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

Instance Method Details

#base58_check_to_bytes(s) ⇒ Object

Raises:



63
64
65
66
67
68
69
# File 'lib/web3ethereum/abi/utils.rb', line 63

def base58_check_to_bytes(s)
  leadingzbytes = s.match(/\A1*/)[0]
  data = Constant::BYTE_ZERO * leadingzbytes.size + BaseConvert.convert(s, 58, 256)

  raise ChecksumError, "double sha256 checksum doesn't match" unless double_sha256(data[0...-4])[0,4] == data[-4..-1]
  data[1...-4]
end

#big_endian_to_int(s) ⇒ Object



90
91
92
# File 'lib/web3ethereum/abi/utils.rb', line 90

def big_endian_to_int(s)
  RLP::Sedes.big_endian_int.deserialize s.sub(/\A(\x00)+/, '')
end

#bytearray_to_int(arr) ⇒ Object



138
139
140
141
142
# File 'lib/web3ethereum/abi/utils.rb', line 138

def bytearray_to_int(arr)
  o = 0
  arr.each {|x| o = (o << 8) + x }
  o
end

#bytes_to_base58_check(bytes, magicbyte = 0) ⇒ Object



71
72
73
74
75
76
# File 'lib/web3ethereum/abi/utils.rb', line 71

def bytes_to_base58_check(bytes, magicbyte=0)
  bs = "#{magicbyte.chr}#{bytes}"
  leadingzbytes = bs.match(/\A#{Constant::BYTE_ZERO}*/)[0]
  checksum = double_sha256(bs)[0,4]
  '1'*leadingzbytes.size + BaseConvert.convert("#{bs}#{checksum}", 256, 58)
end

#bytes_to_int_array(bytes) ⇒ Object



148
149
150
# File 'lib/web3ethereum/abi/utils.rb', line 148

def bytes_to_int_array(bytes)
  bytes.unpack('C*')
end

#ceil32(x) ⇒ Object



78
79
80
# File 'lib/web3ethereum/abi/utils.rb', line 78

def ceil32(x)
  x % 32 == 0 ? x : (x + 32 - x%32)
end

#coerce_addr_to_hex(x) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/web3ethereum/abi/utils.rb', line 172

def coerce_addr_to_hex(x)
  if x.is_a?(Numeric)
    encode_hex zpad(int_to_big_endian(x), 20)
  elsif x.size == 40 || x.size == 0
    x
  else
    encode_hex zpad(x, 20)[-20..-1]
  end
end

#coerce_to_bytes(x) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/web3ethereum/abi/utils.rb', line 162

def coerce_to_bytes(x)
  if x.is_a?(Numeric)
    int_to_big_endian x
  elsif x.size == 40
    decode_hex(x)
  else
    x
  end
end

#coerce_to_int(x) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/web3ethereum/abi/utils.rb', line 152

def coerce_to_int(x)
  if x.is_a?(Numeric)
    x
  elsif x.size == 40
    big_endian_to_int decode_hex(x)
  else
    big_endian_to_int x
  end
end

#decode_hex(s) ⇒ Object



86
87
88
# File 'lib/web3ethereum/abi/utils.rb', line 86

def decode_hex(s)
  RLP::Utils.decode_hex s
end

#decode_int(v) ⇒ Object

Raises:

  • (ArgumentError)


133
134
135
136
# File 'lib/web3ethereum/abi/utils.rb', line 133

def decode_int(v)
  raise ArgumentError, "No leading zero bytes allowed for integers" if v.size > 0 && (v[0] == Constant::BYTE_ZERO || v[0] == 0)
  big_endian_to_int v
end

#double_sha256(x) ⇒ Object



35
36
37
# File 'lib/web3ethereum/abi/utils.rb', line 35

def double_sha256(x)
  sha256 sha256(x)
end

#encode_hex(b) ⇒ Object



82
83
84
# File 'lib/web3ethereum/abi/utils.rb', line 82

def encode_hex(b)
  RLP::Utils.encode_hex b
end

#encode_int(n) ⇒ Object

Raises:

  • (ArgumentError)


128
129
130
131
# File 'lib/web3ethereum/abi/utils.rb', line 128

def encode_int(n)
  raise ArgumentError, "Integer invalid or out of range: #{n}" unless n.is_a?(Integer) && n >= 0 && n <= UINT_MAX
  int_to_big_endian n
end

#function_signature(method_name, arg_types) ⇒ Object



215
216
217
# File 'lib/web3ethereum/abi/utils.rb', line 215

def function_signature method_name, arg_types
  "#{method_name}(#{arg_types.join(',')})"
end

#hash160(x) ⇒ Object



43
44
45
# File 'lib/web3ethereum/abi/utils.rb', line 43

def hash160(x)
  ripemd160 sha256(x)
end

#hash160_hex(x) ⇒ Object



47
48
49
# File 'lib/web3ethereum/abi/utils.rb', line 47

def hash160_hex(x)
  encode_hex hash160(x)
end

#int_array_to_bytes(arr) ⇒ Object



144
145
146
# File 'lib/web3ethereum/abi/utils.rb', line 144

def int_array_to_bytes(arr)
  arr.pack('C*')
end

#int_to_addr(x) ⇒ Object



124
125
126
# File 'lib/web3ethereum/abi/utils.rb', line 124

def int_to_addr(x)
  zpad_int x, 20
end

#int_to_big_endian(n) ⇒ Object



94
95
96
# File 'lib/web3ethereum/abi/utils.rb', line 94

def int_to_big_endian(n)
  RLP::Sedes.big_endian_int.serialize n
end

#keccak256(x) ⇒ Object

Not the keccak in sha3, although it’s underlying lib named SHA3



19
20
21
# File 'lib/web3ethereum/abi/utils.rb', line 19

def keccak256(x)
  SHA3::Digest.new(256).digest(x)
end

#keccak256_rlp(x) ⇒ Object



27
28
29
# File 'lib/web3ethereum/abi/utils.rb', line 27

def keccak256_rlp(x)
  keccak256 RLP.encode(x)
end

#keccak512(x) ⇒ Object



23
24
25
# File 'lib/web3ethereum/abi/utils.rb', line 23

def keccak512(x)
  SHA3::Digest.new(512).digest(x)
end

#lpad(x, symbol, l) ⇒ Object



98
99
100
101
# File 'lib/web3ethereum/abi/utils.rb', line 98

def lpad(x, symbol, l)
  return x if x.size >= l
  symbol * (l - x.size) + x
end

#mk_contract_address(sender, nonce) ⇒ Object



188
189
190
# File 'lib/web3ethereum/abi/utils.rb', line 188

def mk_contract_address(sender, nonce)
  keccak256_rlp([normalize_address(sender), nonce])[12..-1]
end

#mk_metropolis_contract_address(sender, initcode) ⇒ Object



192
193
194
# File 'lib/web3ethereum/abi/utils.rb', line 192

def mk_metropolis_contract_address(sender, initcode)
  keccak256(normalize_address(sender) + initcode)[12..-1]
end

#mod_exp(x, y, n) ⇒ Object



51
52
53
# File 'lib/web3ethereum/abi/utils.rb', line 51

def mod_exp(x, y, n)
  x.to_bn.mod_exp(y, n).to_i
end

#mod_mul(x, y, n) ⇒ Object



55
56
57
# File 'lib/web3ethereum/abi/utils.rb', line 55

def mod_mul(x, y, n)
  x.to_bn.mod_mul(y, n).to_i
end

#normalize_address(x, allow_blank: false) ⇒ Object

Raises:



182
183
184
185
186
# File 'lib/web3ethereum/abi/utils.rb', line 182

def normalize_address(x, allow_blank: false)
  address = Address.new(x)
  raise ValueError, "address is blank" if !allow_blank && address.blank?
  address.to_bytes
end

#normalize_hex_without_prefix(s) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/web3ethereum/abi/utils.rb', line 207

def normalize_hex_without_prefix(s)
  if s[0,2] == '0x'
    (s.size % 2 == 1 ? '0' : '') + s[2..-1]
  else
    s
  end
end

#parse_int_or_hex(s) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/web3ethereum/abi/utils.rb', line 197

def parse_int_or_hex(s)
  if s.is_a?(Numeric)
    s
  elsif s[0,2] == '0x'
    big_endian_to_int decode_hex(normalize_hex_without_prefix(s))
  else
    s.to_i
  end
end

#ripemd160(x) ⇒ Object



39
40
41
# File 'lib/web3ethereum/abi/utils.rb', line 39

def ripemd160(x)
  Digest::RMD160.digest x
end

#rpad(x, symbol, l) ⇒ Object



103
104
105
106
# File 'lib/web3ethereum/abi/utils.rb', line 103

def rpad(x, symbol, l)
  return x if x.size >= l
  x + symbol * (l - x.size)
end

#sha256(x) ⇒ Object



31
32
33
# File 'lib/web3ethereum/abi/utils.rb', line 31

def sha256(x)
  SHA3::Digest.digest x
end

#signature_hash(signature, length = 64) ⇒ Object



219
220
221
# File 'lib/web3ethereum/abi/utils.rb', line 219

def signature_hash signature, length=64
  encode_hex(keccak256(signature))[0...length]
end

#to_signed(i) ⇒ Object



59
60
61
# File 'lib/web3ethereum/abi/utils.rb', line 59

def to_signed(i)
  i > Constant::INT_MAX ? (i-Constant::TT256) : i
end

#zpad(x, l) ⇒ Object



108
109
110
# File 'lib/web3ethereum/abi/utils.rb', line 108

def zpad(x, l)
  lpad x, BYTE_ZERO, l
end

#zpad_hex(s, l = 32) ⇒ Object



120
121
122
# File 'lib/web3ethereum/abi/utils.rb', line 120

def zpad_hex(s, l=32)
  zpad decode_hex(s), l
end

#zpad_int(n, l = 32) ⇒ Object



116
117
118
# File 'lib/web3ethereum/abi/utils.rb', line 116

def zpad_int(n, l=32)
  zpad encode_int(n), l
end

#zunpad(x) ⇒ Object



112
113
114
# File 'lib/web3ethereum/abi/utils.rb', line 112

def zunpad(x)
  x.sub /\A\x00+/, ''
end