def save(cls, items):
        Mongo.init()
        if not isinstance(items, list):
            items = [
                items.to_dict(),
            ]
        else:
            items = [item.to_dict() for item in items]

        for item in items:
            Mongo.db.miner_transactions.insert(item)
Пример #2
0
 def init_local(cls):
     Mongo.init()
     res = Mongo.db.peers.find({
         'active': True,
         'failed': {
             '$lt': 30
         }
     }, {'_id': 0})
     peers = [x for x in res]
     cls.peers = []
     try:
         for peer in peers:
             cls.peers.append(Peer(peer['host'], peer['port']))
     except:
         pass
     return json.dumps({'peers': peers})
    def do_money(self):
        Mongo.init()
        my_address = str(
            P2PKHBitcoinAddress.from_pubkey(self.public_key.decode('hex')))
        input_txns = BU.get_wallet_unspent_transactions(my_address)
        miner_transactions = Mongo.db.miner_transactions.find()
        mtxn_ids = []
        for mtxn in miner_transactions:
            for mtxninput in mtxn['inputs']:
                mtxn_ids.append(mtxninput['id'])

        inputs = [
            Input.from_dict(input_txn) for input_txn in input_txns
            if input_txn['id'] not in mtxn_ids
        ]

        input_sum = 0
        if self.coinbase:
            self.inputs = []
        else:
            needed_inputs = []
            done = False
            for y in inputs:
                print y.id
                txn = BU.get_transaction_by_id(y.id, instance=True)
                for txn_output in txn.outputs:
                    if txn_output.to == my_address:
                        input_sum += txn_output.value
                        needed_inputs.append(y)
                        if input_sum >= (sum([x.value for x in self.outputs]) +
                                         self.fee):
                            done = True
                            break
                if done == True:
                    break

            if not done:
                raise NotEnoughMoneyException('not enough money')
            self.inputs = needed_inputs

            return_change_output = Output(
                to=my_address,
                value=input_sum - (sum([x.value
                                        for x in self.outputs]) + self.fee))
            self.outputs.append(return_change_output)
Пример #4
0
    def __init__(self, bulletin_secret, wallet_mode=False):
        Mongo.init()
        self.wallet_mode = wallet_mode
        self.friend_requests = []
        self.sent_friend_requests = []
        self.friends = []
        self.posts = []
        self.logins = []
        self.messages = []
        self.new_messages = []
        self.already_added_messages = []
        self.bulletin_secret = str(bulletin_secret)

        if self.wallet_mode:
            return self.with_private_key()
        else:
            self.registered = False
            self.pending_registration = False
            bulletin_secrets = sorted(
                [str(Config.get_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 = 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(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)

            registered = Mongo.site_db.friends.find(
                {'relationship.bulletin_secret': bulletin_secret})
            if registered.count():
                self.registered = True

            if not self.registered:
                # not regisered, let's check for a pending transaction
                res = Mongo.db.miner_transactions.find({
                    'rid': self.rid,
                    'public_key': {
                        '$ne': Config.public_key
                    }
                })
                res2 = Mongo.db.miner_transactions.find({
                    'rid':
                    self.rid,
                    'public_key':
                    Config.public_key
                })

                if res.count() and res2.count():
                    self.pending_registration = True

            if self.registered:
                for x in self.friends:
                    for y in x['outputs']:
                        if y['to'] != Config.address:
                            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)