Module: Ethereum::Base::Utils

Defined in:
lib/ethereum/base/utils.rb

Instance Method Summary collapse

Instance Method Details

#big_endian_to_int(s) ⇒ Object



36
37
38
# File 'lib/ethereum/base/utils.rb', line 36

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

#bytearray_to_int(arr) ⇒ Object



116
117
118
119
120
# File 'lib/ethereum/base/utils.rb', line 116

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

#bytes_to_int_array(bytes) ⇒ Object



126
127
128
# File 'lib/ethereum/base/utils.rb', line 126

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

#ceil32(x) ⇒ Object



82
83
84
# File 'lib/ethereum/base/utils.rb', line 82

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

#coerce_addr_to_hex(x) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/ethereum/base/utils.rb', line 150

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



140
141
142
143
144
145
146
147
148
# File 'lib/ethereum/base/utils.rb', line 140

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



130
131
132
133
134
135
136
137
138
# File 'lib/ethereum/base/utils.rb', line 130

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



24
25
26
# File 'lib/ethereum/base/utils.rb', line 24

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

#decode_int(v) ⇒ Object

Raises:

  • (ArgumentError)


111
112
113
114
# File 'lib/ethereum/base/utils.rb', line 111

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

#decode_rlp(s) ⇒ Object



32
33
34
# File 'lib/ethereum/base/utils.rb', line 32

def decode_rlp(s)
  RLP.decode s
end

#encode_hex(b) ⇒ Object



20
21
22
# File 'lib/ethereum/base/utils.rb', line 20

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

#encode_int(n) ⇒ Object

Raises:

  • (ArgumentError)


106
107
108
109
# File 'lib/ethereum/base/utils.rb', line 106

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

#encode_rlp(b) ⇒ Object



28
29
30
# File 'lib/ethereum/base/utils.rb', line 28

def encode_rlp(b)
  RLP.encode b
end

#hash160(x) ⇒ Object



70
71
72
# File 'lib/ethereum/base/utils.rb', line 70

def hash160(x)
  ripemd160 sha256(x)
end

#hash160_hex(x) ⇒ Object



74
75
76
# File 'lib/ethereum/base/utils.rb', line 74

def hash160_hex(x)
  encode_hex hash160(x)
end

#int_array_to_bytes(arr) ⇒ Object



122
123
124
# File 'lib/ethereum/base/utils.rb', line 122

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

#int_to_addr(x) ⇒ Object



102
103
104
# File 'lib/ethereum/base/utils.rb', line 102

def int_to_addr(x)
  zpad_int x, 20
end

#int_to_big_endian(n) ⇒ Object



40
41
42
# File 'lib/ethereum/base/utils.rb', line 40

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

#keccak256(x) ⇒ Object



8
9
10
# File 'lib/ethereum/base/utils.rb', line 8

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

#keccak256_rlp(x) ⇒ Object



16
17
18
# File 'lib/ethereum/base/utils.rb', line 16

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

#keccak512(x) ⇒ Object



12
13
14
# File 'lib/ethereum/base/utils.rb', line 12

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

#normalize_hex_without_prefix(s) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/ethereum/base/utils.rb', line 58

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



48
49
50
51
52
53
54
55
56
# File 'lib/ethereum/base/utils.rb', line 48

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

#remove_0x_head(s) ⇒ Object



44
45
46
# File 'lib/ethereum/base/utils.rb', line 44

def remove_0x_head(s)
  s[0,2] == '0x' ? s[2..-1] : s
end

#ripemd160(x) ⇒ Object



66
67
68
# File 'lib/ethereum/base/utils.rb', line 66

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

#sha256(x) ⇒ Object



4
5
6
# File 'lib/ethereum/base/utils.rb', line 4

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

#to_signed(i) ⇒ Object



78
79
80
# File 'lib/ethereum/base/utils.rb', line 78

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

#zpad(x, l) ⇒ Object



86
87
88
# File 'lib/ethereum/base/utils.rb', line 86

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

#zpad_hex(s, l = 32) ⇒ Object



98
99
100
# File 'lib/ethereum/base/utils.rb', line 98

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

#zpad_int(n, l = 32) ⇒ Object



94
95
96
# File 'lib/ethereum/base/utils.rb', line 94

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

#zunpad(x) ⇒ Object



90
91
92
# File 'lib/ethereum/base/utils.rb', line 90

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