1 # this code comes from ABE. it can probably be simplified
5 from bitcoin import public_key_to_bc_address, hash_160_to_bc_address, hash_encode, hash_160
11 # Workalike python implementation of Bitcoin's CDataStream class.
17 class SerializationError(Exception):
18 """ Thrown when there's a problem deserializing or serializing """
20 class BCDataStream(object):
29 def write(self, bytes): # Initialize with string of bytes
30 if self.input is None:
35 def map_file(self, file, start): # Initialize with bytes from file
36 self.input = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)
37 self.read_cursor = start
39 def seek_file(self, position):
40 self.read_cursor = position
45 def read_string(self):
46 # Strings are encoded depending on length:
47 # 0 to 252 : 1-byte-length followed by bytes (if any)
48 # 253 to 65,535 : byte'253' 2-byte-length followed by bytes
49 # 65,536 to 4,294,967,295 : byte '254' 4-byte-length followed by bytes
50 # ... and the Bitcoin client is coded to understand:
51 # greater than 4,294,967,295 : byte '255' 8-byte-length followed by bytes of string
52 # ... but I don't think it actually handles any strings that big.
53 if self.input is None:
54 raise SerializationError("call write(bytes) before trying to deserialize")
57 length = self.read_compact_size()
59 raise SerializationError("attempt to read past end of buffer")
61 return self.read_bytes(length)
63 def write_string(self, string):
64 # Length-encoded as with read-string
65 self.write_compact_size(len(string))
68 def read_bytes(self, length):
70 result = self.input[self.read_cursor:self.read_cursor+length]
71 self.read_cursor += length
74 raise SerializationError("attempt to read past end of buffer")
78 def read_boolean(self): return self.read_bytes(1)[0] != chr(0)
79 def read_int16(self): return self._read_num('<h')
80 def read_uint16(self): return self._read_num('<H')
81 def read_int32(self): return self._read_num('<i')
82 def read_uint32(self): return self._read_num('<I')
83 def read_int64(self): return self._read_num('<q')
84 def read_uint64(self): return self._read_num('<Q')
86 def write_boolean(self, val): return self.write(chr(1) if val else chr(0))
87 def write_int16(self, val): return self._write_num('<h', val)
88 def write_uint16(self, val): return self._write_num('<H', val)
89 def write_int32(self, val): return self._write_num('<i', val)
90 def write_uint32(self, val): return self._write_num('<I', val)
91 def write_int64(self, val): return self._write_num('<q', val)
92 def write_uint64(self, val): return self._write_num('<Q', val)
94 def read_compact_size(self):
95 size = ord(self.input[self.read_cursor])
98 size = self._read_num('<H')
100 size = self._read_num('<I')
102 size = self._read_num('<Q')
105 def write_compact_size(self, size):
107 raise SerializationError("attempt to write size < 0")
109 self.write(chr(size))
112 self._write_num('<H', size)
115 self._write_num('<I', size)
118 self._write_num('<Q', size)
120 def _read_num(self, format):
121 (i,) = struct.unpack_from(format, self.input, self.read_cursor)
122 self.read_cursor += struct.calcsize(format)
125 def _write_num(self, format, num):
126 s = struct.pack(format, num)
131 # From the Python Cookbook, downloaded from http://code.activestate.com/recipes/67107/
133 import types, string, exceptions
135 class EnumException(exceptions.Exception):
139 def __init__(self, name, enumList):
147 if type(x) == types.TupleType:
149 if type(x) != types.StringType:
150 raise EnumException, "enum name is not a string: " + x
151 if type(i) != types.IntType:
152 raise EnumException, "enum value is not an integer: " + i
154 raise EnumException, "enum name is not unique: " + x
155 if i in uniqueValues:
156 raise EnumException, "enum value is not unique for " + x
157 uniqueNames.append(x)
158 uniqueValues.append(i)
163 self.reverseLookup = reverseLookup
164 def __getattr__(self, attr):
165 if not self.lookup.has_key(attr):
167 return self.lookup[attr]
168 def whatis(self, value):
169 return self.reverseLookup[value]
172 # This function comes from bitcointools, bct-LICENSE.txt.
174 return bytes.encode('hex_codec')
176 # This function comes from bitcointools, bct-LICENSE.txt.
177 def short_hex(bytes):
178 t = bytes.encode('hex_codec')
181 return t[0:4]+"..."+t[-4:]
187 d['prevout_hash'] = hash_encode(vds.read_bytes(32))
188 d['prevout_n'] = vds.read_uint32()
189 scriptSig = vds.read_bytes(vds.read_compact_size())
190 d['sequence'] = vds.read_uint32()
193 pubkeys, signatures, address = get_address_from_input_script(scriptSig)
199 d['address'] = address
200 d['signatures'] = signatures
205 def parse_TxOut(vds, i):
207 d['value'] = vds.read_int64()
208 scriptPubKey = vds.read_bytes(vds.read_compact_size())
209 d['address'] = get_address_from_output_script(scriptPubKey)
210 d['raw_output_script'] = scriptPubKey.encode('hex')
215 def parse_Transaction(vds):
217 start = vds.read_cursor
218 d['version'] = vds.read_int32()
219 n_vin = vds.read_compact_size()
221 for i in xrange(n_vin):
222 d['inputs'].append(parse_TxIn(vds))
223 n_vout = vds.read_compact_size()
225 for i in xrange(n_vout):
226 d['outputs'].append(parse_TxOut(vds, i))
227 d['lockTime'] = vds.read_uint32()
230 def parse_redeemScript(bytes):
231 dec = [ x for x in script_GetOp(bytes.decode('hex')) ]
234 match = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
235 if match_decoded(dec, match):
236 pubkeys = [ dec[1][1].encode('hex'), dec[2][1].encode('hex') ]
240 match = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
241 if match_decoded(dec, match):
242 pubkeys = [ dec[1][1].encode('hex'), dec[2][1].encode('hex'), dec[3][1].encode('hex') ]
247 opcodes = Enumeration("Opcodes", [
248 ("OP_0", 0), ("OP_PUSHDATA1",76), "OP_PUSHDATA2", "OP_PUSHDATA4", "OP_1NEGATE", "OP_RESERVED",
249 "OP_1", "OP_2", "OP_3", "OP_4", "OP_5", "OP_6", "OP_7",
250 "OP_8", "OP_9", "OP_10", "OP_11", "OP_12", "OP_13", "OP_14", "OP_15", "OP_16",
251 "OP_NOP", "OP_VER", "OP_IF", "OP_NOTIF", "OP_VERIF", "OP_VERNOTIF", "OP_ELSE", "OP_ENDIF", "OP_VERIFY",
252 "OP_RETURN", "OP_TOALTSTACK", "OP_FROMALTSTACK", "OP_2DROP", "OP_2DUP", "OP_3DUP", "OP_2OVER", "OP_2ROT", "OP_2SWAP",
253 "OP_IFDUP", "OP_DEPTH", "OP_DROP", "OP_DUP", "OP_NIP", "OP_OVER", "OP_PICK", "OP_ROLL", "OP_ROT",
254 "OP_SWAP", "OP_TUCK", "OP_CAT", "OP_SUBSTR", "OP_LEFT", "OP_RIGHT", "OP_SIZE", "OP_INVERT", "OP_AND",
255 "OP_OR", "OP_XOR", "OP_EQUAL", "OP_EQUALVERIFY", "OP_RESERVED1", "OP_RESERVED2", "OP_1ADD", "OP_1SUB", "OP_2MUL",
256 "OP_2DIV", "OP_NEGATE", "OP_ABS", "OP_NOT", "OP_0NOTEQUAL", "OP_ADD", "OP_SUB", "OP_MUL", "OP_DIV",
257 "OP_MOD", "OP_LSHIFT", "OP_RSHIFT", "OP_BOOLAND", "OP_BOOLOR",
258 "OP_NUMEQUAL", "OP_NUMEQUALVERIFY", "OP_NUMNOTEQUAL", "OP_LESSTHAN",
259 "OP_GREATERTHAN", "OP_LESSTHANOREQUAL", "OP_GREATERTHANOREQUAL", "OP_MIN", "OP_MAX",
260 "OP_WITHIN", "OP_RIPEMD160", "OP_SHA1", "OP_SHA256", "OP_HASH160",
261 "OP_HASH256", "OP_CODESEPARATOR", "OP_CHECKSIG", "OP_CHECKSIGVERIFY", "OP_CHECKMULTISIG",
262 "OP_CHECKMULTISIGVERIFY",
263 ("OP_SINGLEBYTE_END", 0xF0),
264 ("OP_DOUBLEBYTE_BEGIN", 0xF000),
265 "OP_PUBKEY", "OP_PUBKEYHASH",
266 ("OP_INVALIDOPCODE", 0xFFFF),
270 def script_GetOp(bytes):
272 while i < len(bytes):
274 opcode = ord(bytes[i])
276 if opcode >= opcodes.OP_SINGLEBYTE_END:
278 opcode |= ord(bytes[i])
281 if opcode <= opcodes.OP_PUSHDATA4:
283 if opcode == opcodes.OP_PUSHDATA1:
284 nSize = ord(bytes[i])
286 elif opcode == opcodes.OP_PUSHDATA2:
287 (nSize,) = struct.unpack_from('<H', bytes, i)
289 elif opcode == opcodes.OP_PUSHDATA4:
290 (nSize,) = struct.unpack_from('<I', bytes, i)
292 vch = bytes[i:i+nSize]
295 yield (opcode, vch, i)
298 def script_GetOpName(opcode):
299 return (opcodes.whatis(opcode)).replace("OP_", "")
302 def decode_script(bytes):
304 for (opcode, vch, i) in script_GetOp(bytes):
305 if len(result) > 0: result += " "
306 if opcode <= opcodes.OP_PUSHDATA4:
307 result += "%d:"%(opcode,)
308 result += short_hex(vch)
310 result += script_GetOpName(opcode)
314 def match_decoded(decoded, to_match):
315 if len(decoded) != len(to_match):
317 for i in range(len(decoded)):
318 if to_match[i] == opcodes.OP_PUSHDATA4 and decoded[i][0] <= opcodes.OP_PUSHDATA4 and decoded[i][0]>0:
319 continue # Opcodes below OP_PUSHDATA4 all just push data onto stack, and are equivalent.
320 if to_match[i] != decoded[i][0]:
324 def get_address_from_input_script(bytes):
325 decoded = [ x for x in script_GetOp(bytes) ]
327 # non-generated TxIn transactions push a signature
328 # (seventy-something bytes) and then their public key
329 # (65 bytes) onto the stack:
330 match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ]
331 if match_decoded(decoded, match):
332 return None, None, public_key_to_bc_address(decoded[1][1])
334 # p2sh transaction, 2 of n
335 match = [ opcodes.OP_0 ]
336 while len(match) < len(decoded):
337 match.append(opcodes.OP_PUSHDATA4)
339 if match_decoded(decoded, match):
341 redeemScript = decoded[-1][1]
343 signatures = map(lambda x:x[1].encode('hex'), decoded[1:-1])
345 dec2 = [ x for x in script_GetOp(redeemScript) ]
348 match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
349 if match_decoded(dec2, match2):
350 pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex') ]
351 return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5)
354 match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
355 if match_decoded(dec2, match2):
356 pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex') ]
357 return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5)
359 raise BaseException("no match for scriptsig")
363 def get_address_from_output_script(bytes):
364 decoded = [ x for x in script_GetOp(bytes) ]
366 # The Genesis Block, self-payments, and pay-by-IP-address payments look like:
367 # 65 BYTES:... CHECKSIG
368 match = [ opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG ]
369 if match_decoded(decoded, match):
370 return public_key_to_bc_address(decoded[0][1])
372 # Pay-by-Bitcoin-address TxOuts look like:
373 # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
374 match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
375 if match_decoded(decoded, match):
376 return hash_160_to_bc_address(decoded[2][1])
379 match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ]
380 if match_decoded(decoded, match):
381 return hash_160_to_bc_address(decoded[1][1],5)