def command_invite(self, petname, maybe_code=None, reqid=None,
                       override_transports=None, offer_mailbox=False,
                       accept_mailbox_offer=False,
                       _debug_when_done=None):
        #if offer_mailbox:
        #    code = "mailbox-" + code

        if maybe_code:
            count = self.db.execute("SELECT COUNT(*) FROM addressbook"
                                    " WHERE invitation_state != ?"
                                    "  AND invitation_code=?",
                                    (invitation.INVITE_COMPLETE, maybe_code)
                                    ).fetchone()[0]
            if count:
                raise CommandError("invitation code already in use")

        my_signkey = SigningKey.generate()
        channel_key = PrivateKey.generate()
        my_CID_key = os.urandom(32)

        base_transports = self.get_transports()
        if override_transports:
            base_transports = override_transports
        transports = self.individualize_transports(base_transports)

        channel = { "channel_pubkey": channel_key.public_key.encode(Hex),
                    "CID_key": my_CID_key.encode("hex"),
                    "transports": transports.values(),
                     }
        payload = { "channel": channel }

        if offer_mailbox:
            tid = self.mailbox_server.allocate_transport(remote=True)
            payload["mailbox"] = self.mailbox_server.get_mailbox_record(tid)

        encoded_payload = json.dumps(payload).encode("utf-8")
        sigkeypayload = (b"i0:" + my_signkey.verify_key.encode()
                         + my_signkey.sign(encoded_payload))

        cid = self.db.insert(
            "INSERT INTO addressbook"
            " (petname, accept_mailbox_offer,"
            "  invitation_state, when_invited, invitation_code,"
            "  wormhole_payload,"
            "  next_outbound_seqnum, my_signkey,"
            "  my_CID_key, next_CID_token,"
            "  highest_inbound_seqnum,"
            "  my_old_channel_privkey,"
            "  my_new_channel_privkey)"
            " VALUES (?,?, ?,?,?, ?, ?,?, ?,?, ?, ?, ?)",
            (petname, accept_mailbox_offer,
             invitation.INVITE_WAITING_FOR_CODE, time.time(), maybe_code,
             sigkeypayload.encode("hex"),
             1, my_signkey.encode(Hex),
             my_CID_key.encode("hex"), None,
             0,
             channel_key.encode(Hex),
             channel_key.encode(Hex), # at beginning, old=new
             ),
            "addressbook", {"reqid": reqid})

        i = self.im.create_invitation(cid)
        self.db.commit()
        self._activate_invitation(i, cid, _debug_when_done)

        return {"contact-id": cid, "petname": petname,
                "ok": "invitation for %s started: cid=%d" % (petname, cid)}
dataset/ETHPy150Open warner/petmail/petmail/agent.py/Agent.command_invite/original