# Generation of DMTP key pairs

First you connect your wallet to DMTP, you have to do initial setting. The initial set up has two flows「Generate DMTP key pair」and 「Sign to DMTP key」

<figure><img src="https://mermaid.ink/img/pako:eNqtUsFqwzAM_RXh0wbZD-RQ2JptlFEo607DMFRbaU0TOXOUlVD673OSlibbjiMXS355enp-R2W8JZWqmj4bYkOZw23AUnOFQZxxFbLAfeEMTVvzwhHLtJeh4AbrH8jF6mk97TwU3uzNDh3r-PXkd7PZwJjC3DOTEThgUZCAeMiWbyvNw_0I-ExMAYX6e9hTCxW6ADdd-VE1mxdqExiK4GJx-wfHI5vQVjKGwcHJDmq3ZZQmEGzPYyzkwZdnWSOqXn8Kr52BtfQ_Ot6Cz2Ek5Peaq-C_nKXroBHlxcgU1uIDwWQjGiRHPSPRCSDbP7k6-_-D5_poF7b5Iuu2jMbX4BhkNwzo6xKrKrqgWSWqpFCiszFkR80AWkVkSVql8Wgpx6YQrTSfIhQb8euWjUolNJSoprLR-XMmVZpjUccuWRcFLIfg9vlNVAzWu_cXzOkbSY4EKA?type=png" alt=""><figcaption></figcaption></figure>

#### Generate DMTP key pair &#x20;

You have to generate DMTP key pair that are used to encrypt and decrypt messages. These keys are not directly related to the wallet keys, so you don't have to care the risk of compromising your wallet. DMTP key pair are generated by the algorithm of [Elliptic Curve Diffie-Hellman](../cryptographic-algorithm#elliptic-curve-diffie-hellman-ecdh). DHKE generates key pair public key and private key as same as the normal public key encryption. In this model, combined secret generated by  (Alice's pubKey & Bob's priKey) and (Alice's priKey & Bob's pubKey) will be same. This model realizes the E2EE model of P2P messaging. DMTP client generates these key pair automatically, so you don't need do any special setting at this section.

From security perspective we shouldn't store `DMTP_priKey` low data. Thus users encrypt it with public key of their wallet so only wallet holder can decrypt it and see messages.

#### Sign to the DMTP\_pubKey

In this section you sign to the `DMTP_pubKey` to prove that the wallet address of yours and`DMTP_pubKey` of yours are actually linked.&#x20;



`DMTP_pubKey` `encrypted DMTP_priKey` `signature` are stored in DB. We describe you the detailed flow using some diagrams and code.\


## Flow

<figure><img src="https://4002826411-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fhk2vMNItTHga72ho27Cs%2Fuploads%2Fpc22yhaZabNRntTEXUlU%2Fimage.png?alt=media&#x26;token=9ec02a43-0130-42d7-8a39-b01eee3ef25e" alt=""><figcaption></figcaption></figure>

1. Alice connect her wallet to DMTP
2. Client generates DMTP key-pair that is used for en/de crypt messeges in DMTP. \
   We name them `DMTP_pubKey` and `DMTP_priKey.`
3. Alice encrypts her `DMTP_priKey` with her wallet's public key.
4. Alice sign `DMTP_pubKey` with her wallet. \
   This signature proves `DMTP_pubKey` is linked to Alice’s wallet.
5.  Store `DMTP_pubKey &` `encrypted DMTP_priKey & Signature` in DB and IPFS\
    The stored data is like this.

    ```json
    {
      "DMTPpubKey": "DMTP_PUBLIC_KEY",
      "DMTPpriKey": "ENCRYPTED_DMTP_PRIVATE_KEY",
      "Signature": "SIGNATURE",
    }
    ```
6.  Store the CID of keys in the blockchain and DB\
    The CID of keys is stored in `DMTPkeys` like this so anyone can find `DMTP_pubkey` from the contract.

    ```solidity
    mapping(address => string) DMTPkeys;

    function addKey(address _user, string _cid)public onlyOwner{
      DMTPkeys[_user] = _cid
    }
    ```

