<s> __author__ = '<STR_LIT>' <EOL> import base64 <EOL> import bitcointools <EOL> import gnupg <EOL> import httplib <EOL> import json <EOL> import nacl . signing <EOL> import nacl . hash <EOL> import nacl . encoding <EOL> import nacl . utils <EOL> import obelisk <EOL> import os . path <EOL> import pickle <EOL> import time <EOL> from binascii import unhexlify <EOL> from collections import OrderedDict <EOL> from config import DATA_FOLDER , TRANSACTION_FEE <EOL> from dht . node import Node <EOL> from dht . utils import digest <EOL> from keys . bip32utils import derive_childkey <EOL> from keys . keychain import KeyChain <EOL> from log import Logger <EOL> from market . contracts import Contract <EOL> from market . moderation import process_dispute , close_dispute <EOL> from market . profile import Profile <EOL> from market . protocol import MarketProtocol <EOL> from market . transactions import BitcoinTransaction <EOL> from nacl . public import PrivateKey , PublicKey , Box <EOL> from protos import objects <EOL> from seed import peers <EOL> from twisted . internet import defer , reactor , task <EOL> class Server ( object ) : <EOL> def __init__ ( self , kserver , signing_key , database ) : <EOL> """<STR_LIT>""" <EOL> self . kserver = kserver <EOL> self . signing_key = signing_key <EOL> self . router = kserver . protocol . router <EOL> self . db = database <EOL> self . log = Logger ( system = self ) <EOL> self . protocol = MarketProtocol ( kserver . node , self . router , signing_key , database ) <EOL> task . LoopingCall ( self . update_listings ) . start ( <NUM_LIT> , now = True ) <EOL> def querySeed ( self , list_seed_pubkey ) : <EOL> """<STR_LIT>""" <EOL> for sp in list_seed_pubkey : <EOL> seed , pubkey = sp <EOL> try : <EOL> self . log . debug ( "<STR_LIT>" % seed ) <EOL> c = httplib . HTTPConnection ( seed ) <EOL> c . request ( "<STR_LIT:GET>" , "<STR_LIT>" ) <EOL> response = c . getresponse ( ) <EOL> self . log . debug ( "<STR_LIT>" % ( seed , response . status , response . reason ) ) <EOL> data = response . read ( ) <EOL> reread_data = data . decode ( "<STR_LIT>" ) <EOL> proto = peers . PeerSeeds ( ) <EOL> proto . ParseFromString ( reread_data ) <EOL> verify_key = nacl . signing . VerifyKey ( pubkey , encoder = nacl . encoding . HexEncoder ) <EOL> verify_key . verify ( "<STR_LIT>" . join ( proto . serializedNode ) , proto . signature ) <EOL> for peer in proto . serializedNode : <EOL> try : <EOL> n = objects . Node ( ) <EOL> n . ParseFromString ( peer ) <EOL> self . db . vendors . save_vendor ( n . guid . encode ( "<STR_LIT>" ) , peer ) <EOL> except Exception : <EOL> pass <EOL> except Exception , e : <EOL> self . log . error ( "<STR_LIT>" % str ( e ) ) <EOL> def get_contract ( self , node_to_ask , contract_id ) : <EOL> """<STR_LIT>""" <EOL> def get_result ( result ) : <EOL> try : <EOL> if result [ <NUM_LIT:0> ] : <EOL> contract = json . loads ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , object_pairs_hook = OrderedDict ) <EOL> id_in_contract = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> if id_in_contract != contract_id . encode ( "<STR_LIT>" ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> signature = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> verify_obj = json . dumps ( contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] , indent = <NUM_LIT:4> ) <EOL> verify_key = nacl . signing . VerifyKey ( node_to_ask . pubkey ) <EOL> verify_key . verify ( verify_obj , base64 . b64decode ( signature ) ) <EOL> bitcoin_key = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> bitcoin_sig = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> valid = bitcointools . ecdsa_raw_verify ( verify_obj , bitcointools . decode_sig ( bitcoin_sig ) , <EOL> bitcoin_key ) <EOL> if not valid : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> for moderator in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> guid = moderator [ "<STR_LIT>" ] <EOL> guid_key = moderator [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> bitcoin_key = moderator [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:key>" ] <EOL> bitcoin_sig = base64 . b64decode ( moderator [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> h = nacl . hash . sha512 ( unhexlify ( guid_key ) ) <EOL> pow_hash = h [ <NUM_LIT> : ] <EOL> if int ( pow_hash [ : <NUM_LIT:6> ] , <NUM_LIT:16> ) >= <NUM_LIT:50> or guid != h [ : <NUM_LIT> ] : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> verify_key = nacl . signing . VerifyKey ( guid_key , encoder = nacl . encoding . HexEncoder ) <EOL> verify_key . verify ( unhexlify ( bitcoin_key ) , bitcoin_sig ) <EOL> self . cache ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , id_in_contract ) <EOL> if "<STR_LIT>" in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> for image_hash in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> self . get_image ( node_to_ask , unhexlify ( image_hash ) ) <EOL> return contract <EOL> else : <EOL> self . log . warning ( "<STR_LIT>" % node_to_ask . id . encode ( "<STR_LIT>" ) ) <EOL> return None <EOL> except Exception : <EOL> return None <EOL> if node_to_ask . ip is None : <EOL> return defer . succeed ( None ) <EOL> self . log . info ( "<STR_LIT>" % ( contract_id . encode ( "<STR_LIT>" ) , node_to_ask ) ) <EOL> d = self . protocol . callGetContract ( node_to_ask , contract_id ) <EOL> return d . addCallback ( get_result ) <EOL> def get_image ( self , node_to_ask , image_hash ) : <EOL> """<STR_LIT>""" <EOL> def get_result ( result ) : <EOL> try : <EOL> if result [ <NUM_LIT:0> ] and digest ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) == image_hash : <EOL> self . cache ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , digest ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) . encode ( "<STR_LIT>" ) ) <EOL> return result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> except Exception : <EOL> return None <EOL> if node_to_ask . ip is None or len ( image_hash ) != <NUM_LIT:20> : <EOL> return defer . succeed ( None ) <EOL> self . log . info ( "<STR_LIT>" % ( image_hash . encode ( "<STR_LIT>" ) , node_to_ask ) ) <EOL> d = self . protocol . callGetImage ( node_to_ask , image_hash ) <EOL> return d . addCallback ( get_result ) <EOL> def get_profile ( self , node_to_ask ) : <EOL> """<STR_LIT>""" <EOL> def get_result ( result ) : <EOL> try : <EOL> verify_key = nacl . signing . VerifyKey ( node_to_ask . pubkey ) <EOL> verify_key . verify ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> p = objects . Profile ( ) <EOL> p . ParseFromString ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> if p . pgp_key . public_key : <EOL> gpg = gnupg . GPG ( ) <EOL> gpg . import_keys ( p . pgp_key . publicKey ) <EOL> if not gpg . verify ( p . pgp_key . signature ) or node_to_ask . id . encode ( '<STR_LIT>' ) not in p . pgp_key . signature : <EOL> p . ClearField ( "<STR_LIT>" ) <EOL> if not os . path . isfile ( os . path . join ( DATA_FOLDER , '<STR_LIT>' , p . avatar_hash . encode ( "<STR_LIT>" ) ) ) : <EOL> self . get_image ( node_to_ask , p . avatar_hash ) <EOL> if not os . path . isfile ( os . path . join ( DATA_FOLDER , '<STR_LIT>' , p . header_hash . encode ( "<STR_LIT>" ) ) ) : <EOL> self . get_image ( node_to_ask , p . header_hash ) <EOL> self . cache ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , node_to_ask . id . encode ( "<STR_LIT>" ) ) <EOL> return p <EOL> except Exception : <EOL> return None <EOL> if node_to_ask . ip is None : <EOL> return defer . succeed ( None ) <EOL> self . log . info ( "<STR_LIT>" % node_to_ask ) <EOL> d = self . protocol . callGetProfile ( node_to_ask ) <EOL> return d . addCallback ( get_result ) <EOL> def get_user_metadata ( self , node_to_ask ) : <EOL> """<STR_LIT>""" <EOL> def get_result ( result ) : <EOL> try : <EOL> verify_key = nacl . signing . VerifyKey ( node_to_ask . pubkey ) <EOL> verify_key . verify ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> m = objects . Metadata ( ) <EOL> m . ParseFromString ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> if not os . path . isfile ( os . path . join ( DATA_FOLDER , '<STR_LIT>' , m . avatar_hash . encode ( "<STR_LIT>" ) ) ) : <EOL> self . get_image ( node_to_ask , m . avatar_hash ) <EOL> return m <EOL> except Exception : <EOL> return None <EOL> if node_to_ask . ip is None : <EOL> return defer . succeed ( None ) <EOL> self . log . info ( "<STR_LIT>" % node_to_ask ) <EOL> d = self . protocol . callGetUserMetadata ( node_to_ask ) <EOL> return d . addCallback ( get_result ) <EOL> def get_listings ( self , node_to_ask ) : <EOL> """<STR_LIT>""" <EOL> def get_result ( result ) : <EOL> try : <EOL> verify_key = nacl . signing . VerifyKey ( node_to_ask . pubkey ) <EOL> verify_key . verify ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> l = objects . Listings ( ) <EOL> l . ParseFromString ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> return l <EOL> except Exception : <EOL> return None <EOL> if node_to_ask . ip is None : <EOL> return defer . succeed ( None ) <EOL> self . log . info ( "<STR_LIT>" % node_to_ask ) <EOL> d = self . protocol . callGetListings ( node_to_ask ) <EOL> return d . addCallback ( get_result ) <EOL> def get_contract_metadata ( self , node_to_ask , contract_hash ) : <EOL> """<STR_LIT>""" <EOL> def get_result ( result ) : <EOL> try : <EOL> verify_key = nacl . signing . VerifyKey ( node_to_ask . pubkey ) <EOL> verify_key . verify ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> l = objects . Listings ( ) . ListingMetadata ( ) <EOL> l . ParseFromString ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> if l . thumbnail_hash != "<STR_LIT>" : <EOL> if not os . path . isfile ( os . path . join ( DATA_FOLDER , '<STR_LIT>' , l . thumbnail_hash . encode ( "<STR_LIT>" ) ) ) : <EOL> self . get_image ( node_to_ask , l . thumbnail_hash ) <EOL> return l <EOL> except Exception : <EOL> return None <EOL> if node_to_ask . ip is None : <EOL> return defer . succeed ( None ) <EOL> self . log . info ( "<STR_LIT>" % ( contract_hash . encode ( "<STR_LIT>" ) , node_to_ask ) ) <EOL> d = self . protocol . callGetContractMetadata ( node_to_ask , contract_hash ) <EOL> return d . addCallback ( get_result ) <EOL> def make_moderator ( self ) : <EOL> """<STR_LIT>""" <EOL> u = objects . Profile ( ) <EOL> k = u . PublicKey ( ) <EOL> k . public_key = unhexlify ( bitcointools . bip32_extract_key ( KeyChain ( self . db ) . bitcoin_master_pubkey ) ) <EOL> k . signature = self . signing_key . sign ( k . public_key ) [ : <NUM_LIT:64> ] <EOL> u . bitcoin_key . MergeFrom ( k ) <EOL> u . moderator = True <EOL> Profile ( self . db ) . update ( u ) <EOL> proto = self . kserver . node . getProto ( ) . SerializeToString ( ) <EOL> self . kserver . set ( digest ( "<STR_LIT>" ) , digest ( proto ) , proto ) <EOL> self . log . info ( "<STR_LIT>" ) <EOL> def unmake_moderator ( self ) : <EOL> """<STR_LIT>""" <EOL> key = digest ( self . kserver . node . getProto ( ) . SerializeToString ( ) ) <EOL> signature = self . signing_key . sign ( key ) [ : <NUM_LIT:64> ] <EOL> self . kserver . delete ( "<STR_LIT>" , key , signature ) <EOL> Profile ( self . db ) . remove_field ( "<STR_LIT>" ) <EOL> self . log . info ( "<STR_LIT>" ) <EOL> def follow ( self , node_to_follow ) : <EOL> """<STR_LIT>""" <EOL> def save_to_db ( result ) : <EOL> if result [ <NUM_LIT:0> ] and result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == "<STR_LIT:True>" : <EOL> try : <EOL> u = objects . Following . User ( ) <EOL> u . guid = node_to_follow . id <EOL> u . pubkey = node_to_follow . pubkey <EOL> m = objects . Metadata ( ) <EOL> m . ParseFromString ( result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> u . metadata . MergeFrom ( m ) <EOL> u . signature = result [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> verify_key = nacl . signing . VerifyKey ( node_to_follow . pubkey ) <EOL> verify_key . verify ( result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , result [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] ) <EOL> self . db . follow . follow ( u ) <EOL> return True <EOL> except Exception : <EOL> return False <EOL> else : <EOL> return False <EOL> proto = Profile ( self . db ) . get ( False ) <EOL> m = objects . Metadata ( ) <EOL> m . name = proto . name <EOL> m . handle = proto . handle <EOL> m . avatar_hash = proto . avatar_hash <EOL> m . short_description = proto . short_description <EOL> m . nsfw = proto . nsfw <EOL> f = objects . Followers . Follower ( ) <EOL> f . guid = self . kserver . node . id <EOL> f . following = node_to_follow . id <EOL> f . pubkey = self . kserver . node . pubkey <EOL> f . metadata . MergeFrom ( m ) <EOL> signature = self . signing_key . sign ( f . SerializeToString ( ) ) [ : <NUM_LIT:64> ] <EOL> d = self . protocol . callFollow ( node_to_follow , f . SerializeToString ( ) , signature ) <EOL> self . log . info ( "<STR_LIT>" % node_to_follow ) <EOL> return d . addCallback ( save_to_db ) <EOL> def unfollow ( self , node_to_unfollow ) : <EOL> """<STR_LIT>""" <EOL> def save_to_db ( result ) : <EOL> try : <EOL> if result [ <NUM_LIT:0> ] and result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == "<STR_LIT:True>" : <EOL> self . db . follow . unfollow ( node_to_unfollow . id ) <EOL> return True <EOL> else : <EOL> return False <EOL> except Exception : <EOL> return False <EOL> signature = self . signing_key . sign ( "<STR_LIT>" + node_to_unfollow . id ) [ : <NUM_LIT:64> ] <EOL> d = self . protocol . callUnfollow ( node_to_unfollow , signature ) <EOL> self . log . info ( "<STR_LIT>" % node_to_unfollow ) <EOL> return d . addCallback ( save_to_db ) <EOL> def get_followers ( self , node_to_ask ) : <EOL> """<STR_LIT>""" <EOL> def get_response ( response ) : <EOL> f = objects . Followers ( ) <EOL> try : <EOL> verify_key = nacl . signing . VerifyKey ( node_to_ask . pubkey ) <EOL> verify_key . verify ( response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , response [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> f . ParseFromString ( response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> except Exception : <EOL> return None <EOL> for follower in f . followers : <EOL> try : <EOL> v_key = nacl . signing . VerifyKey ( follower . pubkey ) <EOL> signature = follower . signature <EOL> follower . ClearField ( "<STR_LIT>" ) <EOL> v_key . verify ( follower . SerializeToString ( ) , signature ) <EOL> h = nacl . hash . sha512 ( follower . pubkey ) <EOL> pow_hash = h [ <NUM_LIT> : ] <EOL> if int ( pow_hash [ : <NUM_LIT:6> ] , <NUM_LIT:16> ) >= <NUM_LIT:50> or follower . guid . encode ( "<STR_LIT>" ) != h [ : <NUM_LIT> ] : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if follower . following != node_to_ask . id : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> except Exception : <EOL> f . followers . remove ( follower ) <EOL> return f <EOL> d = self . protocol . callGetFollowers ( node_to_ask ) <EOL> self . log . info ( "<STR_LIT>" % node_to_ask ) <EOL> return d . addCallback ( get_response ) <EOL> def get_following ( self , node_to_ask ) : <EOL> """<STR_LIT>""" <EOL> def get_response ( response ) : <EOL> f = objects . Following ( ) <EOL> try : <EOL> verify_key = nacl . signing . VerifyKey ( node_to_ask . pubkey ) <EOL> verify_key . verify ( response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , response [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> f . ParseFromString ( response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> except Exception : <EOL> return None <EOL> for user in f . users : <EOL> try : <EOL> v_key = nacl . signing . VerifyKey ( user . pubkey ) <EOL> signature = user . signature <EOL> v_key . verify ( user . metadata . SerializeToString ( ) , signature ) <EOL> h = nacl . hash . sha512 ( user . pubkey ) <EOL> pow_hash = h [ <NUM_LIT> : ] <EOL> if int ( pow_hash [ : <NUM_LIT:6> ] , <NUM_LIT:16> ) >= <NUM_LIT:50> or user . guid . encode ( "<STR_LIT>" ) != h [ : <NUM_LIT> ] : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> except Exception : <EOL> f . users . remove ( user ) <EOL> return f <EOL> d = self . protocol . callGetFollowing ( node_to_ask ) <EOL> self . log . info ( "<STR_LIT>" % node_to_ask ) <EOL> return d . addCallback ( get_response ) <EOL> def broadcast ( self , message ) : <EOL> """<STR_LIT>""" <EOL> if len ( message ) > <NUM_LIT> : <EOL> return defer . succeed ( <NUM_LIT:0> ) <EOL> def send ( nodes ) : <EOL> def how_many_reached ( responses ) : <EOL> count = <NUM_LIT:0> <EOL> for resp in responses : <EOL> if resp [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] and resp [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == "<STR_LIT:True>" : <EOL> count += <NUM_LIT:1> <EOL> return count <EOL> ds = [ ] <EOL> signature = self . signing_key . sign ( str ( message ) ) [ : <NUM_LIT:64> ] <EOL> for n in nodes : <EOL> if n [ <NUM_LIT:1> ] is not None : <EOL> ds . append ( self . protocol . callBroadcast ( n [ <NUM_LIT:1> ] , message , signature ) ) <EOL> return defer . DeferredList ( ds ) . addCallback ( how_many_reached ) <EOL> dl = [ ] <EOL> f = objects . Followers ( ) <EOL> f . ParseFromString ( self . db . follow . get_followers ( ) ) <EOL> for follower in f . followers : <EOL> dl . append ( self . kserver . resolve ( follower . guid ) ) <EOL> self . log . info ( "<STR_LIT>" % message ) <EOL> return defer . DeferredList ( dl ) . addCallback ( send ) <EOL> def send_message ( self , receiving_node , public_key , message_type , message , subject = None , store_only = False ) : <EOL> """<STR_LIT>""" <EOL> pro = Profile ( self . db ) . get ( ) <EOL> p = objects . PlaintextMessage ( ) <EOL> p . sender_guid = self . kserver . node . id <EOL> p . pubkey = self . kserver . node . pubkey <EOL> p . type = message_type <EOL> p . message = str ( message ) <EOL> if subject is not None : <EOL> p . subject = subject <EOL> if pro . handle : <EOL> p . handle = pro . handle <EOL> if pro . avatar_hash : <EOL> p . avatar_hash = pro . avatar_hash <EOL> p . timestamp = int ( time . time ( ) ) <EOL> signature = self . signing_key . sign ( p . SerializeToString ( ) ) [ : <NUM_LIT:64> ] <EOL> p . signature = signature <EOL> skephem = PrivateKey . generate ( ) <EOL> pkephem = skephem . public_key . encode ( nacl . encoding . RawEncoder ) <EOL> box = Box ( skephem , PublicKey ( public_key ) ) <EOL> nonce = nacl . utils . random ( Box . NONCE_SIZE ) <EOL> ciphertext = box . encrypt ( p . SerializeToString ( ) , nonce ) <EOL> def get_response ( response ) : <EOL> if not response [ <NUM_LIT:0> ] : <EOL> ciphertext = box . encrypt ( p . SerializeToString ( ) . encode ( "<STR_LIT>" ) , nonce ) <EOL> self . kserver . set ( digest ( receiving_node . id ) , pkephem , ciphertext ) <EOL> self . log . info ( "<STR_LIT>" % receiving_node . id . encode ( "<STR_LIT>" ) ) <EOL> if not store_only : <EOL> self . protocol . callMessage ( receiving_node , pkephem , ciphertext ) . addCallback ( get_response ) <EOL> else : <EOL> get_response ( [ False ] ) <EOL> def get_messages ( self , listener ) : <EOL> if self . protocol . multiplexer is None or self . protocol . multiplexer . transport is None : <EOL> return task . deferLater ( reactor , <NUM_LIT:1> , self . get_messages , listener ) <EOL> def parse_messages ( messages ) : <EOL> if messages is not None : <EOL> self . log . info ( "<STR_LIT>" % len ( messages ) ) <EOL> for message in messages : <EOL> try : <EOL> value = objects . Value ( ) <EOL> value . ParseFromString ( message ) <EOL> try : <EOL> box = Box ( self . signing_key . to_curve25519_private_key ( ) , PublicKey ( value . valueKey ) ) <EOL> ciphertext = value . serializedData <EOL> plaintext = box . decrypt ( ciphertext ) . decode ( "<STR_LIT>" ) <EOL> p = objects . PlaintextMessage ( ) <EOL> p . ParseFromString ( plaintext ) <EOL> signature = p . signature <EOL> p . ClearField ( "<STR_LIT>" ) <EOL> verify_key = nacl . signing . VerifyKey ( p . pubkey ) <EOL> verify_key . verify ( p . SerializeToString ( ) , signature ) <EOL> h = nacl . hash . sha512 ( p . pubkey ) <EOL> pow_hash = h [ <NUM_LIT> : ] <EOL> if int ( pow_hash [ : <NUM_LIT:6> ] , <NUM_LIT:16> ) >= <NUM_LIT:50> or p . sender_guid . encode ( "<STR_LIT>" ) != h [ : <NUM_LIT> ] : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if p . type == objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) : <EOL> c = Contract ( self . db , hash_value = unhexlify ( p . subject ) , <EOL> testnet = self . protocol . multiplexer . testnet ) <EOL> c . accept_order_confirmation ( self . protocol . get_notification_listener ( ) , <EOL> confirmation_json = p . message ) <EOL> elif p . type == objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) : <EOL> c = Contract ( self . db , hash_value = unhexlify ( p . subject ) , <EOL> testnet = self . protocol . multiplexer . testnet ) <EOL> c . accept_receipt ( self . protocol . get_notification_listener ( ) , <EOL> self . protocol . multiplexer . blockchain , <EOL> receipt_json = p . message ) <EOL> elif p . type == objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) : <EOL> process_dispute ( json . loads ( p . message , object_pairs_hook = OrderedDict ) , <EOL> self . db , self . protocol . get_message_listener ( ) , <EOL> self . protocol . get_notification_listener ( ) , <EOL> self . protocol . multiplexer . testnet ) <EOL> elif p . type == objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) : <EOL> close_dispute ( json . loads ( p . message , object_pairs_hook = OrderedDict ) , <EOL> self . db , self . protocol . get_message_listener ( ) , <EOL> self . protocol . get_notification_listener ( ) , <EOL> self . protocol . multiplexer . testnet ) <EOL> elif p . type == objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) : <EOL> refund_json = json . loads ( p . message , object_pairs_hook = OrderedDict ) <EOL> c = Contract ( self . db , hash_value = unhexlify ( refund_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) , <EOL> testnet = self . protocol . multiplexer . testnet ) <EOL> c . process_refund ( refund_json , self . protocol . multiplexer . blockchain , <EOL> self . protocol . get_notification_listener ( ) ) <EOL> else : <EOL> listener . notify ( p , signature ) <EOL> except Exception : <EOL> pass <EOL> signature = self . signing_key . sign ( value . valueKey ) [ : <NUM_LIT:64> ] <EOL> self . kserver . delete ( self . kserver . node . id , value . valueKey , signature ) <EOL> except Exception : <EOL> pass <EOL> self . kserver . get ( self . kserver . node . id , False ) . addCallback ( parse_messages ) <EOL> def purchase ( self , node_to_ask , contract ) : <EOL> """<STR_LIT>""" <EOL> def parse_response ( response ) : <EOL> try : <EOL> address = contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:address>" ] <EOL> chaincode = contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> masterkey_b = contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> buyer_key = derive_childkey ( masterkey_b , chaincode ) <EOL> amount = contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> listing_hash = contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> verify_key = nacl . signing . VerifyKey ( node_to_ask . pubkey ) <EOL> verify_key . verify ( <EOL> str ( address ) + str ( amount ) + str ( listing_hash ) + str ( buyer_key ) , response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> return response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> except Exception : <EOL> return False <EOL> public_key = nacl . signing . VerifyKey ( contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) <EOL> skephem = PrivateKey . generate ( ) <EOL> pkephem = skephem . public_key . encode ( nacl . encoding . RawEncoder ) <EOL> box = Box ( skephem , public_key ) <EOL> nonce = nacl . utils . random ( Box . NONCE_SIZE ) <EOL> ciphertext = box . encrypt ( json . dumps ( contract . contract , indent = <NUM_LIT:4> ) , nonce ) <EOL> d = self . protocol . callOrder ( node_to_ask , pkephem , ciphertext ) <EOL> self . log . info ( "<STR_LIT>" % ( contract . get_contract_id ( ) , node_to_ask ) ) <EOL> return d . addCallback ( parse_response ) <EOL> def confirm_order ( self , guid , contract ) : <EOL> """<STR_LIT>""" <EOL> def get_node ( node_to_ask ) : <EOL> def parse_response ( response ) : <EOL> if response [ <NUM_LIT:0> ] and response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == "<STR_LIT:True>" : <EOL> return True <EOL> elif not response [ <NUM_LIT:0> ] : <EOL> contract_dict = json . loads ( json . dumps ( contract . contract , indent = <NUM_LIT:4> ) , <EOL> object_pairs_hook = OrderedDict ) <EOL> del contract_dict [ "<STR_LIT>" ] <EOL> order_id = digest ( json . dumps ( contract_dict , indent = <NUM_LIT:4> ) ) . encode ( "<STR_LIT>" ) <EOL> self . send_message ( Node ( unhexlify ( guid ) ) , <EOL> nacl . signing . VerifyKey ( <EOL> contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) . encode ( ) , <EOL> objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) , <EOL> json . dumps ( contract . contract [ "<STR_LIT>" ] ) , <EOL> order_id , <EOL> store_only = True ) <EOL> return True <EOL> else : <EOL> return response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if node_to_ask : <EOL> public_key = nacl . signing . VerifyKey ( <EOL> contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) <EOL> skephem = PrivateKey . generate ( ) <EOL> pkephem = skephem . public_key . encode ( nacl . encoding . RawEncoder ) <EOL> box = Box ( skephem , public_key ) <EOL> nonce = nacl . utils . random ( Box . NONCE_SIZE ) <EOL> ciphertext = box . encrypt ( json . dumps ( contract . contract , indent = <NUM_LIT:4> ) , nonce ) <EOL> d = self . protocol . callOrderConfirmation ( node_to_ask , pkephem , ciphertext ) <EOL> return d . addCallback ( parse_response ) <EOL> else : <EOL> return parse_response ( [ False ] ) <EOL> self . log . info ( "<STR_LIT>" % guid ) <EOL> return self . kserver . resolve ( unhexlify ( guid ) ) . addCallback ( get_node ) <EOL> def complete_order ( self , guid , contract ) : <EOL> """<STR_LIT>""" <EOL> def get_node ( node_to_ask ) : <EOL> def parse_response ( response ) : <EOL> if response [ <NUM_LIT:0> ] and response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == "<STR_LIT:True>" : <EOL> return True <EOL> elif not response [ <NUM_LIT:0> ] : <EOL> contract_dict = json . loads ( json . dumps ( contract . contract , indent = <NUM_LIT:4> ) , <EOL> object_pairs_hook = OrderedDict ) <EOL> del contract_dict [ "<STR_LIT>" ] <EOL> del contract_dict [ "<STR_LIT>" ] <EOL> order_id = digest ( json . dumps ( contract_dict , indent = <NUM_LIT:4> ) ) . encode ( "<STR_LIT>" ) <EOL> self . send_message ( Node ( unhexlify ( guid ) ) , <EOL> nacl . signing . VerifyKey ( <EOL> contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) . encode ( ) , <EOL> objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) , <EOL> json . dumps ( contract . contract [ "<STR_LIT>" ] ) , <EOL> order_id , <EOL> store_only = True ) <EOL> return True <EOL> else : <EOL> return response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if node_to_ask : <EOL> public_key = nacl . signing . VerifyKey ( <EOL> contract . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) <EOL> skephem = PrivateKey . generate ( ) <EOL> pkephem = skephem . public_key . encode ( nacl . encoding . RawEncoder ) <EOL> box = Box ( skephem , public_key ) <EOL> nonce = nacl . utils . random ( Box . NONCE_SIZE ) <EOL> ciphertext = box . encrypt ( json . dumps ( contract . contract , indent = <NUM_LIT:4> ) , nonce ) <EOL> d = self . protocol . callCompleteOrder ( node_to_ask , pkephem , ciphertext ) <EOL> return d . addCallback ( parse_response ) <EOL> else : <EOL> return parse_response ( [ False ] ) <EOL> self . log . info ( "<STR_LIT>" % guid ) <EOL> return self . kserver . resolve ( unhexlify ( guid ) ) . addCallback ( get_node ) <EOL> def open_dispute ( self , order_id , claim ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) <EOL> with open ( file_path , '<STR_LIT:r>' ) as filename : <EOL> contract = json . load ( filename , object_pairs_hook = OrderedDict ) <EOL> guid = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] <EOL> handle = "<STR_LIT>" <EOL> if "<STR_LIT>" in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] : <EOL> handle = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] <EOL> guid_key = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> proof_sig = self . db . purchases . get_proof_sig ( order_id ) <EOL> except Exception : <EOL> try : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) <EOL> with open ( file_path , '<STR_LIT:r>' ) as filename : <EOL> contract = json . load ( filename , object_pairs_hook = OrderedDict ) <EOL> guid = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] <EOL> handle = "<STR_LIT>" <EOL> if "<STR_LIT>" in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] : <EOL> handle = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] <EOL> guid_key = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> proof_sig = None <EOL> except Exception : <EOL> return False <EOL> if "<STR_LIT>" not in contract : <EOL> keychain = KeyChain ( self . db ) <EOL> contract [ "<STR_LIT>" ] = { } <EOL> contract [ "<STR_LIT>" ] [ "<STR_LIT:info>" ] = { } <EOL> contract [ "<STR_LIT>" ] [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] = claim <EOL> contract [ "<STR_LIT>" ] [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] = keychain . guid . encode ( "<STR_LIT>" ) <EOL> contract [ "<STR_LIT>" ] [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] = Profile ( self . db ) . get ( ) . avatar_hash . encode ( "<STR_LIT>" ) <EOL> if proof_sig : <EOL> contract [ "<STR_LIT>" ] [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] = base64 . b64encode ( proof_sig ) <EOL> info = json . dumps ( contract [ "<STR_LIT>" ] [ "<STR_LIT:info>" ] , indent = <NUM_LIT:4> ) <EOL> contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] = base64 . b64encode ( keychain . signing_key . sign ( info ) [ : <NUM_LIT:64> ] ) <EOL> with open ( file_path , '<STR_LIT:wb>' ) as outfile : <EOL> outfile . write ( json . dumps ( contract , indent = <NUM_LIT:4> ) ) <EOL> if self . db . purchases . get_purchase ( order_id ) is not None : <EOL> self . db . purchases . update_status ( order_id , <NUM_LIT:4> ) <EOL> elif self . db . sales . get_sale ( order_id ) is not None : <EOL> self . db . sales . update_status ( order_id , <NUM_LIT:4> ) <EOL> avatar_hash = Profile ( self . db ) . get ( ) . avatar_hash <EOL> self . db . messages . save_message ( guid , handle , "<STR_LIT>" , order_id , "<STR_LIT>" , <EOL> claim , time . time ( ) , avatar_hash , "<STR_LIT>" , True ) <EOL> mod_guid = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> for mod in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> if mod [ "<STR_LIT>" ] == mod_guid : <EOL> mod_key = mod [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> def get_node ( node_to_ask , recipient_guid , public_key ) : <EOL> def parse_response ( response ) : <EOL> if not response [ <NUM_LIT:0> ] : <EOL> self . send_message ( Node ( unhexlify ( recipient_guid ) ) , <EOL> nacl . signing . VerifyKey ( <EOL> public_key , <EOL> encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) . encode ( ) , <EOL> objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) , <EOL> json . dumps ( contract ) , <EOL> order_id , <EOL> store_only = True ) <EOL> if node_to_ask : <EOL> enc_key = nacl . signing . VerifyKey ( <EOL> public_key , encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) <EOL> skephem = PrivateKey . generate ( ) <EOL> pkephem = skephem . public_key . encode ( nacl . encoding . RawEncoder ) <EOL> box = Box ( skephem , enc_key ) <EOL> nonce = nacl . utils . random ( Box . NONCE_SIZE ) <EOL> ciphertext = box . encrypt ( json . dumps ( contract , indent = <NUM_LIT:4> ) , nonce ) <EOL> d = self . protocol . callDisputeOpen ( node_to_ask , pkephem , ciphertext ) <EOL> return d . addCallback ( parse_response ) <EOL> else : <EOL> return parse_response ( [ False ] ) <EOL> self . kserver . resolve ( unhexlify ( guid ) ) . addCallback ( get_node , guid , guid_key ) <EOL> self . kserver . resolve ( unhexlify ( mod_guid ) ) . addCallback ( get_node , mod_guid , mod_key ) <EOL> def close_dispute ( self , order_id , resolution , buyer_percentage , <EOL> vendor_percentage , moderator_percentage , moderator_address ) : <EOL> """<STR_LIT>""" <EOL> if float ( vendor_percentage ) < <NUM_LIT:0> or float ( moderator_percentage ) < <NUM_LIT:0> or float ( buyer_percentage ) < <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if float ( vendor_percentage ) + float ( buyer_percentage ) > <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if not self . protocol . multiplexer . blockchain . connected : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if not self . protocol . multiplexer . testnet and not ( moderator_address [ : <NUM_LIT:1> ] == "<STR_LIT:1>" or moderator_address [ : <NUM_LIT:1> ] == "<STR_LIT:3>" ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> elif self . protocol . multiplexer . testnet and not ( moderator_address [ : <NUM_LIT:1> ] == "<STR_LIT:n>" or moderator_address [ : <NUM_LIT:1> ] == "<STR_LIT:m>" or moderator_address [ : <NUM_LIT:1> ] == "<STR_LIT:2>" ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> try : <EOL> bitcointools . b58check_to_hex ( moderator_address ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> with open ( os . path . join ( DATA_FOLDER , "<STR_LIT>" , order_id + "<STR_LIT>" ) , "<STR_LIT:r>" ) as filename : <EOL> contract = json . load ( filename , object_pairs_hook = OrderedDict ) <EOL> buyer_address = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> buyer_guid = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] <EOL> buyer_enc_key = nacl . signing . VerifyKey ( <EOL> contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) <EOL> vendor_guid = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] <EOL> vendor_enc_key = nacl . signing . VerifyKey ( <EOL> contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) <EOL> payment_address = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:address>" ] <EOL> def history_fetched ( ec , history ) : <EOL> outpoints = [ ] <EOL> satoshis = <NUM_LIT:0> <EOL> outputs = [ ] <EOL> dispute_json = { "<STR_LIT>" : { "<STR_LIT>" : { } } } <EOL> timeout . cancel ( ) <EOL> if ec : <EOL> print ec <EOL> else : <EOL> for tx_type , txid , i , height , value in history : <EOL> if tx_type == obelisk . PointIdent . Output : <EOL> satoshis += value <EOL> o = { <EOL> "<STR_LIT>" : txid . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : i , <EOL> "<STR_LIT:value>" : value , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> if o not in outpoints : <EOL> outpoints . append ( o ) <EOL> if satoshis <= <NUM_LIT:0> : <EOL> d . callback ( False ) <EOL> else : <EOL> satoshis -= TRANSACTION_FEE <EOL> moderator_fee = int ( float ( moderator_percentage ) * satoshis ) <EOL> satoshis -= moderator_fee <EOL> if moderator_fee > <NUM_LIT:0> : <EOL> outputs . append ( { '<STR_LIT:value>' : moderator_fee , '<STR_LIT:address>' : moderator_address } ) <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = moderator_address <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = round ( moderator_fee / float ( <NUM_LIT> ) , <NUM_LIT:8> ) <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = round ( TRANSACTION_FEE / float ( <NUM_LIT> ) , <NUM_LIT:8> ) <EOL> if float ( buyer_percentage ) > <NUM_LIT:0> : <EOL> amt = int ( float ( buyer_percentage ) * satoshis ) <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = round ( amt / float ( <NUM_LIT> ) , <NUM_LIT:8> ) <EOL> outputs . append ( { '<STR_LIT:value>' : amt , <EOL> '<STR_LIT:address>' : buyer_address } ) <EOL> if float ( vendor_percentage ) > <NUM_LIT:0> : <EOL> amt = int ( float ( vendor_percentage ) * satoshis ) <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = round ( amt / float ( <NUM_LIT> ) , <NUM_LIT:8> ) <EOL> outputs . append ( { '<STR_LIT:value>' : amt , <EOL> '<STR_LIT:address>' : vendor_address } ) <EOL> tx = BitcoinTransaction . make_unsigned ( outpoints , outputs , <EOL> testnet = self . protocol . multiplexer . testnet ) <EOL> chaincode = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> redeem_script = str ( contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> masterkey_m = bitcointools . bip32_extract_key ( KeyChain ( self . db ) . bitcoin_master_privkey ) <EOL> moderator_priv = derive_childkey ( masterkey_m , chaincode , bitcointools . MAINNET_PRIVATE ) <EOL> signatures = tx . create_signature ( moderator_priv , redeem_script ) <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = order_id <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = signatures <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = self . db . cases . get_claim ( order_id ) <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = resolution <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] = base64 . b64encode ( KeyChain ( self . db ) . signing_key . sign ( json . dumps ( <EOL> dispute_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] , indent = <NUM_LIT:4> ) ) [ : <NUM_LIT:64> ] ) <EOL> contract [ "<STR_LIT>" ] = dispute_json [ "<STR_LIT>" ] <EOL> with open ( DATA_FOLDER + "<STR_LIT>" + order_id + "<STR_LIT>" , '<STR_LIT:wb>' ) as outfile : <EOL> outfile . write ( json . dumps ( contract , indent = <NUM_LIT:4> ) ) <EOL> send ( dispute_json ) <EOL> def send ( dispute_json ) : <EOL> def get_node ( node_to_ask , recipient_guid , public_key ) : <EOL> def parse_response ( response ) : <EOL> if not response [ <NUM_LIT:0> ] : <EOL> self . send_message ( Node ( unhexlify ( recipient_guid ) ) , <EOL> public_key . encode ( ) , <EOL> objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) , <EOL> dispute_json , <EOL> order_id , <EOL> store_only = True ) <EOL> if node_to_ask : <EOL> skephem = PrivateKey . generate ( ) <EOL> pkephem = skephem . public_key . encode ( nacl . encoding . RawEncoder ) <EOL> box = Box ( skephem , public_key ) <EOL> nonce = nacl . utils . random ( Box . NONCE_SIZE ) <EOL> ciphertext = box . encrypt ( json . dumps ( dispute_json , indent = <NUM_LIT:4> ) , nonce ) <EOL> self . protocol . callDisputeClose ( node_to_ask , pkephem , ciphertext ) . addCallback ( parse_response ) <EOL> else : <EOL> parse_response ( [ False ] ) <EOL> self . kserver . resolve ( unhexlify ( vendor_guid ) ) . addCallback ( get_node , vendor_guid , vendor_enc_key ) <EOL> self . kserver . resolve ( unhexlify ( buyer_guid ) ) . addCallback ( get_node , buyer_guid , buyer_enc_key ) <EOL> self . db . cases . update_status ( order_id , <NUM_LIT:1> ) <EOL> d . callback ( True ) <EOL> d = defer . Deferred ( ) <EOL> if "<STR_LIT>" not in contract : <EOL> if "<STR_LIT>" in contract and float ( vendor_percentage ) > <NUM_LIT:0> : <EOL> vendor_address = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:address>" ] <EOL> elif "<STR_LIT>" not in contract and float ( vendor_percentage ) > <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def libbitcoin_timeout ( ) : <EOL> d . callback ( "<STR_LIT>" ) <EOL> timeout = reactor . callLater ( <NUM_LIT:5> , libbitcoin_timeout ) <EOL> self . protocol . multiplexer . blockchain . fetch_history2 ( payment_address , history_fetched ) <EOL> else : <EOL> dispute_json = { "<STR_LIT>" : contract [ "<STR_LIT>" ] } <EOL> send ( dispute_json ) <EOL> return d <EOL> def release_funds ( self , order_id ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) <EOL> outpoints = json . loads ( self . db . purchases . get_outpoint ( order_id ) ) <EOL> elif os . path . exists ( os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) <EOL> outpoints = json . loads ( self . db . sales . get_outpoint ( order_id ) ) <EOL> with open ( file_path , '<STR_LIT:r>' ) as filename : <EOL> contract = json . load ( filename , object_pairs_hook = OrderedDict ) <EOL> outputs = [ ] <EOL> moderator_fee = int ( round ( float ( contract [ "<STR_LIT>" ] <EOL> [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) * <NUM_LIT> ) ) <EOL> if moderator_fee > <NUM_LIT:0> : <EOL> outputs . append ( { '<STR_LIT:value>' : moderator_fee , <EOL> '<STR_LIT:address>' : contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] } ) <EOL> if "<STR_LIT>" in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> buyer_address = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> outputs . append ( { '<STR_LIT:value>' : int ( round ( float ( contract [ "<STR_LIT>" ] <EOL> [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) * <NUM_LIT> ) ) , <EOL> '<STR_LIT:address>' : buyer_address } ) <EOL> if "<STR_LIT>" in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> vendor_address = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:address>" ] <EOL> outputs . append ( { '<STR_LIT:value>' : int ( round ( float ( contract [ "<STR_LIT>" ] <EOL> [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) * <NUM_LIT> ) ) , <EOL> '<STR_LIT:address>' : vendor_address } ) <EOL> tx = BitcoinTransaction . make_unsigned ( outpoints , outputs , testnet = self . protocol . multiplexer . testnet ) <EOL> chaincode = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> redeem_script = str ( contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> masterkey = bitcointools . bip32_extract_key ( KeyChain ( self . db ) . bitcoin_master_privkey ) <EOL> childkey = derive_childkey ( masterkey , chaincode , bitcointools . MAINNET_PRIVATE ) <EOL> own_sig = tx . create_signature ( childkey , redeem_script ) <EOL> signatures = [ ] <EOL> for index in range ( <NUM_LIT:0> , len ( outpoints ) ) : <EOL> sig_ob = { "<STR_LIT:index>" : index , "<STR_LIT>" : [ ] } <EOL> for s in own_sig : <EOL> if int ( s [ "<STR_LIT:index>" ] ) == index : <EOL> sig_ob [ "<STR_LIT>" ] . append ( s [ "<STR_LIT>" ] ) <EOL> for s in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> if int ( s [ "<STR_LIT:index>" ] ) == index : <EOL> sig_ob [ "<STR_LIT>" ] . append ( s [ "<STR_LIT>" ] ) <EOL> signatures . append ( sig_ob ) <EOL> tx . multisign ( signatures , redeem_script ) <EOL> tx . broadcast ( self . protocol . multiplexer . blockchain ) <EOL> self . db . transactions . add_transaction ( tx . to_raw_tx ( ) ) <EOL> self . log . info ( "<STR_LIT>" % tx . get_hash ( ) ) <EOL> if self . db . purchases . get_purchase ( order_id ) is not None : <EOL> self . db . purchases . update_status ( order_id , <NUM_LIT:6> ) <EOL> elif self . db . sales . get_sale ( order_id ) is not None : <EOL> self . db . sales . update_status ( order_id , <NUM_LIT:6> ) <EOL> def get_ratings ( self , node_to_ask , listing_hash = None ) : <EOL> """<STR_LIT>""" <EOL> def get_result ( result ) : <EOL> try : <EOL> verify_key = nacl . signing . VerifyKey ( node_to_ask . pubkey ) <EOL> verify_key . verify ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> ratings = json . loads ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT>" ) , object_pairs_hook = OrderedDict ) <EOL> ret = [ ] <EOL> for rating in ratings : <EOL> address = rating [ "<STR_LIT>" ] [ "<STR_LIT:address>" ] <EOL> buyer_key = rating [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> amount = rating [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> listing_hash = rating [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> proof_sig = rating [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> try : <EOL> verify_key . verify ( str ( address ) + str ( amount ) + str ( listing_hash ) + str ( buyer_key ) , <EOL> base64 . b64decode ( proof_sig ) ) <EOL> valid = bitcointools . ecdsa_raw_verify ( json . dumps ( rating [ "<STR_LIT>" ] , indent = <NUM_LIT:4> ) , <EOL> bitcointools . decode_sig ( rating [ "<STR_LIT>" ] ) , <EOL> buyer_key ) <EOL> if not valid : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in rating [ "<STR_LIT>" ] or "<STR_LIT>" in rating [ "<STR_LIT>" ] : <EOL> buyer_key_bin = unhexlify ( rating [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> buyer_key = nacl . signing . VerifyKey ( buyer_key_bin ) <EOL> buyer_key . verify ( json . dumps ( rating [ "<STR_LIT>" ] , indent = <NUM_LIT:4> ) , <EOL> base64 . b64decode ( rating [ "<STR_LIT>" ] ) ) <EOL> h = nacl . hash . sha512 ( buyer_key_bin ) <EOL> pow_hash = h [ <NUM_LIT> : ] <EOL> if int ( pow_hash [ : <NUM_LIT:6> ] , <NUM_LIT:16> ) >= <NUM_LIT:50> or rating [ "<STR_LIT>" ] [ "<STR_LIT>" ] != h [ : <NUM_LIT> ] : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> ret . append ( rating ) <EOL> except Exception : <EOL> pass <EOL> return ret <EOL> except Exception : <EOL> return None <EOL> if node_to_ask . ip is None : <EOL> return defer . succeed ( None ) <EOL> a = "<STR_LIT>" if listing_hash is None else listing_hash . encode ( "<STR_LIT>" ) <EOL> self . log . info ( "<STR_LIT>" % ( a , node_to_ask ) ) <EOL> d = self . protocol . callGetRatings ( node_to_ask , listing_hash ) <EOL> return d . addCallback ( get_result ) <EOL> def refund ( self , order_id ) : <EOL> """<STR_LIT>""" <EOL> file_path = os . path . join ( DATA_FOLDER + "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) <EOL> if not os . path . exists ( file_path ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) <EOL> outpoints = json . loads ( self . db . sales . get_outpoint ( order_id ) ) <EOL> with open ( file_path , '<STR_LIT:r>' ) as filename : <EOL> contract = json . load ( filename , object_pairs_hook = OrderedDict ) <EOL> buyer_guid = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] <EOL> buyer_enc_key = nacl . signing . VerifyKey ( <EOL> contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> encoder = nacl . encoding . HexEncoder ) . to_curve25519_public_key ( ) <EOL> if "<STR_LIT>" in contract : <EOL> refund_json = { "<STR_LIT>" : contract [ "<STR_LIT>" ] } <EOL> else : <EOL> refund_address = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> chaincode = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> masterkey_v = bitcointools . bip32_extract_key ( KeyChain ( self . db ) . bitcoin_master_privkey ) <EOL> vendor_priv = derive_childkey ( masterkey_v , chaincode , bitcointools . MAINNET_PRIVATE ) <EOL> refund_json = { "<STR_LIT>" : { } } <EOL> refund_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] = order_id <EOL> if "<STR_LIT>" in contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> in_value = <NUM_LIT:0> <EOL> for outpoint in outpoints : <EOL> in_value += outpoint [ "<STR_LIT:value>" ] <EOL> out_value = in_value - long ( contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> tx = BitcoinTransaction . make_unsigned ( outpoints , refund_address , <EOL> testnet = self . protocol . multiplexer . testnet , <EOL> out_value = out_value ) <EOL> redeem_script = contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> sigs = tx . create_signature ( vendor_priv , redeem_script ) <EOL> refund_json [ "<STR_LIT>" ] [ "<STR_LIT:value>" ] = round ( tx . get_out_value ( ) / float ( <NUM_LIT> ) , <NUM_LIT:8> ) <EOL> refund_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] = sigs <EOL> else : <EOL> tx = BitcoinTransaction . make_unsigned ( outpoints , refund_address , <EOL> testnet = self . protocol . multiplexer . testnet ) <EOL> tx . sign ( vendor_priv ) <EOL> tx . broadcast ( self . protocol . multiplexer . blockchain ) <EOL> self . db . transactions . add_transaction ( tx . to_raw_tx ( ) ) <EOL> self . log . info ( "<STR_LIT>" % tx . get_hash ( ) ) <EOL> refund_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] = tx . get_hash ( ) <EOL> contract [ "<STR_LIT>" ] = refund_json [ "<STR_LIT>" ] <EOL> self . db . sales . update_status ( order_id , <NUM_LIT:7> ) <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) <EOL> with open ( file_path , '<STR_LIT:w>' ) as outfile : <EOL> outfile . write ( json . dumps ( contract , indent = <NUM_LIT:4> ) ) <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , order_id + "<STR_LIT>" ) <EOL> if os . path . exists ( file_path ) : <EOL> os . remove ( file_path ) <EOL> def get_node ( node_to_ask ) : <EOL> def parse_response ( response ) : <EOL> if response [ <NUM_LIT:0> ] and response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == "<STR_LIT:True>" : <EOL> return True <EOL> if not response [ <NUM_LIT:0> ] : <EOL> self . send_message ( Node ( unhexlify ( buyer_guid ) ) , <EOL> buyer_enc_key . encode ( ) , <EOL> objects . PlaintextMessage . Type . Value ( "<STR_LIT>" ) , <EOL> refund_json , <EOL> order_id , <EOL> store_only = True ) <EOL> return True <EOL> else : <EOL> return response [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if node_to_ask : <EOL> skephem = PrivateKey . generate ( ) <EOL> pkephem = skephem . public_key . encode ( nacl . encoding . RawEncoder ) <EOL> box = Box ( skephem , buyer_enc_key ) <EOL> nonce = nacl . utils . random ( Box . NONCE_SIZE ) <EOL> ciphertext = box . encrypt ( json . dumps ( refund_json , indent = <NUM_LIT:4> ) , nonce ) <EOL> d = self . protocol . callRefund ( node_to_ask , pkephem , ciphertext ) <EOL> return d . addCallback ( parse_response ) <EOL> else : <EOL> return parse_response ( [ False ] ) <EOL> self . log . info ( "<STR_LIT>" % buyer_guid ) <EOL> return self . kserver . resolve ( unhexlify ( buyer_guid ) ) . addCallback ( get_node ) <EOL> def update_listings ( self ) : <EOL> try : <EOL> if self . protocol . multiplexer is None : <EOL> return reactor . callLater ( <NUM_LIT:1> , self . update_listings ) <EOL> fname = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" ) <EOL> if os . path . exists ( fname ) : <EOL> with open ( fname , '<STR_LIT:r>' ) as f : <EOL> data = pickle . load ( f ) <EOL> else : <EOL> data = { } <EOL> l = objects . Listings ( ) <EOL> l . ParseFromString ( self . db . listings . get_proto ( ) ) <EOL> for listing in l . listing : <EOL> contract_hash = listing . contract_hash <EOL> c = Contract ( self . db , hash_value = contract_hash , testnet = self . protocol . multiplexer . testnet ) <EOL> if contract_hash not in data or time . time ( ) - data [ contract_hash ] > <NUM_LIT> : <EOL> for keyword in c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> self . kserver . set ( digest ( keyword . lower ( ) ) , unhexlify ( c . get_contract_id ( ) ) , <EOL> self . kserver . node . getProto ( ) . SerializeToString ( ) ) <EOL> data [ contract_hash ] = time . time ( ) <EOL> if c . check_expired ( ) : <EOL> c . delete ( True ) <EOL> if contract_hash in data : <EOL> del data [ contract_hash ] <EOL> guid = KeyChain ( self . db ) . guid <EOL> moderator = Profile ( self . db ) . get ( ) . moderator <EOL> if ( guid not in data or time . time ( ) - data [ guid ] > <NUM_LIT> ) and moderator : <EOL> self . make_moderator ( ) <EOL> data [ guid ] = time . time ( ) <EOL> with open ( fname , '<STR_LIT:w>' ) as f : <EOL> pickle . dump ( data , f ) <EOL> except Exception : <EOL> pass <EOL> def update_moderators_on_listings ( self , moderator_list ) : <EOL> try : <EOL> l = objects . Listings ( ) <EOL> l . ParseFromString ( self . db . listings . get_proto ( ) ) <EOL> except Exception : <EOL> return <EOL> keychain = KeyChain ( self . db ) <EOL> for listing in l . listing : <EOL> try : <EOL> contract_hash = listing . contract_hash <EOL> c = Contract ( self . db , hash_value = contract_hash , testnet = self . protocol . multiplexer . testnet ) <EOL> contract_moderators = [ ] <EOL> if "<STR_LIT>" in c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> for m in c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> contract_moderators . append ( m [ "<STR_LIT>" ] ) <EOL> mods_to_remove = list ( set ( contract_moderators ) - set ( moderator_list ) ) <EOL> mods_to_add = list ( set ( moderator_list ) - set ( contract_moderators ) ) <EOL> for mod in mods_to_add : <EOL> mod_info = self . db . moderators . get_moderator ( mod ) <EOL> if mod_info is not None : <EOL> moderator_json = { <EOL> "<STR_LIT>" : mod , <EOL> "<STR_LIT:name>" : mod_info [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : mod_info [ <NUM_LIT:7> ] . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : mod_info [ <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : str ( mod_info [ <NUM_LIT:8> ] ) + "<STR_LIT:%>" , <EOL> "<STR_LIT>" : mod_info [ <NUM_LIT:4> ] , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : mod_info [ <NUM_LIT:1> ] . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:key>" : mod_info [ <NUM_LIT:2> ] . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : base64 . b64encode ( mod_info [ <NUM_LIT:3> ] ) <EOL> } <EOL> } <EOL> } <EOL> if "<STR_LIT>" not in c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = [ ] <EOL> c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] . append ( moderator_json ) <EOL> for mod in mods_to_remove : <EOL> for rem in c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> if rem [ "<STR_LIT>" ] == mod : <EOL> c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] . remove ( rem ) <EOL> if "<STR_LIT>" in c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] and len ( c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) == <NUM_LIT:0> : <EOL> del c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> listing = json . dumps ( c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] , indent = <NUM_LIT:4> ) <EOL> c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] = { } <EOL> c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = base64 . b64encode ( keychain . signing_key . sign ( listing ) [ : <NUM_LIT:64> ] ) <EOL> c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = bitcointools . encode_sig ( * bitcointools . ecdsa_raw_sign ( <EOL> listing , bitcointools . bip32_extract_key ( keychain . bitcoin_master_privkey ) ) ) <EOL> c . previous_title = None <EOL> c . save ( ) <EOL> except Exception : <EOL> pass <EOL> @ staticmethod <EOL> def cache ( file_to_save , filename ) : <EOL> """<STR_LIT>""" <EOL> with open ( os . path . join ( DATA_FOLDER , "<STR_LIT>" , filename ) , '<STR_LIT:wb>' ) as outfile : <EOL> outfile . write ( file_to_save ) </s>
