Skip to content

Commit

Permalink
cache encryptionKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Sep 6, 2024
1 parent f389ecd commit 5063aa9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,15 @@ describe('SmartAccountConnector', () => {
const loginResult2 = await connector2.login();
//Thrid device has to post its public key to the UP. So device 1 can share the profile keys
const loginResult3 = await connector3.login();
console.log('check3');
expect(loginResult1.type).toBe('SUCCESS');
expect(loginResult2.type).toBe('NEW_DEVICE');
expect(loginResult3.type).toBe('NEW_DEVICE');

//Connector 1 is the only connector capable of sharing the profile keys

const profile = await mockUserProfile(
upController1 as ethers.Wallet,
'foo.eth',
[],
);
//profileKeys from mockUserProfile are the same as the encryption keys of the connector
await connector1.syncKeys(profile.profileKeys);
await connector1.syncKeys();

const data = JSON.parse(
ethers.utils.toUtf8String(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
encryptAsymmetric,
EncryptedPayload,
getStorageKeyCreationMessage,
KeyPair,
} from '@dm3-org/dm3-lib-crypto';
import {
createProfileKeys as _createProfileKeys,
Expand Down Expand Up @@ -57,6 +58,9 @@ export class SmartAccountConnector {

private readonly defaultDeliveryService;

//After the user created the encryptionKeyPair once, it can be reused to decrypt the profileKeys of other controllers
private encryptioinKeyPair: KeyPair;

constructor(
keyStoreService: KeyStore.IKeyStoreService,
upController: ethers.Signer,
Expand All @@ -70,7 +74,7 @@ export class SmartAccountConnector {
}
//KeySync can be triggered by controller that has used dm3 before.
//This function will normally be called after login on behalf of the user
public async syncKeys(encryptionKeys: ProfileKeys) {
public async syncKeys() {
//1. Get the current keyStore
const keyStore = await this.keyStoreService.readDm3KeyStore();

Expand All @@ -86,16 +90,19 @@ export class SmartAccountConnector {
}

//2. Decrypt the profileKeys of the controller

const payload: EncryptedPayload = JSON.parse(
atob(encryptedControllerKeyStore.encryptedProfileKeys),
) as EncryptedPayload;

const _encryptionKeys = await this.createEncryptionKeys();

//Decrpyt the profileKeys of the controller
const profileKeys = await decryptAsymmetric(
encryptionKeys.encryptionKeyPair,
_encryptionKeys,
payload,
1,
);
console.log('check6');

//For each device in the keyStore, encrypt the profileKeys of the controller
//That only applies for controllers that have a publicKey but not encryptedProfileKeys yet
Expand Down Expand Up @@ -201,6 +208,10 @@ export class SmartAccountConnector {

//Returns Keys to encrypt the actual profile at UP
private async createEncryptionKeys() {
//If the user has created its encryption keys before, we can reuse them. That way we don't have to ask the user to sign again
if (this.encryptioinKeyPair) {
return this.encryptioinKeyPair;
}
const controllerAddress = await this.controller.getAddress();
const statement =
`Connect the DM3 MESSENGER with your wallet. ` +
Expand All @@ -225,7 +236,12 @@ export class SmartAccountConnector {
);
const storageKey = await createStorageKey(signature);

return await _createProfileKeys(storageKey, this.nonce);
const keys = await _createProfileKeys(storageKey, this.nonce);

//Keep the encryptionKeyPair for later use
this.encryptioinKeyPair = keys.encryptionKeyPair;

return keys.encryptionKeyPair;
}

private async createNewSignedUserProfile(
Expand Down Expand Up @@ -262,7 +278,7 @@ export class SmartAccountConnector {
) as EncryptedPayload;

const profileKeys = await decryptAsymmetric(
encryptionKeys?.encryptionKeyPair!,
encryptionKeys!,
payload,
1,
);
Expand Down Expand Up @@ -290,7 +306,7 @@ export class SmartAccountConnector {
const newKeyStore = {
...keyStore,
[await this.controller.getAddress()]: {
signerPublicKey: encryptionKeys.encryptionKeyPair.publicKey,
signerPublicKey: encryptionKeys.publicKey,
},
};

Expand All @@ -314,7 +330,7 @@ export class SmartAccountConnector {
const encryptionKeys = await this.createEncryptionKeys();

const encryptedPayload: EncryptedPayload = await encryptAsymmetric(
encryptionKeys?.encryptionKeyPair?.publicKey!,
encryptionKeys?.publicKey!,
stringify(profileKeys),
1,
);
Expand Down

0 comments on commit 5063aa9

Please sign in to comment.