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
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
38 def seek_file(self, position):
39 self.read_cursor = position
43 def read_string(self):
44 # Strings are encoded depending on length:
45 # 0 to 252 : 1-byte-length followed by bytes (if any)
46 # 253 to 65,535 : byte'253' 2-byte-length followed by bytes
47 # 65,536 to 4,294,967,295 : byte '254' 4-byte-length followed by bytes
48 # ... and the Bitcoin client is coded to understand:
49 # greater than 4,294,967,295 : byte '255' 8-byte-length followed by bytes of string
50 # ... but I don't think it actually handles any strings that big.
51 if self.input is None:
52 raise SerializationError("call write(bytes) before trying to deserialize")
55 length = self.read_compact_size()
57 raise SerializationError("attempt to read past end of buffer")
59 return self.read_bytes(length)
61 def write_string(self, string):
62 # Length-encoded as with read-string
63 self.write_compact_size(len(string))
66 def read_bytes(self, length):
68 result = self.input[self.read_cursor:self.read_cursor+length]
69 self.read_cursor += length
72 raise SerializationError("attempt to read past end of buffer")
76 def read_boolean(self): return self.read_bytes(1)[0] != chr(0)
77 def read_int16(self): return self._read_num('<h')
78 def read_uint16(self): return self._read_num('<H')
79 def read_int32(self): return self._read_num('<i')
80 def read_uint32(self): return self._read_num('<I')
81 def read_int64(self): return self._read_num('<q')
82 def read_uint64(self): return self._read_num('<Q')
84 def write_boolean(self, val): return self.write(chr(1) if val else chr(0))
85 def write_int16(self, val): return self._write_num('<h', val)
86 def write_uint16(self, val): return self._write_num('<H', val)
87 def write_int32(self, val): return self._write_num('<i', val)
88 def write_uint32(self, val): return self._write_num('<I', val)
89 def write_int64(self, val): return self._write_num('<q', val)
90 def write_uint64(self, val): return self._write_num('<Q', val)
92 def read_compact_size(self):
93 size = ord(self.input[self.read_cursor])
96 size = self._read_num('<H')
98 size = self._read_num('<I')
100 size = self._read_num('<Q')
103 def write_compact_size(self, size):
105 raise SerializationError("attempt to write size < 0")
107 self.write(chr(size))
110 self._write_num('<H', size)
113 self._write_num('<I', size)
116 self._write_num('<Q', size)
118 def _read_num(self, format):
119 (i,) = struct.unpack_from(format, self.input, self.read_cursor)
120 self.read_cursor += struct.calcsize(format)
123 def _write_num(self, format, num):
124 s = struct.pack(format, num)
129 # From the Python Cookbook, downloaded from http://code.activestate.com/recipes/67107/
131 import types, string, exceptions
133 class EnumException(exceptions.Exception):
137 def __init__(self, name, enumList):
145 if type(x) == types.TupleType:
147 if type(x) != types.StringType:
148 raise EnumException, "enum name is not a string: " + x
149 if type(i) != types.IntType:
150 raise EnumException, "enum value is not an integer: " + i
152 raise EnumException, "enum name is not unique: " + x
153 if i in uniqueValues:
154 raise EnumException, "enum value is not unique for " + x
155 uniqueNames.append(x)
156 uniqueValues.append(i)
161 self.reverseLookup = reverseLookup
162 def __getattr__(self, attr):
163 if not self.lookup.has_key(attr):
165 return self.lookup[attr]
166 def whatis(self, value):
167 return self.reverseLookup[value]
170 # This function comes from bitcointools, bct-LICENSE.txt.
172 return bytes.encode('hex_codec')
174 # This function comes from bitcointools, bct-LICENSE.txt.
175 def short_hex(bytes):
176 t = bytes.encode('hex_codec')
179 return t[0:4]+"..."+t[-4:]
185 d['prevout_hash'] = hash_encode(vds.read_bytes(32))
186 d['prevout_n'] = vds.read_uint32()
187 scriptSig = vds.read_bytes(vds.read_compact_size())
188 d['sequence'] = vds.read_uint32()
189 d['address'] = extract_public_key(scriptSig)
190 #d['script'] = decode_script(scriptSig)
194 def parse_TxOut(vds, i):
196 d['value'] = vds.read_int64()
197 scriptPubKey = vds.read_bytes(vds.read_compact_size())
198 d['address'] = extract_public_key(scriptPubKey)
199 #d['script'] = decode_script(scriptPubKey)
200 d['raw_output_script'] = scriptPubKey.encode('hex')
205 def parse_Transaction(vds):
207 start = vds.read_cursor
208 d['version'] = vds.read_int32()
209 n_vin = vds.read_compact_size()
211 for i in xrange(n_vin):
212 d['inputs'].append(parse_TxIn(vds))
213 n_vout = vds.read_compact_size()
215 for i in xrange(n_vout):
216 d['outputs'].append(parse_TxOut(vds, i))
217 d['lockTime'] = vds.read_uint32()
224 opcodes = Enumeration("Opcodes", [
225 ("OP_0", 0), ("OP_PUSHDATA1",76), "OP_PUSHDATA2", "OP_PUSHDATA4", "OP_1NEGATE", "OP_RESERVED",
226 "OP_1", "OP_2", "OP_3", "OP_4", "OP_5", "OP_6", "OP_7",
227 "OP_8", "OP_9", "OP_10", "OP_11", "OP_12", "OP_13", "OP_14", "OP_15", "OP_16",
228 "OP_NOP", "OP_VER", "OP_IF", "OP_NOTIF", "OP_VERIF", "OP_VERNOTIF", "OP_ELSE", "OP_ENDIF", "OP_VERIFY",
229 "OP_RETURN", "OP_TOALTSTACK", "OP_FROMALTSTACK", "OP_2DROP", "OP_2DUP", "OP_3DUP", "OP_2OVER", "OP_2ROT", "OP_2SWAP",
230 "OP_IFDUP", "OP_DEPTH", "OP_DROP", "OP_DUP", "OP_NIP", "OP_OVER", "OP_PICK", "OP_ROLL", "OP_ROT",
231 "OP_SWAP", "OP_TUCK", "OP_CAT", "OP_SUBSTR", "OP_LEFT", "OP_RIGHT", "OP_SIZE", "OP_INVERT", "OP_AND",
232 "OP_OR", "OP_XOR", "OP_EQUAL", "OP_EQUALVERIFY", "OP_RESERVED1", "OP_RESERVED2", "OP_1ADD", "OP_1SUB", "OP_2MUL",
233 "OP_2DIV", "OP_NEGATE", "OP_ABS", "OP_NOT", "OP_0NOTEQUAL", "OP_ADD", "OP_SUB", "OP_MUL", "OP_DIV",
234 "OP_MOD", "OP_LSHIFT", "OP_RSHIFT", "OP_BOOLAND", "OP_BOOLOR",
235 "OP_NUMEQUAL", "OP_NUMEQUALVERIFY", "OP_NUMNOTEQUAL", "OP_LESSTHAN",
236 "OP_GREATERTHAN", "OP_LESSTHANOREQUAL", "OP_GREATERTHANOREQUAL", "OP_MIN", "OP_MAX",
237 "OP_WITHIN", "OP_RIPEMD160", "OP_SHA1", "OP_SHA256", "OP_HASH160",
238 "OP_HASH256", "OP_CODESEPARATOR", "OP_CHECKSIG", "OP_CHECKSIGVERIFY", "OP_CHECKMULTISIG",
239 "OP_CHECKMULTISIGVERIFY",
240 ("OP_SINGLEBYTE_END", 0xF0),
241 ("OP_DOUBLEBYTE_BEGIN", 0xF000),
242 "OP_PUBKEY", "OP_PUBKEYHASH",
243 ("OP_INVALIDOPCODE", 0xFFFF),
246 def script_GetOp(bytes):
248 while i < len(bytes):
250 opcode = ord(bytes[i])
252 if opcode >= opcodes.OP_SINGLEBYTE_END:
254 opcode |= ord(bytes[i])
257 if opcode <= opcodes.OP_PUSHDATA4:
259 if opcode == opcodes.OP_PUSHDATA1:
260 nSize = ord(bytes[i])
262 elif opcode == opcodes.OP_PUSHDATA2:
263 (nSize,) = struct.unpack_from('<H', bytes, i)
265 elif opcode == opcodes.OP_PUSHDATA4:
266 (nSize,) = struct.unpack_from('<I', bytes, i)
268 vch = bytes[i:i+nSize]
271 yield (opcode, vch, i)
273 def script_GetOpName(opcode):
274 return (opcodes.whatis(opcode)).replace("OP_", "")
276 def decode_script(bytes):
278 for (opcode, vch, i) in script_GetOp(bytes):
279 if len(result) > 0: result += " "
280 if opcode <= opcodes.OP_PUSHDATA4:
281 result += "%d:"%(opcode,)
282 result += short_hex(vch)
284 result += script_GetOpName(opcode)
287 def match_decoded(decoded, to_match):
288 if len(decoded) != len(to_match):
290 for i in range(len(decoded)):
291 if to_match[i] == opcodes.OP_PUSHDATA4 and decoded[i][0] <= opcodes.OP_PUSHDATA4:
292 continue # Opcodes below OP_PUSHDATA4 all just push data onto stack, and are equivalent.
293 if to_match[i] != decoded[i][0]:
297 def extract_public_key(bytes):
298 decoded = [ x for x in script_GetOp(bytes) ]
300 # non-generated TxIn transactions push a signature
301 # (seventy-something bytes) and then their public key
302 # (65 bytes) onto the stack:
303 match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ]
304 if match_decoded(decoded, match):
305 return public_key_to_bc_address(decoded[1][1])
307 # The Genesis Block, self-payments, and pay-by-IP-address payments look like:
308 # 65 BYTES:... CHECKSIG
309 match = [ opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG ]
310 if match_decoded(decoded, match):
311 return public_key_to_bc_address(decoded[0][1])
313 # Pay-by-Bitcoin-address TxOuts look like:
314 # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
315 match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
316 if match_decoded(decoded, match):
317 return hash_160_to_bc_address(decoded[2][1])