def generate_identity(self, connection): identity = self._identities_processor.get_identity(connection.currency, connection.pubkey, connection.uid) if not identity: identity = Identity(connection.currency, connection.pubkey, connection.uid) sig_window = self._blockchain_processor.parameters(connection.currency).sig_window current_time = self._blockchain_processor.time(connection.currency) if identity.is_obsolete(sig_window, current_time): block_uid = self._blockchain_processor.current_buid(connection.currency) identity.blockstamp = block_uid timestamp = self._blockchain_processor.time(connection.currency) identity.timestamp = timestamp identity_doc = IdentityDoc(10, connection.currency, connection.pubkey, connection.uid, block_uid, None) else: identity_doc = IdentityDoc(10, connection.currency, connection.pubkey, connection.uid, identity.blockstamp, identity.signature) return identity, identity_doc
async def load_certs_in_lookup(self, identity, certifiers, certified): """ :param sakia.data.entities.Identity identity: the identity :param dict[sakia.data.entities.Certification] certifiers: the list of certifiers got in /wot/certifiers-of :param dict[sakia.data.entities.Certification] certified: the list of certified got in /wot/certified-by """ try: lookup_data = await self._bma_connector.get(self.currency, bma.wot.lookup, {'search': identity.pubkey}) for result in lookup_data['results']: if result["pubkey"] == identity.pubkey: for uid_data in result['uids']: if not identity.uid or uid_data["uid"] == identity.uid: if not identity.blockstamp or identity.blockstamp == block_uid(uid_data["meta"]["timestamp"]): for other_data in uid_data["others"]: cert = Certification(currency=self.currency, certified=identity.pubkey, certifier=other_data["pubkey"], block=other_data["meta"]["block_number"], timestamp=0, signature=other_data['signature']) certifier = Identity(currency=self.currency, pubkey=other_data["pubkey"], uid=other_data["uids"][0], member=other_data["isMember"]) if cert not in certifiers: cert.timestamp = self._blockchain_processor.rounded_timestamp(self.currency, cert.block) certifiers[cert] = certifier # We save connections pubkeys if self.is_identity_of_connection(identity): self._certs_processor.insert_or_update_certification(cert) for signed_data in result["signed"]: cert = Certification(currency=self.currency, certified=signed_data["pubkey"], certifier=identity.pubkey, block=signed_data["cert_time"]["block"], timestamp=0, signature=signed_data['signature']) certified_idty = Identity(currency=self.currency, pubkey=signed_data["pubkey"], uid=signed_data["uid"], member=signed_data["isMember"]) if cert not in certified: certified[cert] = certified_idty # We save connections pubkeys if self.is_identity_of_connection(identity): cert.timestamp = self._blockchain_processor.rounded_timestamp(self.currency, cert.block) self._certs_processor.insert_or_update_certification(cert) except errors.DuniterError as e: logging.debug("Certified by error : {0}".format(str(e))) except NoPeerAvailable as e: logging.debug(str(e)) return certifiers, certified
def test_add_update_identity(meta_repo): identities_repo = IdentitiesRepo(meta_repo.conn) identity = Identity("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", "john", "20-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==", 1473108382) identities_repo.insert(identity) identity.member = True identities_repo.update(identity) identity2 = identities_repo.get_one(currency="testcurrency", pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ") assert identity2.member is True
async def test_new_block_with_certs(application_with_one_connection, fake_server_with_blockchain, bob, alice): certs_before_send = application_with_one_connection.identities_service.certifications_sent( bob.key.pubkey) alice_user_identity = fake_server_with_blockchain.forge.user_identities[ bob.key.pubkey] alice_identity = Identity( currency=fake_server_with_blockchain.forge.currency, pubkey=alice.key.pubkey, uid=alice.uid, blockstamp=alice_user_identity.blockstamp, signature=alice_user_identity.signature) bob_connection = application_with_one_connection.db.connections_repo.get_one( pubkey=bob.key.pubkey) await application_with_one_connection.documents_service.certify( bob_connection, bob.salt, bob.password, alice_identity) certs_after_send = application_with_one_connection.identities_service.certifications_sent( bob.key.pubkey) assert len(certs_after_send) == len(certs_before_send) + 1 assert certs_after_send[0].written_on == -1 assert isinstance(fake_server_with_blockchain.forge.pool[0], Certification) fake_server_with_blockchain.forge.forge_block() fake_server_with_blockchain.forge.forge_block() fake_server_with_blockchain.forge.forge_block() new_blocks = fake_server_with_blockchain.forge.blocks[-3:] await application_with_one_connection.identities_service.handle_new_blocks( new_blocks) certs_after_parse = application_with_one_connection.identities_service.certifications_sent( bob.key.pubkey) assert len(certs_after_parse) == len(certs_after_send) assert certs_after_parse[ 0].written_on == fake_server_with_blockchain.forge.blocks[-3].number await fake_server_with_blockchain.close()
async def test_new_block_with_idty(application_with_one_connection, john, fake_server_with_blockchain): john_identity = Identity( currency=fake_server_with_blockchain.forge.currency, pubkey=john.key.pubkey, uid=john.uid, blockstamp=john.blockstamp, signature=john.identity().signatures[0]) john_connection = Connection(currency="test_currency", pubkey=john.key.pubkey, uid=john.uid, scrypt_N=4096, scrypt_r=4, scrypt_p=2, blockstamp=john.blockstamp) application_with_one_connection.db.connections_repo.insert(john_connection) application_with_one_connection.db.identities_repo.insert(john_identity) application_with_one_connection.instanciate_services() john_found = application_with_one_connection.db.identities_repo.get_one( pubkey=john_identity.pubkey) assert john_found.written is False fake_server_with_blockchain.forge.forge_block() fake_server_with_blockchain.forge.push(john.identity()) fake_server_with_blockchain.forge.push(john.join(BlockUID.empty())) fake_server_with_blockchain.forge.forge_block() fake_server_with_blockchain.forge.forge_block() new_blocks = fake_server_with_blockchain.forge.blocks[-3:] await application_with_one_connection.identities_service.handle_new_blocks( new_blocks) john_found = application_with_one_connection.db.identities_repo.get_one( pubkey=john_identity.pubkey) assert john_found.written is True await fake_server_with_blockchain.close()
def generate_identity(self, connection): identity = self._identities_processor.get_identity(connection.currency, connection.pubkey, connection.uid) if not identity: identity = Identity(connection.currency, connection.pubkey, connection.uid) sig_window = self._blockchain_processor.parameters(connection.currency).sig_window current_time = self._blockchain_processor.time(connection.currency) if identity.is_obsolete(sig_window, current_time): block_uid = self._blockchain_processor.current_buid(connection.currency) identity.blockstamp = block_uid timestamp = self._blockchain_processor.time(connection.currency) identity.timestamp = timestamp identity.signature = None return identity
def test_add_get_drop_identity(meta_repo): identities_repo = IdentitiesRepo(meta_repo.conn) identities_repo.insert(Identity("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", "john", "20-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==", 1473108382)) identity = identities_repo.get_one(currency="testcurrency", pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", uid="john", blockstamp=BlockUID(20, "7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67") ) assert identity.currency == "testcurrency" assert identity.pubkey == "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ" assert identity.uid == "john" assert identity.blockstamp.number == 20 assert identity.blockstamp.sha_hash == "7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67" assert identity.timestamp == 1473108382 assert identity.signature == "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==" assert identity.member == False assert identity.membership_buid == BlockUID.empty() assert identity.membership_timestamp == 0 assert identity.membership_written_on == 0 identities_repo.drop(identity) identity = identities_repo.get_one(currency="testcurrency", pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", uid="john", blockstamp=BlockUID(20, "7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67") ) assert identity is None
def test_add_get_multiple_identity(meta_repo): identities_repo = IdentitiesRepo(meta_repo.conn) identities_repo.insert(Identity("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", "john", "20-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==", 1473108382)) identities_repo.insert(Identity("testcurrency", "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn", "doe", "101-BAD49448A1AD73C978CEDCB8F137D20A5715EBAA739DAEF76B1E28EE67B2C00C", "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==", 1455433535)) identities = identities_repo.get_all(currency="testcurrency") assert "testcurrency" in [i.currency for i in identities] assert "john" in [i.uid for i in identities] assert "doe" in [i.uid for i in identities]
def load_identity_document(self, identity_doc): """ Load an identity document :param duniterpy.documents.Identity identity_doc: """ identity = Identity(currency=identity_doc.currency, pubkey=identity_doc.pubkey, uid=identity_doc.uid, blockstamp=identity_doc.timestamp, signature=identity_doc.signatures[0]) self.user_information.change_identity(identity)
async def broadcast_identity(self, connection, secret_key, password): """ Send our self certification to a target community :param sakia.data.entities.Connection connection: the connection published :param str secret_key: the private key secret key :param str password: the private key password """ block_uid = self._blockchain_processor.current_buid( connection.currency) timestamp = self._blockchain_processor.time(connection.currency) selfcert = IdentityDoc(10, connection.currency, connection.pubkey, connection.uid, block_uid, None) key = SigningKey(secret_key, password, connection.scrypt_params) selfcert.sign([key]) self._logger.debug("Key publish : {0}".format(selfcert.signed_raw())) responses = await self._bma_connector.broadcast( connection.currency, bma.wot.add, req_args={'identity': selfcert.signed_raw()}) result = await parse_bma_responses(responses) if result[0]: identity = self._identities_processor.get_identity( connection.currency, connection.pubkey, connection.uid) if not identity: identity = Identity(connection.currency, connection.pubkey, connection.uid) identity.blockstamp = block_uid identity.signature = selfcert.signatures[0] identity.timestamp = timestamp else: identity = None return result, identity
def history_context_menu(self, point): index = self.view.table_history.indexAt(point) valid, identity, transfer = self.model.table_data(index) if valid: if identity is None: if isinstance(transfer, Transaction): if transfer.issuer != self.model.connection.pubkey: pubkey = transfer.issuer else: pubkey = transfer.receiver identity = Identity(currency=transfer.currency, pubkey=pubkey) menu = ContextMenu.from_data(self.view, self.model.app, self.model.connection, (identity, transfer)) menu.view_identity_in_wot.connect(self.view_in_wot) # Show the context menu. menu.qmenu.popup(QCursor.pos())
def certifier_data(self, certification): """ Return the identity in the form a tuple to display :param sakia.data.entities.Certification certification: The certification to get data from :return: The certification data in the form of a tuple :rtype: tuple """ parameters = self.blockchain_service.parameters() publication_date = certification.timestamp identity = self.identities_service.get_identity( certification.certifier) if not identity: identity = Identity(currency=certification.currency, pubkey=certification.certifier, uid="") written = certification.written_on >= 0 if written: expiration_date = publication_date + parameters.sig_validity else: expiration_date = publication_date + parameters.sig_window return identity.uid, identity.pubkey, publication_date, expiration_date, written, identity
async def load_certifiers_of(self, identity): """ Request the identity data and save it to written certifications It does nothing if the identity is already written and updated with blockchain lookups :param sakia.data.entities.Identity identity: the identity """ certifications = {} try: data = await self._bma_connector.get(self.currency, bma.wot.certifiers_of, {'search': identity.pubkey}) for certifier_data in data['certifications']: cert = Certification(currency=self.currency, certified=data["pubkey"], certifier=certifier_data["pubkey"], block=certifier_data["cert_time"]["block"], timestamp=certifier_data["cert_time"]["medianTime"], signature=certifier_data['signature']) certifier = Identity(currency=self.currency, pubkey=certifier_data["pubkey"], uid=certifier_data["uid"], member=certifier_data["isMember"]) if certifier_data['written']: cert.written_on = certifier_data['written']['number'] certifications[cert] = certifier # We save connections pubkeys if identity.pubkey in self._connections_processor.pubkeys(): self._certs_processor.insert_or_update_certification(cert) identity.written = True if self.is_identity_of_connection(identity): self._identities_processor.insert_or_update_identity(identity) except errors.DuniterError as e: if e.ucode in (errors.NO_MATCHING_IDENTITY, errors.NO_MEMBER_MATCHING_PUB_OR_UID): identity.written = False if identity.pubkey in self._connections_processor.pubkeys(): self._identities_processor.insert_or_update_identity(identity) logging.debug("Certified by error : {0}".format(str(e))) except NoPeerAvailable as e: logging.debug(str(e)) return certifications