🔑User Keys

Using our KeyPair class, create your users' keys, and use them for encrypting and decrypting messages.

Protico is a decentralized communication protocol, and as such, these keys can be used by multiple dApps. The first step is to check if the user already possesses a key pair, by checking their Ceramic Basic Profile.

1. Checking the profile for keys

Assuming you already have the user's Basic Profile, you can quickly check if they have a public key associated with their account. If this is the case, a key pair was already generated, if not, a new one needs to be created.

// ... Obtain Basic Profile
const publicKey=profile.public_key;
if(publicKey){
    //request the user to import the key
} else {
    //create a new key pair and associate it with the user profile
}

Note that changing an existing key pair will render all the previous messages inaccessible to the user.

2a. Existing keypair

If the keypair already exists, it can be recovered from its mnemonics as follows:

 const keyPair = new Keypair(currentAddress).fromMnemonic(mnemonic);
 const {public as publicKey, private as privateKey} = keyPair.get();

It has to be noted that this key pair is only used for encryption and decryption purposes. By importing his keys into a dApp, the user signifies his trust in the dApp to process encryption and decryption operations on their behalf.

2b. New keypair

If there is no existing keypair for the user, create a new one:

import { KeyPair } from "protico-sdk";

const kp = new KeyPair(walletAddress);
const keys= kp.generate();

Then, update the user's Basic Profile to include the new public key:

const {public as publicKey, private as privateKey, mnemonic} = keys.get(); 
await ceramicClient.profile.update({public_key:publicKey});

Make sure the user saves the mnemonic for later recovery or use in another dApp, and to safely store the messaging private key on your dApp side.

Last updated