Module: Ethlite::UtilHelper
- Included in:
- Utils
- Defined in:
- lib/ethlite/utils.rb
Instance Method Summary collapse
- #big_endian_to_int(s) ⇒ Object
- #bytearray_to_int(arr) ⇒ Object
- #bytes_to_int_array(bytes) ⇒ Object
- #ceil32(x) ⇒ Object
- #coerce_addr_to_hex(x) ⇒ Object
- #coerce_to_bytes(x) ⇒ Object
- #coerce_to_int(x) ⇒ Object
- #decode_hex(str) ⇒ Object
- #decode_int(v) ⇒ Object
- #double_sha256(x) ⇒ Object
- #encode_hex(b) ⇒ Object
- #encode_int(n) ⇒ Object
- #hash160(x) ⇒ Object
- #hash160_hex(x) ⇒ Object
- #int_array_to_bytes(arr) ⇒ Object
- #int_to_addr(x) ⇒ Object
- #int_to_big_endian(n) ⇒ Object
-
#keccak256(x) ⇒ Object
Not the keccak in sha3, although it’s underlying lib named SHA3.
- #keccak256_rlp(x) ⇒ Object
-
#keccak512(x) ⇒ Object
todo/check where required / in use - what for?.
- #lpad(x, symbol, l) ⇒ Object
- #mod_exp(x, y, n) ⇒ Object
- #mod_mul(x, y, n) ⇒ Object
- #normalize_hex_without_prefix(s) ⇒ Object
- #parse_int_or_hex(s) ⇒ Object
- #remove_0x_head(s) ⇒ Object
- #ripemd160(x) ⇒ Object
- #rpad(x, symbol, l) ⇒ Object
- #sha256(x) ⇒ Object
- #signature_hash(signature, length = 8) ⇒ Object
- #to_signed(i) ⇒ Object
- #zpad(x, l) ⇒ Object
- #zpad_hex(s, l = 32) ⇒ Object
- #zpad_int(n, l = 32) ⇒ Object
- #zunpad(x) ⇒ Object
Instance Method Details
#big_endian_to_int(s) ⇒ Object
81 82 83 |
# File 'lib/ethlite/utils.rb', line 81 def big_endian_to_int(s) RLP::Sedes.big_endian_int.deserialize s.sub(/\A(\x00)+/, '') end |
#bytearray_to_int(arr) ⇒ Object
130 131 132 133 134 |
# File 'lib/ethlite/utils.rb', line 130 def bytearray_to_int(arr) o = 0 arr.each {|x| o = (o << 8) + x } o end |
#bytes_to_int_array(bytes) ⇒ Object
140 141 142 |
# File 'lib/ethlite/utils.rb', line 140 def bytes_to_int_array(bytes) bytes.unpack('C*') end |
#ceil32(x) ⇒ Object
59 60 61 |
# File 'lib/ethlite/utils.rb', line 59 def ceil32(x) x % 32 == 0 ? x : (x + 32 - x%32) end |
#coerce_addr_to_hex(x) ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/ethlite/utils.rb', line 165 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
155 156 157 158 159 160 161 162 163 |
# File 'lib/ethlite/utils.rb', line 155 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
145 146 147 148 149 150 151 152 153 |
# File 'lib/ethlite/utils.rb', line 145 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(str) ⇒ Object
68 69 70 71 72 |
# File 'lib/ethlite/utils.rb', line 68 def decode_hex(str) raise TypeError, "Value must be an instance of string" unless str.instance_of?(String) raise TypeError, 'Non-hexadecimal digit found' unless str =~ /\A[0-9a-fA-F]*\z/ [str].pack("H*") end |
#decode_int(v) ⇒ Object
91 92 93 94 |
# File 'lib/ethlite/utils.rb', line 91 def decode_int(v) raise ArgumentError, "No leading zero bytes allowed for integers" if v.size > 0 && (v[0] == BYTE_ZERO || v[0] == 0) big_endian_to_int v end |
#double_sha256(x) ⇒ Object
30 31 32 |
# File 'lib/ethlite/utils.rb', line 30 def double_sha256(x) sha256 sha256(x) end |
#encode_hex(b) ⇒ Object
63 64 65 66 |
# File 'lib/ethlite/utils.rb', line 63 def encode_hex(b) raise TypeError, "Value must be an instance of String" unless b.instance_of?(String) b.unpack("H*").first end |
#encode_int(n) ⇒ Object
86 87 88 89 |
# File 'lib/ethlite/utils.rb', line 86 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 |
#hash160(x) ⇒ Object
38 39 40 |
# File 'lib/ethlite/utils.rb', line 38 def hash160(x) ripemd160 sha256(x) end |
#hash160_hex(x) ⇒ Object
42 43 44 |
# File 'lib/ethlite/utils.rb', line 42 def hash160_hex(x) encode_hex hash160(x) end |
#int_array_to_bytes(arr) ⇒ Object
136 137 138 |
# File 'lib/ethlite/utils.rb', line 136 def int_array_to_bytes(arr) arr.pack('C*') end |
#int_to_addr(x) ⇒ Object
125 126 127 |
# File 'lib/ethlite/utils.rb', line 125 def int_to_addr(x) zpad_int x, 20 end |
#int_to_big_endian(n) ⇒ Object
77 78 79 |
# File 'lib/ethlite/utils.rb', line 77 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
8 9 10 11 12 |
# File 'lib/ethlite/utils.rb', line 8 def keccak256(x) # Digest::SHA3.new(256).digest(x) ## Digest::Keccak.digest(x, 256) Digest::KeccakLite.new(256).digest( x ) end |
#keccak256_rlp(x) ⇒ Object
22 23 24 |
# File 'lib/ethlite/utils.rb', line 22 def keccak256_rlp(x) keccak256 RLP.encode(x) end |
#keccak512(x) ⇒ Object
todo/check where required / in use - what for?
15 16 17 18 19 |
# File 'lib/ethlite/utils.rb', line 15 def keccak512(x) # Digest::SHA3.new(512).digest(x) # Digest::Keccak.digest(x, 512) Digest::KeccakLite.new(512).digest( x ) end |
#lpad(x, symbol, l) ⇒ Object
99 100 101 102 |
# File 'lib/ethlite/utils.rb', line 99 def lpad(x, symbol, l) return x if x.size >= l symbol * (l - x.size) + x end |
#mod_exp(x, y, n) ⇒ Object
46 47 48 |
# File 'lib/ethlite/utils.rb', line 46 def mod_exp(x, y, n) x.to_bn.mod_exp(y, n).to_i end |
#mod_mul(x, y, n) ⇒ Object
50 51 52 |
# File 'lib/ethlite/utils.rb', line 50 def mod_mul(x, y, n) x.to_bn.mod_mul(y, n).to_i end |
#normalize_hex_without_prefix(s) ⇒ Object
205 206 207 208 209 210 211 |
# File 'lib/ethlite/utils.rb', line 205 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
177 178 179 180 181 182 183 184 185 |
# File 'lib/ethlite/utils.rb', line 177 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
200 201 202 203 |
# File 'lib/ethlite/utils.rb', line 200 def remove_0x_head( s ) return s if !s || s.length<2 s[0,2] == '0x' ? s[2..-1] : s end |
#ripemd160(x) ⇒ Object
34 35 36 |
# File 'lib/ethlite/utils.rb', line 34 def ripemd160(x) Digest::RMD160.digest x end |
#rpad(x, symbol, l) ⇒ Object
104 105 106 107 |
# File 'lib/ethlite/utils.rb', line 104 def rpad(x, symbol, l) return x if x.size >= l x + symbol * (l - x.size) end |
#sha256(x) ⇒ Object
26 27 28 |
# File 'lib/ethlite/utils.rb', line 26 def sha256(x) Digest::SHA256.digest x end |
#signature_hash(signature, length = 8) ⇒ Object
213 214 215 |
# File 'lib/ethlite/utils.rb', line 213 def signature_hash( signature, length=8 ) encode_hex( keccak256(signature) )[0...length] end |
#to_signed(i) ⇒ Object
54 55 56 |
# File 'lib/ethlite/utils.rb', line 54 def to_signed(i) i > INT_MAX ? (i-TT256) : i end |
#zpad(x, l) ⇒ Object
109 110 111 |
# File 'lib/ethlite/utils.rb', line 109 def zpad(x, l) lpad x, BYTE_ZERO, l end |
#zpad_hex(s, l = 32) ⇒ Object
121 122 123 |
# File 'lib/ethlite/utils.rb', line 121 def zpad_hex(s, l=32) zpad decode_hex(s), l end |
#zpad_int(n, l = 32) ⇒ Object
117 118 119 |
# File 'lib/ethlite/utils.rb', line 117 def zpad_int(n, l=32) zpad encode_int(n), l end |
#zunpad(x) ⇒ Object
113 114 115 |
# File 'lib/ethlite/utils.rb', line 113 def zunpad(x) x.sub /\A\x00+/, '' end |