def verify_message(cls, rid, message):
     from crypt import Crypt
     sent = False
     received = False
     txns = cls.get_transactions_by_rid(rid, rid=True, raw=True)
     shared_secrets = TU.get_shared_secrets_by_rid(rid)
     for txn in txns:
         for shared_secret in list(set(shared_secrets)):
             try:
                 cipher = Crypt(shared_secret.encode('hex'), shared=True)
                 decrypted = cipher.shared_decrypt(txn['relationship'])
                 signin = json.loads(decrypted)
                 if u'signIn' in signin and message == signin['signIn']:
                     if Config.public_key != txn['public_key']:
                         received = True
                     else:
                         sent = True
             except:
                 pass
     return sent, received
Example #2
0
    def __init__(self,
                 config,
                 mongo,
                 block_height,
                 bulletin_secret='',
                 username='',
                 value=0,
                 fee=0.0,
                 requester_rid='',
                 requested_rid='',
                 public_key='',
                 dh_public_key='',
                 private_key='',
                 dh_private_key='',
                 to='',
                 inputs='',
                 outputs='',
                 coinbase=False,
                 chattext=None,
                 signin=None):
        self.config = config
        self.mongo = mongo
        self.block_height = block_height
        self.bulletin_secret = bulletin_secret
        self.username = username
        self.requester_rid = requester_rid
        self.requested_rid = requested_rid
        self.public_key = public_key
        self.dh_public_key = dh_public_key
        self.private_key = private_key
        self.value = value
        self.fee = float(fee)
        self.dh_private_key = dh_private_key
        self.to = to
        self.time = str(int(time.time()))
        self.outputs = []
        for x in outputs:
            self.outputs.append(Output.from_dict(x))
        self.inputs = []
        for x in inputs:
            if 'signature' in x:
                self.inputs.append(
                    ExternalInput.from_dict(self.config, self.mongo, x))
            else:
                self.inputs.append(Input.from_dict(x))
        self.coinbase = coinbase
        self.chattext = chattext
        self.signin = signin
        self.do_money()
        inputs_concat = self.get_input_hashes()
        outputs_concat = self.get_output_hashes()
        if bulletin_secret:
            self.rid = self.generate_rid()
            if self.chattext:
                self.relationship = json.dumps({"chatText": self.chattext})
                self.cipher = Crypt(self.config.wif)
                self.encrypted_relationship = self.cipher.encrypt(
                    self.relationship)
            elif self.signin:
                for shared_secret in TU.get_shared_secrets_by_rid(
                        self.config, self.mongo, self.rid):
                    self.relationship = SignIn(self.signin)
                    self.cipher = Crypt(shared_secret.encode('hex'),
                                        shared=True)
                    self.encrypted_relationship = self.cipher.shared_encrypt(
                        self.relationship.to_json())
            else:
                if not self.dh_public_key or not self.dh_private_key:
                    a = os.urandom(32)
                    self.dh_public_key = scalarmult_base(a).encode('hex')
                    self.dh_private_key = a.encode('hex')
                self.relationship = self.generate_relationship()
                if not private_key:
                    raise BaseException('missing private key')
                self.cipher = Crypt(self.config.wif)
                self.encrypted_relationship = self.cipher.encrypt(
                    self.relationship.to_json())
        else:
            self.rid = ''
            self.encrypted_relationship = ''

        self.header = (self.public_key + self.time + self.dh_public_key +
                       self.rid + self.encrypted_relationship +
                       "{0:.8f}".format(self.fee) + self.requester_rid +
                       self.requested_rid + inputs_concat + outputs_concat)
        self.hash = hashlib.sha256(self.header).digest().encode('hex')
        if self.private_key:
            self.transaction_signature = TU.generate_signature_with_private_key(
                private_key, self.hash)
        else:
            self.transaction_signature = ''
        self.transaction = self.generate_transaction()
