<s> '''<STR_LIT>''' <EOL> from BCDataStream import * <EOL> from enumeration import Enumeration <EOL> from base58 import public_key_to_bc_address , hash_160_to_bc_address <EOL> import struct <EOL> import hashlib , binascii <EOL> import helper <EOL> def getTxId ( _data ) : <EOL> _temp = hashlib . sha256 ( hashlib . sha256 ( _data ) . digest ( ) ) . hexdigest ( ) <EOL> _result = '<STR_LIT>' <EOL> while _temp != '<STR_LIT>' : <EOL> _result = _result + _temp [ - <NUM_LIT:2> : ] <EOL> _temp = _temp [ : - <NUM_LIT:2> ] <EOL> return _result <EOL> def short_hex ( bytes ) : <EOL> t = bytes . encode ( '<STR_LIT>' ) <EOL> if len ( t ) < <NUM_LIT:11> : <EOL> return t <EOL> return t [ <NUM_LIT:0> : <NUM_LIT:4> ] + "<STR_LIT>" + t [ - <NUM_LIT:4> : ] <EOL> def parse_TxIn ( vds ) : <EOL> d = { } <EOL> d [ '<STR_LIT>' ] = vds . read_bytes ( <NUM_LIT:32> ) <EOL> d [ '<STR_LIT>' ] = vds . read_uint32 ( ) <EOL> d [ '<STR_LIT>' ] = vds . read_bytes ( vds . read_compact_size ( ) ) <EOL> d [ '<STR_LIT>' ] = vds . read_uint32 ( ) <EOL> return d <EOL> def deserialize_TxIn ( d ) : <EOL> if d [ '<STR_LIT>' ] == "<STR_LIT:\x00>" * <NUM_LIT:32> : <EOL> result = { "<STR_LIT:address>" : '<STR_LIT>' } <EOL> else : <EOL> pk = extract_public_key ( d [ '<STR_LIT>' ] ) <EOL> result = { "<STR_LIT:address>" : pk } <EOL> return result <EOL> def parse_TxOut ( vds ) : <EOL> d = { } <EOL> d [ '<STR_LIT:value>' ] = vds . read_int64 ( ) <EOL> d [ '<STR_LIT>' ] = vds . read_bytes ( vds . read_compact_size ( ) ) <EOL> return d <EOL> def deserialize_TxOut ( d ) : <EOL> pk = extract_public_key ( d [ '<STR_LIT>' ] ) <EOL> result = { "<STR_LIT:address>" : pk , "<STR_LIT:value>" : "<STR_LIT>" % ( d [ '<STR_LIT:value>' ] / <NUM_LIT> , ) } <EOL> return result <EOL> def parse_Transaction ( vds ) : <EOL> d = { } <EOL> start_pos = vds . read_cursor <EOL> d [ '<STR_LIT:version>' ] = vds . read_int32 ( ) <EOL> n_vin = vds . read_compact_size ( ) <EOL> d [ '<STR_LIT>' ] = [ ] <EOL> for i in xrange ( n_vin ) : <EOL> d [ '<STR_LIT>' ] . append ( parse_TxIn ( vds ) ) <EOL> n_vout = vds . read_compact_size ( ) <EOL> d [ '<STR_LIT>' ] = [ ] <EOL> for i in xrange ( n_vout ) : <EOL> d [ '<STR_LIT>' ] . append ( parse_TxOut ( vds ) ) <EOL> d [ '<STR_LIT>' ] = vds . read_uint32 ( ) <EOL> d [ '<STR_LIT>' ] = vds . input [ start_pos : vds . read_cursor ] <EOL> return d <EOL> def deserialize_Transaction ( d , block_time , addresses ) : <EOL> txIns = [ ] <EOL> txOuts = [ ] <EOL> ct = False <EOL> for txIn in d [ '<STR_LIT>' ] : <EOL> txIns . append ( deserialize_TxIn ( txIn ) ) <EOL> for txOut in d [ '<STR_LIT>' ] : <EOL> res = deserialize_TxOut ( txOut ) <EOL> txOuts . append ( res ) <EOL> if res [ "<STR_LIT:address>" ] in addresses : <EOL> ct = True <EOL> if ct : <EOL> tx_id = getTxId ( d [ '<STR_LIT>' ] ) <EOL> sql = helper . sqlhelper ( ) <EOL> if sql . testxtid ( tx_id ) : <EOL> id = sql . inserttx ( tx_id , int ( block_time ) ) <EOL> for txI in txIns : <EOL> sql . insert ( int ( block_time ) , txI [ "<STR_LIT:address>" ] , <NUM_LIT:0> , False , id ) <EOL> for txO in txOuts : <EOL> sql . insert ( int ( block_time ) , txO [ "<STR_LIT:address>" ] , txO [ "<STR_LIT:value>" ] , True , id ) <EOL> def deserialize_TransactionRaw ( d ) : <EOL> txIns = [ ] <EOL> txOuts = [ ] <EOL> for txIn in d [ '<STR_LIT>' ] : <EOL> txIns . append ( deserialize_TxIn ( txIn ) ) <EOL> for txOut in d [ '<STR_LIT>' ] : <EOL> res = deserialize_TxOut ( txOut ) <EOL> txOuts . append ( res ) <EOL> return txIns <EOL> def parse_BlockHeader ( vds ) : <EOL> d = { } <EOL> header_start = vds . read_cursor <EOL> d [ '<STR_LIT:version>' ] = vds . read_int32 ( ) <EOL> d [ '<STR_LIT>' ] = vds . read_bytes ( <NUM_LIT:32> ) <EOL> d [ '<STR_LIT>' ] = vds . read_bytes ( <NUM_LIT:32> ) <EOL> d [ '<STR_LIT>' ] = vds . read_uint32 ( ) <EOL> d [ '<STR_LIT>' ] = vds . read_uint32 ( ) <EOL> d [ '<STR_LIT>' ] = vds . read_uint32 ( ) <EOL> header_end = vds . read_cursor <EOL> d [ '<STR_LIT>' ] = vds . input [ header_start : header_end ] <EOL> return d <EOL> def parse_Block ( vds ) : <EOL> d = parse_BlockHeader ( vds ) <EOL> d [ '<STR_LIT>' ] = [ ] <EOL> nTransactions = vds . read_compact_size ( ) <EOL> for i in xrange ( nTransactions ) : <EOL> d [ '<STR_LIT>' ] . append ( parse_Transaction ( vds ) ) <EOL> return d <EOL> def deserialize_Block ( d , block_time , addresses ) : <EOL> result = [ ] <EOL> for t in d [ '<STR_LIT>' ] : <EOL> deserialize_Transaction ( t , block_time , addresses ) <EOL> opcodes = Enumeration ( "<STR_LIT>" , [ <EOL> ( "<STR_LIT>" , <NUM_LIT:0> ) , ( "<STR_LIT>" , <NUM_LIT> ) , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> ( "<STR_LIT>" , <NUM_LIT> ) , <EOL> ] ) <EOL> def script_GetOp ( bytes ) : <EOL> i = <NUM_LIT:0> <EOL> while i < len ( bytes ) : <EOL> vch = None <EOL> opcode = ord ( bytes [ i ] ) <EOL> i += <NUM_LIT:1> <EOL> if opcode <= opcodes . OP_PUSHDATA4 : <EOL> nSize = opcode <EOL> if opcode == opcodes . OP_PUSHDATA1 : <EOL> if i + <NUM_LIT:1> > len ( bytes ) : <EOL> vch = "<STR_LIT>" <EOL> i = len ( bytes ) <EOL> else : <EOL> nSize = ord ( bytes [ i ] ) <EOL> i += <NUM_LIT:1> <EOL> elif opcode == opcodes . OP_PUSHDATA2 : <EOL> if i + <NUM_LIT:2> > len ( bytes ) : <EOL> vch = "<STR_LIT>" <EOL> i = len ( bytes ) <EOL> else : <EOL> ( nSize , ) = struct . unpack_from ( '<STR_LIT>' , bytes , i ) <EOL> i += <NUM_LIT:2> <EOL> elif opcode == opcodes . OP_PUSHDATA4 : <EOL> if i + <NUM_LIT:4> > len ( bytes ) : <EOL> vch = "<STR_LIT>" <EOL> i = len ( bytes ) <EOL> else : <EOL> ( nSize , ) = struct . unpack_from ( '<STR_LIT>' , bytes , i ) <EOL> i += <NUM_LIT:4> <EOL> if i + nSize > len ( bytes ) : <EOL> vch = "<STR_LIT>" + bytes [ i : ] <EOL> i = len ( bytes ) <EOL> else : <EOL> vch = bytes [ i : i + nSize ] <EOL> i += nSize <EOL> yield ( opcode , vch ) <EOL> def script_GetOpName ( opcode ) : <EOL> try : <EOL> return ( opcodes . whatis ( opcode ) ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> except KeyError : <EOL> return "<STR_LIT>" + str ( opcode ) <EOL> def decode_script ( bytes ) : <EOL> result = '<STR_LIT>' <EOL> for ( opcode , vch ) in script_GetOp ( bytes ) : <EOL> if len ( result ) > <NUM_LIT:0> : result += "<STR_LIT:U+0020>" <EOL> if opcode <= opcodes . OP_PUSHDATA4 : <EOL> result += "<STR_LIT>" % ( opcode , ) <EOL> result += short_hex ( vch ) <EOL> else : <EOL> result += script_GetOpName ( opcode ) <EOL> return result <EOL> def match_decoded ( decoded , to_match ) : <EOL> if len ( decoded ) != len ( to_match ) : <EOL> return False ; <EOL> for i in range ( len ( decoded ) ) : <EOL> if to_match [ i ] == opcodes . OP_PUSHDATA4 and decoded [ i ] [ <NUM_LIT:0> ] <= opcodes . OP_PUSHDATA4 : <EOL> continue <EOL> if to_match [ i ] != decoded [ i ] [ <NUM_LIT:0> ] : <EOL> return False <EOL> return True <EOL> def extract_public_key ( bytes , version = '<STR_LIT:\x00>' ) : <EOL> try : <EOL> decoded = [ x for x in script_GetOp ( bytes ) ] <EOL> except struct . error : <EOL> return "<STR_LIT>" <EOL> match = [ opcodes . OP_PUSHDATA4 , opcodes . OP_PUSHDATA4 ] <EOL> if match_decoded ( decoded , match ) : <EOL> return public_key_to_bc_address ( decoded [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , version = version ) <EOL> match = [ opcodes . OP_PUSHDATA4 , opcodes . OP_CHECKSIG ] <EOL> if match_decoded ( decoded , match ) : <EOL> return public_key_to_bc_address ( decoded [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , version = version ) <EOL> match = [ opcodes . OP_DUP , opcodes . OP_HASH160 , opcodes . OP_PUSHDATA4 , opcodes . OP_EQUALVERIFY , opcodes . OP_CHECKSIG ] <EOL> if match_decoded ( decoded , match ) : <EOL> return hash_160_to_bc_address ( decoded [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] , version = version ) <EOL> multisigs = [ <EOL> [ opcodes . OP_1 , opcodes . OP_PUSHDATA4 , opcodes . OP_1 , opcodes . OP_CHECKMULTISIG ] , <EOL> [ opcodes . OP_2 , opcodes . OP_PUSHDATA4 , opcodes . OP_PUSHDATA4 , opcodes . OP_2 , opcodes . OP_CHECKMULTISIG ] , <EOL> [ opcodes . OP_3 , opcodes . OP_PUSHDATA4 , opcodes . OP_PUSHDATA4 , opcodes . OP_3 , opcodes . OP_CHECKMULTISIG ] <EOL> ] <EOL> for match in multisigs : <EOL> if match_decoded ( decoded , match ) : <EOL> return "<STR_LIT:[>" + '<STR_LIT:U+002C>' . join ( [ public_key_to_bc_address ( decoded [ i ] [ <NUM_LIT:1> ] ) for i in range ( <NUM_LIT:1> , len ( decoded ) - <NUM_LIT:1> ) ] ) + "<STR_LIT:]>" <EOL> match = [ opcodes . OP_HASH160 , <NUM_LIT> , opcodes . OP_EQUAL ] <EOL> if match_decoded ( decoded , match ) : <EOL> return hash_160_to_bc_address ( decoded [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , version = "<STR_LIT>" ) <EOL> return "<STR_LIT>" </s>
