Exemple #1
0
    def test_address_pad__message(self):
        mappings = []

        km1 = ni.KeyMapping("key1", ["adr1-1", "adr1-2", "adr1-3"])
        mappings.append(km1)
        km2 = ni.KeyMapping("key2", ["adr2-1", "adr2-2", "adr2-3"])
        mappings.append(km2)

        email = ni.generate_address_pad("myself", mappings)
        #print(email.as_string())

        self.assertEqual('application/json', email.get('Content-Type'))
Exemple #2
0
    def _generate_own_address_pad(self, contact: contact.Contact, size: int) -> EmailMessage:
        """
        This method will generate an address pad MIME message. It takes the message
        recipients contact_id as argument as well as the intended size of the address pad.

        This implementation defaults the alias to send along with the address pad to be the
        same as the one registered for the contact - This is not mandated, but probably a
        reasonable guess at what a normal user would expect.
        """
        # Retrieve a suitable public key to send
        (key_id, public_key) = self._generate_new_key()

        # Produce address pad - We start with a block of addresses tied to a single key
        # This should probably be a double iterator to make new keys as we need them
        # (typically a new key per handful of addresses)

        address_list = []
        for _ in range(size):
            adr = "ADR-" + str(uuid.uuid4())
            address_list.append(adr)

        key_mapping = NodeIntercom.KeyMapping(public_key, address_list)

        # Persist the new addresses to the database, making sure we can read the messages
        # sent from the contact to us later.
        for adr in address_list:
            self._db.store_own_address(adr, contact.contact_id, key_id)

        # Remember: contact.alias is the name by which we are known to the contact
        return NodeIntercom.generate_address_pad(contact.alias, [key_mapping])
Exemple #3
0
    def test_address_pad_serialization(self):
        ap = ni.AddressPad("myself")

        adr_set_1 = []
        adr_set_1.append("someaddress1-1")
        adr_set_1.append("someaddress1-2")
        mapping_1 = ni.KeyMapping("somekey1", adr_set_1)
        ap.add_key_mapping(mapping_1)

        adr_set_2 = []
        adr_set_2.append("someaddress2-1")
        adr_set_2.append("someaddress2-2")
        mapping_2 = ni.KeyMapping("somekey2", adr_set_2)
        ap.add_key_mapping(mapping_2)

        json_str = ap.serialize()
        #print(json_str)

        ap2 = ni.AddressPad.deserialize(json_str)
        self.assertEqual(len(ap.key_mappings), len(ap2.key_mappings))

        for i in range(len(ap.key_mappings)):
            self.assertEqual(ap.key_mappings[i].key, ap2.key_mappings[i].key)
Exemple #4
0
    def _generate_foreign_address_pad(self, recipient: contact.Contact, \
                                      address_owner: contact.Contact, size: int) -> EmailMessage:
        """
        This method will generate an address pad MIME message containing addresses beloning to the
        address_owner - A contact that should be known to this node - the recipient is another
        contact to which the address pad should be sent
        """
        address_list = self._db.get_address_pad_nacl(address_owner.contact_id, size)

        mappings = {}
        for address in address_list:
            if not address.key_id in mappings:
                mappings[address.key_id] = NodeIntercom.KeyMapping(address.public_key, [])
            mappings[address.key_id].add_address(address.address_id)
            # Note that as we are exporting these addresses, we need to mark them as used in
            # order not to send addresses that might have been used / might be used in the future
            # by our contact.
            self._db.mark_contact_address(address.address)

        return NodeIntercom.generate_address_pad(address_owner.nickname, mappings.values())