Example #3
0
    def __init__(self, config, mongo, bulletin_secret, ids, wallet=None):
        self.config = config
        self.mongo = mongo
        self.friend_requests = []
        self.sent_friend_requests = []
        self.friends = []
        self.posts = []
        self.logins = []
        self.messages = []
        self.new_messages = []
        self.reacts = []
        self.comments = []
        self.comment_reacts = []
        self.already_added_messages = []
        self.bulletin_secret = str(bulletin_secret)
        self.ids = ids

        all_relationships = [x for x in BU.get_all_usernames(config, mongo)]
        self.rid_usernames = dict([(x['rid'],
                                    x['relationship']['their_username'])
                                   for x in all_relationships])
        if wallet:  # disabling for now
            self.wallet_mode = True

            rids = [x['rid'] for x in all_relationships]
            self.rid_transactions = BU.get_transactions_by_rid(
                self.config,
                self.mongo,
                rids,
                bulletin_secret=wallet.bulletin_secret,
                rid=True,
                raw=True,
                returnheight=True)
        else:
            self.wallet_mode = False
            self.registered = False
            self.pending_registration = False
            bulletin_secrets = sorted(
                [str(config.bulletin_secret),
                 str(bulletin_secret)],
                key=str.lower)
            rid = hashlib.sha256(
                str(bulletin_secrets[0]) +
                str(bulletin_secrets[1])).digest().encode('hex')
            self.rid = rid

            res = self.mongo.site_db.usernames.find({"rid": self.rid})
            if res.count():
                self.human_hash = res[0]['username']
            else:
                self.human_hash = humanhash.humanize(self.rid)
            start_height = 0
            # this will get any transactions between the client and server
            nodes = BU.get_transactions_by_rid(self.config,
                                               self.mongo,
                                               bulletin_secret,
                                               config.bulletin_secret,
                                               raw=True,
                                               returnheight=True)
            already_done = []
            for node in nodes:
                if node.get('dh_public_key'):
                    test = {
                        'rid': node.get('rid'),
                        'requester_rid': node.get('requester_id'),
                        'requested_rid': node.get('requested_id'),
                        'id': node.get('id')
                    }
                    node['username'] = '******'
                    if test in already_done:
                        continue
                    else:
                        self.friends.append(node)
                        already_done.append(test)

            self.registered = False
            shared_secrets = TU.get_shared_secrets_by_rid(config, mongo, rid)
            if shared_secrets:
                self.registered = True

            if self.registered:
                for x in self.friends:
                    for y in x['outputs']:
                        if y['to'] != config.address:
                            self.mongo.site_db.usernames.update(
                                {
                                    'rid': self.rid,
                                    'username': self.human_hash,
                                }, {
                                    'rid': self.rid,
                                    'username': self.human_hash,
                                    'to': y['to'],
                                    'relationship': {
                                        'bulletin_secret': bulletin_secret
                                    }
                                },
                                upsert=True)
            else:
                # not regisered, let's check for a pending transaction
                res = self.mongo.db.miner_transactions.find({
                    'rid': self.rid,
                    'public_key': {
                        '$ne': self.config.public_key
                    }
                })
                res2 = self.mongo.db.miner_transactions.find({
                    'rid':
                    self.rid,
                    'public_key':
                    self.config.public_key
                })

                if res.count() and res2.count():
                    self.pending_registration = True
    def __init__(self,
                 bulletin_secret='',
                 username='',
                 value=0,
                 fee=0.0,
                 requester_rid='',
                 requested_rid='',
                 public_key='',
                 dh_public_key='',
                 private_key='',
                 dh_private_key='',
                 to='',
                 inputs='',
                 outputs='',
                 coinbase=False,
                 chattext=None,
                 signin=None):
        self.bulletin_secret = bulletin_secret
        self.username = username
        self.requester_rid = requester_rid
        self.requested_rid = requested_rid
        self.public_key = public_key
        self.dh_public_key = dh_public_key
        self.private_key = private_key
        self.value = value
        self.fee = float(fee)
        self.dh_private_key = dh_private_key
        self.to = to
        self.outputs = outputs or []
        self.inputs = inputs
        self.coinbase = coinbase
        self.chattext = chattext
        self.signin = signin
        self.do_money()
        inputs_concat = self.get_input_hashes()
        outputs_concat = self.get_output_hashes()
        if bulletin_secret:
            self.rid = self.generate_rid()
            if self.chattext:
                self.relationship = json.dumps({"chatText": self.chattext})
            elif self.signin:
                for shared_secret in TU.get_shared_secrets_by_rid(self.rid):
                    self.relationship = SignIn(self.signin)
                    self.cipher = Crypt(shared_secret.encode('hex'),
                                        shared=True)
                    self.encrypted_relationship = self.cipher.shared_encrypt(
                        self.relationship.to_json())
            else:
                self.relationship = self.generate_relationship()
                if not private_key:
                    raise BaseException('missing private key')
                self.cipher = Crypt(Config.wif)
                self.encrypted_relationship = self.cipher.encrypt(
                    self.relationship.to_json())
        else:
            self.rid = ''
            self.encrypted_relationship = ''
        self.hash = hashlib.sha256(self.dh_public_key + self.rid +
                                   self.encrypted_relationship +
                                   "{0:.8f}".format(self.fee) +
                                   self.requester_rid + self.requested_rid +
                                   inputs_concat +
                                   outputs_concat).digest().encode('hex')

        self.transaction_signature = self.generate_transaction_signature()
        self.transaction = self.generate_transaction()