abicoder 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,81 +0,0 @@
1
- module ABI
2
- module Helpers
3
-
4
- ########################
5
- # encoding / decoding helpers
6
-
7
- def lpad(x, symbol, l)
8
- return x if x.size >= l
9
- symbol * (l - x.size) + x
10
- end
11
-
12
- def rpad(x, symbol, l)
13
- return x if x.size >= l
14
- x + symbol * (l - x.size)
15
- end
16
-
17
- def zpad(x, l)
18
- lpad( x, BYTE_ZERO, l )
19
- end
20
-
21
- def zpad_int(n, l=32)
22
- zpad( encode_int(n), l )
23
- end
24
-
25
- def zpad_hex(s, l=32)
26
- zpad( decode_hex(s), l )
27
- end
28
-
29
-
30
-
31
- def big_endian_to_int(s)
32
- s = s.sub( /\A(\x00)+/, '' ) ## keep "performance" shortcut - why? why not?
33
- ### todo/check - allow nil - why? why not?
34
- ## raise DeserializationError, "Invalid serialization (not minimal length)" if !@size && serial.size > 0 && serial[0] == BYTE_ZERO
35
- s = s || BYTE_ZERO
36
- s.unpack("H*").first.to_i(16)
37
- end
38
-
39
-
40
- def int_to_big_endian(n)
41
- if n == 0
42
- BYTE_EMPTY
43
- else
44
- hex = n.to_s(16)
45
- hex = "0#{hex}" if hex.size.odd?
46
-
47
- [hex].pack("H*") ## note Util.hex_to_bin() "inline" shortcut
48
- end
49
- end
50
-
51
-
52
- def encode_int(n)
53
- raise ArgumentError, "Integer invalid or out of range: #{n}" unless n.is_a?(Integer) && n >= 0 && n <= UINT_MAX
54
- int_to_big_endian( n )
55
- end
56
-
57
-
58
- def encode_hex(b)
59
- raise TypeError, "Value must be an instance of String" unless b.instance_of?(String)
60
- b.unpack("H*").first
61
- end
62
-
63
- def decode_hex(str)
64
- raise TypeError, "Value must be an instance of string" unless str.instance_of?(String)
65
- raise TypeError, 'Non-hexadecimal digit found' unless str =~ /\A[0-9a-fA-F]*\z/
66
- [str].pack("H*")
67
- end
68
-
69
- def ceil32(x)
70
- x % 32 == 0 ? x : (x + 32 - x%32)
71
- end
72
- end # module Helpers
73
-
74
-
75
- module Utils
76
- extend Helpers
77
- ## e.g. Utils.encode_hex( ) etc.
78
- end
79
-
80
- end # module ABI
81
-