18
18
19
19
# Py3 compatibility
20
20
import sys
21
- bchr = chr
22
- bord = ord
21
+
23
22
if sys .version > '3' :
24
23
bchr = lambda x : bytes ([x ])
25
24
bord = lambda x : x [0 ]
26
25
from io import BytesIO
27
26
else :
27
+ bchr = chr
28
+ bord = ord
28
29
from cStringIO import StringIO as BytesIO
29
30
30
31
MAX_SIZE = 0x02000000
31
32
33
+
32
34
class SerializationError (Exception ):
33
35
"""Base class for serialization errors"""
34
36
37
+
35
38
class SerializationTruncationError (Exception ):
36
39
"""Serialized data was truncated
37
40
38
41
Thrown by deserialize() and stream_deserialize()
39
42
"""
40
43
44
+
41
45
def ser_read (f , n ):
42
46
"""Read from a stream safely
43
47
@@ -49,9 +53,17 @@ def ser_read(f, n):
49
53
raise SerializationError ('Asked to read 0x%x bytes; MAX_SIZE exceeded' )
50
54
r = f .read (n )
51
55
if len (r ) < n :
52
- raise SerializationTruncationError ()
56
+ raise SerializationTruncationError ('Asked to read %i bytes, but only got %i' % ( n , len ( r )) )
53
57
return r
54
58
59
+
60
+ def num_null_bytes (num ):
61
+ res = b''
62
+ for i in range (num ):
63
+ res += b'\x00 '
64
+ return res
65
+
66
+
55
67
class Serializable (object ):
56
68
"""Base class for serializable objects"""
57
69
def stream_serialize (self , f ):
@@ -86,6 +98,7 @@ def __ne__(self, other):
86
98
def __hash__ (self ):
87
99
return hash (self .serialize ())
88
100
101
+
89
102
class Serializer (object ):
90
103
"""Base class for object serializers"""
91
104
def __new__ (cls ):
@@ -108,6 +121,7 @@ def serialize(cls, obj):
108
121
def deserialize (cls , buf ):
109
122
return cls .stream_deserialize (BytesIO (buf ))
110
123
124
+
111
125
class VarIntSerializer (Serializer ):
112
126
"""Serialization of variable length ints"""
113
127
@classmethod
@@ -138,6 +152,7 @@ def stream_deserialize(cls, f):
138
152
else :
139
153
return struct .unpack (b'<Q' , ser_read (f , 8 ))[0 ]
140
154
155
+
141
156
class BytesSerializer (Serializer ):
142
157
"""Serialization of bytes instances"""
143
158
@classmethod
@@ -150,6 +165,7 @@ def stream_deserialize(cls, f):
150
165
l = VarIntSerializer .stream_deserialize (f )
151
166
return ser_read (f , l )
152
167
168
+
153
169
class VectorSerializer (Serializer ):
154
170
"""Base class for serializers of object vectors"""
155
171
@classmethod
@@ -166,23 +182,55 @@ def stream_deserialize(cls, inner_cls, f):
166
182
r .append (inner_cls .stream_deserialize (f ))
167
183
return r
168
184
185
+
169
186
class uint256VectorSerializer (Serializer ):
170
187
"""Serialize vectors of uint256"""
171
188
@classmethod
172
- def stream_serialize (cls , inner_cls , objs , f ):
173
- VarIntSerializer .stream_serialize (len (objs ), f )
174
- for obj in objs :
175
- assert len (obj ) == 32
176
- f .write (obj )
189
+ def stream_serialize (cls , uints , f ):
190
+ VarIntSerializer .stream_serialize (len (uints ), f )
191
+ for uint in uints :
192
+ assert len (uint ) == 32
193
+ f .write (uint )
177
194
178
195
@classmethod
179
- def stream_deserialize (cls , inner_cls , f ):
196
+ def stream_deserialize (cls , f ):
180
197
n = VarIntSerializer .stream_deserialize (f )
181
198
r = []
182
199
for i in range (n ):
183
200
r .append (ser_read (f , 32 ))
184
201
return r
185
202
203
+
204
+ class intVectorSerialzer (Serializer ):
205
+ @classmethod
206
+ def stream_serialize (cls , ints , f ):
207
+ l = len (ints )
208
+ VarIntSerializer .stream_serialize (l , f )
209
+ for i in ints :
210
+ f .write (struct .pack (b"<i" , i ))
211
+
212
+ @classmethod
213
+ def stream_deserialize (cls , f ):
214
+ l = VarIntSerializer .stream_deserialize (f )
215
+ ints = []
216
+ for i in range (l ):
217
+ ints .append (struct .unpack (b"<i" , ser_read (f , 4 )))
218
+
219
+
220
+ class VarStringSerializer (Serializer ):
221
+ """Serialize variable length strings"""
222
+ @classmethod
223
+ def stream_serialize (cls , s , f ):
224
+ l = len (s )
225
+ VarIntSerializer .stream_serialize (l , f )
226
+ f .write (s )
227
+
228
+ @classmethod
229
+ def stream_deserialize (cls , f ):
230
+ l = VarIntSerializer .stream_deserialize (f )
231
+ return ser_read (f , l )
232
+
233
+
186
234
def uint256_from_str (s ):
187
235
"""Convert bytes to uint256"""
188
236
r = 0
@@ -191,6 +239,7 @@ def uint256_from_str(s):
191
239
r += t [i ] << (i * 32 )
192
240
return r
193
241
242
+
194
243
def uint256_from_compact (c ):
195
244
"""Convert compact encoding to uint256
196
245
@@ -200,44 +249,17 @@ def uint256_from_compact(c):
200
249
v = (c & 0xFFFFFF ) << (8 * (nbytes - 3 ))
201
250
return v
202
251
252
+
203
253
def uint256_to_shortstr (u ):
204
254
s = "%064x" % (u ,)
205
255
return s [:16 ]
206
256
207
- def deser_int_vector (f ):
208
- """Deserialize a vector of ints"""
209
- nit = struct .unpack (b"<B" , f .read (1 ))[0 ]
210
- if nit == 253 :
211
- nit = struct .unpack (b"<H" , f .read (2 ))[0 ]
212
- elif nit == 254 :
213
- nit = struct .unpack (b"<I" , f .read (4 ))[0 ]
214
- elif nit == 255 :
215
- nit = struct .unpack (b"<Q" , f .read (8 ))[0 ]
216
- r = []
217
- for i in range (nit ):
218
- t = struct .unpack (b"<i" , f .read (4 ))[0 ]
219
- r .append (t )
220
- return r
221
-
222
- def ser_int_vector (l ):
223
- """Serialize a vector of ints"""
224
- r = b""
225
- if len (l ) < 253 :
226
- r = bchr (len (l ))
227
- elif len (s ) < 0x10000 :
228
- r = bchr (253 ) + struct .pack (b"<H" , len (l ))
229
- elif len (s ) < 0x100000000 :
230
- r = bchr (254 ) + struct .pack (b"<I" , len (l ))
231
- else :
232
- r = bchr (255 ) + struct .pack (b"<Q" , len (l ))
233
- for i in l :
234
- r += struct .pack (b"<i" , i )
235
- return r
236
257
237
258
def Hash (msg ):
238
259
"""SHA256^2)(msg) -> bytes"""
239
260
return hashlib .sha256 (hashlib .sha256 (msg ).digest ()).digest ()
240
261
262
+
241
263
def Hash160 (msg ):
242
264
"""RIPEME160(SHA256(msg)) -> bytes"""
243
265
h = hashlib .new ('ripemd160' )
0 commit comments