9af5fe077b291fb9be9cf93fb633358458c62523
[electrum-nvc.git] / lib / deserialize.py
1 # this code comes from ABE. it can probably be simplified
2 #
3 #
4
5 from bitcoin import public_key_to_bc_address, hash_160_to_bc_address, hash_encode, hash_160
6 from util import print_error
7 #import socket
8 import time
9 import struct
10
11 #
12 # Workalike python implementation of Bitcoin's CDataStream class.
13 #
14 import struct
15 import StringIO
16 import mmap
17
18 class SerializationError(Exception):
19     """ Thrown when there's a problem deserializing or serializing """
20
21 class BCDataStream(object):
22     def __init__(self):
23         self.input = None
24         self.read_cursor = 0
25
26     def clear(self):
27         self.input = None
28         self.read_cursor = 0
29
30     def write(self, bytes):  # Initialize with string of bytes
31         if self.input is None:
32             self.input = bytes
33         else:
34             self.input += bytes
35
36     def map_file(self, file, start):  # Initialize with bytes from file
37         self.input = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)
38         self.read_cursor = start
39
40     def seek_file(self, position):
41         self.read_cursor = position
42         
43     def close_file(self):
44         self.input.close()
45
46     def read_string(self):
47         # Strings are encoded depending on length:
48         # 0 to 252 :  1-byte-length followed by bytes (if any)
49         # 253 to 65,535 : byte'253' 2-byte-length followed by bytes
50         # 65,536 to 4,294,967,295 : byte '254' 4-byte-length followed by bytes
51         # ... and the Bitcoin client is coded to understand:
52         # greater than 4,294,967,295 : byte '255' 8-byte-length followed by bytes of string
53         # ... but I don't think it actually handles any strings that big.
54         if self.input is None:
55             raise SerializationError("call write(bytes) before trying to deserialize")
56
57         try:
58             length = self.read_compact_size()
59         except IndexError:
60             raise SerializationError("attempt to read past end of buffer")
61
62         return self.read_bytes(length)
63
64     def write_string(self, string):
65         # Length-encoded as with read-string
66         self.write_compact_size(len(string))
67         self.write(string)
68
69     def read_bytes(self, length):
70         try:
71             result = self.input[self.read_cursor:self.read_cursor+length]
72             self.read_cursor += length
73             return result
74         except IndexError:
75             raise SerializationError("attempt to read past end of buffer")
76
77         return ''
78
79     def read_boolean(self): return self.read_bytes(1)[0] != chr(0)
80     def read_int16(self): return self._read_num('<h')
81     def read_uint16(self): return self._read_num('<H')
82     def read_int32(self): return self._read_num('<i')
83     def read_uint32(self): return self._read_num('<I')
84     def read_int64(self): return self._read_num('<q')
85     def read_uint64(self): return self._read_num('<Q')
86
87     def write_boolean(self, val): return self.write(chr(1) if val else chr(0))
88     def write_int16(self, val): return self._write_num('<h', val)
89     def write_uint16(self, val): return self._write_num('<H', val)
90     def write_int32(self, val): return self._write_num('<i', val)
91     def write_uint32(self, val): return self._write_num('<I', val)
92     def write_int64(self, val): return self._write_num('<q', val)
93     def write_uint64(self, val): return self._write_num('<Q', val)
94
95     def read_compact_size(self):
96         size = ord(self.input[self.read_cursor])
97         self.read_cursor += 1
98         if size == 253:
99             size = self._read_num('<H')
100         elif size == 254:
101             size = self._read_num('<I')
102         elif size == 255:
103             size = self._read_num('<Q')
104         return size
105
106     def write_compact_size(self, size):
107         if size < 0:
108             raise SerializationError("attempt to write size < 0")
109         elif size < 253:
110             self.write(chr(size))
111         elif size < 2**16:
112             self.write('\xfd')
113             self._write_num('<H', size)
114         elif size < 2**32:
115             self.write('\xfe')
116             self._write_num('<I', size)
117         elif size < 2**64:
118             self.write('\xff')
119             self._write_num('<Q', size)
120
121     def _read_num(self, format):
122         (i,) = struct.unpack_from(format, self.input, self.read_cursor)
123         self.read_cursor += struct.calcsize(format)
124         return i
125
126     def _write_num(self, format, num):
127         s = struct.pack(format, num)
128         self.write(s)
129
130 #
131 # enum-like type
132 # From the Python Cookbook, downloaded from http://code.activestate.com/recipes/67107/
133 #
134 import types, string, exceptions
135
136 class EnumException(exceptions.Exception):
137     pass
138
139 class Enumeration:
140     def __init__(self, name, enumList):
141         self.__doc__ = name
142         lookup = { }
143         reverseLookup = { }
144         i = 0
145         uniqueNames = [ ]
146         uniqueValues = [ ]
147         for x in enumList:
148             if type(x) == types.TupleType:
149                 x, i = x
150             if type(x) != types.StringType:
151                 raise EnumException, "enum name is not a string: " + x
152             if type(i) != types.IntType:
153                 raise EnumException, "enum value is not an integer: " + i
154             if x in uniqueNames:
155                 raise EnumException, "enum name is not unique: " + x
156             if i in uniqueValues:
157                 raise EnumException, "enum value is not unique for " + x
158             uniqueNames.append(x)
159             uniqueValues.append(i)
160             lookup[x] = i
161             reverseLookup[i] = x
162             i = i + 1
163         self.lookup = lookup
164         self.reverseLookup = reverseLookup
165     def __getattr__(self, attr):
166         if not self.lookup.has_key(attr):
167             raise AttributeError
168         return self.lookup[attr]
169     def whatis(self, value):
170         return self.reverseLookup[value]
171
172
173 # This function comes from bitcointools, bct-LICENSE.txt.
174 def long_hex(bytes):
175     return bytes.encode('hex_codec')
176
177 # This function comes from bitcointools, bct-LICENSE.txt.
178 def short_hex(bytes):
179     t = bytes.encode('hex_codec')
180     if len(t) < 11:
181         return t
182     return t[0:4]+"..."+t[-4:]
183
184
185
186 def parse_TxIn(vds):
187     d = {}
188     d['prevout_hash'] = hash_encode(vds.read_bytes(32))
189     d['prevout_n'] = vds.read_uint32()
190     scriptSig = vds.read_bytes(vds.read_compact_size())
191     d['sequence'] = vds.read_uint32()
192
193     if scriptSig:
194         pubkeys, signatures, address = get_address_from_input_script(scriptSig)
195     else:
196         pubkeys = []
197         signatures = []
198         address = None
199     
200     d['address'] = address
201     d['signatures'] = signatures
202
203     return d
204
205
206 def parse_TxOut(vds, i):
207     d = {}
208     d['value'] = vds.read_int64()
209     scriptPubKey = vds.read_bytes(vds.read_compact_size())
210     d['address'] = get_address_from_output_script(scriptPubKey)
211     d['raw_output_script'] = scriptPubKey.encode('hex')
212     d['index'] = i
213     return d
214
215
216 def parse_Transaction(vds):
217     d = {}
218     start = vds.read_cursor
219     d['version'] = vds.read_int32()
220     n_vin = vds.read_compact_size()
221     d['inputs'] = []
222     for i in xrange(n_vin):
223         d['inputs'].append(parse_TxIn(vds))
224     n_vout = vds.read_compact_size()
225     d['outputs'] = []
226     for i in xrange(n_vout):
227         d['outputs'].append(parse_TxOut(vds, i))
228     d['lockTime'] = vds.read_uint32()
229     return d
230
231 def parse_redeemScript(bytes):
232     dec = [ x for x in script_GetOp(bytes.decode('hex')) ]
233
234     # 2 of 2
235     match = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
236     if match_decoded(dec, match):
237         pubkeys = [ dec[1][1].encode('hex'), dec[2][1].encode('hex') ]
238         return 2, pubkeys
239
240     # 2 of 3
241     match = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
242     if match_decoded(dec, match):
243         pubkeys = [ dec[1][1].encode('hex'), dec[2][1].encode('hex'), dec[3][1].encode('hex') ]
244         return 2, pubkeys
245
246
247
248 opcodes = Enumeration("Opcodes", [
249     ("OP_0", 0), ("OP_PUSHDATA1",76), "OP_PUSHDATA2", "OP_PUSHDATA4", "OP_1NEGATE", "OP_RESERVED",
250     "OP_1", "OP_2", "OP_3", "OP_4", "OP_5", "OP_6", "OP_7",
251     "OP_8", "OP_9", "OP_10", "OP_11", "OP_12", "OP_13", "OP_14", "OP_15", "OP_16",
252     "OP_NOP", "OP_VER", "OP_IF", "OP_NOTIF", "OP_VERIF", "OP_VERNOTIF", "OP_ELSE", "OP_ENDIF", "OP_VERIFY",
253     "OP_RETURN", "OP_TOALTSTACK", "OP_FROMALTSTACK", "OP_2DROP", "OP_2DUP", "OP_3DUP", "OP_2OVER", "OP_2ROT", "OP_2SWAP",
254     "OP_IFDUP", "OP_DEPTH", "OP_DROP", "OP_DUP", "OP_NIP", "OP_OVER", "OP_PICK", "OP_ROLL", "OP_ROT",
255     "OP_SWAP", "OP_TUCK", "OP_CAT", "OP_SUBSTR", "OP_LEFT", "OP_RIGHT", "OP_SIZE", "OP_INVERT", "OP_AND",
256     "OP_OR", "OP_XOR", "OP_EQUAL", "OP_EQUALVERIFY", "OP_RESERVED1", "OP_RESERVED2", "OP_1ADD", "OP_1SUB", "OP_2MUL",
257     "OP_2DIV", "OP_NEGATE", "OP_ABS", "OP_NOT", "OP_0NOTEQUAL", "OP_ADD", "OP_SUB", "OP_MUL", "OP_DIV",
258     "OP_MOD", "OP_LSHIFT", "OP_RSHIFT", "OP_BOOLAND", "OP_BOOLOR",
259     "OP_NUMEQUAL", "OP_NUMEQUALVERIFY", "OP_NUMNOTEQUAL", "OP_LESSTHAN",
260     "OP_GREATERTHAN", "OP_LESSTHANOREQUAL", "OP_GREATERTHANOREQUAL", "OP_MIN", "OP_MAX",
261     "OP_WITHIN", "OP_RIPEMD160", "OP_SHA1", "OP_SHA256", "OP_HASH160",
262     "OP_HASH256", "OP_CODESEPARATOR", "OP_CHECKSIG", "OP_CHECKSIGVERIFY", "OP_CHECKMULTISIG",
263     "OP_CHECKMULTISIGVERIFY",
264     ("OP_SINGLEBYTE_END", 0xF0),
265     ("OP_DOUBLEBYTE_BEGIN", 0xF000),
266     "OP_PUBKEY", "OP_PUBKEYHASH",
267     ("OP_INVALIDOPCODE", 0xFFFF),
268 ])
269
270
271 def script_GetOp(bytes):
272     i = 0
273     while i < len(bytes):
274         vch = None
275         opcode = ord(bytes[i])
276         i += 1
277         if opcode >= opcodes.OP_SINGLEBYTE_END:
278             opcode <<= 8
279             opcode |= ord(bytes[i])
280             i += 1
281
282         if opcode <= opcodes.OP_PUSHDATA4:
283             nSize = opcode
284             if opcode == opcodes.OP_PUSHDATA1:
285                 nSize = ord(bytes[i])
286                 i += 1
287             elif opcode == opcodes.OP_PUSHDATA2:
288                 (nSize,) = struct.unpack_from('<H', bytes, i)
289                 i += 2
290             elif opcode == opcodes.OP_PUSHDATA4:
291                 (nSize,) = struct.unpack_from('<I', bytes, i)
292                 i += 4
293             vch = bytes[i:i+nSize]
294             i += nSize
295
296         yield (opcode, vch, i)
297
298
299 def script_GetOpName(opcode):
300     return (opcodes.whatis(opcode)).replace("OP_", "")
301
302
303 def decode_script(bytes):
304     result = ''
305     for (opcode, vch, i) in script_GetOp(bytes):
306         if len(result) > 0: result += " "
307         if opcode <= opcodes.OP_PUSHDATA4:
308             result += "%d:"%(opcode,)
309             result += short_hex(vch)
310         else:
311             result += script_GetOpName(opcode)
312     return result
313
314
315 def match_decoded(decoded, to_match):
316     if len(decoded) != len(to_match):
317         return False;
318     for i in range(len(decoded)):
319         if to_match[i] == opcodes.OP_PUSHDATA4 and decoded[i][0] <= opcodes.OP_PUSHDATA4 and decoded[i][0]>0:
320             continue  # Opcodes below OP_PUSHDATA4 all just push data onto stack, and are equivalent.
321         if to_match[i] != decoded[i][0]:
322             return False
323     return True
324
325 def get_address_from_input_script(bytes):
326     try:
327         decoded = [ x for x in script_GetOp(bytes) ]
328     except:
329         # coinbase transactions raise an exception
330         print_error("cannot find address in input script", bytes.encode('hex'))
331         return [], [], "(None)"
332
333     # non-generated TxIn transactions push a signature
334     # (seventy-something bytes) and then their public key
335     # (65 bytes) onto the stack:
336     match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ]
337     if match_decoded(decoded, match):
338         return None, None, public_key_to_bc_address(decoded[1][1])
339
340     # p2sh transaction, 2 of n
341     match = [ opcodes.OP_0 ]
342     while len(match) < len(decoded):
343         match.append(opcodes.OP_PUSHDATA4)
344
345     if match_decoded(decoded, match):
346
347         redeemScript = decoded[-1][1]
348         num = len(match) - 2
349         signatures = map(lambda x:x[1][:-1].encode('hex'), decoded[1:-1])
350
351         dec2 = [ x for x in script_GetOp(redeemScript) ]
352
353         # 2 of 2
354         match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
355         if match_decoded(dec2, match2):
356             pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex') ]
357             return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5)
358  
359         # 2 of 3
360         match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
361         if match_decoded(dec2, match2):
362             pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex') ]
363             return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5)
364
365     print_error("cannot find address in input script", bytes.encode('hex'))
366     return [], [], "(None)"
367
368
369
370 def get_address_from_output_script(bytes):
371     decoded = [ x for x in script_GetOp(bytes) ]
372
373     # The Genesis Block, self-payments, and pay-by-IP-address payments look like:
374     # 65 BYTES:... CHECKSIG
375     match = [ opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG ]
376     if match_decoded(decoded, match):
377         return public_key_to_bc_address(decoded[0][1])
378
379     # Pay-by-Bitcoin-address TxOuts look like:
380     # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
381     match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
382     if match_decoded(decoded, match):
383         return hash_160_to_bc_address(decoded[2][1])
384
385     # p2sh
386     match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ]
387     if match_decoded(decoded, match):
388         return hash_160_to_bc_address(decoded[1][1],5)
389
390     return "(None)"
391
392